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