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