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)
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 pid = do_fork_limited();
52 pr_info("Drawing image %s\n", image->image_filename);
55 strncat(tmpfile, image->image_filename, sizeof(tmp) - strlen(tmp) - 1);
56 strncat(tmpfile, ".tmp", sizeof(tmp) - strlen(tmp) - 1);
59 updatestr = image->updatestr;
61 strftime(tmp, sizeof(tmp), updatestr, localtime(&t));
62 for (i = 0, j = 0; j < sizeof(tmp);) {
64 timestamp[j++] = '\\';
66 timestamp[j++] = tmp[i++];
73 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
74 add_arg(args, argcnt, argstr, idx, "graph");
75 add_arg(args, argcnt, argstr, idx, "%s", tmpfile);
77 add_arg(args, argcnt, argstr, idx, "--start");
78 add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
79 add_arg(args, argcnt, argstr, idx, "--end");
80 add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
81 add_arg(args, argcnt, argstr, idx, "--width");
82 add_arg(args, argcnt, argstr, idx, "%d", image->width);
83 add_arg(args, argcnt, argstr, idx, "--height");
84 add_arg(args, argcnt, argstr, idx, "%d", image->height);
85 add_arg(args, argcnt, argstr, idx, "--imgformat");
86 add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
88 for (i = 0; image->options[i]; i++) {
89 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
92 for (i = 0; image->text[i]; i++) {
93 args[argcnt++] = (char *)image->text[i];
96 add_arg(args, argcnt, argstr, idx, "COMMENT: %s\\c", timestamp);
101 rename(tmpfile, image->image_filename);
106 int rrdtool_draw_images(struct rrd_image **image)
109 for (i = 0; image[i]; i++)
110 rrdtool_draw_image(image[i]);
115 static int sanitize_rrd_update_data(char *data)
117 char clean_data[RRD_DATA_MAX_LEN];
120 char *src, *end, *cln;
122 data[RRD_DATA_MAX_LEN - 1] = 0;
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.
133 while (src < data + RRD_DATA_MAX_LEN && *src) {
136 /* skip any non_numbers but not ':' */
137 while (*src && !isdigit(*src) && *src != '-' && *src != ':')
145 /* Now find the end of the number */
146 end = skip_numbers(src);
148 /* Floating point numberrs may have a dot with more numbers */
151 end = skip_numbers(end);
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.
159 if ((*end == ':' || !*end) && src != end) {
166 * Copy the legit number and start copying the
169 for (; src <= end; src++, cln++)
175 /* Skip over whatever junk there might be */
176 while (*end != ':' && *end)
179 /* Mark the entry as undefined */
191 * If last entry was undefined, we need to remove the extra
194 if (*(cln - 1) == ':')
198 strncpy(data, clean_data, RRD_DATA_MAX_LEN);
202 static int write_to_logfile(struct rrd_database *rrd, const char *data)
204 time_t t = time(NULL);
208 char logstr[RRD_DATA_MAX_LEN * 2] = { 0 };
209 const char *time_stamp_fmt = "%Y.%m.%d %H:%M ";
215 if (rrd->logfile_timestamp_fmt)
216 time_stamp_fmt = rrd->logfile_timestamp_fmt;
218 strftime(filename, sizeof(filename), rrd->logfile, localtime(&t));
220 fd = open(filename, O_RDWR | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
222 pr_err("Failed to open file %s for logging: %m\n", filename);
226 strftime(logstr, sizeof(logstr), time_stamp_fmt, localtime(&t));
228 str_ptr = logstr + strlen(logstr);
230 data += 2; /* 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 int rrdtool_update_data(struct rrd_database *rrd)
261 char data[RRD_DATA_MAX_LEN + 3]; /* 3 == "N:" + NULL termination */
262 char cmd[] = RRDTOOL_CMD;
263 // char cmd[] = "echo";
264 char *const cmdline[] = {
267 (char *const)rrd->filename,
273 rrd->last_update = time(0);
277 l = sprintf(data, "N:");
279 if (rrd->parser && rrd->parser->parse) {
280 rrd->parser->parse(data + l, rrd->parser_data);
281 data[RRD_DATA_MAX_LEN + 2] = '\0';
283 pr_info("Data: %s\n", data);
285 sanitize_rrd_update_data(data + l);
286 write_to_logfile(rrd, data);
288 pid = run(cmd, cmdline);
289 harvest_zombies(pid);
292 if (rrd->pre_draw_cmd && !strcmp(rrd->pre_draw_cmd[0], "shell")) {
293 pid = run(rrd->pre_draw_cmd[1], &rrd->pre_draw_cmd[1]);
294 harvest_zombies(pid);
298 rrdtool_draw_images(rrd->images);
300 while (harvest_zombies(0));
305 * Walk through the database list and return the first database which
306 * last update is too far in past
308 struct rrd_database *get_outdated_db(struct rrd_database **dblist)
311 time_t now = time(0);
313 for (i = 0; dblist[i]; i++) {
314 if ((dblist[i]->last_update + dblist[i]->interval) - now <= 0)
318 /* Nothing to update this time, return null */
323 * See how long we may sleep until it is required to run an update
326 int get_next_update(struct rrd_database **dblist, const char **name)
328 int i, sleeptime = 0, diff;
329 time_t now = time(0);
331 for (i = 0; dblist[i]; i++) {
332 diff = dblist[i]->last_update + dblist[i]->interval - now;
335 *name = dblist[i]->name;
337 if (sleeptime > diff) {
339 *name = dblist[i]->name;
348 static int database_exists(struct rrd_database *db)
352 /* If the filename exists, stat will return zero */
354 return !stat(db->filename, &s);
359 static int create_database(struct rrd_database *db)
361 char cmd[] = RRDTOOL_CMD;
362 // char cmd[] = "echo";
363 char *args[512], argstr[ARGSTR_LEN];
364 int idx = 0, argcnt = 0;
368 pr_err("Database %s missing database filename\n", db->name);
372 if (!db->sources || !db->archives) {
373 pr_err("Cannot create db \"%s\", insufficient source data\n",
378 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
379 add_arg(args, argcnt, argstr, idx, "create");
380 add_arg(args, argcnt, argstr, idx, "%s", db->filename);
381 add_arg(args, argcnt, argstr, idx, "--step");
382 add_arg(args, argcnt, argstr, idx, "%d", db->interval);
384 for (i = 0; db->sources[i].type; i++) {
385 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
388 db->sources[i].heartbeat,
393 for (i = 0; db->archives[i].type; i++) {
394 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
395 db->archives[i].type,
397 db->archives[i].steps,
398 db->archives[i].rows);
401 child = run(cmd, args);
403 harvest_zombies(child);
408 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
410 struct rrd_database *db;
413 for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
414 if (database_exists(db)) {
415 pr_info("database %s found\n", db->filename);
418 pr_info("Database %s missing, creating\n", db->filename);
419 ret |= create_database(db);