38b1ec9a0e5f0596ad58f78c788bcddd7be38c5b
[collection4.git] / action_graph.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <dirent.h> /* for PATH_MAX */
8 #include <assert.h>
9
10 #include <fcgiapp.h>
11 #include <fcgi_stdio.h>
12
13 #include <rrd.h>
14
15 #include "common.h"
16 #include "action_graph.h"
17 #include "graph_list.h"
18 #include "utils_params.h"
19
20 struct data_source_s
21 {
22   char *file;
23   char *name;
24   char *legend;
25   double scale;
26   _Bool nan_to_zero;
27   _Bool draw_area;
28   uint32_t color;
29 };
30 typedef struct data_source_s data_source_t;
31
32 struct graph_def_s
33 {
34   data_source_t *data_sources;
35   size_t data_sources_num;
36
37   _Bool stack;
38 };
39 typedef struct graph_def_s graph_def_t;
40
41 static int graph_def_add_ds (graph_def_t *gd, /* {{{ */
42     const char *file,
43     const char *in_ds_name, size_t ds_name_len)
44 {
45   char ds_name[ds_name_len + 1];
46   data_source_t *ds;
47
48   strncpy (ds_name, in_ds_name, sizeof (ds_name));
49   ds_name[sizeof (ds_name) - 1] = 0;
50
51   ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1));
52   if (ds == NULL)
53     return (ENOMEM);
54   gd->data_sources = ds;
55
56   ds = gd->data_sources + gd->data_sources_num;
57   memset (ds, 0, sizeof (*ds));
58
59   ds->file = strdup (file);
60   if (ds->file == NULL)
61     return (ENOMEM);
62
63   ds->name = strdup (ds_name);
64   if (ds->name == NULL)
65   {
66     free (ds->file);
67     return (ENOMEM);
68   }
69
70   ds->legend = NULL;
71   ds->color = 0xff0000;
72
73   gd->data_sources_num++;
74
75   return (0);
76 } /* }}} int graph_def_add_ds */
77
78 static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */
79 {
80   char *rrd_argv[] = { "info", file, NULL };
81   int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
82   rrd_info_t *info;
83   rrd_info_t *ptr;
84   graph_def_t *gd;
85
86   gd = malloc (sizeof (*gd));
87   if (gd == NULL)
88     return (NULL);
89   memset (gd, 0, sizeof (*gd));
90
91   gd->data_sources = NULL;
92
93   info = rrd_info (rrd_argc, rrd_argv);
94   if (info == NULL)
95   {
96     printf ("%s: rrd_info (%s) failed.\n", __func__, file);
97     free (gd);
98     return (NULL);
99   }
100
101   for (ptr = info; ptr != NULL; ptr = ptr->next)
102   {
103     size_t keylen;
104     size_t dslen;
105
106     if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
107       continue;
108
109     keylen = strlen (ptr->key);
110     if (keylen < strlen ("ds[?].index"))
111       continue;
112
113     dslen = keylen - strlen ("ds[].index");
114     assert (dslen >= 1);
115
116     if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
117       continue;
118
119     graph_def_add_ds (gd, file, ptr->key + strlen ("ds["), dslen);
120   }
121
122   rrd_info_free (info);
123
124   return (gd);
125 } /* }}} graph_def_t *graph_def_from_rrd_file */
126
127 static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */
128 {
129   char rrd_file[PATH_MAX];
130
131   if ((gl->plugin_instance == NULL) && (gl->type_instance == NULL))
132     snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s.rrd",
133         DATA_DIR, gl->host, gl->plugin, gl->type);
134   else if (gl->type_instance == NULL)
135     snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s.rrd",
136         DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, gl->type);
137   else if (gl->plugin_instance == NULL)
138     snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s-%s.rrd",
139         DATA_DIR, gl->host, gl->plugin, gl->type, gl->type_instance);
140   else
141     snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s-%s.rrd",
142         DATA_DIR, gl->host, gl->plugin, gl->plugin_instance,
143         gl->type, gl->type_instance);
144   rrd_file[sizeof (rrd_file) - 1] = 0;
145
146   printf ("rrd_file = %s;\n", rrd_file);
147
148   return (graph_def_from_rrd_file (rrd_file));
149 } /* }}} graph_def_t *graph_def_from_gl */
150
151 static int init_gl (graph_list_t *gl) /* {{{ */
152 {
153   gl->host = param ("host");
154   gl->plugin = param ("plugin");
155   gl->plugin_instance = param ("plugin_instance");
156   gl->type = param ("type");
157   gl->type_instance = param ("type_instance");
158
159   if ((gl->host == NULL)
160       || (gl->plugin == NULL)
161       || (gl->type == NULL))
162     return (EINVAL);
163
164   if ((gl->host[0] == 0) || (gl->host[0] == '.')
165       || (gl->plugin[0] == 0) || (gl->plugin[0] == '.')
166       || (gl->type[0] == 0) || (gl->type[0] == '.'))
167     return (EINVAL);
168
169   if ((strchr (gl->plugin, '-') != NULL)
170       || (strchr (gl->type, '-') != NULL))
171     return (EINVAL);
172
173   if ((gl->plugin_instance != NULL)
174       && (gl->plugin_instance[0] == 0))
175     gl->plugin_instance = NULL;
176
177   if ((gl->type_instance != NULL)
178       && (gl->type_instance[0] == 0))
179     gl->type_instance = NULL;
180
181   return (0);
182 } /* }}} int init_gl */
183
184 int action_graph (void) /* {{{ */
185 {
186   graph_list_t gl;
187   graph_def_t *gd;
188   int status;
189   size_t i;
190
191   memset (&gl, 0, sizeof (gl));
192   status = init_gl (&gl);
193   if (status != 0)
194   {
195     printf ("Content-Type: text/plain\n\n"
196         "init_gl failed with status %i.\n", status);
197     return (status);
198   }
199
200   printf ("Content-Type: text/plain\n\n"
201       "Hello, this is %s\n", __func__);
202   gd = graph_def_from_gl (&gl);
203   if (gd == NULL)
204   {
205     printf ("graph_def_from_gl failed.\n");
206     return (0);
207   }
208
209   for (i = 0; i < gd->data_sources_num; i++)
210   {
211     printf ("data source %lu: %s @ %s\n",
212         (unsigned long) i, gd->data_sources[i].name, gd->data_sources[i].file);
213   }
214
215   /* FIXME: Free gd */
216
217   return (0);
218 } /* }}} int action_graph */
219
220 /* vim: set sw=2 sts=2 et fdm=marker : */