X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=action_graph.c;h=5771e17676394f07e9d5b1d71ac8fa030e16201f;hb=765bbe76d57b127ca2e72e40af3e8123aeeb217c;hp=afad4888eebb65726b58dd1a3a5cfe8e43ea62d9;hpb=7e8bef2ff6fcec4812a6075cd7e690d9b13bec0d;p=collection4.git diff --git a/action_graph.c b/action_graph.c index afad488..5771e17 100644 --- a/action_graph.c +++ b/action_graph.c @@ -2,18 +2,163 @@ #include #include #include +#include +#include +#include /* for PATH_MAX */ +#include +#include -#include -#include +#include +#include "common.h" #include "action_graph.h" #include "graph_list.h" #include "utils_params.h" +#include "utils_array.h" + +#include +#include -int action_graph (void) +struct data_source_s { - printf ("Content-Type: text/plain\n\n" - "Hello, this is %s\n", __func__); +}; +typedef struct data_source_s data_source_t; + +struct graph_def_s +{ + data_source_t *data_sources; + size_t data_sources_num; + + _Bool stack; + + int def_num; +}; +typedef struct graph_def_s graph_def_t; + +static void emulate_graph (int argc, char **argv) /* {{{ */ +{ + int i; + + printf ("rrdtool \\\n"); + for (i = 0; i < argc; i++) + { + if (i < (argc - 1)) + printf (" \"%s\" \\\n", argv[i]); + else + printf (" \"%s\"\n", argv[i]); + } +} /* }}} void emulate_graph */ + +static int ag_info_print (rrd_info_t *info) /* {{{ */ +{ + if (info->type == RD_I_VAL) + printf ("[info] %s = %g;\n", info->key, info->value.u_val); + else if (info->type == RD_I_CNT) + printf ("[info] %s = %lu;\n", info->key, info->value.u_cnt); + else if (info->type == RD_I_STR) + printf ("[info] %s = %s;\n", info->key, info->value.u_str); + else if (info->type == RD_I_INT) + printf ("[info] %s = %i;\n", info->key, info->value.u_int); + else if (info->type == RD_I_BLO) + printf ("[info] %s = [blob, %lu bytes];\n", info->key, info->value.u_blo.size); + else + printf ("[info] %s = [unknown type %#x];\n", info->key, info->type); + + return (0); +} /* }}} int ag_info_print */ + +static int output_graph (rrd_info_t *info) /* {{{ */ +{ + rrd_info_t *img; + + for (img = info; img != NULL; img = img->next) + if ((strcmp ("image", img->key) == 0) + && (img->type == RD_I_BLO)) + break; + + if (img == NULL) + return (ENOENT); + + printf ("Content-Type: image/png\n" + "Content-Length: %lu\n" + "\n", + img->value.u_blo.size); + fwrite (img->value.u_blo.ptr, img->value.u_blo.size, + /* nmemb = */ 1, stdout); + + return (0); +} /* }}} int output_graph */ + +#define OUTPUT_ERROR(...) do { \ + printf ("Content-Type: text/plain\n\n"); \ + printf (__VA_ARGS__); \ + return (0); \ +} while (0) + +int action_graph (void) /* {{{ */ +{ + str_array_t *args; + graph_config_t *cfg; + graph_instance_t *inst; + rrd_info_t *info; + int status; + + cfg = graph_get_selected (); + if (cfg == NULL) + OUTPUT_ERROR ("graph_get_selected () failed.\n"); + + inst = inst_get_selected (cfg); + if (inst == NULL) + OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg); + + args = array_create (); + if (args == NULL) + return (ENOMEM); + + array_append (args, "graph"); + array_append (args, "-"); + array_append (args, "--imgformat"); + array_append (args, "PNG"); + + status = gl_instance_get_rrdargs (cfg, inst, args); + if (status != 0) + { + array_destroy (args); + OUTPUT_ERROR ("gl_instance_get_rrdargs failed with status %i.\n", status); + } + + rrd_clear_error (); + info = rrd_graph_v (array_argc (args), array_argv (args)); + if ((info == NULL) || rrd_test_error ()) + { + printf ("Content-Type: text/plain\n\n"); + printf ("rrd_graph_v failed: %s\n", rrd_get_error ()); + emulate_graph (array_argc (args), array_argv (args)); + } + else + { + int status; + + status = output_graph (info); + if (status != 0) + { + rrd_info_t *ptr; + + printf ("Content-Type: text/plain\n\n"); + printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n"); + + for (ptr = info; ptr != NULL; ptr = ptr->next) + { + ag_info_print (ptr); + } + } + } + + if (info != NULL) + rrd_info_free (info); + + array_destroy (args); + args = NULL; return (0); } /* }}} int action_graph */