From bd0b2b775eba823bd6325409dd287c0fd80a715a Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 19 Apr 2006 21:56:04 +0000 Subject: added support for rrd graphs --- tools/rrdui-cgi.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 142 insertions(+), 16 deletions(-) (limited to 'tools') 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 #include #include @@ -10,6 +12,8 @@ #include #include #include +#include +#include #include "../common/config-parser.h" @@ -33,6 +37,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(" name, l->category, l->title); @@ -273,15 +314,45 @@ listgraphs () printf("\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"); -- cgit v1.2.3