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