]> git.itanic.dy.fi Git - rrdd/blob - rrdtool.c
rrdtool: Add pre_draw_cmd
[rrdd] / rrdtool.c
1 #include <time.h>
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <string.h>
7
8 #include "rrdtool.h"
9 #include "process.h"
10 #include "parser.h"
11 #include "debug.h"
12 #include "string.h"
13
14 #define MAX_ARGS        512
15 #define ARGSTR_LEN      32768
16
17 #define RRDTOOL_CMD "/usr/bin/rrdtool"
18
19 /*
20  * Add new argument to a argument list
21  *
22  * args         pointer list to arguments
23  * argcnt       argument counter
24  * argstr       array where the actual arguments are stored
25  * idx          index in the argstr where the new argument will be appended
26  */
27 #define add_arg(args, argcnt, argstr, idx, fmt, arg...) \
28         args[argcnt] = argstr + idx;                    \
29         idx += sprintf(argstr + idx, fmt, ##arg);       \
30         argcnt++;                                       \
31         argstr[++idx] = 0
32
33 int rrdtool_draw_image(struct rrd_image *image)
34 {
35         int pid;
36         char cmd[] = RRDTOOL_CMD;
37 //      char cmd[] = "echo";
38         char *args[512], argstr[ARGSTR_LEN];
39         int idx = 0, argcnt = 0, i,j;
40         char timestamp[256];
41         char tmp[sizeof(timestamp)];
42         char tmpfile[256];
43         time_t t = time(0);
44         const char *updatestr = "Last update %d.%m.%Y %T (%Z)";
45
46         pid = do_fork_limited();
47         if (pid)
48                 return pid;
49
50         pr_info("Drawing image %s\n", image->image_filename);
51
52         tmpfile[0] = 0;
53         strncat(tmpfile, image->image_filename, sizeof(tmp) - strlen(tmp) - 1);
54         strncat(tmpfile, ".tmp", sizeof(tmp) - strlen(tmp) - 1);
55
56         if (image->updatestr)
57                 updatestr = image->updatestr;
58
59         strftime(tmp, sizeof(tmp), updatestr, localtime(&t));
60         for (i = 0, j = 0; j < sizeof(tmp);) {
61                 if (tmp[i] == ':') {
62                         timestamp[j++] = '\\';
63                 }
64                 timestamp[j++] = tmp[i++];
65                 if (!tmp[i])
66                         break;
67         }
68         timestamp[j] = 0;
69
70
71         add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
72         add_arg(args, argcnt, argstr, idx, "graph");
73         add_arg(args, argcnt, argstr, idx, "%s", tmpfile);
74
75         add_arg(args, argcnt, argstr, idx, "--start");
76         add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
77         add_arg(args, argcnt, argstr, idx, "--end");
78         add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
79         add_arg(args, argcnt, argstr, idx, "--width");
80         add_arg(args, argcnt, argstr, idx, "%d", image->width);
81         add_arg(args, argcnt, argstr, idx, "--height");
82         add_arg(args, argcnt, argstr, idx, "%d", image->height);
83         add_arg(args, argcnt, argstr, idx, "--imgformat");
84         add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
85
86         for (i = 0; image->options[i]; i++) {
87                 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
88         }
89
90         for (i = 0; image->text[i]; i++) {
91                 args[argcnt++] = (char *)image->text[i];
92         }
93
94         add_arg(args, argcnt, argstr, idx, "COMMENT: %s\\c", timestamp);
95
96         args[argcnt] = 0;
97
98         pid = run(cmd, args);
99         harvest_zombies(pid);
100
101         rename(tmpfile, image->image_filename);
102
103         exit(0);
104 }
105
106 int rrdtool_draw_images(struct rrd_image **image)
107 {
108         int i;
109         for (i = 0; image[i]; i++)
110                 rrdtool_draw_image(image[i]);
111
112         return 0;
113 }
114
115 static int sanitize_rrd_update_data(char *data)
116 {
117         char clean_data[RRD_DATA_MAX_LEN];
118         int entries = 0;
119         int minus;
120         char *src, *end, *cln;
121
122         data[RRD_DATA_MAX_LEN - 1] = 0;
123         src = data;
124         cln = clean_data;
125
126         /*
127          * Copy a legit floating point number to clean_data buffer
128          * starting from *src and ending to next ':'. If no legit
129          * number could be found, put a 'U' there instead to make
130          * rrdtool to understand this datapoint is undefined.
131          */
132
133         while (src < data + RRD_DATA_MAX_LEN && *src) {
134                 minus = 0;
135
136                 /* skip any non_numbers but not ':' */
137                 while (*src && !isdigit(*src) && *src != '-' && *src != ':')
138                         src++;
139
140                 if (*src == '-') {
141                         src++;
142                         minus = 1;
143                 }
144
145                 /* Now find the end of the number */
146                 end = skip_numbers(src);
147
148                 /* Floating point numberrs may have a dot with more numbers */
149                 if (*end == '.') {
150                         end++;
151                         end = skip_numbers(end);
152                 }
153
154                 /*
155                  * Now we have gone past the number, there should be a
156                  * colon or zero byte. If src == end, there was no
157                  * number and the entry is undefined instead.
158                  */
159                 if ((*end == ':' || !*end) && src != end) {
160                         if (minus) {
161                                 *cln = '-';
162                                 cln++;
163                         }
164
165                         /*
166                          * Copy the legit number and start copying the
167                          * next one
168                          */
169                         for (; src <= end; src++, cln++)
170                                 *cln = *src;
171
172                         goto next;
173                 }
174
175                 /* Skip over whatever junk there might be */
176                 while (*end != ':' && *end)
177                         end++;
178
179                 /* Mark the entry as undefined */
180                 *cln = 'U';
181                 cln++;
182                 *cln = ':';
183                 cln++;
184         next:
185                 end++;
186                 src = end;
187                 entries++;
188         }
189
190         /*
191          * If last entry was undefined, we need to remove the extra
192          * colon at the end
193          */
194         if (*(cln - 1) == ':')
195                 cln--;
196         *cln = '\0';
197
198         strncpy(data, clean_data, RRD_DATA_MAX_LEN);
199         return entries;
200 }
201
202 int rrdtool_update_data(struct rrd_database *rrd)
203 {
204         int pid;
205         char data[RRD_DATA_MAX_LEN + 3]; /* 3 == "N:" + NULL termination */
206         char cmd[] = RRDTOOL_CMD;
207 //      char cmd[] = "echo";
208         char *const cmdline[] = {
209                 RRDTOOL_CMD,
210                 "update",
211                 (char *const)rrd->filename,
212                 data,
213                 0
214         };
215         int l;
216
217         rrd->last_update = time(0);
218         if (do_fork())
219                 return 0;
220
221         l = sprintf(data, "N:");
222
223         if (rrd->parse) {
224                 rrd->parse(data + l, rrd->parser_data);
225                 data[RRD_DATA_MAX_LEN + 2] = '\0';
226
227                 pr_info("Data: %s\n", data);
228
229                 sanitize_rrd_update_data(data + l);
230                 pid = run(cmd, cmdline);
231                 harvest_zombies(pid);
232         }
233
234         if (rrd->pre_draw_cmd && !strcmp(rrd->pre_draw_cmd[0], "shell")) {
235                 pid = run(rrd->pre_draw_cmd[1], &rrd->pre_draw_cmd[1]);
236                 harvest_zombies(pid);
237         }
238
239         if (rrd->images)
240                 rrdtool_draw_images(rrd->images);
241
242         while (harvest_zombies(0));
243         exit(0);
244 }
245
246 /*
247  * Walk through the database list and return the first database which
248  * last update is too far in past
249  */
250 struct rrd_database *get_outdated_db(struct rrd_database **dblist)
251 {
252         int i;
253         time_t now = time(0);
254
255         for (i = 0; dblist[i]; i++) {
256                 if ((dblist[i]->last_update + dblist[i]->interval) - now <= 0)
257                         return dblist[i];
258         }
259
260         /* Nothing to update this time, return null */
261         return NULL;
262 }
263
264 /*
265  * See how long we may sleep until it is required to run an update
266  * again
267  */
268 int get_next_update(struct rrd_database **dblist, const char **name)
269 {
270         int i, sleeptime = 0, diff;
271         time_t now = time(0);
272
273         for (i = 0; dblist[i]; i++) {
274                 diff = dblist[i]->last_update + dblist[i]->interval - now;
275                 if (!sleeptime) {
276                         sleeptime = diff;
277                         *name = dblist[i]->name;
278                 }
279                 if (sleeptime > diff) {
280                         sleeptime = diff;
281                         *name = dblist[i]->name;
282                 }
283                 if (sleeptime <= 0)
284                         return 0;
285         }
286
287         return sleeptime;
288 }
289
290 static int database_exists(struct rrd_database *db)
291 {
292         struct stat s;
293
294         /* If the filename exists, stat will return zero */
295         if (db->filename)
296                 return !stat(db->filename, &s);
297
298         return 0;
299 }
300
301 static int create_database(struct rrd_database *db)
302 {
303         char cmd[] = RRDTOOL_CMD;
304 //      char cmd[] = "echo";
305         char *args[512], argstr[ARGSTR_LEN];
306         int idx = 0, argcnt = 0;
307         int child, i;
308
309         if (!db->filename) {
310                 pr_err("Database %s missing database filename\n", db->name);
311                 return -1;
312         }
313
314         if (!db->sources || !db->archives) {
315                 pr_err("Cannot create db \"%s\", insufficient source data\n",
316                         db->filename);
317                 return -1;
318         }
319
320         add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
321         add_arg(args, argcnt, argstr, idx, "create");
322         add_arg(args, argcnt, argstr, idx, "%s", db->filename);
323         add_arg(args, argcnt, argstr, idx, "--step");
324         add_arg(args, argcnt, argstr, idx, "%d", db->interval);
325
326         for (i = 0; db->sources[i].type; i++) {
327                 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
328                         db->sources[i].name,
329                         db->sources[i].type,
330                         db->sources[i].heartbeat,
331                         db->sources[i].min,
332                         db->sources[i].max);
333         }
334
335         for (i = 0; db->archives[i].type; i++) {
336                 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
337                         db->archives[i].type,
338                         db->archives[i].xff,
339                         db->archives[i].steps,
340                         db->archives[i].rows);
341         }
342
343         child = run(cmd, args);
344
345         harvest_zombies(child);
346
347         return 0;
348 }
349
350 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
351 {
352         struct rrd_database *db;
353         int i, ret = 0;
354
355         for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
356                 if (database_exists(db)) {
357                         pr_info("database %s found\n", db->filename);
358                         continue;
359                 }
360                 pr_info("Database %s missing, creating\n", db->filename);
361                 ret |= create_database(db);
362         }
363
364         return ret;
365 }