Fix the filename in the head comment.
[collectd.git] / src / routeros.c
1 /**
2  * collectd - src/routeros.c
3  * Copyright (C) 2009  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 "common.h"
24 #include "plugin.h"
25
26 #include <routeros_api.h>
27
28 struct cr_data_s
29 {
30   ros_connection_t *connection;
31
32   char *node;
33   char *service;
34   char *username;
35   char *password;
36 };
37 typedef struct cr_data_s cr_data_t;
38
39 static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */
40     counter_t rx, counter_t tx)
41 {
42         value_t values[2];
43         value_list_t vl = VALUE_LIST_INIT;
44
45         values[0].counter = rx;
46         values[1].counter = tx;
47
48         vl.values = values;
49         vl.values_len = STATIC_ARRAY_SIZE (values);
50         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
51         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
52         sstrncpy (vl.type, type, sizeof (vl.type));
53         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
54
55         plugin_dispatch_values (&vl);
56 } /* }}} void cr_submit_io */
57
58 static void submit_interface (const ros_interface_t *i) /* {{{ */
59 {
60   if (i == NULL)
61     return;
62
63   if (!i->running)
64   {
65     submit_interface (i->next);
66     return;
67   }
68
69   cr_submit_io ("if_packets", i->name,
70       (counter_t) i->rx_packets, (counter_t) i->tx_packets);
71   cr_submit_io ("if_octets", i->name,
72       (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
73   cr_submit_io ("if_errors", i->name,
74       (counter_t) i->rx_errors, (counter_t) i->tx_errors);
75   cr_submit_io ("if_dropped", i->name,
76       (counter_t) i->rx_drops, (counter_t) i->tx_drops);
77
78   submit_interface (i->next);
79 } /* }}} void submit_interface */
80
81 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
82     const ros_interface_t *i, __attribute__((unused)) void *user_data)
83 {
84   submit_interface (i);
85   return (0);
86 } /* }}} int handle_interface */
87
88 static void cr_submit_gauge (const char *type, /* {{{ */
89     const char *type_instance, gauge_t value)
90 {
91         value_t values[1];
92         value_list_t vl = VALUE_LIST_INIT;
93
94         values[0].gauge = value;
95
96         vl.values = values;
97         vl.values_len = STATIC_ARRAY_SIZE (values);
98         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
99         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
100         sstrncpy (vl.type, type, sizeof (vl.type));
101         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
102
103         plugin_dispatch_values (&vl);
104 } /* }}} void cr_submit_gauge */
105
106 static void submit_regtable (const ros_registration_table_t *r) /* {{{ */
107 {
108   char type_instance[DATA_MAX_NAME_LEN];
109
110   if (r == NULL)
111     return;
112
113   /*** RX ***/
114   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
115       r->interface, r->radio_name);
116   cr_submit_gauge ("bitrate", type_instance,
117       (gauge_t) (1000000.0 * r->rx_rate));
118   cr_submit_gauge ("signal_power", type_instance,
119       (gauge_t) r->rx_signal_strength);
120   cr_submit_gauge ("signal_quality", type_instance,
121       (gauge_t) r->rx_ccq);
122
123   /*** TX ***/
124   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
125       r->interface, r->radio_name);
126   cr_submit_gauge ("bitrate", type_instance,
127       (gauge_t) (1000000.0 * r->tx_rate));
128   cr_submit_gauge ("signal_power", type_instance,
129       (gauge_t) r->tx_signal_strength);
130   cr_submit_gauge ("signal_quality", type_instance,
131       (gauge_t) r->tx_ccq);
132
133   /*** RX / TX ***/
134   ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
135       r->interface, r->radio_name);
136   cr_submit_io ("if_octets", type_instance,
137       (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
138   cr_submit_gauge ("snr", type_instance, (gauge_t) r->signal_to_noise);
139
140   submit_regtable (r->next);
141 } /* }}} void submit_regtable */
142
143 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
144     const ros_registration_table_t *r,
145     __attribute__((unused)) void *user_data)
146 {
147   submit_regtable (r);
148   return (0);
149 } /* }}} int handle_regtable */
150
151 static int cr_read (user_data_t *user_data) /* {{{ */
152 {
153   int status;
154   cr_data_t *rd;
155
156   if (user_data == NULL)
157     return (EINVAL);
158
159   rd = user_data->data;
160   if (rd == NULL)
161     return (EINVAL);
162
163   if (rd->connection == NULL)
164   {
165     rd->connection = ros_connect (rd->node, rd->service,
166         rd->username, rd->password);
167     if (rd->connection == NULL)
168     {
169       char errbuf[128];
170       ERROR ("routeros plugin: ros_connect failed: %s",
171           sstrerror (errno, errbuf, sizeof (errbuf)));
172       return (-1);
173     }
174   }
175   assert (rd->connection != NULL);
176
177   status = ros_interface (rd->connection, handle_interface,
178       /* user data = */ NULL);
179   if (status != 0)
180   {
181     char errbuf[128];
182     ERROR ("routeros plugin: ros_interface failed: %s",
183         sstrerror (status, errbuf, sizeof (errbuf)));
184     ros_disconnect (rd->connection);
185     rd->connection = NULL;
186     return (-1);
187   }
188
189   status = ros_registration_table (rd->connection, handle_regtable,
190       /* user data = */ NULL);
191   if (status != 0)
192   {
193     char errbuf[128];
194     ERROR ("routeros plugin: ros_registration_table failed: %s",
195         sstrerror (status, errbuf, sizeof (errbuf)));
196     ros_disconnect (rd->connection);
197     rd->connection = NULL;
198     return (-1);
199   }
200
201   return (0);
202 } /* }}} int cr_read */
203
204 static void cr_free_data (cr_data_t *ptr) /* {{{ */
205 {
206   if (ptr == NULL)
207     return;
208
209   ros_disconnect (ptr->connection);
210   ptr->connection = NULL;
211
212   sfree (ptr->node);
213   sfree (ptr->service);
214   sfree (ptr->username);
215   sfree (ptr->password);
216
217   sfree (ptr);
218 } /* }}} void cr_free_data */
219
220 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
221 {
222   cr_data_t *router_data;
223   char read_name[128];
224   user_data_t user_data;
225   int status;
226   int i;
227
228   router_data = malloc (sizeof (*router_data));
229   if (router_data == NULL)
230     return (-1);
231   memset (router_data, 0, sizeof (router_data));
232   router_data->connection = NULL;
233   router_data->node = NULL;
234   router_data->service = NULL;
235   router_data->username = NULL;
236   router_data->password = NULL;
237
238   status = 0;
239   for (i = 0; i < ci->children_num; i++)
240   {
241     oconfig_item_t *child = ci->children + i;
242
243     if (strcasecmp ("Host", child->key) == 0)
244       status = cf_util_get_string (child, &router_data->node);
245     else if (strcasecmp ("Port", child->key) == 0)
246       status = cf_util_get_string (child, &router_data->service);
247     else if (strcasecmp ("User", child->key) == 0)
248       status = cf_util_get_string (child, &router_data->username);
249     else if (strcasecmp ("Password", child->key) == 0)
250       status = cf_util_get_string (child, &router_data->service);
251     else
252     {
253       WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
254     }
255
256     if (status != 0)
257       break;
258   }
259
260   if (status == 0)
261   {
262     if (router_data->node == NULL)
263     {
264       ERROR ("routeros plugin: No `Host' option within a `Router' block. "
265           "Where should I connect to?");
266       status = -1;
267     }
268
269     if (router_data->password == NULL)
270     {
271       ERROR ("routeros plugin: No `Password' option within a `Router' block. "
272           "How should I authenticate?");
273       status = -1;
274     }
275   }
276
277   if ((status == 0) && (router_data->username == NULL))
278   {
279     router_data->username = sstrdup ("admin");
280     if (router_data->username == NULL)
281     {
282       ERROR ("routeros plugin: sstrdup failed.");
283       status = -1;
284     }
285   }
286
287   ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
288   user_data.data = router_data;
289   user_data.free_func = (void *) cr_free_data;
290   if (status == 0)
291     status = plugin_register_complex_read (read_name, cr_read,
292         /* interval = */ NULL, &user_data);
293
294   if (status != 0)
295     cr_free_data (router_data);
296
297   return (status);
298 } /* }}} int cr_config_router */
299
300 static int cr_config (oconfig_item_t *ci)
301 {
302   int i;
303
304   for (i = 0; i < ci->children_num; i++)
305   {
306     oconfig_item_t *child = ci->children + i;
307
308     if (strcasecmp ("Router", child->key) == 0)
309       cr_config_router (child);
310     else
311     {
312       WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
313     }
314   }
315
316   return (0);
317 } /* }}} int cr_config */
318
319 void module_register (void)
320 {
321   plugin_register_complex_config ("routeros", cr_config);
322 } /* void module_register */
323
324 /* vim: set sw=2 noet fdm=marker : */