#include <inttypes.h>
#include <dirent.h> /* for PATH_MAX */
#include <assert.h>
-
-#include <fcgiapp.h>
-#include <fcgi_stdio.h>
+#include <math.h>
#include <rrd.h>
#include "action_graph.h"
#include "graph_list.h"
#include "utils_params.h"
+#include "utils_array.h"
-struct data_source_s
-{
- char *file;
- char *name;
- char *legend;
- double scale;
- _Bool nan_to_zero;
- _Bool draw_area;
- uint32_t color;
-};
-typedef struct data_source_s data_source_t;
-
-struct graph_def_s
-{
- data_source_t *data_sources;
- size_t data_sources_num;
-
- _Bool stack;
-};
-typedef struct graph_def_s graph_def_t;
+#include <fcgiapp.h>
+#include <fcgi_stdio.h>
-static int graph_def_add_ds (graph_def_t *gd, /* {{{ */
- const char *file,
- const char *in_ds_name, size_t ds_name_len)
+static void emulate_graph (int argc, char **argv) /* {{{ */
{
- char ds_name[ds_name_len + 1];
- data_source_t *ds;
-
- strncpy (ds_name, in_ds_name, sizeof (ds_name));
- ds_name[sizeof (ds_name) - 1] = 0;
-
- ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
- if (ds == NULL)
- return (ENOMEM);
- gd->data_sources = ds;
-
- ds = gd->data_sources + gd->data_sources_num;
- memset (ds, 0, sizeof (*ds));
+ int i;
- ds->file = strdup (file);
- if (ds->file == NULL)
- return (ENOMEM);
-
- ds->name = strdup (ds_name);
- if (ds->name == NULL)
+ printf ("rrdtool \\\n");
+ for (i = 0; i < argc; i++)
{
- free (ds->file);
- return (ENOMEM);
+ if (i < (argc - 1))
+ printf (" \"%s\" \\\n", argv[i]);
+ else
+ printf (" \"%s\"\n", argv[i]);
}
+} /* }}} void emulate_graph */
- ds->legend = NULL;
- ds->color = 0xff0000;
-
- gd->data_sources_num++;
+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 graph_def_add_ds */
+} /* }}} int ag_info_print */
-static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
+static int output_graph (rrd_info_t *info) /* {{{ */
{
- char *rrd_argv[] = { "info", file, NULL };
- int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
- rrd_info_t *info;
- rrd_info_t *ptr;
- graph_def_t *gd;
-
- gd = malloc (sizeof (*gd));
- if (gd == NULL)
- return (NULL);
- memset (gd, 0, sizeof (*gd));
-
- gd->data_sources = NULL;
+ rrd_info_t *img;
- info = rrd_info (rrd_argc, rrd_argv);
- if (info == NULL)
- {
- printf ("%s: rrd_info (%s) failed.\n", __func__, file);
- free (gd);
- return (NULL);
- }
+ for (img = info; img != NULL; img = img->next)
+ if ((strcmp ("image", img->key) == 0)
+ && (img->type == RD_I_BLO))
+ break;
- for (ptr = info; ptr != NULL; ptr = ptr->next)
- {
- size_t keylen;
- size_t dslen;
+ if (img == NULL)
+ return (ENOENT);
- if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
- continue;
+ 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);
- keylen = strlen (ptr->key);
- if (keylen < strlen ("ds[?].index"))
- continue;
-
- dslen = keylen - strlen ("ds[].index");
- assert (dslen >= 1);
-
- if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
- continue;
-
- graph_def_add_ds (gd, file, ptr->key + strlen ("ds["), dslen);
- }
-
- rrd_info_free (info);
+ return (0);
+} /* }}} int output_graph */
- return (gd);
-} /* }}} graph_def_t *graph_def_from_rrd_file */
+#define OUTPUT_ERROR(...) do { \
+ printf ("Content-Type: text/plain\n\n"); \
+ printf (__VA_ARGS__); \
+ return (0); \
+} while (0)
-static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
+int action_graph (void) /* {{{ */
{
- char rrd_file[PATH_MAX];
-
- if ((gl->plugin_instance == NULL) && (gl->type_instance == NULL))
- snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s.rrd",
- DATA_DIR, gl->host, gl->plugin, gl->type);
- else if (gl->type_instance == NULL)
- snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s.rrd",
- DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, gl->type);
- else if (gl->plugin_instance == NULL)
- snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s-%s.rrd",
- DATA_DIR, gl->host, gl->plugin, gl->type, gl->type_instance);
- else
- snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s-%s.rrd",
- DATA_DIR, gl->host, gl->plugin, gl->plugin_instance,
- gl->type, gl->type_instance);
- rrd_file[sizeof (rrd_file) - 1] = 0;
-
- printf ("rrd_file = %s;\n", rrd_file);
+ str_array_t *args;
+ graph_config_t *cfg;
+ graph_instance_t *inst;
+ rrd_info_t *info;
+ int status;
- return (graph_def_from_rrd_file (rrd_file));
-} /* }}} graph_def_t *graph_def_from_gl */
+ cfg = gl_graph_get_selected ();
+ if (cfg == NULL)
+ OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
-static int init_gl (graph_list_t *gl) /* {{{ */
-{
- gl->host = param ("host");
- gl->plugin = param ("plugin");
- gl->plugin_instance = param ("plugin_instance");
- gl->type = param ("type");
- gl->type_instance = param ("type_instance");
-
- if ((gl->host == NULL)
- || (gl->plugin == NULL)
- || (gl->type == NULL))
- return (EINVAL);
-
- if ((gl->host[0] == 0) || (gl->host[0] == '.')
- || (gl->plugin[0] == 0) || (gl->plugin[0] == '.')
- || (gl->type[0] == 0) || (gl->type[0] == '.'))
- return (EINVAL);
-
- if ((strchr (gl->plugin, '-') != NULL)
- || (strchr (gl->type, '-') != NULL))
- return (EINVAL);
-
- if ((gl->plugin_instance != NULL)
- && (gl->plugin_instance[0] == 0))
- gl->plugin_instance = NULL;
-
- if ((gl->type_instance != NULL)
- && (gl->type_instance[0] == 0))
- gl->type_instance = NULL;
+ inst = inst_get_selected (cfg);
+ if (inst == NULL)
+ OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
- return (0);
-} /* }}} int init_gl */
+ args = array_create ();
+ if (args == NULL)
+ return (ENOMEM);
-int action_graph (void) /* {{{ */
-{
- graph_list_t gl;
- graph_def_t *gd;
- int status;
- size_t i;
+ array_append (args, "graph");
+ array_append (args, "-");
+ array_append (args, "--imgformat");
+ array_append (args, "PNG");
- memset (&gl, 0, sizeof (gl));
- status = init_gl (&gl);
+ status = inst_get_rrdargs (cfg, inst, args);
if (status != 0)
{
- printf ("Content-Type: text/plain\n\n"
- "init_gl failed with status %i.\n", status);
- return (status);
+ array_destroy (args);
+ OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
}
- printf ("Content-Type: text/plain\n\n"
- "Hello, this is %s\n", __func__);
- gd = graph_def_from_gl (&gl);
- if (gd == NULL)
+ rrd_clear_error ();
+ info = rrd_graph_v (array_argc (args), array_argv (args));
+ if ((info == NULL) || rrd_test_error ())
{
- printf ("graph_def_from_gl failed.\n");
- return (0);
+ 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));
}
-
- for (i = 0; i < gd->data_sources_num; i++)
+ else
{
- printf ("data source %lu: %s @ %s\n",
- (unsigned long) i, gd->data_sources[i].name, gd->data_sources[i].file);
+ 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);
+ }
+ }
}
- /* FIXME: Free gd */
+ if (info != NULL)
+ rrd_info_free (info);
+
+ array_destroy (args);
+ args = NULL;
return (0);
} /* }}} int action_graph */