action graph: Remove unused functions.
[collection4.git] / common.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <inttypes.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <dirent.h>
11 #include <assert.h>
12 #include <math.h>
13
14 #include <rrd.h>
15
16 #include "common.h"
17 #include "graph_list.h"
18
19 static int foreach_rrd_file (const char *dir, /* {{{ */
20     int (*callback) (const char *, void *),
21     void *user_data)
22 {
23   DIR *dh;
24   struct dirent *entry;
25   int status;
26
27   if (callback == NULL)
28     return (EINVAL);
29
30   dh = opendir (dir);
31   if (dh == NULL)
32     return (errno);
33
34   while ((entry = readdir (dh)) != NULL)
35   {
36     struct stat statbuf;
37     char abspath[PATH_MAX + 1];
38     size_t d_name_len;
39
40     if (entry->d_name[0] == '.')
41       continue;
42
43     d_name_len = strlen (entry->d_name);
44     if (d_name_len <= 4)
45       continue;
46
47     if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0)
48       continue;
49
50     snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
51     abspath[sizeof (abspath) - 1] = 0;
52
53     memset (&statbuf, 0, sizeof (statbuf));
54
55     status = stat (abspath, &statbuf);
56     if (status != 0)
57       continue;
58
59     if (!S_ISREG (statbuf.st_mode))
60       continue;
61
62     entry->d_name[d_name_len - 4] = 0;
63
64     status = (*callback) (entry->d_name, user_data);
65     if (status != 0)
66       break;
67   } /* while (readdir) */
68
69   closedir (dh);
70   return (status);
71 } /* }}} int foreach_rrd_file */
72
73 static int foreach_dir (const char *dir, /* {{{ */
74     int (*callback) (const char *, void *),
75     void *user_data)
76 {
77   DIR *dh;
78   struct dirent *entry;
79   int status;
80
81   if (callback == NULL)
82     return (EINVAL);
83
84   dh = opendir (dir);
85   if (dh == NULL)
86     return (errno);
87
88   while ((entry = readdir (dh)) != NULL)
89   {
90     struct stat statbuf;
91     char abspath[PATH_MAX + 1];
92
93     if (entry->d_name[0] == '.')
94       continue;
95
96     snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name);
97     abspath[sizeof (abspath) - 1] = 0;
98
99     memset (&statbuf, 0, sizeof (statbuf));
100
101     status = stat (abspath, &statbuf);
102     if (status != 0)
103       continue;
104
105     if (!S_ISDIR (statbuf.st_mode))
106       continue;
107
108     status = (*callback) (entry->d_name, user_data);
109     if (status != 0)
110       break;
111   } /* while (readdir) */
112
113   closedir (dh);
114   return (status);
115 } /* }}} int foreach_dir */
116
117 int foreach_type (const char *host, const char *plugin, /* {{{ */
118     callback_type_t callback, void *user_data)
119 {
120   char abspath[PATH_MAX + 1];
121
122   if ((host == NULL) || (plugin == NULL))
123     return (EINVAL);
124
125   snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin);
126   abspath[sizeof (abspath) - 1] = 0;
127
128   return (foreach_rrd_file (abspath, callback, user_data));
129 } /* }}} int foreach_type */
130
131 int foreach_plugin (const char *host, /* {{{ */
132     callback_plugin_t callback,
133     void *user_data)
134 {
135   char abspath[PATH_MAX + 1];
136
137   if (host == NULL)
138     return (EINVAL);
139
140   snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host);
141   abspath[sizeof (abspath) - 1] = 0;
142
143   return (foreach_dir (abspath, callback, user_data));
144 } /* }}} int foreach_plugin */
145
146 int foreach_host (callback_host_t callback, /* {{{ */
147     void *user_data)
148 {
149   return (foreach_dir (DATA_DIR, callback, user_data));
150 } /* }}} int foreach_host */
151
152 size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */
153 {
154   size_t retval;
155   size_t dst_len;
156   size_t src_len;
157
158   dst_len = strlen (dst);
159   src_len = strlen (src);
160   retval = dst_len + src_len;
161
162   if ((dst_len + 1) >= size)
163     return (retval);
164
165   dst  += dst_len;
166   size -= dst_len;
167   assert (size >= 2);
168
169   /* Result will be truncated. */
170   if (src_len >= size)
171     src_len = size - 1;
172
173   memcpy (dst, src, src_len);
174   dst[src_len] = 0;
175
176   return (retval);
177 } /* }}} size_t c_strlcat */
178
179 int ds_list_from_rrd_file (char *file, /* {{{ */
180     size_t *ret_dses_num, char ***ret_dses)
181 {
182   char *rrd_argv[] = { "info", file, NULL };
183   int rrd_argc = (sizeof (rrd_argv) / sizeof (rrd_argv[0])) - 1;
184
185   rrd_info_t *info;
186   rrd_info_t *ptr;
187
188   char **dses = NULL;
189   size_t dses_num = 0;
190
191   info = rrd_info (rrd_argc, rrd_argv);
192   if (info == NULL)
193   {
194     printf ("%s: rrd_info (%s) failed.\n", __func__, file);
195     return (-1);
196   }
197
198   for (ptr = info; ptr != NULL; ptr = ptr->next)
199   {
200     size_t keylen;
201     size_t dslen;
202     char *ds;
203     char **tmp;
204
205     if (strncmp ("ds[", ptr->key, strlen ("ds[")) != 0)
206       continue;
207
208     keylen = strlen (ptr->key);
209     if (keylen < strlen ("ds[?].index"))
210       continue;
211
212     dslen = keylen - strlen ("ds[].index");
213     assert (dslen >= 1);
214
215     if (strcmp ("].index", ptr->key + (strlen ("ds[") + dslen)) != 0)
216       continue;
217
218     ds = malloc (dslen + 1);
219     if (ds == NULL)
220       continue;
221
222     memcpy (ds, ptr->key + strlen ("ds["), dslen);
223     ds[dslen] = 0;
224
225     tmp = realloc (dses, sizeof (*dses) * (dses_num + 1));
226     if (tmp == NULL)
227     {
228       free (ds);
229       continue;
230     }
231     dses = tmp;
232
233     dses[dses_num] = ds;
234     dses_num++;
235   }
236
237   rrd_info_free (info);
238
239   if (dses_num < 1)
240   {
241     assert (dses == NULL);
242     return (ENOENT);
243   }
244
245   *ret_dses_num = dses_num;
246   *ret_dses = dses;
247
248   return (0);
249 } /* }}} int ds_list_from_rrd_file */
250
251 static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */
252 {
253   double c = hsv[2] * hsv[1];
254   double h = hsv[0] / 60.0;
255   double x = c * (1.0 - fabs (fmod (h, 2.0) - 1));
256   double m = hsv[2] - c;
257
258   rgb[0] = 0.0;
259   rgb[1] = 0.0;
260   rgb[2] = 0.0;
261
262        if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; }
263   else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; }
264   else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; }
265   else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; }
266   else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; }
267   else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; }
268
269   rgb[0] += m;
270   rgb[1] += m;
271   rgb[2] += m;
272
273   return (0);
274 } /* }}} int hsv_to_rgb */
275
276 static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */
277 {
278   uint8_t r;
279   uint8_t g;
280   uint8_t b;
281
282   r = (uint8_t) (255.0 * rgb[0]);
283   g = (uint8_t) (255.0 * rgb[1]);
284   b = (uint8_t) (255.0 * rgb[2]);
285
286   return ((((uint32_t) r) << 16)
287       | (((uint32_t) g) << 8)
288       | ((uint32_t) b));
289 } /* }}} uint32_t rgb_to_uint32 */
290
291 uint32_t get_random_color (void) /* {{{ */
292 {
293   double hsv[3] = { 0.0, 1.0, 1.0 };
294   double rgb[3] = { 0.0, 0.0, 0.0 };
295
296   hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0);
297
298   hsv_to_rgb (hsv, rgb);
299
300   return (rgb_to_uint32 (rgb));
301 } /* }}} uint32_t get_random_color */
302
303 /* vim: set sw=2 sts=2 et fdm=marker : */