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);
50 strncat(tmpfile, image->image_filename, sizeof(tmp) - 1);
51 strncat(tmpfile, ".tmp", sizeof(tmp) - 1);
54 updatestr = image->updatestr;
56 strftime(tmp, sizeof(tmp), updatestr, localtime(&t));
57 for (i = 0, j = 0; j < sizeof(tmp);) {
59 timestamp[j++] = '\\';
61 timestamp[j++] = tmp[i++];
68 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
69 add_arg(args, argcnt, argstr, idx, "graph");
70 add_arg(args, argcnt, argstr, idx, "%s", tmpfile);
72 add_arg(args, argcnt, argstr, idx, "--start");
73 add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
74 add_arg(args, argcnt, argstr, idx, "--end");
75 add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
76 add_arg(args, argcnt, argstr, idx, "--width");
77 add_arg(args, argcnt, argstr, idx, "%d", image->width);
78 add_arg(args, argcnt, argstr, idx, "--height");
79 add_arg(args, argcnt, argstr, idx, "%d", image->height);
80 add_arg(args, argcnt, argstr, idx, "--imgformat");
81 add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
83 for (i = 0; image->options[i]; i++) {
84 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
87 for (i = 0; image->text[i]; i++) {
88 args[argcnt++] = (char *)image->text[i];
91 add_arg(args, argcnt, argstr, idx, "COMMENT: %s\\c", timestamp);
95 rename(tmpfile, image->image_filename);
100 int rrdtool_draw_images(struct rrd_image **image)
103 for (i = 0; image[i]; i++)
104 queue_work(WORK_PRIORITY_LOW, "rrdtool_draw_image",
105 rrdtool_draw_image, image[i]);
110 static int sanitize_rrd_update_data(char *data)
112 char clean_data[RRD_DATA_MAX_LEN];
115 char *src, *end, *cln;
117 data[RRD_DATA_MAX_LEN - 1] = 0;
122 * Copy a legit floating point number to clean_data buffer
123 * starting from *src and ending to next ':'. If no legit
124 * number could be found, put a 'U' there instead to make
125 * rrdtool to understand this datapoint is undefined.
128 while (src < data + RRD_DATA_MAX_LEN && *src) {
131 /* skip any non_numbers but not ':' */
132 while (*src && !isdigit(*src) && *src != '-' && *src != ':')
140 /* Now find the end of the number */
141 end = skip_numbers(src);
143 /* Floating point numberrs may have a dot with more numbers */
146 end = skip_numbers(end);
150 * Now we have gone past the number, there should be a
151 * colon or zero byte. If src == end, there was no
152 * number and the entry is undefined instead.
154 if ((*end == ':' || !*end) && src != end) {
161 * Copy the legit number and start copying the
164 for (; src <= end; src++, cln++)
170 /* Skip over whatever junk there might be */
171 while (*end != ':' && *end)
174 /* Mark the entry as undefined */
186 * If last entry was undefined, we need to remove the extra
189 if (*(cln - 1) == ':')
193 strncpy(data, clean_data, RRD_DATA_MAX_LEN);
197 static int write_to_logfile(struct rrd_database *rrd, const char *data)
199 time_t t = time(NULL);
203 char logstr[RRD_DATA_MAX_LEN * 2] = { 0 };
204 const char *time_stamp_fmt = "%Y.%m.%d %H:%M ";
210 if (rrd->logfile_timestamp_fmt)
211 time_stamp_fmt = rrd->logfile_timestamp_fmt;
213 strftime(filename, sizeof(filename), rrd->logfile, localtime(&t));
215 fd = open(filename, O_RDWR | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
217 pr_err("Failed to open file %s for logging: %m\n", filename);
221 strftime(logstr, sizeof(logstr), time_stamp_fmt, localtime(&t));
223 str_ptr = logstr + strlen(logstr);
225 data += 2; /* Skip the "N: part */
228 while (*data && str_ptr - logstr < sizeof(logstr) - 1) {
231 for (i = 0; i < spacing; i++)
238 *str_ptr++ = *data++;
244 ret = write(fd, logstr, strlen(logstr));
246 pr_err("Failed to write to logfile %s: %m\n", filename);
250 return ret < 0 ? ret : 0;
253 static int do_rrdtool_update_data(struct rrd_database *rrd)
255 char data[RRD_DATA_MAX_LEN + 3]; /* 3 == "N:" + NULL termination */
256 char cmd[] = RRDTOOL_CMD;
257 // char cmd[] = "echo";
258 char *const cmdline[] = {
261 (char *const)rrd->filename,
267 bzero(data, sizeof(data));
268 l = sprintf(data, "N:");
270 if (rrd->parser && rrd->parser->parse) {
271 rrd->parser->parse(data + l, rrd->parser_data);
272 data[RRD_DATA_MAX_LEN + 2] = '\0';
274 pr_info("Data: %s\n", data);
276 sanitize_rrd_update_data(data + l);
277 write_to_logfile(rrd, data);
282 if (rrd->pre_draw_cmd && !strcmp(rrd->pre_draw_cmd[0], "shell")) {
283 run(rrd->pre_draw_cmd[1], &rrd->pre_draw_cmd[1]);
287 rrdtool_draw_images(rrd->images);
289 if (rrd->post_draw_cmd && !strcmp(rrd->post_draw_cmd[0], "shell"))
290 run(rrd->post_draw_cmd[1], &rrd->post_draw_cmd[1]);
295 int rrdtool_update_data(struct rrd_database *rrd)
297 rrd->last_update = time(0);
299 return queue_work(WORK_PRIORITY_HIGH, "rrdtool_update_data",
300 do_rrdtool_update_data, rrd);
304 * Walk through the database list and return the first database which
305 * last update is too far in past
307 struct rrd_database *get_outdated_db(struct rrd_database **dblist)
310 time_t now = time(0);
312 for (i = 0; dblist[i]; i++) {
313 if ((dblist[i]->last_update + dblist[i]->interval) - now <= 0)
317 /* Nothing to update this time, return null */
322 * See how long we may sleep until it is required to run an update
325 int get_next_update(struct rrd_database **dblist, const char **name)
327 int i, sleeptime = 0, diff;
328 time_t now = time(0);
330 for (i = 0; dblist[i]; i++) {
331 diff = dblist[i]->last_update + dblist[i]->interval - now;
334 *name = dblist[i]->name;
336 if (sleeptime > diff) {
338 *name = dblist[i]->name;
347 static int database_exists(struct rrd_database *db)
351 /* If the filename exists, stat will return zero */
353 return !stat(db->filename, &s);
358 static int create_database(struct rrd_database *db)
360 char cmd[] = RRDTOOL_CMD;
361 // char cmd[] = "echo";
362 char *args[512], argstr[ARGSTR_LEN];
363 int idx = 0, argcnt = 0;
367 pr_err("Database %s missing database filename\n", db->name);
371 if (!db->sources || !db->archives) {
372 pr_err("Cannot create db \"%s\", insufficient source data\n",
377 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
378 add_arg(args, argcnt, argstr, idx, "create");
379 add_arg(args, argcnt, argstr, idx, "%s", db->filename);
380 add_arg(args, argcnt, argstr, idx, "--step");
381 add_arg(args, argcnt, argstr, idx, "%d", db->interval);
383 for (i = 0; db->sources[i].type; i++) {
384 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
387 db->sources[i].heartbeat,
392 for (i = 0; db->archives[i].type; i++) {
393 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
394 db->archives[i].type,
396 db->archives[i].steps,
397 db->archives[i].rows);
405 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
407 struct rrd_database *db;
410 for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
411 if (database_exists(db)) {
412 pr_info("database %s found\n", db->filename);
415 pr_info("Database %s missing, creating\n", db->filename);
416 ret |= create_database(db);