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 uint32_t color;
17 graph_def_t *next;
18 };
20 /*
21 * Private functions
22 */
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;
32 selector = gl_graph_get_selector (cfg);
33 if (selector == NULL)
34 return (NULL);
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;
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 }
55 ident_destroy (selector);
56 return (ret);
57 }; /* }}} graph_def_t *def_create */
59 void def_destroy (graph_def_t *def) /* {{{ */
60 {
61 graph_def_t *next;
63 if (def == NULL)
64 return;
66 next = def->next;
68 ident_destroy (def->select);
70 free (def);
72 def_destroy (next);
73 } /* }}} void def_destroy */
75 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
76 {
77 graph_def_t *ptr;
79 if ((head == NULL) || (def == NULL))
80 return (EINVAL);
82 ptr = head;
83 while (ptr->next != NULL)
84 ptr = ptr->next;
86 ptr->next = def;
88 return (0);
89 } /* }}} int def_append */
91 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident) /* {{{ */
92 {
93 graph_def_t *ptr;
95 if ((head == NULL) || (ident == NULL))
96 return (NULL);
98 for (ptr = head; ptr != NULL; ptr = ptr->next)
99 if (ident_matches (ptr->select, ident))
100 return (ptr);
102 return (NULL);
103 } /* }}} graph_def_t *def_search */
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;
114 if ((def == NULL) || (ident == NULL) || (args == NULL))
115 return (EINVAL);
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 }
124 DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
126 status = ds_list_from_rrd_file (file, &dses_num, &dses);
127 if (status != 0)
128 {
129 free (file);
130 return (status);
131 }
133 for (i = 0; i < dses_num; i++)
134 {
135 int index;
137 DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
139 index = array_argc (args);
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);
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);
166 free (dses[i]);
167 }
169 free (dses);
170 free (file);
172 return (0);
173 } /* }}} int def_get_rrdargs */
175 /* vim: set sw=2 sts=2 et fdm=marker : */