17 #include "graph_list.h"
20 #include <fcgi_stdio.h>
22 static int foreach_rrd_file (const char *dir, /* {{{ */
23 int (*callback) (const char *, void *),
37 while ((entry = readdir (dh)) != NULL)
40 char abspath[PATH_MAX + 1];
43 if (entry->d_name[0] == '.')
46 d_name_len = strlen (entry->d_name);
50 if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0)
53 snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
54 abspath[sizeof (abspath) - 1] = 0;
56 memset (&statbuf, 0, sizeof (statbuf));
58 status = stat (abspath, &statbuf);
62 if (!S_ISREG (statbuf.st_mode))
65 entry->d_name[d_name_len - 4] = 0;
67 status = (*callback) (entry->d_name, user_data);
70 } /* while (readdir) */
74 } /* }}} int foreach_rrd_file */
76 static int foreach_dir (const char *dir, /* {{{ */
77 int (*callback) (const char *, void *),
91 while ((entry = readdir (dh)) != NULL)
94 char abspath[PATH_MAX + 1];
96 if (entry->d_name[0] == '.')
99 snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
100 abspath[sizeof (abspath) - 1] = 0;
102 memset (&statbuf, 0, sizeof (statbuf));
104 status = stat (abspath, &statbuf);
108 if (!S_ISDIR (statbuf.st_mode))
111 status = (*callback) (entry->d_name, user_data);
114 } /* while (readdir) */
118 } /* }}} int foreach_dir */
120 int foreach_type (const char *host, const char *plugin, /* {{{ */
121 callback_type_t callback, void *user_data)
123 char abspath[PATH_MAX + 1];
125 if ((host == NULL) || (plugin == NULL))
128 snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin);
129 abspath[sizeof (abspath) - 1] = 0;
131 return (foreach_rrd_file (abspath, callback, user_data));
132 } /* }}} int foreach_type */
134 int foreach_plugin (const char *host, /* {{{ */
135 callback_plugin_t callback,
138 char abspath[PATH_MAX + 1];
143 snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host);
144 abspath[sizeof (abspath) - 1] = 0;
146 return (foreach_dir (abspath, callback, user_data));
147 } /* }}} int foreach_plugin */
149 int foreach_host (callback_host_t callback, /* {{{ */
152 return (foreach_dir (DATA_DIR, callback, user_data));
153 } /* }}} int foreach_host */
155 size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */
161 dst_len = strlen (dst);
162 src_len = strlen (src);
163 retval = dst_len + src_len;
165 if ((dst_len + 1) >= size)
172 /* Result will be truncated. */
176 memcpy (dst, src, src_len);
180 } /* }}} size_t c_strlcat */
182 int ds_list_from_rrd_file (char *file, /* {{{ */
183 size_t *ret_dses_num, char ***ret_dses)
185 char *rrd_argv[] = { "info", file, NULL };
186 int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
194 info = rrd_info (rrd_argc, rrd_argv);
197 printf ("%s: rrd_info (%s) failed.\n", __func__, file);
201 for (ptr = info; ptr != NULL; ptr = ptr->next)
208 if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
211 keylen = strlen (ptr->key);
212 if (keylen < strlen ("ds[?].index"))
215 dslen = keylen - strlen ("ds[].index");
218 if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
221 ds = malloc (dslen + 1);
225 memcpy (ds, ptr->key + strlen ("ds["), dslen);
228 tmp = realloc (dses, sizeof (*dses) * (dses_num + 1));
240 rrd_info_free (info);
244 assert (dses == NULL);
248 *ret_dses_num = dses_num;
252 } /* }}} int ds_list_from_rrd_file */
254 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
256 double c = hsv[2] * hsv[1];
257 double h = hsv[0] / 60.0;
258 double x = c * (1.0 - fabs (fmod (h, 2.0) - 1));
259 double m = hsv[2] - c;
265 if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; }
266 else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; }
267 else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; }
268 else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; }
269 else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; }
270 else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; }
277 } /* }}} int hsv_to_rgb */
279 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
285 r = (uint8_t) (255.0 * rgb[0]);
286 g = (uint8_t) (255.0 * rgb[1]);
287 b = (uint8_t) (255.0 * rgb[2]);
289 return ((((uint32_t) r) << 16)
290 | (((uint32_t) g) << 8)
292 } /* }}} uint32_t rgb_to_uint32 */
294 uint32_t get_random_color (void) /* {{{ */
296 double hsv[3] = { 0.0, 1.0, 1.0 };
297 double rgb[3] = { 0.0, 0.0, 0.0 };
299 hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
301 hsv_to_rgb (hsv, rgb);
303 return (rgb_to_uint32 (rgb));
304 } /* }}} uint32_t get_random_color */
306 int print_debug (const char *format, ...) /* {{{ */
308 static _Bool have_header = 0;
315 printf ("Content-Type: text/plain\n\n");
319 va_start (ap, format);
320 status = vprintf (format, ap);
324 } /* }}} int print_debug */
326 /* vim: set sw=2 sts=2 et fdm=marker : */