10 #include "graph_list.h"
11 #include "graph_ident.h"
12 #include "graph_def.h"
14 #include "utils_params.h"
17 #include <fcgi_stdio.h>
22 #define UPDATE_INTERVAL 10
24 #define ANY_TOKEN "/any/"
25 #define ALL_TOKEN "/all/"
30 struct gl_ident_stage_s /* {{{ */
34 char *plugin_instance;
38 typedef struct gl_ident_stage_s gl_ident_stage_t;
40 struct graph_instance_s /* {{{ */
42 graph_ident_t *select;
44 graph_ident_t **files;
47 graph_instance_t *next;
48 }; /* }}} struct graph_instance_s */
50 struct graph_config_s /* {{{ */
52 graph_ident_t *select;
59 graph_instance_t *instances;
62 }; /* }}} struct graph_config_s */
67 static graph_config_t *graph_config_head = NULL;
69 static time_t gl_last_update = 0;
74 /* FIXME: These "print_*" functions are used for debugging. They should be
75 * removed at some point. */
76 static int print_files (const graph_instance_t *inst) /* {{{ */
80 for (i = 0; i < inst->files_num; i++)
82 graph_ident_t *ident = inst->files[i];
85 file = ident_to_file (ident);
86 printf (" File \"%s\"\n", file);
91 } /* }}} int print_instances */
93 static int print_instances (const graph_config_t *cfg) /* {{{ */
95 graph_instance_t *inst;
97 for (inst = cfg->instances; inst != NULL; inst = inst->next)
101 str = ident_to_string (inst->select);
102 printf (" Instance \"%s\"\n", str);
109 } /* }}} int print_instances */
111 static int print_graphs (void) /* {{{ */
115 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
119 str = ident_to_string (cfg->select);
120 printf ("Graph \"%s\"\n", str);
123 print_instances (cfg);
127 } /* }}} int print_graphs */
130 /* "Safe" version of strcmp(3): Either or both pointers may be NULL. */
131 static int strcmp_s (const char *s1, const char *s2) /* {{{ */
133 if ((s1 == NULL) && (s2 == NULL))
139 assert ((s1 != NULL) && (s2 != NULL));
141 return (strcmp (s1, s2));
142 } /* }}} int strcmp_s */
145 static void instance_destroy (graph_instance_t *inst) /* {{{ */
147 graph_instance_t *next;
155 ident_destroy (inst->select);
157 for (i = 0; i < inst->files_num; i++)
158 ident_destroy (inst->files[i]);
163 instance_destroy (next);
164 } /* }}} void instance_destroy */
167 * Copy part of an identifier. If the "template" value is ANY_TOKEN, "value" is
168 * copied and returned. This function is used when creating graph_instance_t
169 * from graph_config_t.
171 static graph_instance_t *instance_create (graph_config_t *cfg, /* {{{ */
172 const graph_ident_t *file)
176 if ((cfg == NULL) || (file == NULL))
179 i = malloc (sizeof (*i));
182 memset (i, 0, sizeof (*i));
184 i->select = ident_copy_with_selector (cfg->select, file,
185 IDENT_FLAG_REPLACE_ANY);
192 if (cfg->instances == NULL)
196 graph_instance_t *last;
198 last = cfg->instances;
199 while (last->next != NULL)
206 } /* }}} graph_instance_t *instance_create */
208 static int instance_add_file (graph_instance_t *inst, /* {{{ */
209 const graph_ident_t *file)
213 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
218 inst->files[inst->files_num] = ident_clone (file);
219 if (inst->files[inst->files_num] == NULL)
225 } /* }}} int instance_add_file */
227 static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
228 const graph_ident_t *file)
232 if ((cfg == NULL) || (file == NULL))
235 for (i = cfg->instances; i != NULL; i = i->next)
236 if (ident_matches (i->select, file))
240 } /* }}} graph_instance_t *graph_find_instance */
242 static int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
244 graph_instance_t *inst;
246 inst = graph_find_instance (cfg, file);
249 inst = instance_create (cfg, file);
254 return (instance_add_file (inst, file));
255 } /* }}} int graph_add_file */
257 static int graph_append (graph_config_t *cfg) /* {{{ */
259 graph_config_t *last;
264 if (graph_config_head == NULL)
266 graph_config_head = cfg;
270 last = graph_config_head;
271 while (last->next != NULL)
277 } /* }}} int graph_append */
279 static graph_config_t *graph_create (const graph_ident_t *selector) /* {{{ */
283 cfg = malloc (sizeof (*cfg));
286 memset (cfg, 0, sizeof (*cfg));
288 cfg->select = ident_clone (selector);
291 cfg->vertical_label = NULL;
293 cfg->instances = NULL;
297 } /* }}} int graph_create */
299 static void graph_destroy (graph_config_t *cfg) /* {{{ */
301 graph_config_t *next;
308 ident_destroy (cfg->select);
311 free (cfg->vertical_label);
313 def_destroy (cfg->defs);
314 instance_destroy (cfg->instances);
316 graph_destroy (next);
317 } /* }}} void graph_destroy */
319 static int register_file (const graph_ident_t *file) /* {{{ */
324 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
328 if (!ident_matches (cfg->select, file))
331 status = graph_add_file (cfg, file);
344 cfg = graph_create (file);
346 graph_add_file (cfg, file);
350 } /* }}} int register_file */
352 static int FIXME_graph_create_from_file (const char *host, /* {{{ */
353 const char *plugin, const char *plugin_instance,
354 const char *type, const char *type_instance,
359 cfg = malloc (sizeof (*cfg));
362 memset (cfg, 0, sizeof (*cfg));
364 cfg->select = ident_create (host, plugin, plugin_instance, type, type_instance);
368 cfg->title = strdup (title);
369 cfg->vertical_label = NULL;
370 cfg->instances = NULL;
376 } /* }}} int FIXME_graph_create_from_file */
378 /* FIXME: Actually read the config file here. */
379 static int read_graph_config (void) /* {{{ */
381 if (graph_config_head != NULL)
384 FIXME_graph_create_from_file (ANY_TOKEN, "cpu", ANY_TOKEN, "cpu", ALL_TOKEN,
385 "CPU {instance} usage");
386 FIXME_graph_create_from_file (ANY_TOKEN, "memory", "", "memory", ALL_TOKEN,
388 FIXME_graph_create_from_file (ANY_TOKEN, "swap", "", "swap", ALL_TOKEN,
390 FIXME_graph_create_from_file (ANY_TOKEN, ANY_TOKEN, ANY_TOKEN, "ps_state", ALL_TOKEN,
392 FIXME_graph_create_from_file (ANY_TOKEN, "cpu", ALL_TOKEN, "cpu", "idle",
393 "CPU idle overview");
396 } /* }}} int read_graph_config */
398 static void gl_clear (void) /* {{{ */
402 cfg = graph_config_head;
403 graph_config_head = NULL;
407 } /* }}} void gl_clear */
409 static int callback_type (const char *type, void *user_data) /* {{{ */
411 gl_ident_stage_t *gl;
412 graph_ident_t *ident;
415 if ((type == NULL) || (user_data == NULL))
419 if ((gl->type != NULL) || (gl->type_instance != NULL))
422 gl->type = strdup (type);
423 if (gl->type == NULL)
426 gl->type_instance = strchr (gl->type, '-');
427 if (gl->type_instance != NULL)
429 *gl->type_instance = 0;
434 gl->type_instance = gl->type + strlen (gl->type);
437 ident = ident_create (gl->host,
438 gl->plugin, gl->plugin_instance,
439 gl->type, gl->type_instance);
446 status = register_file (ident);
447 ident_destroy (ident);
452 gl->type_instance = NULL;
455 } /* }}} int callback_type */
457 static int callback_plugin (const char *plugin, void *user_data) /* {{{ */
459 gl_ident_stage_t *gl;
462 if ((plugin == NULL) || (user_data == NULL))
466 if ((gl->plugin != NULL) || (gl->plugin_instance != NULL))
469 gl->plugin = strdup (plugin);
470 if (gl->plugin == NULL)
473 gl->plugin_instance = strchr (gl->plugin, '-');
474 if (gl->plugin_instance != NULL)
476 *gl->plugin_instance = 0;
477 gl->plugin_instance++;
481 gl->plugin_instance = gl->plugin + strlen (gl->plugin);
484 status = foreach_type (gl->host, plugin, callback_type, gl);
488 gl->plugin_instance = NULL;
491 } /* }}} int callback_plugin */
493 static int callback_host (const char *host, void *user_data) /* {{{ */
495 gl_ident_stage_t *gl;
498 if ((host == NULL) || (user_data == NULL))
502 if (gl->host != NULL)
505 gl->host = strdup (host);
506 if (gl->host == NULL)
509 status = foreach_plugin (host, callback_plugin, gl);
515 } /* }}} int callback_host */
517 static const char *get_part_from_param (const char *prim_key, /* {{{ */
522 val = param (prim_key);
526 return (param (sec_key));
527 } /* }}} const char *get_part_from_param */
529 int gl_ident_get_rrdargs (graph_config_t *cfg, /* {{{ */
530 graph_instance_t *inst,
531 graph_ident_t *ident,
540 if ((cfg == NULL) || (inst == NULL) || (ident == NULL) || (args == NULL))
543 file = ident_to_file (ident);
546 DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
550 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
552 status = ds_list_from_rrd_file (file, &dses_num, &dses);
559 for (i = 0; i < dses_num; i++)
563 DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
565 index = array_argc (args);
568 array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
569 index, file, dses[i]);
570 array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
571 index, file, dses[i]);
572 array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
573 index, file, dses[i]);
575 array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
577 array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
579 array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
581 array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
585 array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
586 index, get_random_color (), dses[i]);
587 array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
588 array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
589 array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
590 array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
599 } /* }}} int gl_ident_get_rrdargs */
604 int gl_instance_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
605 char *buffer, size_t buffer_size)
607 if ((inst == NULL) || (buffer == NULL) || (buffer_size < 1))
612 #define COPY_FIELD(field) do { \
613 const char *cfg_f = ident_get_##field (cfg->select); \
614 const char *inst_f = ident_get_##field (inst->select); \
615 if (strcmp (cfg_f, inst_f) == 0) \
617 strlcat (buffer, #field, buffer_size); \
618 strlcat (buffer, "=", buffer_size); \
619 strlcat (buffer, cfg_f, buffer_size); \
623 strlcat (buffer, "graph_", buffer_size); \
624 strlcat (buffer, #field, buffer_size); \
625 strlcat (buffer, "=", buffer_size); \
626 strlcat (buffer, cfg_f, buffer_size); \
627 strlcat (buffer, ";", buffer_size); \
628 strlcat (buffer, "inst_", buffer_size); \
629 strlcat (buffer, #field, buffer_size); \
630 strlcat (buffer, "=", buffer_size); \
631 strlcat (buffer, inst_f, buffer_size); \
636 strlcat (buffer, ";", buffer_size);
638 strlcat (buffer, ";", buffer_size);
639 COPY_FIELD(plugin_instance);
640 strlcat (buffer, ";", buffer_size);
642 strlcat (buffer, ";", buffer_size);
643 COPY_FIELD(type_instance);
648 } /* }}} int gl_instance_get_params */
650 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
652 const char *host = get_part_from_param ("inst_host", "host");
653 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
654 const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
655 const char *type = get_part_from_param ("inst_type", "type");
656 const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
657 graph_ident_t *ident;
658 graph_instance_t *inst;
661 cfg = graph_get_selected ();
665 DEBUG ("inst_get_selected: cfg == NULL;\n");
670 || (plugin == NULL) || (plugin_instance == NULL)
671 || (type == NULL) || (type_instance == NULL))
673 DEBUG ("inst_get_selected: A parameter is NULL.\n");
677 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
679 for (inst = cfg->instances; inst != NULL; inst = inst->next)
681 if (ident_compare (ident, inst->select) != 0)
684 ident_destroy (ident);
688 DEBUG ("inst_get_selected: No match found.\n");
689 ident_destroy (ident);
691 } /* }}} graph_instance_t *inst_get_selected */
693 int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */
698 if (callback == NULL)
703 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
707 status = (*callback) (cfg, user_data);
713 } /* }}} int gl_graph_get_all */
715 graph_config_t *graph_get_selected (void) /* {{{ */
717 const char *host = get_part_from_param ("graph_host", "host");
718 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
719 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
720 const char *type = get_part_from_param ("graph_type", "type");
721 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
722 graph_ident_t *ident;
726 || (plugin == NULL) || (plugin_instance == NULL)
727 || (type == NULL) || (type_instance == NULL))
730 ident = ident_create (host, plugin, plugin_instance, type, type_instance);
734 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
736 if (ident_compare (ident, cfg->select) != 0)
739 ident_destroy (ident);
743 ident_destroy (ident);
745 } /* }}} graph_config_t *graph_get_selected */
747 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
748 gl_inst_callback callback, void *user_data)
750 graph_instance_t *inst;
752 if ((cfg == NULL) || (callback == NULL))
755 for (inst = cfg->instances; inst != NULL; inst = inst->next)
759 status = (*callback) (cfg, inst, user_data);
765 } /* }}} int gl_graph_instance_get_all */
767 int gl_graph_get_title (graph_config_t *cfg, /* {{{ */
768 char *buffer, size_t buffer_size)
772 if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
775 if (cfg->title != NULL)
778 str = ident_to_string (cfg->select);
783 strncpy (buffer, str, buffer_size);
784 buffer[buffer_size - 1] = 0;
789 } /* }}} int gl_graph_get_title */
791 graph_ident_t *gl_graph_get_selector (graph_config_t *cfg) /* {{{ */
796 return (ident_clone (cfg->select));
797 } /* }}} graph_ident_t *gl_graph_get_selector */
799 int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
806 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
808 graph_instance_t *inst;
810 for (inst = cfg->instances; inst != NULL; inst = inst->next)
814 status = (*callback) (cfg, inst, user_data);
821 } /* }}} int gl_instance_get_all */
823 int gl_instance_get_rrdargs (graph_config_t *cfg, /* {{{ */
824 graph_instance_t *inst,
829 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
832 if (cfg->title != NULL)
834 array_append (args, "-t");
835 array_append (args, cfg->title);
838 for (i = 0; i < inst->files_num; i++)
843 def = def_search (cfg->defs, inst->files[i]);
846 def = def_create (cfg, inst->files[i]);
850 if (cfg->defs == NULL)
853 def_append (cfg->defs, def);
856 status = def_get_rrdargs (def, inst->files[i], args);
862 } /* }}} int gl_instance_get_rrdargs */
864 graph_ident_t *gl_instance_get_selector (graph_instance_t *inst) /* {{{ */
869 return (ident_clone (inst->select));
870 } /* }}} graph_ident_t *gl_instance_get_selector */
874 int gl_update (void) /* {{{ */
881 printf ("Content-Type: text/plain\n\n");
886 if ((gl_last_update + UPDATE_INTERVAL) >= now)
891 read_graph_config ();
893 memset (&gl, 0, sizeof (gl));
896 gl.plugin_instance = NULL;
898 gl.type_instance = NULL;
900 status = foreach_host (callback_host, &gl);
902 /* print_graphs (); */
905 } /* }}} int gl_update */
907 /* vim: set sw=2 sts=2 et fdm=marker : */