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); \
34 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 pid = do_fork_limited();
51 pr_info("Drawing image %s\n", image->image_filename);
54 strncat(tmpfile, image->image_filename, sizeof(tmp) - strlen(tmp) - 1);
55 strncat(tmpfile, ".tmp", sizeof(tmp) - strlen(tmp) - 1);
58 updatestr = image->updatestr;
60 strftime(tmp, sizeof(tmp), updatestr, localtime(&t));
61 for (i = 0, j = 0; j < sizeof(tmp);) {
63 timestamp[j++] = '\\';
65 timestamp[j++] = tmp[i++];
72 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
73 add_arg(args, argcnt, argstr, idx, "graph");
74 add_arg(args, argcnt, argstr, idx, "%s", tmpfile);
76 add_arg(args, argcnt, argstr, idx, "--start");
77 add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
78 add_arg(args, argcnt, argstr, idx, "--end");
79 add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
80 add_arg(args, argcnt, argstr, idx, "--width");
81 add_arg(args, argcnt, argstr, idx, "%d", image->width);
82 add_arg(args, argcnt, argstr, idx, "--height");
83 add_arg(args, argcnt, argstr, idx, "%d", image->height);
84 add_arg(args, argcnt, argstr, idx, "--imgformat");
85 add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
87 for (i = 0; image->options[i]; i++) {
88 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
91 for (i = 0; image->text[i]; i++) {
92 args[argcnt++] = (char *)image->text[i];
95 add_arg(args, argcnt, argstr, idx, "COMMENT: %s\\c", timestamp);
100 harvest_zombies(pid);
102 rename(tmpfile, image->image_filename);
107 int rrdtool_draw_images(struct rrd_image **image)
110 for (i = 0; image[i]; i++)
111 rrdtool_draw_image(image[i]);
116 static int sanitize_rrd_update_data(char *data)
118 char clean_data[RRD_DATA_MAX_LEN];
121 char *src, *end, *cln;
123 data[RRD_DATA_MAX_LEN - 1] = 0;
128 * Copy a legit floating point number to clean_data buffer
129 * starting from *src and ending to next ':'. If no legit
130 * number could be found, put a 'U' there instead to make
131 * rrdtool to understand this datapoint is undefined.
134 while (src < data + RRD_DATA_MAX_LEN && *src) {
137 /* skip any non_numbers but not ':' */
138 while (*src && !isdigit(*src) && *src != '-' && *src != ':')
146 /* Now find the end of the number */
147 end = skip_numbers(src);
149 /* Floating point numberrs may have a dot with more numbers */
152 end = skip_numbers(end);
156 * Now we have gone past the number, there should be a
157 * colon or zero byte. If src == end, there was no
158 * number and the entry is undefined instead.
160 if ((*end == ':' || !*end) && src != end) {
167 * Copy the legit number and start copying the
170 for (; src <= end; src++, cln++)
176 /* Skip over whatever junk there might be */
177 while (*end != ':' && *end)
180 /* Mark the entry as undefined */
192 * If last entry was undefined, we need to remove the extra
195 if (*(cln - 1) == ':')
199 strncpy(data, clean_data, RRD_DATA_MAX_LEN);
203 static int write_to_logfile(struct rrd_database *rrd, const char *data)
205 time_t t = time(NULL);
209 char logstr[RRD_DATA_MAX_LEN * 2] = { 0 };
210 const char *time_stamp_fmt = "%Y.%m.%d %H:%M ";
216 if (rrd->logfile_timestamp_fmt)
217 time_stamp_fmt = rrd->logfile_timestamp_fmt;
219 strftime(filename, sizeof(filename), rrd->logfile, localtime(&t));
221 fd = open(filename, O_RDWR | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
223 pr_err("Failed to open file %s for logging: %m\n", filename);
227 strftime(logstr, sizeof(logstr), time_stamp_fmt, localtime(&t));
229 str_ptr = logstr + strlen(logstr);
231 data += 2; /* Skip the "N: part */
234 while (*data && str_ptr - logstr < sizeof(logstr) - 1) {
237 for (i = 0; i < spacing; i++)
244 *str_ptr++ = *data++;
250 ret = write(fd, logstr, strlen(logstr));
252 pr_err("Failed to write to logfile %s: %m\n", filename);
256 return ret < 0 ? ret : 0;
259 int rrdtool_update_data(struct rrd_database *rrd)
262 char data[RRD_DATA_MAX_LEN + 3]; /* 3 == "N:" + NULL termination */
263 char cmd[] = RRDTOOL_CMD;
264 // char cmd[] = "echo";
265 char *const cmdline[] = {
268 (char *const)rrd->filename,
274 rrd->last_update = time(0);
278 l = sprintf(data, "N:");
280 if (rrd->parser && rrd->parser->parse) {
281 rrd->parser->parse(data + l, rrd->parser_data);
282 data[RRD_DATA_MAX_LEN + 2] = '\0';
284 pr_info("Data: %s\n", data);
286 sanitize_rrd_update_data(data + l);
287 write_to_logfile(rrd, data);
289 pid = run(cmd, cmdline);
290 harvest_zombies(pid);
293 if (rrd->pre_draw_cmd && !strcmp(rrd->pre_draw_cmd[0], "shell")) {
294 pid = run(rrd->pre_draw_cmd[1], &rrd->pre_draw_cmd[1]);
295 harvest_zombies(pid);
299 rrdtool_draw_images(rrd->images);
301 while (harvest_zombies(0));
306 * Walk through the database list and return the first database which
307 * last update is too far in past
309 struct rrd_database *get_outdated_db(struct rrd_database **dblist)
312 time_t now = time(0);
314 for (i = 0; dblist[i]; i++) {
315 if ((dblist[i]->last_update + dblist[i]->interval) - now <= 0)
319 /* Nothing to update this time, return null */
324 * See how long we may sleep until it is required to run an update
327 int get_next_update(struct rrd_database **dblist, const char **name)
329 int i, sleeptime = 0, diff;
330 time_t now = time(0);
332 for (i = 0; dblist[i]; i++) {
333 diff = dblist[i]->last_update + dblist[i]->interval - now;
336 *name = dblist[i]->name;
338 if (sleeptime > diff) {
340 *name = dblist[i]->name;
349 static int database_exists(struct rrd_database *db)
353 /* If the filename exists, stat will return zero */
355 return !stat(db->filename, &s);
360 static int create_database(struct rrd_database *db)
362 char cmd[] = RRDTOOL_CMD;
363 // char cmd[] = "echo";
364 char *args[512], argstr[ARGSTR_LEN];
365 int idx = 0, argcnt = 0;
369 pr_err("Database %s missing database filename\n", db->name);
373 if (!db->sources || !db->archives) {
374 pr_err("Cannot create db \"%s\", insufficient source data\n",
379 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
380 add_arg(args, argcnt, argstr, idx, "create");
381 add_arg(args, argcnt, argstr, idx, "%s", db->filename);
382 add_arg(args, argcnt, argstr, idx, "--step");
383 add_arg(args, argcnt, argstr, idx, "%d", db->interval);
385 for (i = 0; db->sources[i].type; i++) {
386 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
389 db->sources[i].heartbeat,
394 for (i = 0; db->archives[i].type; i++) {
395 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
396 db->archives[i].type,
398 db->archives[i].steps,
399 db->archives[i].rows);
402 child = run(cmd, args);
404 harvest_zombies(child);
409 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
411 struct rrd_database *db;
414 for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
415 if (database_exists(db)) {
416 pr_info("database %s found\n", db->filename);
419 pr_info("Database %s missing, creating\n", db->filename);
420 ret |= create_database(db);