+static int draw_graph_ds (graph_def_t *gd, /* {{{ */
+ size_t index, str_array_t *args)
+{
+ data_source_t *ds;
+
+ assert (index < gd->data_sources_num);
+
+ ds = gd->data_sources + index;
+
+ /* CDEFs */
+ array_append_format (args, "DEF:def_%04zu_min=%s:%s:MIN",
+ index, ds->file, ds->name);
+ array_append_format (args, "DEF:def_%04zu_avg=%s:%s:AVERAGE",
+ index, ds->file, ds->name);
+ array_append_format (args, "DEF:def_%04zu_max=%s:%s:MAX",
+ index, ds->file, ds->name);
+ /* VDEFs */
+ array_append_format (args, "VDEF:vdef_%04zu_min=def_%04zu_min,MINIMUM",
+ index, index);
+ array_append_format (args, "VDEF:vdef_%04zu_avg=def_%04zu_avg,AVERAGE",
+ index, index);
+ array_append_format (args, "VDEF:vdef_%04zu_max=def_%04zu_max,MAXIMUM",
+ index, index);
+ array_append_format (args, "VDEF:vdef_%04zu_lst=def_%04zu_avg,LAST",
+ index, index);
+
+ /* Graph part */
+ array_append_format (args, "LINE1:def_%04zu_avg#%06x:%s", index, ds->color,
+ (ds->legend != NULL) ? ds->legend : ds->name);
+ array_append_format (args, "GPRINT:vdef_%04zu_min:%%lg min,", index);
+ array_append_format (args, "GPRINT:vdef_%04zu_avg:%%lg avg,", index);
+ array_append_format (args, "GPRINT:vdef_%04zu_max:%%lg max,", index);
+ array_append_format (args, "GPRINT:vdef_%04zu_lst:%%lg last\\l", index);
+
+ return (0);
+} /* }}} int draw_graph_ds */
+
+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 */
+
+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 */
+