]> git.itanic.dy.fi Git - rrdd/blob - rrdtool.c
jobcontrol: Fork bomb prevental
[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->images)
235                 rrdtool_draw_images(rrd->images);
236
237         while (harvest_zombies(0));
238         exit(0);
239 }
240
241 /*
242  * Walk through the database list and return the first database which
243  * last update is too far in past
244  */
245 struct rrd_database *get_outdated_db(struct rrd_database **dblist)
246 {
247         int i;
248         time_t now = time(0);
249
250         for (i = 0; dblist[i]; i++) {
251                 if ((dblist[i]->last_update + dblist[i]->interval) - now <= 0)
252                         return dblist[i];
253         }
254
255         /* Nothing to update this time, return null */
256         return NULL;
257 }
258
259 /*
260  * See how long we may sleep until it is required to run an update
261  * again
262  */
263 int get_next_update(struct rrd_database **dblist, const char **name)
264 {
265         int i, sleeptime = 0, diff;
266         time_t now = time(0);
267
268         for (i = 0; dblist[i]; i++) {
269                 diff = dblist[i]->last_update + dblist[i]->interval - now;
270                 if (!sleeptime) {
271                         sleeptime = diff;
272                         *name = dblist[i]->name;
273                 }
274                 if (sleeptime > diff) {
275                         sleeptime = diff;
276                         *name = dblist[i]->name;
277                 }
278                 if (sleeptime <= 0)
279                         return 0;
280         }
281
282         return sleeptime;
283 }
284
285 static int database_exists(struct rrd_database *db)
286 {
287         struct stat s;
288
289         /* If the filename exists, stat will return zero */
290         if (db->filename)
291                 return !stat(db->filename, &s);
292
293         return 0;
294 }
295
296 static int create_database(struct rrd_database *db)
297 {
298         char cmd[] = RRDTOOL_CMD;
299 //      char cmd[] = "echo";
300         char *args[512], argstr[ARGSTR_LEN];
301         int idx = 0, argcnt = 0;
302         int child, i;
303
304         if (!db->filename) {
305                 pr_err("Database %s missing database filename\n", db->name);
306                 return -1;
307         }
308
309         if (!db->sources || !db->archives) {
310                 pr_err("Cannot create db \"%s\", insufficient source data\n",
311                         db->filename);
312                 return -1;
313         }
314
315         add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
316         add_arg(args, argcnt, argstr, idx, "create");
317         add_arg(args, argcnt, argstr, idx, "%s", db->filename);
318         add_arg(args, argcnt, argstr, idx, "--step");
319         add_arg(args, argcnt, argstr, idx, "%d", db->interval);
320
321         for (i = 0; db->sources[i].type; i++) {
322                 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
323                         db->sources[i].name,
324                         db->sources[i].type,
325                         db->sources[i].heartbeat,
326                         db->sources[i].min,
327                         db->sources[i].max);
328         }
329
330         for (i = 0; db->archives[i].type; i++) {
331                 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
332                         db->archives[i].type,
333                         db->archives[i].xff,
334                         db->archives[i].steps,
335                         db->archives[i].rows);
336         }
337
338         child = run(cmd, args);
339
340         harvest_zombies(child);
341
342         return 0;
343 }
344
345 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
346 {
347         struct rrd_database *db;
348         int i, ret = 0;
349
350         for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
351                 if (database_exists(db)) {
352                         pr_info("database %s found\n", db->filename);
353                         continue;
354                 }
355                 pr_info("Database %s missing, creating\n", db->filename);
356                 ret |= create_database(db);
357         }
358
359         return ret;
360 }