853508edcf875ef6f38e224510cadcba5657f15d
[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   graph_def_t *next;
16 };
17
18 /*
19  * Private functions
20  */
21
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;
29
30   selector = gl_graph_get_selector (cfg);
31   if (selector == NULL)
32     return (NULL);
33
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;
42
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   }
51
52   ident_destroy (selector);
53   return (ret);
54 }; /* }}} graph_def_t *def_create */
55
56 void def_destroy (graph_def_t *def) /* {{{ */
57 {
58   graph_def_t *next;
59
60   if (def == NULL)
61     return;
62
63   next = def->next;
64
65   ident_destroy (def->select);
66
67   free (def);
68
69   def_destroy (next);
70 } /* }}} void def_destroy */
71
72 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
73 {
74   graph_def_t *ptr;
75
76   if ((head == NULL) || (def == NULL))
77     return (EINVAL);
78
79   ptr = head;
80   while (ptr->next != NULL)
81     ptr = ptr->next;
82
83   ptr->next = def;
84
85   return (0);
86 } /* }}} int def_append */
87
88 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident) /* {{{ */
89 {
90   graph_def_t *ptr;
91
92   if ((head == NULL) || (ident == NULL))
93     return (NULL);
94
95   for (ptr = head; ptr != NULL; ptr = ptr->next)
96     if (ident_matches (ptr->select, ident))
97       return (ptr);
98
99   return (NULL);
100 } /* }}} graph_def_t *def_search */
101
102 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
103     str_array_t *args)
104 {
105   char *file;
106   char **dses = NULL;
107   size_t dses_num = 0;
108   int status;
109   size_t i;
110
111   if ((def == NULL) || (ident == NULL) || (args == NULL))
112     return (EINVAL);
113
114   file = ident_to_file (ident);
115   if (file == NULL)
116   {
117     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
118     return (-1);
119   }
120
121   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
122
123   status = ds_list_from_rrd_file (file, &dses_num, &dses);
124   if (status != 0)
125   {
126     free (file);
127     return (status);
128   }
129
130   for (i = 0; i < dses_num; i++)
131   {
132     int index;
133
134     DEBUG ("gl_ident_get_rrdargs: ds[%lu] = %s;\n", (unsigned long) i, dses[i]);
135
136     index = array_argc (args);
137
138     /* CDEFs */
139     array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
140         index, file, dses[i]);
141     array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
142         index, file, dses[i]);
143     array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
144         index, file, dses[i]);
145     /* VDEFs */
146     array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
147         index, index);
148     array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
149         index, index);
150     array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
151         index, index);
152     array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
153         index, index);
154
155     /* Graph part */
156     array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
157         index, get_random_color (), dses[i]);
158     array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
159     array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
160     array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
161     array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
162
163     free (dses[i]);
164   }
165
166   free (dses);
167   free (file);
168
169   return (0);
170 } /* }}} int def_get_rrdargs */
171
172 /* vim: set sw=2 sts=2 et fdm=marker : */