X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=action_graph.c;h=a0f257e47b4d168571c02d0e8f2660af39194d67;hb=7a6beaf075a32bfd93d314c00179f258c8e43ee5;hp=585a49a6758e9415aa6a8a5ee77ac1470cd1fef6;hpb=0bdac0286e8d3c42dd8d5c3058d822e9ad0e6313;p=collection4.git diff --git a/action_graph.c b/action_graph.c index 585a49a..a0f257e 100644 --- a/action_graph.c +++ b/action_graph.c @@ -19,221 +19,6 @@ #include #include -struct data_source_s -{ - char *file; - char *name; - char *legend; - double scale; - _Bool nan_to_zero; - _Bool draw_area; - uint32_t color; -}; -typedef struct data_source_s data_source_t; - -struct graph_def_s -{ - data_source_t *data_sources; - size_t data_sources_num; - - _Bool stack; - - int def_num; -}; -typedef struct graph_def_s graph_def_t; - -static void graph_def_free (graph_def_t *gd) /* {{{ */ -{ - size_t i; - - if (gd == NULL) - return; - - for (i = 0; i < gd->data_sources_num; i++) - { - free (gd->data_sources[i].file); - free (gd->data_sources[i].name); - free (gd->data_sources[i].legend); - } - free (gd->data_sources); - free (gd); -} /* }}} void graph_def_free */ - -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 */ - -static 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 */ - -static int graph_def_add_ds (graph_def_t *gd, /* {{{ */ - const char *file, const char *ds_name) -{ - data_source_t *ds; - - ds = realloc (gd->data_sources, sizeof (*ds) * (gd->data_sources_num + 1)); - if (ds == NULL) - return (ENOMEM); - gd->data_sources = ds; - - ds = gd->data_sources + gd->data_sources_num; - memset (ds, 0, sizeof (*ds)); - - ds->file = strdup (file); - if (ds->file == NULL) - return (ENOMEM); - - ds->name = strdup (ds_name); - if (ds->name == NULL) - { - free (ds->file); - return (ENOMEM); - } - - ds->legend = NULL; - ds->color = get_random_color (); - - gd->data_sources_num++; - - return (0); -} /* }}} int graph_def_add_ds */ - -static graph_def_t *graph_def_from_rrd_file (char *file) /* {{{ */ -{ - graph_def_t *gd; - char **dses = NULL; - size_t dses_num = 0; - int status; - size_t i; - - gd = malloc (sizeof (*gd)); - if (gd == NULL) - return (NULL); - memset (gd, 0, sizeof (*gd)); - gd->data_sources = NULL; - - status = ds_list_from_rrd_file (file, &dses_num, &dses); - if (status != 0) - { - free (gd); - return (NULL); - } - - for (i = 0; i < dses_num; i++) - { - graph_def_add_ds (gd, file, dses[i]); - free (dses[i]); - } - - free (dses); - - return (gd); -} /* }}} graph_def_t *graph_def_from_rrd_file */ - -static graph_def_t *graph_def_from_gl (const graph_list_t *gl) /* {{{ */ -{ - char rrd_file[PATH_MAX]; - - if ((gl->plugin_instance == NULL) && (gl->type_instance == NULL)) - snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s.rrd", - DATA_DIR, gl->host, gl->plugin, gl->type); - else if (gl->type_instance == NULL) - snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s.rrd", - DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, gl->type); - else if (gl->plugin_instance == NULL) - snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s/%s-%s.rrd", - DATA_DIR, gl->host, gl->plugin, gl->type, gl->type_instance); - else - snprintf (rrd_file, sizeof (rrd_file), "%s/%s/%s-%s/%s-%s.rrd", - DATA_DIR, gl->host, gl->plugin, gl->plugin_instance, - gl->type, gl->type_instance); - rrd_file[sizeof (rrd_file) - 1] = 0; - - return (graph_def_from_rrd_file (rrd_file)); -} /* }}} graph_def_t *graph_def_from_gl */ - -static int draw_graph_ds (graph_def_t *gd, /* {{{ */ - size_t index, str_array_t *args) -{ - data_source_t *ds; - - assert (index < gd->data_sources_num); - - ds = gd->data_sources + index; - - /* CDEFs */ - array_append_format (args, "DEF:def_%04zu_min=%s:%s:MIN", - index, ds->file, ds->name); - array_append_format (args, "DEF:def_%04zu_avg=%s:%s:AVERAGE", - index, ds->file, ds->name); - array_append_format (args, "DEF:def_%04zu_max=%s:%s:MAX", - index, ds->file, ds->name); - /* VDEFs */ - array_append_format (args, "VDEF:vdef_%04zu_min=def_%04zu_min,MINIMUM", - index, index); - array_append_format (args, "VDEF:vdef_%04zu_avg=def_%04zu_avg,AVERAGE", - index, index); - array_append_format (args, "VDEF:vdef_%04zu_max=def_%04zu_max,MAXIMUM", - index, index); - array_append_format (args, "VDEF:vdef_%04zu_lst=def_%04zu_avg,LAST", - index, index); - - /* Graph part */ - array_append_format (args, "LINE1:def_%04zu_avg#%06x:%s", index, ds->color, - (ds->legend != NULL) ? ds->legend : ds->name); - array_append_format (args, "GPRINT:vdef_%04zu_min:%%lg min,", index); - array_append_format (args, "GPRINT:vdef_%04zu_avg:%%lg avg,", index); - array_append_format (args, "GPRINT:vdef_%04zu_max:%%lg max,", index); - array_append_format (args, "GPRINT:vdef_%04zu_lst:%%lg last\\l", index); - - return (0); -} /* }}} int draw_graph_ds */ - static void emulate_graph (int argc, char **argv) /* {{{ */ { int i; @@ -294,39 +79,6 @@ static int output_graph (rrd_info_t *info) /* {{{ */ return (0); \ } while (0) -static int init_gl (graph_list_t *gl) /* {{{ */ -{ - gl->host = param ("host"); - gl->plugin = param ("plugin"); - gl->plugin_instance = param ("plugin_instance"); - gl->type = param ("type"); - gl->type_instance = param ("type_instance"); - - if ((gl->host == NULL) - || (gl->plugin == NULL) - || (gl->type == NULL)) - return (EINVAL); - - if ((gl->host[0] == 0) || (gl->host[0] == '.') - || (gl->plugin[0] == 0) || (gl->plugin[0] == '.') - || (gl->type[0] == 0) || (gl->type[0] == '.')) - return (EINVAL); - - if ((strchr (gl->plugin, '-') != NULL) - || (strchr (gl->type, '-') != NULL)) - return (EINVAL); - - if ((gl->plugin_instance != NULL) - && (gl->plugin_instance[0] == 0)) - gl->plugin_instance = NULL; - - if ((gl->type_instance != NULL) - && (gl->type_instance[0] == 0)) - gl->type_instance = NULL; - - return (0); -} /* }}} int init_gl */ - int action_graph (void) /* {{{ */ { str_array_t *args; @@ -335,9 +87,9 @@ int action_graph (void) /* {{{ */ rrd_info_t *info; int status; - cfg = graph_get_selected (); + cfg = gl_graph_get_selected (); if (cfg == NULL) - OUTPUT_ERROR ("graph_get_selected () failed.\n"); + OUTPUT_ERROR ("gl_graph_get_selected () failed.\n"); inst = inst_get_selected (cfg); if (inst == NULL) @@ -352,11 +104,11 @@ int action_graph (void) /* {{{ */ array_append (args, "--imgformat"); array_append (args, "PNG"); - status = gl_instance_get_rrdargs (cfg, inst, args); + status = inst_get_rrdargs (cfg, inst, args); if (status != 0) { array_destroy (args); - OUTPUT_ERROR ("gl_instance_get_rrdargs failed with status %i.\n", status); + OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status); } rrd_clear_error ();