X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=common.c;h=2b6b5eb9180efa2272695de031d0d3d4c6d49ac0;hb=fab918278f2919ebc74a2f7b1a9db2bfeab46593;hp=5d2a67deaef7276cb0e86c6c4da8ce9b51dec13f;hpb=0bdac0286e8d3c42dd8d5c3058d822e9ad0e6313;p=collection4.git diff --git a/common.c b/common.c index 5d2a67d..2b6b5eb 100644 --- a/common.c +++ b/common.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -7,144 +9,15 @@ #include #include #include +#include #include #include "common.h" #include "graph_list.h" -static int foreach_rrd_file (const char *dir, /* {{{ */ - int (*callback) (const char *, void *), - void *user_data) -{ - DIR *dh; - struct dirent *entry; - int status; - - if (callback == NULL) - return (EINVAL); - - dh = opendir (dir); - if (dh == NULL) - return (errno); - - while ((entry = readdir (dh)) != NULL) - { - struct stat statbuf; - char abspath[PATH_MAX + 1]; - size_t d_name_len; - - if (entry->d_name[0] == '.') - continue; - - d_name_len = strlen (entry->d_name); - if (d_name_len <= 4) - continue; - - if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0) - continue; - - snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); - abspath[sizeof (abspath) - 1] = 0; - - memset (&statbuf, 0, sizeof (statbuf)); - - status = stat (abspath, &statbuf); - if (status != 0) - continue; - - if (!S_ISREG (statbuf.st_mode)) - continue; - - entry->d_name[d_name_len - 4] = 0; - - status = (*callback) (entry->d_name, user_data); - if (status != 0) - break; - } /* while (readdir) */ - - closedir (dh); - return (status); -} /* }}} int foreach_rrd_file */ - -static int foreach_dir (const char *dir, /* {{{ */ - int (*callback) (const char *, void *), - void *user_data) -{ - DIR *dh; - struct dirent *entry; - int status; - - if (callback == NULL) - return (EINVAL); - - dh = opendir (dir); - if (dh == NULL) - return (errno); - - while ((entry = readdir (dh)) != NULL) - { - struct stat statbuf; - char abspath[PATH_MAX + 1]; - - if (entry->d_name[0] == '.') - continue; - - snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); - abspath[sizeof (abspath) - 1] = 0; - - memset (&statbuf, 0, sizeof (statbuf)); - - status = stat (abspath, &statbuf); - if (status != 0) - continue; - - if (!S_ISDIR (statbuf.st_mode)) - continue; - - status = (*callback) (entry->d_name, user_data); - if (status != 0) - break; - } /* while (readdir) */ - - closedir (dh); - return (status); -} /* }}} int foreach_dir */ - -int foreach_type (const char *host, const char *plugin, /* {{{ */ - callback_type_t callback, void *user_data) -{ - char abspath[PATH_MAX + 1]; - - if ((host == NULL) || (plugin == NULL)) - return (EINVAL); - - snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin); - abspath[sizeof (abspath) - 1] = 0; - - return (foreach_rrd_file (abspath, callback, user_data)); -} /* }}} int foreach_type */ - -int foreach_plugin (const char *host, /* {{{ */ - callback_plugin_t callback, - void *user_data) -{ - char abspath[PATH_MAX + 1]; - - if (host == NULL) - return (EINVAL); - - snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host); - abspath[sizeof (abspath) - 1] = 0; - - return (foreach_dir (abspath, callback, user_data)); -} /* }}} int foreach_plugin */ - -int foreach_host (callback_host_t callback, /* {{{ */ - void *user_data) -{ - return (foreach_dir (DATA_DIR, callback, user_data)); -} /* }}} int foreach_host */ +#include +#include size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */ { @@ -245,4 +118,76 @@ int ds_list_from_rrd_file (char *file, /* {{{ */ return (0); } /* }}} int ds_list_from_rrd_file */ +static int hsv_to_rgb (double *hsv, double *rgb) /* {{{ */ +{ + double c = hsv[2] * hsv[1]; + double h = hsv[0] / 60.0; + double x = c * (1.0 - fabs (fmod (h, 2.0) - 1)); + double m = hsv[2] - c; + + rgb[0] = 0.0; + rgb[1] = 0.0; + rgb[2] = 0.0; + + if ((0.0 <= h) && (h < 1.0)) { rgb[0] = 1.0; rgb[1] = x; rgb[2] = 0.0; } + else if ((1.0 <= h) && (h < 2.0)) { rgb[0] = x; rgb[1] = 1.0; rgb[2] = 0.0; } + else if ((2.0 <= h) && (h < 3.0)) { rgb[0] = 0.0; rgb[1] = 1.0; rgb[2] = x; } + else if ((3.0 <= h) && (h < 4.0)) { rgb[0] = 0.0; rgb[1] = x; rgb[2] = 1.0; } + else if ((4.0 <= h) && (h < 5.0)) { rgb[0] = x; rgb[1] = 0.0; rgb[2] = 1.0; } + else if ((5.0 <= h) && (h < 6.0)) { rgb[0] = 1.0; rgb[1] = 0.0; rgb[2] = x; } + + rgb[0] += m; + rgb[1] += m; + rgb[2] += m; + + return (0); +} /* }}} int hsv_to_rgb */ + +static uint32_t rgb_to_uint32 (double *rgb) /* {{{ */ +{ + uint8_t r; + uint8_t g; + uint8_t b; + + r = (uint8_t) (255.0 * rgb[0]); + g = (uint8_t) (255.0 * rgb[1]); + b = (uint8_t) (255.0 * rgb[2]); + + return ((((uint32_t) r) << 16) + | (((uint32_t) g) << 8) + | ((uint32_t) b)); +} /* }}} uint32_t rgb_to_uint32 */ + +uint32_t get_random_color (void) /* {{{ */ +{ + double hsv[3] = { 0.0, 1.0, 1.0 }; + double rgb[3] = { 0.0, 0.0, 0.0 }; + + hsv[0] = 360.0 * ((double) rand ()) / (((double) RAND_MAX) + 1.0); + + hsv_to_rgb (hsv, rgb); + + return (rgb_to_uint32 (rgb)); +} /* }}} uint32_t get_random_color */ + +int print_debug (const char *format, ...) /* {{{ */ +{ + static _Bool have_header = 0; + + va_list ap; + int status; + + if (!have_header) + { + printf ("Content-Type: text/plain\n\n"); + have_header = 1; + } + + va_start (ap, format); + status = vprintf (format, ap); + va_end (ap); + + return (status); +} /* }}} int print_debug */ + /* vim: set sw=2 sts=2 et fdm=marker : */