+ 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 */
+
+static int draw_graph (graph_def_t *gd) /* {{{ */
+{
+ str_array_t *args;
+ rrd_info_t *info;
+ size_t i;
+
+ args = array_create ();
+ if (args == NULL)
+ return (ENOMEM);
+
+ array_append (args, "graph");
+ array_append (args, "-");
+ array_append (args, "--imgformat");
+ array_append (args, "PNG");
+
+ for (i = 0; i < gd->data_sources_num; i++)
+ draw_graph_ds (gd, i, args);
+
+ 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);
+
+ return (0);
+} /* }}} int draw_graph */
+
+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;
+
+ return (0);
+} /* }}} int init_gl */
+
+int action_graph (void) /* {{{ */
+{
+ graph_list_t gl;
+ graph_def_t *gd;
+ int status;
+
+ memset (&gl, 0, sizeof (gl));
+ status = init_gl (&gl);
+ if (status != 0)
+ {
+ printf ("Content-Type: text/plain\n\n"
+ "init_gl failed with status %i.\n", status);
+ return (status);
+ }
+
+ gd = graph_def_from_gl (&gl);
+ if (gd == NULL)
+ {
+ printf ("graph_def_from_gl failed.\n");
+ return (0);
+ }
+
+ draw_graph (gd);
+ graph_def_free (gd);