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 pr_info("Drawing image %s\n", image->image_filename);
51 strncat(tmpfile, image->image_filename, sizeof(tmp) - 1);
52 strncat(tmpfile, ".tmp", sizeof(tmp) - 1);
55 updatestr = image->updatestr;
57 strftime(tmp, sizeof(tmp), updatestr, localtime(&t));
58 for (i = 0, j = 0; j < sizeof(tmp);) {
60 timestamp[j++] = '\\';
62 timestamp[j++] = tmp[i++];
69 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
70 add_arg(args, argcnt, argstr, idx, "graph");
71 add_arg(args, argcnt, argstr, idx, "%s", tmpfile);
73 add_arg(args, argcnt, argstr, idx, "--start");
74 add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
75 add_arg(args, argcnt, argstr, idx, "--end");
76 add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
77 add_arg(args, argcnt, argstr, idx, "--width");
78 add_arg(args, argcnt, argstr, idx, "%d", image->width);
79 add_arg(args, argcnt, argstr, idx, "--height");
80 add_arg(args, argcnt, argstr, idx, "%d", image->height);
81 add_arg(args, argcnt, argstr, idx, "--imgformat");
82 add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
84 for (i = 0; image->options[i]; i++) {
85 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
88 for (i = 0; image->text[i]; i++) {
89 args[argcnt++] = (char *)image->text[i];
92 add_arg(args, argcnt, argstr, idx, "COMMENT: %s\\c", timestamp);
96 rename(tmpfile, image->image_filename);
101 int rrdtool_draw_images(struct rrd_image **image)
104 for (i = 0; image[i]; i++)
105 queue_work(WORK_PRIORITY_LOW, "rrdtool_draw_image",
106 rrdtool_draw_image, image[i]);
111 static int sanitize_rrd_update_data(char *data)
113 char clean_data[RRD_DATA_MAX_LEN];
116 char *src, *end, *cln;
118 data[RRD_DATA_MAX_LEN - 1] = 0;
123 * Copy a legit floating point number to clean_data buffer
124 * starting from *src and ending to next ':'. If no legit
125 * number could be found, put a 'U' there instead to make
126 * rrdtool to understand this datapoint is undefined.
129 while (src < data + RRD_DATA_MAX_LEN && *src) {
132 /* skip any non_numbers but not ':' */
133 while (*src && !isdigit(*src) && *src != '-' && *src != ':')
141 /* Now find the end of the number */
142 end = skip_numbers(src);
144 /* Floating point numberrs may have a dot with more numbers */
147 end = skip_numbers(end);
151 * Now we have gone past the number, there should be a
152 * colon or zero byte. If src == end, there was no
153 * number and the entry is undefined instead.
155 if ((*end == ':' || !*end) && src != end) {
162 * Copy the legit number and start copying the
165 for (; src <= end; src++, cln++)
171 /* Skip over whatever junk there might be */
172 while (*end != ':' && *end)
175 /* Mark the entry as undefined */
187 * If last entry was undefined, we need to remove the extra
190 if (*(cln - 1) == ':')
194 strncpy(data, clean_data, RRD_DATA_MAX_LEN);
198 static int write_to_logfile(struct rrd_database *rrd, const char *data)
200 time_t t = time(NULL);
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(&t));
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(&t));
224 str_ptr = logstr + strlen(logstr);
226 data += 2; /* Skip the "N: part */
229 while (*data && str_ptr - logstr < sizeof(logstr) - 1) {
232 for (i = 0; i < spacing; i++)
239 *str_ptr++ = *data++;
245 ret = write(fd, logstr, strlen(logstr));
247 pr_err("Failed to write to logfile %s: %m\n", filename);
251 return ret < 0 ? ret : 0;
254 static int do_rrdtool_update_data(struct rrd_database *rrd)
257 char data[RRD_DATA_MAX_LEN + 3]; /* 3 == "N:" + NULL termination */
258 char cmd[] = RRDTOOL_CMD;
259 // char cmd[] = "echo";
260 char *const cmdline[] = {
263 (char *const)rrd->filename,
269 l = sprintf(data, "N:");
271 if (rrd->parser && rrd->parser->parse) {
272 rrd->parser->parse(data + l, rrd->parser_data);
273 data[RRD_DATA_MAX_LEN + 2] = '\0';
275 pr_info("Data: %s\n", data);
277 sanitize_rrd_update_data(data + l);
278 write_to_logfile(rrd, data);
283 if (rrd->pre_draw_cmd && !strcmp(rrd->pre_draw_cmd[0], "shell")) {
284 run(rrd->pre_draw_cmd[1], &rrd->pre_draw_cmd[1]);
288 rrdtool_draw_images(rrd->images);
290 if (rrd->post_draw_cmd && !strcmp(rrd->post_draw_cmd[0], "shell"))
291 run(rrd->post_draw_cmd[1], &rrd->post_draw_cmd[1]);
296 int rrdtool_update_data(struct rrd_database *rrd)
298 rrd->last_update = time(0);
300 return queue_work(WORK_PRIORITY_HIGH, "rrdtool_update_data",
301 do_rrdtool_update_data, rrd);
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);
406 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
408 struct rrd_database *db;
411 for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
412 if (database_exists(db)) {
413 pr_info("database %s found\n", db->filename);
416 pr_info("Database %s missing, creating\n", db->filename);
417 ret |= create_database(db);