]> git.itanic.dy.fi Git - rrdd/blob - rrdtool.c
rrdtool: Improve argument list modification macro
[rrdd] / rrdtool.c
1 #include <time.h>
2
3 #include "rrdtool.h"
4 #include "process.h"
5
6 #define MAX_ARGS        512
7 #define ARGSTR_LEN      32768
8
9 #define RRDTOOL_CMD "/usr/bin/rrdtool"
10
11 /*
12  * Add new argument to a argument list
13  *
14  * args         pointer list to arguments
15  * argcnt       argument counter
16  * argstr       array where the actual arguments are stored
17  * idx          index in the argstr where the new argument will be appended
18  */
19 #define add_arg(args, argcnt, argstr, idx, fmt, arg...) \
20         args[argcnt] = argstr + idx;                    \
21         idx += sprintf(argstr + idx, fmt, ##arg);       \
22         argcnt++;                                       \
23         argstr[++idx] = 0
24
25 int rrdtool_draw_image(struct rrd_image *image)
26 {
27         char cmd[] = RRDTOOL_CMD;
28 //      char cmd[] = "echo";
29         char *args[512], argstr[ARGSTR_LEN];
30         int idx = 0, argcnt = 0, i,j;
31         char timestamp[256];
32         char tmp[256];
33         time_t t = time(0);
34
35         strftime(tmp, 256, "%d.%m.%Y %T (%Z) ", localtime(&t));
36         for (i = 0, j = 0; j < 256;) {
37                 if (tmp[i] == ':') {
38                         timestamp[j++] = '\\';
39                 }
40                 timestamp[j++] = tmp[i++];
41                 if (!tmp[i])
42                         break;
43         }
44         timestamp[j] = 0;
45
46
47         add_arg(args, argcnt, argstr, idx, " ");
48         add_arg(args, argcnt, argstr, idx, "graph");
49         add_arg(args, argcnt, argstr, idx, "%s", image->image_filename);
50
51         add_arg(args, argcnt, argstr, idx, "--start");
52         add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
53         add_arg(args, argcnt, argstr, idx, "--end");
54         add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
55         add_arg(args, argcnt, argstr, idx, "--width");
56         add_arg(args, argcnt, argstr, idx, "%d", image->width);
57         add_arg(args, argcnt, argstr, idx, "--height");
58         add_arg(args, argcnt, argstr, idx, "%d", image->height);
59         add_arg(args, argcnt, argstr, idx, "--imgformat");
60         add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
61
62         for (i = 0; image->options[i]; i++) {
63                 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
64         }
65
66         for (i = 0; image->text[i]; i++) {
67                 args[argcnt++] = image->text[i];
68         }
69
70         add_arg(args, argcnt, argstr, idx, "COMMENT:Last update %s\\c", timestamp);
71
72         args[argcnt] = 0;
73
74         run(cmd, args);
75
76         return 0;
77 }
78
79 int rrdtool_draw_images(struct rrd_image **image)
80 {
81         int i;
82         for (i = 0; image[i]; i++)
83                 rrdtool_draw_image(image[i]);
84
85         return 0;
86 }
87
88 int rrdtool_update_data(struct rrd_database *rrd)
89 {
90         char data[1024];
91         char cmd[] = RRDTOOL_CMD;
92 //      char cmd[] = "echo";
93         char *cmdline[] = {
94                 "",
95                 "update",
96                 rrd->filename,
97                 data,
98                 0
99         };
100         int l;
101
102         rrd->last_update = time(0);
103         if (do_fork())
104                 return 0;
105
106         l = sprintf(data, "N:");
107
108         if (rrd->parse) {
109                 rrd->parse(data + l);
110                 run(cmd, cmdline);
111         }
112
113         if (rrd->images)
114                 rrdtool_draw_images(rrd->images);
115
116         while (harvest_zombies(0));
117         exit(0);
118 }