7 #include <limits.h> /* PATH_MAX */
9 #include "graph_list.h"
11 #include "utils_params.h"
14 #include <fcgi_stdio.h>
19 #define UPDATE_INTERVAL 10
21 #define ANY_TOKEN "/any/"
22 #define ALL_TOKEN "/all/"
25 # define GL_DEBUG(...) do { \
26 printf ("Content-Type: text/plain\n\n"); \
27 printf (__VA_ARGS__); \
30 # define GL_DEBUG(...) /**/
36 struct graph_ident_s /* {{{ */
40 char *plugin_instance;
43 }; /* }}} struct graph_ident_s */
45 struct graph_instance_s /* {{{ */
52 graph_instance_t *next;
53 }; /* }}} struct graph_instance_s */
55 struct graph_config_s /* {{{ */
62 graph_instance_t *instances;
65 }; /* }}} struct graph_config_s */
70 static graph_config_t *graph_config_head = NULL;
72 static graph_ident_t *graph_list = NULL;
73 static size_t graph_list_length = 0;
74 static time_t gl_last_update = 0;
79 /* FIXME: These "print_*" functions are used for debugging. They should be
80 * removed at some point. */
81 static int print_files (const graph_instance_t *inst) /* {{{ */
85 for (i = 0; i < inst->files_num; i++)
87 graph_ident_t *file = inst->files + i;
89 printf (" File \"%s/%s-%s/%s-%s\"\n",
91 file->plugin, file->plugin_instance,
92 file->type, file->type_instance);
96 } /* }}} int print_instances */
98 static int print_instances (const graph_config_t *cfg) /* {{{ */
100 graph_instance_t *inst;
102 for (inst = cfg->instances; inst != NULL; inst = inst->next)
104 printf (" Instance \"%s/%s-%s/%s-%s\"\n",
106 inst->select.plugin, inst->select.plugin_instance,
107 inst->select.type, inst->select.type_instance);
113 } /* }}} int print_instances */
115 static int print_graphs (void) /* {{{ */
119 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
121 printf ("Graph \"%s/%s-%s/%s-%s\"\n",
123 cfg->select.plugin, cfg->select.plugin_instance,
124 cfg->select.type, cfg->select.type_instance);
126 print_instances (cfg);
130 } /* }}} int print_graphs */
132 /* "Safe" version of strcmp(3): Either or both pointers may be NULL. */
133 static int strcmp_s (const char *s1, const char *s2) /* {{{ */
135 if ((s1 == NULL) && (s2 == NULL))
141 assert ((s1 != NULL) && (s2 != NULL));
143 return (strcmp (s1, s2));
144 } /* }}} int strcmp_s */
146 static _Bool part_matches (const char *sel, const char *val) /* {{{ */
148 if ((sel == NULL) && (val == NULL))
151 if (sel == NULL) /* && (val != NULL) */
154 if ((strcasecmp (ALL_TOKEN, sel) == 0)
155 || (strcasecmp (ANY_TOKEN, sel) == 0))
158 if (val == NULL) /* && (sel != NULL) */
161 if (strcmp (sel, val) == 0)
165 } /* }}} _Bool part_matches */
167 static _Bool file_matches (const graph_ident_t *sel, /* {{{ */
168 const graph_ident_t *val)
170 if ((sel == NULL) && (val == NULL))
172 else if (sel == NULL)
174 else if (val == NULL)
177 if (!part_matches (sel->host, val->host))
180 if (!part_matches (sel->plugin, val->plugin))
183 if (!part_matches (sel->plugin_instance, val->plugin_instance))
186 if (!part_matches (sel->type, val->type))
189 if (!part_matches (sel->type_instance, val->type_instance))
193 } /* }}} _Bool ident_compare */
195 /* Free all the pointers contained in the graph_ident_t but don't free the
196 * graph_ident_t* itself. */
197 static void ident_destroy (graph_ident_t *ident) /* {{{ */
204 free (ident->plugin);
205 ident->plugin = NULL;
206 free (ident->plugin_instance);
207 ident->plugin_instance = NULL;
210 free (ident->type_instance);
211 ident->type_instance = NULL;
212 } /* }}} void ident_destroy */
214 static void instance_destroy (graph_instance_t *inst) /* {{{ */
216 graph_instance_t *next;
224 ident_destroy (&inst->select);
226 for (i = 0; i < inst->files_num; i++)
227 ident_destroy (inst->files + i);
232 instance_destroy (next);
233 } /* }}} void instance_destroy */
235 static void graph_destroy (graph_config_t *cfg) /* {{{ */
237 graph_config_t *next;
244 ident_destroy (&cfg->select);
247 free (cfg->vertical_label);
249 instance_destroy (cfg->instances);
251 graph_destroy (next);
252 } /* }}} void graph_destroy */
255 * Copy part of an identifier. If the "template" value is ANY_TOKEN, "value" is
256 * copied and returned. This function is used when creating graph_instance_t
257 * from graph_config_t.
259 static char *identifier_copy_part (const char *template, const char *value) /* {{{ */
261 if (template == NULL)
265 else if (strcasecmp (ANY_TOKEN, template) == 0)
267 /* ANY in the graph selection => concrete value in the instance. */
271 return (strdup (value));
273 else if (strcasecmp (ALL_TOKEN, template) == 0)
275 /* ALL in the graph selection => ALL in the instance. */
276 return (strdup (ALL_TOKEN));
280 assert (value != NULL);
281 assert (strcmp (template, value) == 0);
282 return (strdup (template));
284 } /* }}} int identifier_copy_part */
286 static graph_instance_t *instance_create (graph_config_t *cfg, /* {{{ */
287 const graph_ident_t *file)
291 if ((cfg == NULL) || (file == NULL))
294 i = malloc (sizeof (*i));
297 memset (i, 0, sizeof (*i));
299 i->select.host = identifier_copy_part (cfg->select.host, file->host);
300 i->select.plugin = identifier_copy_part (cfg->select.plugin, file->plugin);
301 i->select.plugin_instance = identifier_copy_part (cfg->select.plugin_instance,
302 file->plugin_instance);
303 i->select.type = identifier_copy_part (cfg->select.type, file->type);
304 i->select.type_instance = identifier_copy_part (cfg->select.type_instance,
305 file->type_instance);
312 if (cfg->instances == NULL)
316 graph_instance_t *last;
318 last = cfg->instances;
319 while (last->next != NULL)
326 } /* }}} graph_instance_t *instance_create */
328 static int instance_add_file (graph_instance_t *inst, /* {{{ */
329 const graph_ident_t *file)
333 tmp = realloc (inst->files, sizeof (*inst->files) * (inst->files_num + 1));
337 tmp = inst->files + inst->files_num;
339 memset (tmp, 0, sizeof (*tmp));
340 tmp->host = strdup (file->host);
341 tmp->plugin = strdup (file->plugin);
342 if (file->plugin_instance != NULL)
343 tmp->plugin_instance = strdup (file->plugin_instance);
345 tmp->plugin_instance = NULL;
346 tmp->type = strdup (file->type);
347 if (file->type_instance != NULL)
348 tmp->type_instance = strdup (file->type_instance);
350 tmp->type_instance = NULL;
355 } /* }}} int instance_add_file */
357 static graph_instance_t *graph_find_instance (graph_config_t *cfg, /* {{{ */
358 const graph_ident_t *file)
362 if ((cfg == NULL) || (file == NULL))
365 for (i = cfg->instances; i != NULL; i = i->next)
366 if (file_matches (&i->select, file))
370 } /* }}} graph_instance_t *graph_find_instance */
372 static int graph_add_file (graph_config_t *cfg, const graph_ident_t *file) /* {{{ */
374 graph_instance_t *inst;
376 inst = graph_find_instance (cfg, file);
379 inst = instance_create (cfg, file);
384 return (instance_add_file (inst, file));
385 } /* }}} int graph_add_file */
387 static int graph_append (graph_config_t *cfg) /* {{{ */
389 graph_config_t *last;
394 if (graph_config_head == NULL)
396 graph_config_head = cfg;
400 last = graph_config_head;
401 while (last->next != NULL)
407 } /* }}} int graph_append */
409 static int graph_create_from_file (const graph_ident_t *file) /* {{{ */
413 cfg = malloc (sizeof (*cfg));
416 memset (cfg, 0, sizeof (*cfg));
418 cfg->select.host = strdup (file->host);
419 cfg->select.plugin = strdup (file->plugin);
420 cfg->select.plugin_instance = strdup (file->plugin_instance);
421 cfg->select.type = strdup (file->type);
422 cfg->select.type_instance = strdup (file->type_instance);
425 cfg->vertical_label = NULL;
426 cfg->instances = NULL;
431 return (graph_add_file (cfg, file));
432 } /* }}} int graph_create_from_file */
434 static int register_file (const graph_ident_t *file) /* {{{ */
439 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
443 if (!file_matches (&cfg->select, file))
446 status = graph_add_file (cfg, file);
458 graph_create_from_file (file);
461 } /* }}} int register_file */
463 static int FIXME_graph_create_from_file (const graph_ident_t *file, /* {{{ */
468 cfg = malloc (sizeof (*cfg));
471 memset (cfg, 0, sizeof (*cfg));
473 cfg->select.host = strdup (file->host);
474 cfg->select.plugin = strdup (file->plugin);
475 cfg->select.plugin_instance = strdup (file->plugin_instance);
476 cfg->select.type = strdup (file->type);
477 cfg->select.type_instance = strdup (file->type_instance);
481 cfg->title = strdup (title);
482 cfg->vertical_label = NULL;
483 cfg->instances = NULL;
489 } /* }}} int FIXME_graph_create_from_file */
491 /* FIXME: Actually read the config file here. */
492 static int read_graph_config (void) /* {{{ */
494 if (graph_config_head != NULL)
500 ident.host = ANY_TOKEN;
501 ident.plugin = "cpu";
502 ident.plugin_instance = ANY_TOKEN;
504 ident.type_instance = ALL_TOKEN;
505 FIXME_graph_create_from_file (&ident, "CPU {instance} usage");
507 ident.plugin = "memory";
508 ident.plugin_instance = "";
509 ident.type = "memory";
510 FIXME_graph_create_from_file (&ident, "Memory usage");
512 ident.plugin = "swap";
513 ident.plugin_instance = "";
515 FIXME_graph_create_from_file (&ident, "Swap");
517 ident.plugin = ANY_TOKEN;
518 ident.plugin_instance = ANY_TOKEN;
519 ident.type = "ps_state";
520 FIXME_graph_create_from_file (&ident, "Processes");
522 ident.host = ALL_TOKEN;
523 ident.plugin = "cpu";
524 ident.plugin_instance = ALL_TOKEN;
526 ident.type_instance = "idle";
527 FIXME_graph_create_from_file (&ident, "CPU idle overview");
530 } /* }}} int read_graph_config */
532 static int gl_compare (const void *p0, const void *p1) /* {{{ */
534 const graph_ident_t *gl0 = p0;
535 const graph_ident_t *gl1 = p1;
538 status = strcmp (gl0->host, gl1->host);
542 status = strcmp (gl0->plugin, gl1->plugin);
546 status = strcmp_s (gl0->plugin_instance, gl1->plugin_instance);
550 status = strcmp (gl0->type, gl1->type);
554 return (strcmp_s (gl0->type_instance, gl1->type_instance));
555 } /* }}} int gl_compare */
557 static void gl_clear_entry (graph_ident_t *gl) /* {{{ */
564 free (gl->plugin_instance);
566 free (gl->type_instance);
570 gl->plugin_instance = NULL;
572 gl->type_instance = NULL;
573 } /* }}} void gl_clear_entry */
575 static void gl_clear (void) /* {{{ */
580 cfg = graph_config_head;
581 graph_config_head = NULL;
584 for (i = 0; i < graph_list_length; i++)
585 gl_clear_entry (graph_list + i);
589 graph_list_length = 0;
591 } /* }}} void gl_clear */
593 static int gl_add_copy (graph_ident_t *gl) /* {{{ */
601 ptr = realloc (graph_list, sizeof (*graph_list) * (graph_list_length + 1));
606 ptr = graph_list + graph_list_length;
607 memset (ptr, 0, sizeof (*ptr));
610 ptr->plugin_instance = NULL;
612 ptr->type_instance = NULL;
614 #define DUP_OR_BREAK(member) do { \
615 ptr->member = NULL; \
616 if (gl->member != NULL) \
618 ptr->member = strdup (gl->member); \
619 if (ptr->member == NULL) \
628 DUP_OR_BREAK(plugin);
629 DUP_OR_BREAK(plugin_instance);
631 DUP_OR_BREAK(type_instance);
642 free (ptr->plugin_instance);
644 free (ptr->type_instance);
650 } /* }}} int gl_add_copy */
652 static int callback_type (const char *type, void *user_data) /* {{{ */
657 if ((type == NULL) || (user_data == NULL))
661 if ((gl->type != NULL) || (gl->type_instance != NULL))
664 gl->type = strdup (type);
665 if (gl->type == NULL)
668 gl->type_instance = strchr (gl->type, '-');
669 if (gl->type_instance != NULL)
671 *gl->type_instance = 0;
676 gl->type_instance = gl->type + strlen (gl->type);
681 status = gl_add_copy (gl);
685 gl->type_instance = NULL;
688 } /* }}} int callback_type */
690 static int callback_plugin (const char *plugin, void *user_data) /* {{{ */
695 if ((plugin == NULL) || (user_data == NULL))
699 if ((gl->plugin != NULL) || (gl->plugin_instance != NULL))
702 gl->plugin = strdup (plugin);
703 if (gl->plugin == NULL)
706 gl->plugin_instance = strchr (gl->plugin, '-');
707 if (gl->plugin_instance != NULL)
709 *gl->plugin_instance = 0;
710 gl->plugin_instance++;
714 gl->plugin_instance = gl->plugin + strlen (gl->plugin);
717 status = foreach_type (gl->host, plugin, callback_type, gl);
721 gl->plugin_instance = NULL;
724 } /* }}} int callback_plugin */
726 static int callback_host (const char *host, void *user_data) /* {{{ */
731 if ((host == NULL) || (user_data == NULL))
735 if (gl->host != NULL)
738 gl->host = strdup (host);
739 if (gl->host == NULL)
742 status = foreach_plugin (host, callback_plugin, gl);
748 } /* }}} int callback_host */
750 static const char *get_part_from_param (const char *prim_key, /* {{{ */
755 val = param (prim_key);
759 return (param (sec_key));
760 } /* }}} const char *get_part_from_param */
762 int gl_ident_get_rrdargs (graph_config_t *cfg, /* {{{ */
763 graph_instance_t *inst,
764 graph_ident_t *ident,
773 if ((cfg == NULL) || (inst == NULL) || (ident == NULL) || (args == NULL))
776 file = ident_to_file (ident);
779 GL_DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
783 GL_DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
785 status = ds_list_from_rrd_file (file, &dses_num, &dses);
792 for (i = 0; i < dses_num; i++)
796 GL_DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
798 id = array_argc (args);
799 array_append_format (args, "DEF:avg%i=%s:%s:AVERAGE", id, file, dses[i]);
800 array_append_format (args, "LINE1:avg%i#ff0000:%s\\l", id, file);
809 } /* }}} int gl_ident_get_rrdargs */
814 char *ident_to_file (const graph_ident_t *ident) /* {{{ */
816 char buffer[PATH_MAX];
820 strlcat (buffer, DATA_DIR, sizeof (buffer));
821 strlcat (buffer, "/", sizeof (buffer));
823 strlcat (buffer, ident->host, sizeof (buffer));
824 strlcat (buffer, "/", sizeof (buffer));
825 strlcat (buffer, ident->plugin, sizeof (buffer));
826 if (ident->plugin_instance[0] != 0)
828 strlcat (buffer, "-", sizeof (buffer));
829 strlcat (buffer, ident->plugin_instance, sizeof (buffer));
831 strlcat (buffer, "/", sizeof (buffer));
832 strlcat (buffer, ident->type, sizeof (buffer));
833 if (ident->type_instance[0] != 0)
835 strlcat (buffer, "-", sizeof (buffer));
836 strlcat (buffer, ident->type_instance, sizeof (buffer));
839 strlcat (buffer, ".rrd", sizeof (buffer));
841 return (strdup (buffer));
842 } /* }}} char *ident_to_file */
844 int gl_instance_get_params (graph_config_t *cfg, graph_instance_t *inst, /* {{{ */
845 char *buffer, size_t buffer_size)
847 if ((inst == NULL) || (buffer == NULL) || (buffer_size < 1))
852 #define COPY_FIELD(field) do { \
853 if (strcmp (cfg->select.field, inst->select.field) == 0) \
855 strlcat (buffer, #field, buffer_size); \
856 strlcat (buffer, "=", buffer_size); \
857 strlcat (buffer, cfg->select.field, buffer_size); \
861 strlcat (buffer, "graph_", buffer_size); \
862 strlcat (buffer, #field, buffer_size); \
863 strlcat (buffer, "=", buffer_size); \
864 strlcat (buffer, cfg->select.field, buffer_size); \
865 strlcat (buffer, ";", buffer_size); \
866 strlcat (buffer, "inst_", buffer_size); \
867 strlcat (buffer, #field, buffer_size); \
868 strlcat (buffer, "=", buffer_size); \
869 strlcat (buffer, inst->select.field, buffer_size); \
874 strlcat (buffer, ";", buffer_size);
876 strlcat (buffer, ";", buffer_size);
877 COPY_FIELD(plugin_instance);
878 strlcat (buffer, ";", buffer_size);
880 strlcat (buffer, ";", buffer_size);
881 COPY_FIELD(type_instance);
886 } /* }}} int gl_instance_get_params */
888 graph_instance_t *inst_get_selected (graph_config_t *cfg) /* {{{ */
890 const char *host = get_part_from_param ("inst_host", "host");
891 const char *plugin = get_part_from_param ("inst_plugin", "plugin");
892 const char *plugin_instance = get_part_from_param ("inst_plugin_instance", "plugin_instance");
893 const char *type = get_part_from_param ("inst_type", "type");
894 const char *type_instance = get_part_from_param ("inst_type_instance", "type_instance");
895 graph_instance_t *inst;
898 cfg = graph_get_selected ();
902 GL_DEBUG ("inst_get_selected: cfg == NULL;\n");
907 || (plugin == NULL) || (plugin_instance == NULL)
908 || (type == NULL) || (type_instance == NULL))
910 GL_DEBUG ("inst_get_selected: A parameter is NULL.\n");
914 for (inst = cfg->instances; inst != NULL; inst = inst->next)
916 if ((strcmp (inst->select.host, host) != 0)
917 || (strcmp (inst->select.plugin, plugin) != 0)
918 || (strcmp (inst->select.plugin_instance, plugin_instance) != 0)
919 || (strcmp (inst->select.type, type) != 0)
920 || (strcmp (inst->select.type_instance, type_instance) != 0))
926 GL_DEBUG ("inst_get_selected: No match found.\n");
928 } /* }}} graph_instance_t *inst_get_selected */
930 int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */
935 if (callback == NULL)
940 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
944 status = (*callback) (cfg, user_data);
950 } /* }}} int gl_graph_get_all */
952 graph_config_t *graph_get_selected (void) /* {{{ */
954 const char *host = get_part_from_param ("graph_host", "host");
955 const char *plugin = get_part_from_param ("graph_plugin", "plugin");
956 const char *plugin_instance = get_part_from_param ("graph_plugin_instance", "plugin_instance");
957 const char *type = get_part_from_param ("graph_type", "type");
958 const char *type_instance = get_part_from_param ("graph_type_instance", "type_instance");
962 || (plugin == NULL) || (plugin_instance == NULL)
963 || (type == NULL) || (type_instance == NULL))
965 printf ("Content-Type: text/plain\n\n");
966 printf ("graph_get_selected(): A param is NULL.\n");
972 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
974 if ((strcmp (cfg->select.host, host) != 0)
975 || (strcmp (cfg->select.plugin, plugin) != 0)
976 || (strcmp (cfg->select.plugin_instance, plugin_instance) != 0)
977 || (strcmp (cfg->select.type, type) != 0)
978 || (strcmp (cfg->select.type_instance, type_instance) != 0))
984 printf ("Content-Type: text/plain\n\n");
985 printf ("graph_get_selected(): No matching graph found.\n");
988 } /* }}} graph_config_t *graph_get_selected */
990 int gl_graph_instance_get_all (graph_config_t *cfg, /* {{{ */
991 gl_inst_callback callback, void *user_data)
993 graph_instance_t *inst;
995 if ((cfg == NULL) || (callback == NULL))
998 for (inst = cfg->instances; inst != NULL; inst = inst->next)
1002 status = (*callback) (cfg, inst, user_data);
1008 } /* }}} int gl_graph_instance_get_all */
1010 int gl_graph_get_title (graph_config_t *cfg, /* {{{ */
1011 char *buffer, size_t buffer_size)
1013 if ((cfg == NULL) || (buffer == NULL) || (buffer_size < 1))
1017 strlcat (buffer, cfg->select.host, buffer_size);
1018 strlcat (buffer, "/", buffer_size);
1019 strlcat (buffer, cfg->select.plugin, buffer_size);
1020 if (cfg->select.plugin_instance[0] != 0)
1022 strlcat (buffer, "-", buffer_size);
1023 strlcat (buffer, cfg->select.plugin_instance, buffer_size);
1025 strlcat (buffer, "/", buffer_size);
1026 strlcat (buffer, cfg->select.type, buffer_size);
1027 if (cfg->select.type_instance[0] != 0)
1029 strlcat (buffer, "-", buffer_size);
1030 strlcat (buffer, cfg->select.type_instance, buffer_size);
1034 } /* }}} int gl_graph_get_title */
1036 int gl_instance_get_all (gl_inst_callback callback, /* {{{ */
1039 graph_config_t *cfg;
1043 for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next)
1045 graph_instance_t *inst;
1047 for (inst = cfg->instances; inst != NULL; inst = inst->next)
1051 status = (*callback) (cfg, inst, user_data);
1058 } /* }}} int gl_instance_get_all */
1060 int gl_instance_get_rrdargs (graph_config_t *cfg, /* {{{ */
1061 graph_instance_t *inst,
1066 if ((cfg == NULL) || (inst == NULL) || (args == NULL))
1069 if (cfg->title != NULL)
1071 array_append (args, "-t");
1072 array_append (args, cfg->title);
1075 for (i = 0; i < inst->files_num; i++)
1079 status = gl_ident_get_rrdargs (cfg, inst, inst->files + i, args);
1085 } /* }}} int gl_instance_get_rrdargs */
1089 int gl_update (void) /* {{{ */
1096 printf ("Content-Type: text/plain\n\n");
1101 if ((gl_last_update + UPDATE_INTERVAL) >= now)
1106 read_graph_config ();
1108 memset (&gl, 0, sizeof (gl));
1111 gl.plugin_instance = NULL;
1113 gl.type_instance = NULL;
1115 status = foreach_host (callback_host, &gl);
1117 /* print_graphs (); */
1119 if (graph_list_length > 1)
1120 qsort (graph_list, graph_list_length, sizeof (*graph_list), gl_compare);
1123 } /* }}} int gl_update */
1125 int gl_foreach (gl_callback callback, void *user_data) /* {{{ */
1129 for (i = 0; i < graph_list_length; i++)
1133 status = (*callback) ((void *) (graph_list + i), user_data);
1139 } /* }}} int gl_foreach */
1141 /* vim: set sw=2 sts=2 et fdm=marker : */