Code

graph_def.[ch]: Class for handling data sources in graphs (DEFs).
[collection4.git] / graph_def.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
5 #include "graph_def.h"
6 #include "common.h"
8 /*
9  * Data structures
10  */
11 struct graph_def_s
12 {
13   graph_ident_t *select;
15   graph_def_t *next;
16 };
18 /*
19  * Private functions
20  */
22 /*
23  * Public functions
24  */
25 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident) /* {{{ */
26 {
27   graph_ident_t *selector;
28   graph_def_t *ret;
30   selector = gl_graph_get_selector (cfg);
31   if (selector == NULL)
32     return (NULL);
34   ret = malloc (sizeof (*ret));
35   if (ret == NULL)
36   {
37     ident_destroy (selector);
38     return (NULL);
39   }
40   memset (ret, 0, sizeof (*ret));
41   ret->next = NULL;
43   ret->select = ident_copy_with_selector (selector, ident,
44       IDENT_FLAG_REPLACE_ALL);
45   if (ret->select == NULL)
46   {
47     ident_destroy (selector);
48     free (ret);
49     return (NULL);
50   }
52   ident_destroy (selector);
53   return (ret);
54 }; /* }}} graph_def_t *def_create */
56 void def_destroy (graph_def_t *def) /* {{{ */
57 {
58   graph_def_t *next;
60   if (def == NULL)
61     return;
63   next = def->next;
65   ident_destroy (def->select);
67   free (def);
69   def_destroy (next);
70 } /* }}} void def_destroy */
72 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident) /* {{{ */
73 {
74   graph_def_t *ptr;
76   if ((head == NULL) || (ident == NULL))
77     return (NULL);
79   for (ptr = head; ptr != NULL; ptr = ptr->next)
80     if (ident_matches (ptr->select, ident))
81       return (ptr);
83   return (NULL);
84 } /* }}} graph_def_t *def_search */
86 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
87     str_array_t *args)
88 {
89   char *file;
90   char **dses = NULL;
91   size_t dses_num = 0;
92   int status;
93   size_t i;
95   if ((def == NULL) || (ident == NULL) || (args == NULL))
96     return (EINVAL);
98   file = ident_to_file (ident);
99   if (file == NULL)
100   {
101     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
102     return (-1);
103   }
105   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
107   status = ds_list_from_rrd_file (file, &dses_num, &dses);
108   if (status != 0)
109   {
110     free (file);
111     return (status);
112   }
114   for (i = 0; i < dses_num; i++)
115   {
116     int index;
118     DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
120     index = array_argc (args);
122     /* CDEFs */
123     array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
124         index, file, dses[i]);
125     array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
126         index, file, dses[i]);
127     array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
128         index, file, dses[i]);
129     /* VDEFs */
130     array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
131         index, index);
132     array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
133         index, index);
134     array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
135         index, index);
136     array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
137         index, index);
139     /* Graph part */
140     array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
141         index, get_random_color (), dses[i]);
142     array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
143     array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
144     array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
145     array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
147     free (dses[i]);
148   }
150   free (dses);
151   free (file);
153   return (0);
154 } /* }}} int def_get_rrdargs */
156 /* vim: set sw=2 sts=2 et fdm=marker : */