graph_list.c: Allocate graph_def_ts on the fly.
[collection4.git] / graph_def.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4
5 #include "graph_def.h"
6 #include "common.h"
7
8 /*
9  * Data structures
10  */
11 struct graph_def_s
12 {
13   graph_ident_t *select;
14
15   uint32_t color;
16
17   graph_def_t *next;
18 };
19
20 /*
21  * Private functions
22  */
23
24 /*
25  * Public functions
26  */
27 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident) /* {{{ */
28 {
29   graph_ident_t *selector;
30   graph_def_t *ret;
31
32   selector = gl_graph_get_selector (cfg);
33   if (selector == NULL)
34     return (NULL);
35
36   ret = malloc (sizeof (*ret));
37   if (ret == NULL)
38   {
39     ident_destroy (selector);
40     return (NULL);
41   }
42   memset (ret, 0, sizeof (*ret));
43   ret->color = get_random_color ();
44   ret->next = NULL;
45
46   ret->select = ident_copy_with_selector (selector, ident,
47       IDENT_FLAG_REPLACE_ALL);
48   if (ret->select == NULL)
49   {
50     ident_destroy (selector);
51     free (ret);
52     return (NULL);
53   }
54
55   ident_destroy (selector);
56   return (ret);
57 }; /* }}} graph_def_t *def_create */
58
59 void def_destroy (graph_def_t *def) /* {{{ */
60 {
61   graph_def_t *next;
62
63   if (def == NULL)
64     return;
65
66   next = def->next;
67
68   ident_destroy (def->select);
69
70   free (def);
71
72   def_destroy (next);
73 } /* }}} void def_destroy */
74
75 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
76 {
77   graph_def_t *ptr;
78
79   if ((head == NULL) || (def == NULL))
80     return (EINVAL);
81
82   ptr = head;
83   while (ptr->next != NULL)
84     ptr = ptr->next;
85
86   ptr->next = def;
87
88   return (0);
89 } /* }}} int def_append */
90
91 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident) /* {{{ */
92 {
93   graph_def_t *ptr;
94
95   if ((head == NULL) || (ident == NULL))
96     return (NULL);
97
98   for (ptr = head; ptr != NULL; ptr = ptr->next)
99     if (ident_matches (ptr->select, ident))
100       return (ptr);
101
102   return (NULL);
103 } /* }}} graph_def_t *def_search */
104
105 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
106     str_array_t *args)
107 {
108   char *file;
109   char **dses = NULL;
110   size_t dses_num = 0;
111   int status;
112   size_t i;
113
114   if ((def == NULL) || (ident == NULL) || (args == NULL))
115     return (EINVAL);
116
117   file = ident_to_file (ident);
118   if (file == NULL)
119   {
120     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
121     return (-1);
122   }
123
124   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
125
126   status = ds_list_from_rrd_file (file, &dses_num, &dses);
127   if (status != 0)
128   {
129     free (file);
130     return (status);
131   }
132
133   for (i = 0; i < dses_num; i++)
134   {
135     int index;
136
137     DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
138
139     index = array_argc (args);
140
141     /* CDEFs */
142     array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
143         index, file, dses[i]);
144     array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
145         index, file, dses[i]);
146     array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
147         index, file, dses[i]);
148     /* VDEFs */
149     array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
150         index, index);
151     array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
152         index, index);
153     array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
154         index, index);
155     array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
156         index, index);
157
158     /* Graph part */
159     array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
160         index, def->color, dses[i]);
161     array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
162     array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
163     array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
164     array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
165
166     free (dses[i]);
167   }
168
169   free (dses);
170   free (file);
171
172   return (0);
173 } /* }}} int def_get_rrdargs */
174
175 /* vim: set sw=2 sts=2 et fdm=marker : */