17 #define ARGSTR_LEN 32768
19 #define RRDTOOL_CMD "/usr/bin/rrdtool"
22 * Add new argument to a argument list
24 * args pointer list to arguments
25 * argcnt argument counter
26 * argstr array where the actual arguments are stored
27 * idx index in the argstr where the new argument will be appended
29 #define add_arg(args, argcnt, argstr, idx, fmt, arg...) \
30 args[argcnt] = argstr + idx; \
31 idx += sprintf(argstr + idx, fmt, ##arg); \
36 int rrdtool_draw_image(struct rrd_image *image)
38 char cmd[] = RRDTOOL_CMD;
39 // char cmd[] = "echo";
40 char *args[512], argstr[ARGSTR_LEN];
41 int idx = 0, argcnt = 0, i,j;
43 char tmp[sizeof(timestamp)];
46 const char *updatestr = "Last update %d.%m.%Y %T (%Z)";
48 pr_info("Drawing image %s\n", image->image_filename);
52 strncpy(tmpfile, image->image_filename, sizeof(tmpfile) - 1);
53 _strlcat(tmpfile, ".tmp", sizeof(tmpfile));
56 updatestr = image->updatestr;
58 strftime(tmp, sizeof(tmp), updatestr, localtime(&t));
59 for (i = 0, j = 0; j < sizeof(tmp);) {
61 timestamp[j++] = '\\';
63 timestamp[j++] = tmp[i++];
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);
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);
85 for (i = 0; image->options[i]; i++) {
86 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
89 for (i = 0; image->text[i]; i++) {
90 args[argcnt++] = (char *)image->text[i];
93 add_arg(args, argcnt, argstr, idx, "COMMENT: %s\\c", timestamp);
97 rename(tmpfile, image->image_filename);
102 int rrdtool_draw_images(struct rrd_image **image)
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]);
112 static int sanitize_rrd_update_data(char *data)
114 char clean_data[RRD_DATA_MAX_LEN];
117 char *src, *end, *cln;
119 data[RRD_DATA_MAX_LEN - 1] = 0;
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.
130 while (src < data + RRD_DATA_MAX_LEN && *src) {
133 /* skip any non_numbers but not ':' */
134 while (*src && !isdigit(*src) && *src != '-' && *src != ':')
142 /* Now find the end of the number */
143 end = skip_numbers(src);
145 /* Floating point numberrs may have a dot with more numbers */
148 end = skip_numbers(end);
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.
156 if ((*end == ':' || !*end) && src != end) {
163 * Copy the legit number and start copying the
166 for (; src <= end; src++, cln++)
172 /* Skip over whatever junk there might be */
173 while (*end != ':' && *end)
176 /* Mark the entry as undefined */
188 * If last entry was undefined, we need to remove the extra
191 if (*(cln - 1) == ':')
195 strncpy(data, clean_data, RRD_DATA_MAX_LEN);
199 static int write_to_logfile(struct rrd_database *rrd, const char *data, time_t now)
204 char logstr[RRD_DATA_MAX_LEN * 2] = { 0 };
205 const char *time_stamp_fmt = "%Y.%m.%d %H:%M ";
211 if (rrd->logfile_timestamp_fmt)
212 time_stamp_fmt = rrd->logfile_timestamp_fmt;
214 strftime(filename, sizeof(filename), rrd->logfile, localtime(&now));
216 fd = open(filename, O_RDWR | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
218 pr_err("Failed to open file %s for logging: %m\n", filename);
222 strftime(logstr, sizeof(logstr), time_stamp_fmt, localtime(&now));
224 str_ptr = logstr + strlen(logstr);
226 /* Skip the "N: part */
233 while (*data && str_ptr - logstr < sizeof(logstr) - 1) {
236 for (i = 0; i < spacing; i++)
243 *str_ptr++ = *data++;
249 ret = write(fd, logstr, strlen(logstr));
251 pr_err("Failed to write to logfile %s: %m\n", filename);
255 return ret < 0 ? ret : 0;
258 static int run_post_draw_cmd(struct rrd_database *rrd)
260 pr_info("Running post draw command for %s\n", rrd->name);
262 if (rrd->post_draw_cmd && !strcmp(rrd->post_draw_cmd[0], "shell"))
263 run(rrd->post_draw_cmd[1], &rrd->post_draw_cmd[1]);
268 static int rrdtool_update_data_multi(struct rrd_database *rrd)
272 char cmd[] = RRDTOOL_CMD;
273 char *cmdline[512] = {
276 (char *const)rrd->filename,
278 int old_last_update = rrd->last_update;
280 ret = rrd->parser->parse_multi(&data, rrd->parser_data,
281 &rrd->parser_state, rrd->last_update);
283 pr_err("Parser failure: %d\n", ret);
287 for (i = 3, d = 0; i < ARRAY_SIZE(cmdline) - 1; i++, d++) {
293 sanitize_rrd_update_data(data[d]);
295 then = atoi(data[d]);
296 write_to_logfile(rrd, data[d], then);
297 cmdline[i] = data[d];
298 pr_info("Data: %s\n", data[d]);
300 rrd->last_update = then;
310 for (d = 0; data[d]; d++)
314 if (old_last_update == rrd->last_update) {
315 rrd->update_backoff = time(NULL) + 10;
316 pr_info("Setting backoff\n");
318 rrd->update_backoff = 0;
321 * Re-schedule job processing in case we are too far behind
322 * with updates on this database and can start parsing more
325 notify_job_request();
330 static int do_rrdtool_update_data(struct rrd_database *rrd)
332 char data[RRD_DATA_MAX_LEN + 12]; /* 12 == "%s:" + NULL termination */
333 char cmd[] = RRDTOOL_CMD;
334 // char cmd[] = "echo";
335 char *const cmdline[] = {
338 (char *const)rrd->filename,
343 time_t now = time(NULL);
345 bzero(data, sizeof(data));
346 l = sprintf(data, "%zd:", now);
348 if (rrd->parser && rrd->parser->parse_multi) {
349 rrdtool_update_data_multi(rrd);
350 } else if (rrd->parser && rrd->parser->parse) {
351 rrd->parser->parse(data + l, rrd->parser_data,
353 data[RRD_DATA_MAX_LEN + l] = '\0';
355 pr_info("Data: %s\n", data);
357 sanitize_rrd_update_data(data + l);
358 write_to_logfile(rrd, data, now);
361 rrd->last_update = now;
363 rrd->last_update = now;
365 if (rrd->pre_draw_cmd && !strcmp(rrd->pre_draw_cmd[0], "shell")) {
366 run(rrd->pre_draw_cmd[1], &rrd->pre_draw_cmd[1]);
370 rrdtool_draw_images(rrd->images);
373 * We rely on the fact that rrdtool_draw_images queues image
374 * drawings into low priority queue and the post draw queue is
375 * placed on the queue after images. This ensures post draw
376 * command is not started before images are started.
378 * There is nothing that guarantees post_draw_cmd is executed
379 * after all images are completed though, but it's close..
381 if (rrd->post_draw_cmd)
382 queue_work(WORK_PRIORITY_LOW, "rrdtool_post_draw_cmd",
383 (work_fn_t *)run_post_draw_cmd, rrd);
385 rrd->update_active = 0;
390 int rrdtool_update_data(struct rrd_database *rrd)
392 rrd->update_active = 1;
394 return queue_work(WORK_PRIORITY_HIGH, "rrdtool_update_data",
395 (work_fn_t *)do_rrdtool_update_data, rrd);
399 * Walk through the database list and return the first database which
400 * last update is too far in past
402 struct rrd_database *get_outdated_db(struct rrd_database **dblist)
405 time_t now = time(0), last;
407 for (i = 0; dblist[i]; i++) {
408 last = max(ROUND_UP(dblist[i]->last_update, dblist[i]->interval),
409 dblist[i]->update_backoff);
410 if (!dblist[i]->update_active && last - now <= 0)
414 /* Nothing to update this time, return null */
419 * See how long we may sleep until next update interval window begins
421 int get_next_update(struct rrd_database **dblist, const char **name)
423 int i, sleeptime = 0, diff;
424 time_t now = time(0);
426 for (i = 0; dblist[i]; i++) {
427 diff = ROUND_UP(dblist[i]->last_update, dblist[i]->interval) - now;
428 diff = max(diff, dblist[i]->update_backoff - now);
430 if (dblist[i]->update_active)
431 diff = (now + dblist[i]->interval) % dblist[i]->interval;
435 *name = dblist[i]->name;
437 if (sleeptime > diff) {
439 *name = dblist[i]->name;
449 static int database_exists(struct rrd_database *db)
453 /* If the filename exists, stat will return zero */
455 return !stat(db->filename, &s);
460 static int get_last_update(struct rrd_database *db)
462 char cmd[] = RRDTOOL_CMD;
463 char *args[10], argstr[ARGSTR_LEN];
465 int idx = 0, argcnt = 0;
469 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
470 add_arg(args, argcnt, argstr, idx, "last");
471 add_arg(args, argcnt, argstr, idx, db->filename);
473 child = run_piped(cmd, args, NULL, &ofd, &efd);
474 ret = read(ofd, buf, sizeof(buf) - 1);
476 pr_err("Error reading: %m\n");
482 db->last_update = atoi(buf);
483 pr_info("Last update for %s is: %ld, %ld sec ago\n", db->name, db->last_update,
484 time(NULL) - db->last_update);
491 static int create_database(struct rrd_database *db)
493 char cmd[] = RRDTOOL_CMD;
494 // char cmd[] = "echo";
495 char *args[512], argstr[ARGSTR_LEN];
496 int idx = 0, argcnt = 0;
500 pr_err("Database %s missing database filename\n", db->name);
504 if (!db->sources || !db->archives) {
505 pr_err("Cannot create db \"%s\", insufficient source data\n",
510 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
511 add_arg(args, argcnt, argstr, idx, "create");
512 add_arg(args, argcnt, argstr, idx, "%s", db->filename);
513 add_arg(args, argcnt, argstr, idx, "--step");
514 add_arg(args, argcnt, argstr, idx, "%d", db->interval);
516 for (i = 0; db->sources[i].type; i++) {
517 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
520 db->sources[i].heartbeat,
525 for (i = 0; db->archives[i].type; i++) {
526 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
527 db->archives[i].type,
529 db->archives[i].steps,
530 db->archives[i].rows);
538 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
540 struct rrd_database *db;
543 for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
544 if (database_exists(db)) {
545 pr_info("database %s found\n", db->filename);
549 pr_info("Database %s missing, creating\n", db->filename);
550 ret |= create_database(db);