]> git.itanic.dy.fi Git - rrdd/blob - rrdtool.c
rrdtool: Print out the image that is being drawn
[rrdd] / rrdtool.c
1 #include <time.h>
2
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <string.h>
7
8 #include "rrdtool.h"
9 #include "process.h"
10 #include "parser.h"
11 #include "debug.h"
12
13 #define MAX_ARGS        512
14 #define ARGSTR_LEN      32768
15
16 #define RRDTOOL_CMD "/usr/bin/rrdtool"
17
18 /*
19  * Add new argument to a argument list
20  *
21  * args         pointer list to arguments
22  * argcnt       argument counter
23  * argstr       array where the actual arguments are stored
24  * idx          index in the argstr where the new argument will be appended
25  */
26 #define add_arg(args, argcnt, argstr, idx, fmt, arg...) \
27         args[argcnt] = argstr + idx;                    \
28         idx += sprintf(argstr + idx, fmt, ##arg);       \
29         argcnt++;                                       \
30         argstr[++idx] = 0
31
32 int rrdtool_draw_image(struct rrd_image *image)
33 {
34         char cmd[] = RRDTOOL_CMD;
35 //      char cmd[] = "echo";
36         char *args[512], argstr[ARGSTR_LEN];
37         int idx = 0, argcnt = 0, i,j;
38         char timestamp[256];
39         char tmp[256];
40         time_t t = time(0);
41
42         pr_info("Drawing image %s\n", image->image_filename);
43
44         strftime(tmp, 256, "%d.%m.%Y %T (%Z) ", localtime(&t));
45         for (i = 0, j = 0; j < 256;) {
46                 if (tmp[i] == ':') {
47                         timestamp[j++] = '\\';
48                 }
49                 timestamp[j++] = tmp[i++];
50                 if (!tmp[i])
51                         break;
52         }
53         timestamp[j] = 0;
54
55
56         add_arg(args, argcnt, argstr, idx, " ");
57         add_arg(args, argcnt, argstr, idx, "graph");
58         add_arg(args, argcnt, argstr, idx, "%s", image->image_filename);
59
60         add_arg(args, argcnt, argstr, idx, "--start");
61         add_arg(args, argcnt, argstr, idx, "%s", image->timestart);
62         add_arg(args, argcnt, argstr, idx, "--end");
63         add_arg(args, argcnt, argstr, idx, "%s", image->timeend);
64         add_arg(args, argcnt, argstr, idx, "--width");
65         add_arg(args, argcnt, argstr, idx, "%d", image->width);
66         add_arg(args, argcnt, argstr, idx, "--height");
67         add_arg(args, argcnt, argstr, idx, "%d", image->height);
68         add_arg(args, argcnt, argstr, idx, "--imgformat");
69         add_arg(args, argcnt, argstr, idx, "%s", image->imageformat);
70
71         for (i = 0; image->options[i]; i++) {
72                 add_arg(args, argcnt, argstr, idx, "%s", image->options[i]);
73         }
74
75         for (i = 0; image->text[i]; i++) {
76                 args[argcnt++] = image->text[i];
77         }
78
79         add_arg(args, argcnt, argstr, idx, "COMMENT:Last update %s\\c", timestamp);
80
81         args[argcnt] = 0;
82
83         run(cmd, args);
84
85         return 0;
86 }
87
88 int rrdtool_draw_images(struct rrd_image **image)
89 {
90         int i;
91         for (i = 0; image[i]; i++)
92                 rrdtool_draw_image(image[i]);
93
94         return 0;
95 }
96
97 int rrdtool_update_data(struct rrd_database *rrd)
98 {
99         char data[RRD_DATA_MAX_LEN];
100         char cmd[] = RRDTOOL_CMD;
101 //      char cmd[] = "echo";
102         char *cmdline[] = {
103                 "",
104                 "update",
105                 rrd->filename,
106                 data,
107                 0
108         };
109         int l;
110
111         rrd->last_update = time(0);
112         if (do_fork())
113                 return 0;
114
115         l = sprintf(data, "N:");
116
117         if (rrd->parse) {
118                 rrd->parse(data + l, rrd->parser_data);
119                 run(cmd, cmdline);
120         }
121
122         if (rrd->images)
123                 rrdtool_draw_images(rrd->images);
124
125         while (harvest_zombies(0));
126         exit(0);
127 }
128
129 static int database_exists(struct rrd_database *db)
130 {
131         struct stat s;
132
133         /* If the filename exists, stat will return zero */
134         if (db->filename)
135                 return !stat(db->filename, &s);
136
137         return 1;
138 }
139
140 static int create_database(struct rrd_database *db)
141 {
142         char cmd[] = RRDTOOL_CMD;
143 //      char cmd[] = "echo";
144         char *args[512], argstr[ARGSTR_LEN];
145         int idx = 0, argcnt = 0;
146         int child, i;
147
148         if (!db->sources || !db->archives) {
149                 printf("Cannot create db \"%s\", insufficient source data\n",
150                         db->filename);
151                 return -1;
152         }
153
154         add_arg(args, argcnt, argstr, idx, " ");
155         add_arg(args, argcnt, argstr, idx, "create");
156         add_arg(args, argcnt, argstr, idx, "%s", db->filename);
157         add_arg(args, argcnt, argstr, idx, "--step");
158         add_arg(args, argcnt, argstr, idx, "%d", db->interval);
159
160         for (i = 0; db->sources[i].type; i++) {
161                 add_arg(args, argcnt, argstr, idx, "DS:%s:%s:%d:%f:%f",
162                         db->sources[i].name,
163                         db->sources[i].type,
164                         db->sources[i].heartbeat,
165                         db->sources[i].min,
166                         db->sources[i].max);
167         }
168
169         for (i = 0; db->archives[i].type; i++) {
170                 add_arg(args, argcnt, argstr, idx, "RRA:%s:%f:%d:%d",
171                         db->archives[i].type,
172                         db->archives[i].xff,
173                         db->archives[i].steps,
174                         db->archives[i].rows);
175         }
176
177         child = run(cmd, args);
178
179         harvest_zombies(child);
180
181         return 0;
182 }
183
184 int rrdtool_check_databases(struct rrd_database *dbs[])
185 {
186         struct rrd_database *db;
187         int i;
188
189         for (i = 0, db = dbs[i]; db; i++, db = dbs[i]) {
190                 if (database_exists(db)) {
191                         printf("database %s found\n", db->filename);
192                         continue;
193                 }
194                 printf("Database %s missing, creating\n", db->filename);
195                 create_database(db);
196         }
197         printf("All done\n");
198
199         return 0;
200 }