rrdcached plugin: Added a new plugin for the yet unreleased `rrdcached'.
[collectd.git] / src / rrdcached.c
1 /**
2  * collectd - src/rrdcached.c
3  * Copyright (C) 2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25
26 #include <rrd_client.h>
27
28 /*
29  * Private variables
30  */
31 static const char *config_keys[] =
32 {
33   "DaemonAddress",
34   "DataDir"
35 };
36 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
37
38 static char *datadir = NULL;
39 static char *daemon_address = NULL;
40
41 static int value_list_to_string (char *buffer, int buffer_len,
42     const data_set_t *ds, const value_list_t *vl)
43 {
44   int offset;
45   int status;
46   int i;
47
48   assert (0 == strcmp (ds->type, vl->type));
49
50   memset (buffer, '\0', buffer_len);
51
52   status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
53   if ((status < 1) || (status >= buffer_len))
54     return (-1);
55   offset = status;
56
57   for (i = 0; i < ds->ds_num; i++)
58   {
59     if ((ds->ds[i].type != DS_TYPE_COUNTER)
60         && (ds->ds[i].type != DS_TYPE_GAUGE))
61       return (-1);
62
63     if (ds->ds[i].type == DS_TYPE_COUNTER)
64     {
65       status = ssnprintf (buffer + offset, buffer_len - offset,
66           ",%llu", vl->values[i].counter);
67     }
68     else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
69     {
70       status = ssnprintf (buffer + offset, buffer_len - offset,
71           ",%lf", vl->values[i].gauge);
72     }
73
74     if ((status < 1) || (status >= (buffer_len - offset)))
75       return (-1);
76
77     offset += status;
78   } /* for ds->ds_num */
79
80   return (0);
81 } /* int value_list_to_string */
82
83 static int value_list_to_filename (char *buffer, int buffer_len,
84     const data_set_t *ds, const value_list_t *vl)
85 {
86   int offset = 0;
87   int status;
88
89   assert (0 == strcmp (ds->type, vl->type));
90
91   if (datadir != NULL)
92   {
93     status = ssnprintf (buffer + offset, buffer_len - offset,
94         "%s/", datadir);
95     if ((status < 1) || (status >= buffer_len - offset))
96       return (-1);
97     offset += status;
98   }
99
100   status = ssnprintf (buffer + offset, buffer_len - offset,
101       "%s/", vl->host);
102   if ((status < 1) || (status >= buffer_len - offset))
103     return (-1);
104   offset += status;
105
106   if (strlen (vl->plugin_instance) > 0)
107     status = ssnprintf (buffer + offset, buffer_len - offset,
108         "%s-%s/", vl->plugin, vl->plugin_instance);
109   else
110     status = ssnprintf (buffer + offset, buffer_len - offset,
111         "%s/", vl->plugin);
112   if ((status < 1) || (status >= buffer_len - offset))
113     return (-1);
114   offset += status;
115
116   if (strlen (vl->type_instance) > 0)
117     status = ssnprintf (buffer + offset, buffer_len - offset,
118         "%s-%s", vl->type, vl->type_instance);
119   else
120     status = ssnprintf (buffer + offset, buffer_len - offset,
121         "%s", vl->type);
122   if ((status < 1) || (status >= buffer_len - offset))
123     return (-1);
124   offset += status;
125
126   {
127     time_t now;
128     struct tm stm;
129
130     /* TODO: Find a way to minimize the calls to `localtime_r',
131      * since they are pretty expensive.. */
132     now = time (NULL);
133     if (localtime_r (&now, &stm) == NULL)
134     {
135       ERROR ("rrdcached plugin: localtime_r failed");
136       return (1);
137     }
138
139     strftime (buffer + offset, buffer_len - offset,
140         "-%Y-%m-%d", &stm);
141   }
142
143   return (0);
144 } /* int value_list_to_filename */
145
146 static int rc_config (const char *key, const char *value)
147 {
148   if (strcasecmp ("DataDir", key) == 0)
149   {
150     if (datadir != NULL)
151       free (datadir);
152     datadir = strdup (value);
153     if (datadir != NULL)
154     {
155       int len = strlen (datadir);
156       while ((len > 0) && (datadir[len - 1] == '/'))
157       {
158         len--;
159         datadir[len] = '\0';
160       }
161       if (len <= 0)
162       {
163         free (datadir);
164         datadir = NULL;
165       }
166     }
167   }
168   else if (strcasecmp ("DaemonAddress", key) == 0)
169   {
170     sfree (daemon_address);
171     daemon_address = strdup (value);
172     if (daemon_address == NULL)
173     {
174       ERROR ("rrdcached plugin: strdup failed.");
175       return (1);
176     }
177   }
178   else
179   {
180     return (-1);
181   }
182   return (0);
183 } /* int rc_config */
184
185 static int rc_write (const data_set_t *ds, const value_list_t *vl)
186 {
187   char filename[512];
188   char values[512];
189   char *values_array[2];
190   int status;
191
192   if (daemon_address == NULL)
193   {
194     ERROR ("rrdcached plugin: daemon_address == NULL.");
195     plugin_unregister_write ("rrdcached");
196     return (-1);
197   }
198
199   if (strcmp (ds->type, vl->type) != 0)
200   {
201     ERROR ("rrdcached plugin: DS type does not match value list type");
202     return (-1);
203   }
204
205   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
206   {
207     ERROR ("rrdcached plugin: value_list_to_filename failed.");
208     return (-1);
209   }
210
211   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
212   {
213     ERROR ("rrdcached plugin: value_list_to_string failed.");
214     return (-1);
215   }
216
217   values_array[0] = values;
218   values_array[1] = NULL;
219
220   /* TODO: Check if the file exists. */
221
222   status = rrdc_connect (daemon_address);
223   if (status != 0)
224   {
225     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
226         daemon_address, status);
227     return (-1);
228   }
229
230   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
231   if (status != 0)
232   {
233     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
234         "status %i.",
235         filename, values_array[0], status);
236     return (-1);
237   }
238
239   return (0);
240 } /* int rc_write */
241
242 static int rc_shutdown (void)
243 {
244   rrdc_disconnect ();
245   return (0);
246 } /* int rc_shutdown */
247
248 void module_register (void)
249 {
250   plugin_register_config ("rrdcached", rc_config,
251       config_keys, config_keys_num);
252   plugin_register_write ("rrdcached", rc_write);
253   plugin_register_shutdown ("rrdcached", rc_shutdown);
254 } /* void module_register */
255
256 /*
257  * vim: set sw=2 sts=2 et :
258  */