16 #define ARGSTR_LEN 32768
18 #define RRDTOOL_CMD "/usr/bin/rrdtool"
21 * Add new argument to a argument list
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
28 #define add_arg(args, argcnt, argstr, idx, fmt, arg...) \
29 args[argcnt] = argstr + idx; \
30 idx += sprintf(argstr + idx, fmt, ##arg); \
35 int rrdtool_draw_image(struct rrd_image *image)
37 char cmd[] = RRDTOOL_CMD;
38 // char cmd[] = "echo";
39 char *args[512], argstr[ARGSTR_LEN];
40 int idx = 0, argcnt = 0, i,j;
42 char tmp[sizeof(timestamp)];
45 const char *updatestr = "Last update %d.%m.%Y %T (%Z)";
47 pr_info("Drawing image %s\n", image->image_filename);
51 strncpy(tmpfile, image->image_filename, sizeof(tmpfile) - 1);
52 strncat(tmpfile, ".tmp",
53 sizeof(tmpfile) - strlen(image->image_filename) - 1);
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 do_rrdtool_update_data(struct rrd_database *rrd)
270 char data[RRD_DATA_MAX_LEN + 12]; /* 12 == "%s:" + NULL termination */
271 char cmd[] = RRDTOOL_CMD;
272 // char cmd[] = "echo";
273 char *const cmdline[] = {
276 (char *const)rrd->filename,
281 time_t now = time(NULL);
283 bzero(data, sizeof(data));
284 l = sprintf(data, "%zd:", now);
286 if (rrd->parser && rrd->parser->parse) {
287 rrd->parser->parse(data + l, rrd->parser_data,
289 data[RRD_DATA_MAX_LEN + 2] = '\0';
291 pr_info("Data: %s\n", data);
293 sanitize_rrd_update_data(data + l);
294 write_to_logfile(rrd, data, now);
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]);
304 rrdtool_draw_images(rrd->images);
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.
312 * There is nothing that guarantees post_draw_cmd is executed
313 * after all images are completed though, but it's close..
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);
322 int rrdtool_update_data(struct rrd_database *rrd)
324 time_t now = time(0);
327 * This will put our last update slightly into past, but
328 * ensures our update interval will not drift over time.
330 rrd->last_update = now - now % rrd->interval;
332 return queue_work(WORK_PRIORITY_HIGH, "rrdtool_update_data",
333 (work_fn_t *)do_rrdtool_update_data, rrd);
337 * Walk through the database list and return the first database which
338 * last update is too far in past
340 struct rrd_database *get_outdated_db(struct rrd_database **dblist)
343 time_t now = time(0);
345 for (i = 0; dblist[i]; i++) {
346 if ((dblist[i]->last_update + dblist[i]->interval) - now <= 0)
350 /* Nothing to update this time, return null */
355 * See how long we may sleep until it is required to run an update
358 int get_next_update(struct rrd_database **dblist, const char **name)
360 int i, sleeptime = 0, diff;
361 time_t now = time(0);
363 for (i = 0; dblist[i]; i++) {
364 diff = dblist[i]->last_update + dblist[i]->interval - now;
367 *name = dblist[i]->name;
369 if (sleeptime > diff) {
371 *name = dblist[i]->name;
380 static int database_exists(struct rrd_database *db)
384 /* If the filename exists, stat will return zero */
386 return !stat(db->filename, &s);
391 static int get_last_update(struct rrd_database *db)
393 char cmd[] = RRDTOOL_CMD;
394 char *args[10], argstr[ARGSTR_LEN];
396 int idx = 0, argcnt = 0;
400 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
401 add_arg(args, argcnt, argstr, idx, "last");
402 add_arg(args, argcnt, argstr, idx, db->filename);
404 child = run_piped(cmd, args, NULL, &ofd, &efd);
405 ret = read(ofd, buf, sizeof(buf) - 1);
407 pr_err("Error reading: %m\n");
413 db->last_update = atoi(buf);
420 static int create_database(struct rrd_database *db)
422 char cmd[] = RRDTOOL_CMD;
423 // char cmd[] = "echo";
424 char *args[512], argstr[ARGSTR_LEN];
425 int idx = 0, argcnt = 0;
429 pr_err("Database %s missing database filename\n", db->name);
433 if (!db->sources || !db->archives) {
434 pr_err("Cannot create db \"%s\", insufficient source data\n",
439 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
440 add_arg(args, argcnt, argstr, idx, "create");
441 add_arg(args, argcnt, argstr, idx, "%s", db->filename);
442 add_arg(args, argcnt, argstr, idx, "--step");
443 add_arg(args, argcnt, argstr, idx, "%d", db->interval);
445 for (i = 0; db->sources[i].type; i++) {
446 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
449 db->sources[i].heartbeat,
454 for (i = 0; db->archives[i].type; i++) {
455 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
456 db->archives[i].type,
458 db->archives[i].steps,
459 db->archives[i].rows);
467 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
469 struct rrd_database *db;
472 for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
473 if (database_exists(db)) {
474 pr_info("database %s found\n", db->filename);
478 pr_info("Database %s missing, creating\n", db->filename);
479 ret |= create_database(db);