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