graph_def.[ch]: Class for handling data sources in graphs (DEFs).
authorFlorian Forster <ff@octo.it>
Mon, 14 Jun 2010 15:07:56 +0000 (17:07 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Mon, 14 Jun 2010 15:07:56 +0000 (17:07 +0200)
Makefile
graph_def.c [new file with mode: 0644]
graph_def.h [new file with mode: 0644]

index cfec3c5..c1d879f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,8 @@ common.o: common.c common.h
 
 graph_ident.o: graph_ident.c graph_ident.h
 
+graph_def.o: graph_def.c graph_def.h
+
 graph_list.o: graph_list.c graph_list.h
 
 utils_array.o: utils_array.c utils_array.h
@@ -26,7 +28,7 @@ action_list_graphs.o: action_list_graphs.c action_list_graphs.h
 test: test.c utils_params.o
 
 test.fcgi: LDLIBS = -lfcgi -lrrd
-test.fcgi: test.fcgi.c common.o graph_ident.o graph_list.o utils_array.o utils_params.o action_graph.o action_list_graphs.o
+test.fcgi: test.fcgi.c common.o graph_ident.o graph_def.o graph_list.o utils_array.o utils_params.o action_graph.o action_list_graphs.o
 
 .PHONY: clean
 
diff --git a/graph_def.c b/graph_def.c
new file mode 100644 (file)
index 0000000..80f6a0c
--- /dev/null
@@ -0,0 +1,156 @@
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "graph_def.h"
+#include "common.h"
+
+/*
+ * Data structures
+ */
+struct graph_def_s
+{
+  graph_ident_t *select;
+
+  graph_def_t *next;
+};
+
+/*
+ * Private functions
+ */
+
+/*
+ * Public functions
+ */
+graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident) /* {{{ */
+{
+  graph_ident_t *selector;
+  graph_def_t *ret;
+
+  selector = gl_graph_get_selector (cfg);
+  if (selector == NULL)
+    return (NULL);
+
+  ret = malloc (sizeof (*ret));
+  if (ret == NULL)
+  {
+    ident_destroy (selector);
+    return (NULL);
+  }
+  memset (ret, 0, sizeof (*ret));
+  ret->next = NULL;
+
+  ret->select = ident_copy_with_selector (selector, ident,
+      IDENT_FLAG_REPLACE_ALL);
+  if (ret->select == NULL)
+  {
+    ident_destroy (selector);
+    free (ret);
+    return (NULL);
+  }
+
+  ident_destroy (selector);
+  return (ret);
+}; /* }}} graph_def_t *def_create */
+
+void def_destroy (graph_def_t *def) /* {{{ */
+{
+  graph_def_t *next;
+
+  if (def == NULL)
+    return;
+
+  next = def->next;
+
+  ident_destroy (def->select);
+
+  free (def);
+
+  def_destroy (next);
+} /* }}} void def_destroy */
+
+graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident) /* {{{ */
+{
+  graph_def_t *ptr;
+
+  if ((head == NULL) || (ident == NULL))
+    return (NULL);
+
+  for (ptr = head; ptr != NULL; ptr = ptr->next)
+    if (ident_matches (ptr->select, ident))
+      return (ptr);
+
+  return (NULL);
+} /* }}} graph_def_t *def_search */
+
+int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
+    str_array_t *args)
+{
+  char *file;
+  char **dses = NULL;
+  size_t dses_num = 0;
+  int status;
+  size_t i;
+
+  if ((def == NULL) || (ident == NULL) || (args == NULL))
+    return (EINVAL);
+
+  file = ident_to_file (ident);
+  if (file == NULL)
+  {
+    DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
+    return (-1);
+  }
+
+  DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
+
+  status = ds_list_from_rrd_file (file, &dses_num, &dses);
+  if (status != 0)
+  {
+    free (file);
+    return (status);
+  }
+
+  for (i = 0; i < dses_num; i++)
+  {
+    int index;
+
+    DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
+
+    index = array_argc (args);
+
+    /* CDEFs */
+    array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
+        index, file, dses[i]);
+    array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
+        index, file, dses[i]);
+    array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
+        index, file, dses[i]);
+    /* VDEFs */
+    array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
+        index, index);
+    array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
+        index, index);
+    array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
+        index, index);
+    array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
+        index, index);
+
+    /* Graph part */
+    array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
+        index, get_random_color (), dses[i]);
+    array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
+    array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
+    array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
+    array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
+
+    free (dses[i]);
+  }
+
+  free (dses);
+  free (file);
+
+  return (0);
+} /* }}} int def_get_rrdargs */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/graph_def.h b/graph_def.h
new file mode 100644 (file)
index 0000000..780ed37
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef GRAPH_DEF_H
+#define GRAPH_DEF_H 1
+
+#include "graph_ident.h"
+#include "utils_array.h"
+#include "graph_list.h"
+
+struct graph_def_s;
+typedef struct graph_def_s graph_def_t;
+
+graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident);
+
+void def_destroy (graph_def_t *def);
+
+graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident);
+
+int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident,
+    str_array_t *args);
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
+#endif