diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/rrdui-cgi.c | 158 |
1 files changed, 142 insertions, 16 deletions
diff --git a/tools/rrdui-cgi.c b/tools/rrdui-cgi.c index 45dbf83..ad755a4 100644 --- a/tools/rrdui-cgi.c +++ b/tools/rrdui-cgi.c @@ -3,6 +3,8 @@ file accompanying popt source distributions, available from ftp://ftp.rpm.org/pub/rpm/dist. */ +#define _GNU_SOURCE + #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -10,6 +12,8 @@ #include <unistd.h> #include <ctype.h> #include <errno.h> +#include <time.h> +#include <rrd.h> #include "../common/config-parser.h" @@ -34,6 +38,20 @@ graph; static graph* allgraphs = NULL; /* ----------------------------------------------------------------------------- + * ERRORS + */ + +static void +usererror (const char* msg) +{ + printf("Content-Type: text/plain\n\n"); + printf("%s\n", msg); + + /* Custom error so webserver doesn't think it's a server error */ + exit(0); +} + +/* ----------------------------------------------------------------------------- * CONFIG PARSING */ @@ -241,6 +259,26 @@ freegraphs () } } +graph* +findgraphs (char* name) +{ + graph* l; + + if (!name) + return NULL; + + for (l = allgraphs; l != NULL; l = l->next) + { + if (!l->commands) + continue; + + if (strcmp(l->name, name) == 0) + return l; + } + + return NULL; +} + /* ----------------------------------------------------------------------------- * METHODS */ @@ -256,6 +294,9 @@ listgraphs () for (l = allgraphs; l != NULL; l = l->next) { + if (!l->commands) + continue; + printf(" <graph name=\"%s\" category=\"%s\" title=\"%s\"", \ l->name, l->category, l->title); @@ -273,15 +314,45 @@ listgraphs () printf("</data>\n"); } -#if 0 +static int +runrrd(int argc, const char** argv) +{ + int x, y; + double min, max; + char** text; + + optind = 0; + opterr = 0; + + return rrd_graph(argc, (char**)argv, &text, &x, &y, stdout, &min, &max); +} + static void displaygraphs () { + #define MAX_SIZE 1024 + const char* args[MAX_SIZE]; + int numargs = 0; + char* allocs[MAX_SIZE]; + int numalloc = 0; char* name = NULL; - int width; - int height; + int width, height, start, end; + char* t; + const char** k; char* q; + char *nm, *vl; char* query = NULL; + graph* gr = NULL; + int r; + int i; + + width = height = start = end = 0; + memset(args, 0, sizeof(args)); + + args[numargs++] = "graph"; + args[numargs++] = "-"; + args[numargs++] = "--imgformat=PNG"; + args[numargs++] = "--rigid"; q = getenv("QUERY_STRING"); @@ -304,28 +375,83 @@ displaygraphs () name = vl; else if (strcmp(nm, "width") == 0) width = atoi(vl); - else if (strcmp(nm, "height") == ) + else if (strcmp(nm, "height") == 0) height = atoi(vl); + else if (strcmp(nm, "start") == 0) + start = atoi(vl); + else if (strcmp(nm, "end") == 0) + end = atoi(vl); + else if (strcmp(nm, "color") == 0) + { + args[numargs++] = "--color"; + t = strchr(vl, ':'); + if (t != NULL) + *t = '#'; + args[numargs++] = vl; + } } + } + + gr = findgraphs(name); + if (gr == NULL) + usererror("Invalid graph"); + + if (width || gr->width) + { + asprintf(&t, "--width=%d", width ? width : gr->width); + + args[numargs++] = allocs[numalloc++] = t; + } + + if (height || gr->height) + { + asprintf(&t, "--height=%d", height ? height : gr->height); + + args[numargs++] = allocs[numalloc++] = t; + } + + if (!end) + end = (int)time(NULL); + + asprintf(&t, "--end=%d", end); + args[numargs++] = allocs[numalloc++] = t; + + if (!start) + start = 86400; + + asprintf(&t, "--start=%d", start); + args[numargs++] = allocs[numalloc++] = t; + asprintf(&t, "--title=%s", gr->title); + args[numargs++] = allocs[numalloc++] = t; + + if (gr->options) + { + for (k = gr->options; *k != NULL; k++) + args[numargs++] = *k; } + + for (k = gr->commands; *k != NULL; k++) + args[numargs++] = *k; + + printf("Content-Type: image/png\n\n"); + +/* for (i = 0; i < numargs; i++) + fprintf(stderr, "%s\n", args[i]);*/ + + rrd_clear_error(); + r = runrrd(numargs, args); + if (r != 0) + errx(2, "could't create graph: %s", rrd_get_error()); + + for (i = 0; i < numalloc; i++) + free(allocs[i]); } -#endif /* ----------------------------------------------------------------------------- * MAIN */ -static void -usererror (const char* msg) -{ - printf("Content-Type: text/plain\n\n"); - printf("%s\n", msg); - - /* Custom error so webserver doesn't think it's a server error */ - exit(0); -} - static int pathstarts (const char* path, const char* method) { @@ -370,7 +496,7 @@ main (int argc, char* argv[]) if (!path || !path[0] || pathstarts(path, "list")) listgraphs(); else if (pathstarts(path, "graph")) - printf("graph\n"); + displaygraphs(); else usererror("Invalid request"); |