rrdcached plugin: Use ".rrd" as suffix, not the current date.
[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   strncpy (buffer + offset, ".rrd", buffer_len - offset);
127   buffer[buffer_len - 1] = 0;
128
129   return (0);
130 } /* int value_list_to_filename */
131
132 static int rc_config (const char *key, const char *value)
133 {
134   if (strcasecmp ("DataDir", key) == 0)
135   {
136     if (datadir != NULL)
137       free (datadir);
138     datadir = strdup (value);
139     if (datadir != NULL)
140     {
141       int len = strlen (datadir);
142       while ((len > 0) && (datadir[len - 1] == '/'))
143       {
144         len--;
145         datadir[len] = '\0';
146       }
147       if (len <= 0)
148       {
149         free (datadir);
150         datadir = NULL;
151       }
152     }
153   }
154   else if (strcasecmp ("DaemonAddress", key) == 0)
155   {
156     sfree (daemon_address);
157     daemon_address = strdup (value);
158     if (daemon_address == NULL)
159     {
160       ERROR ("rrdcached plugin: strdup failed.");
161       return (1);
162     }
163   }
164   else
165   {
166     return (-1);
167   }
168   return (0);
169 } /* int rc_config */
170
171 static int rc_write (const data_set_t *ds, const value_list_t *vl)
172 {
173   char filename[512];
174   char values[512];
175   char *values_array[2];
176   int status;
177
178   if (daemon_address == NULL)
179   {
180     ERROR ("rrdcached plugin: daemon_address == NULL.");
181     plugin_unregister_write ("rrdcached");
182     return (-1);
183   }
184
185   if (strcmp (ds->type, vl->type) != 0)
186   {
187     ERROR ("rrdcached plugin: DS type does not match value list type");
188     return (-1);
189   }
190
191   if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
192   {
193     ERROR ("rrdcached plugin: value_list_to_filename failed.");
194     return (-1);
195   }
196
197   if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
198   {
199     ERROR ("rrdcached plugin: value_list_to_string failed.");
200     return (-1);
201   }
202
203   values_array[0] = values;
204   values_array[1] = NULL;
205
206   /* TODO: Check if the file exists. */
207
208   status = rrdc_connect (daemon_address);
209   if (status != 0)
210   {
211     ERROR ("rrdcached plugin: rrdc_connect (%s) failed with status %i.",
212         daemon_address, status);
213     return (-1);
214   }
215
216   status = rrdc_update (filename, /* values_num = */ 1, (void *) values_array);
217   if (status != 0)
218   {
219     ERROR ("rrdcached plugin: rrdc_update (%s, [%s], 1) failed with "
220         "status %i.",
221         filename, values_array[0], status);
222     return (-1);
223   }
224
225   return (0);
226 } /* int rc_write */
227
228 static int rc_shutdown (void)
229 {
230   rrdc_disconnect ();
231   return (0);
232 } /* int rc_shutdown */
233
234 void module_register (void)
235 {
236   plugin_register_config ("rrdcached", rc_config,
237       config_keys, config_keys_num);
238   plugin_register_write ("rrdcached", rc_write);
239   plugin_register_shutdown ("rrdcached", rc_shutdown);
240 } /* void module_register */
241
242 /*
243  * vim: set sw=2 sts=2 et :
244  */