src/dp_rrdtool.c: Implement basic RRDtool data provider.
[collection4.git] / src / dp_rrdtool.c
1 /**
2  * collection4 - data_provider.h
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include "graph_types.h"
25 #include "graph_ident.h"
26 #include "graph_list.h"
27 #include "data_provider.h"
28 #include "filesystem.h"
29 #include "oconfig.h"
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <errno.h>
36
37 struct dp_rrdtool_s
38 {
39   char *data_dir;
40 };
41 typedef struct dp_rrdtool_s dp_rrdtool_t;
42
43 struct dp_get_idents_data_s
44 { /* {{{ */
45   graph_ident_t *ident;
46   dp_get_idents_callback callback;
47   void *user_data;
48 }; /* }}} */
49 typedef struct dp_get_idents_data_s dp_get_idents_data_t;
50
51 static int scan_type_cb (__attribute__((unused)) const char *base_dir,
52     const char *sub_dir, void *ud)
53 { /* {{{ */
54   dp_get_idents_data_t *data = ud;
55   size_t sub_dir_len;
56   char type_copy[1024];
57   char *type_inst;
58
59   sub_dir_len = strlen (sub_dir);
60   if (sub_dir_len < 5)
61     return (0);
62
63   /* Ignore files that don't end in ".rrd". */
64   if (strcasecmp (".rrd", sub_dir + (sub_dir_len - 4)) != 0)
65     return (0);
66
67   strncpy (type_copy, sub_dir, sizeof (type_copy));
68   type_copy[sub_dir_len - 4] = 0;
69
70   type_inst = strchr (type_copy, '-');
71   if (type_inst != NULL)
72   {
73     *type_inst = 0;
74     type_inst++;
75   }
76   else
77   {
78     type_inst = "";
79   }
80
81   ident_set_type (data->ident, type_copy);
82   ident_set_type_instance (data->ident, type_inst);
83
84   return (data->callback (data->ident, data->user_data));
85 } /* }}} int scan_type_cb */
86
87 static int scan_plugin_cb (const char *base_dir,
88     const char *sub_dir, void *ud)
89 { /* {{{ */
90   char plugin_copy[1024];
91   char *plugin_inst;
92
93   dp_get_idents_data_t *data = ud;
94   char abs_dir[PATH_MAX + 1];
95
96   strncpy (plugin_copy, sub_dir, sizeof (plugin_copy));
97   plugin_copy[sizeof (plugin_copy) - 1] = 0;
98
99   plugin_inst = strchr (plugin_copy, '-');
100   if (plugin_inst != NULL)
101   {
102     *plugin_inst = 0;
103     plugin_inst++;
104   }
105   else
106   {
107     plugin_inst = "";
108   }
109
110   ident_set_plugin (data->ident, plugin_copy);
111   ident_set_plugin_instance (data->ident, plugin_inst);
112
113   snprintf (abs_dir, sizeof (abs_dir), "%s/%s", base_dir, sub_dir);
114   abs_dir[sizeof (abs_dir) - 1] = 0;
115
116   return (fs_foreach_file (abs_dir, scan_type_cb, data));
117 } /* }}} int scan_host_cb */
118
119 static int scan_host_cb (const char *base_dir,
120     const char *sub_dir, void *ud)
121 { /* {{{ */
122   dp_get_idents_data_t *data = ud;
123   char abs_dir[PATH_MAX + 1];
124
125   ident_set_host (data->ident, sub_dir);
126
127   snprintf (abs_dir, sizeof (abs_dir), "%s/%s", base_dir, sub_dir);
128   abs_dir[sizeof (abs_dir) - 1] = 0;
129
130   return (fs_foreach_dir (abs_dir, scan_plugin_cb, data));
131 } /* }}} int scan_host_cb */
132
133 static int get_idents (void *priv,
134     dp_get_idents_callback cb, void *ud)
135 { /* {{{ */
136   dp_rrdtool_t *config = priv;
137   dp_get_idents_data_t data;
138   int status;
139
140   data.ident = ident_create ("", "", "", "", "");
141   if (data.ident == NULL)
142     return (ENOMEM);
143   data.callback = cb;
144   data.user_data = ud;
145
146   status = fs_foreach_dir (config->data_dir, scan_host_cb, &data);
147
148   ident_destroy (data.ident);
149   return (status);
150 } /* }}} int get_idents */
151
152 static int get_ident_ds_names (void *priv, graph_ident_t *ident,
153     dp_list_get_ident_ds_names_callback cb, void *ud)
154 { /* {{{ */
155   priv = NULL;
156   ident = NULL;
157   cb = NULL;
158   ud = NULL;
159   return (0);
160 } /* }}} int get_ident_ds_names */
161
162 static int get_ident_data (void *priv,
163     graph_ident_t *ident, const char *ds_name,
164     dp_time_t begin, dp_time_t end,
165     dp_get_ident_data_callback cb, void *ud)
166 { /* {{{ */
167   dp_rrdtool_t *config = priv;
168
169   ident = NULL;
170   ds_name = NULL;
171   begin.tv_sec = 0;
172   end.tv_sec = 0;
173   cb = NULL;
174   ud = NULL;
175
176   config = NULL;
177
178   return (EINVAL);
179 } /* }}} int get_ident_data */
180
181 static int print_graph (void *priv,
182     graph_config_t *cfg, graph_instance_t *inst)
183 { /* {{{ */
184   priv = NULL;
185   cfg = NULL;
186   inst = NULL;
187
188   return (-1);
189 } /* }}} int print_graph */
190
191 int dp_rrdtool_config (oconfig_item_t *ci)
192 { /* {{{ */
193   dp_rrdtool_t *conf;
194
195   data_provider_t dp =
196   {
197     get_idents,
198     get_ident_ds_names,
199     get_ident_data,
200     print_graph,
201     /* private_data = */ NULL
202   };
203
204   /* FIXME: Actuelly do config parsing here. */
205   ci = NULL; /* FIXME */
206   conf = malloc (sizeof (dp_rrdtool_t));
207   conf->data_dir = strdup ("/var/lib/collectd/rrd");
208
209   dp.private_data = conf;
210
211   gl_register_data_provider ("rrdtool", &dp);
212
213   return (0);
214 } /* }}} int dp_rrdtool_config */
215
216 /* vim: set sw=2 sts=2 et fdm=marker : */