15 #define ARGSTR_LEN 32768
17 #define RRDTOOL_CMD "/usr/bin/rrdtool"
20 * Add new argument to a argument list
22 * args pointer list to arguments
23 * argcnt argument counter
24 * argstr array where the actual arguments are stored
25 * idx index in the argstr where the new argument will be appended
27 #define add_arg(args, argcnt, argstr, idx, fmt, arg...) \
28 args[argcnt] = argstr + idx; \
29 idx += sprintf(argstr + idx, fmt, ##arg); \
33 int rrdtool_draw_image(struct rrd_image *image)
36 char cmd[] = RRDTOOL_CMD;
37 // char cmd[] = "echo";
38 char *args[512], argstr[ARGSTR_LEN];
39 int idx = 0, argcnt = 0, i,j;
45 pid = do_fork_limited();
49 pr_info("Drawing image %s\n", image->image_filename);
52 strncat(tmpfile, image->image_filename, sizeof(tmp) - strlen(tmp) - 1);
53 strncat(tmpfile, ".tmp", sizeof(tmp) - strlen(tmp) - 1);
55 strftime(tmp, 256, "%d.%m.%Y %T (%Z) ", localtime(&t));
56 for (i = 0, j = 0; j < 256;) {
58 timestamp[j++] = '\\';
60 timestamp[j++] = tmp[i++];
67 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
68 add_arg(args, argcnt, argstr, idx, "graph");
69 add_arg(args, argcnt, argstr, idx, "%s", tmpfile);
71 add_arg(args, argcnt, argstr, idx, "--start");
72 add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
73 add_arg(args, argcnt, argstr, idx, "--end");
74 add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
75 add_arg(args, argcnt, argstr, idx, "--width");
76 add_arg(args, argcnt, argstr, idx, "%d", image->width);
77 add_arg(args, argcnt, argstr, idx, "--height");
78 add_arg(args, argcnt, argstr, idx, "%d", image->height);
79 add_arg(args, argcnt, argstr, idx, "--imgformat");
80 add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
82 for (i = 0; image->options[i]; i++) {
83 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
86 for (i = 0; image->text[i]; i++) {
87 args[argcnt++] = (char *)image->text[i];
90 add_arg(args, argcnt, argstr, idx, "COMMENT:Last update %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 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 int rrdtool_update_data(struct rrd_database *rrd)
201 char data[RRD_DATA_MAX_LEN + 2];
202 char cmd[] = RRDTOOL_CMD;
203 // char cmd[] = "echo";
204 char *const cmdline[] = {
207 (char *const)rrd->filename,
213 rrd->last_update = time(0);
217 l = sprintf(data, "N:");
220 rrd->parse(data + l, rrd->parser_data);
221 sanitize_rrd_update_data(data + l);
222 pid = run(cmd, cmdline);
223 harvest_zombies(pid);
227 rrdtool_draw_images(rrd->images);
229 while (harvest_zombies(0));
233 static int database_exists(struct rrd_database *db)
237 /* If the filename exists, stat will return zero */
239 return !stat(db->filename, &s);
244 static int create_database(struct rrd_database *db)
246 char cmd[] = RRDTOOL_CMD;
247 // char cmd[] = "echo";
248 char *args[512], argstr[ARGSTR_LEN];
249 int idx = 0, argcnt = 0;
253 pr_err("Database %s missing database filename\n", db->name);
257 if (!db->sources || !db->archives) {
258 pr_err("Cannot create db \"%s\", insufficient source data\n",
263 add_arg(args, argcnt, argstr, idx, RRDTOOL_CMD);
264 add_arg(args, argcnt, argstr, idx, "create");
265 add_arg(args, argcnt, argstr, idx, "%s", db->filename);
266 add_arg(args, argcnt, argstr, idx, "--step");
267 add_arg(args, argcnt, argstr, idx, "%d", db->interval);
269 for (i = 0; db->sources[i].type; i++) {
270 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
273 db->sources[i].heartbeat,
278 for (i = 0; db->archives[i].type; i++) {
279 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
280 db->archives[i].type,
282 db->archives[i].steps,
283 db->archives[i].rows);
286 child = run(cmd, args);
288 harvest_zombies(child);
293 int rrdtool_create_missing_databases(struct rrd_database *dbs[])
295 struct rrd_database *db;
298 for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
299 if (database_exists(db)) {
300 pr_info("database %s found\n", db->filename);
303 pr_info("Database %s missing, creating\n", db->filename);
304 ret |= create_database(db);