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 (cr_data_t *rd, const char *type, /* {{{ */
43 const char *type_instance, 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, rd->node, 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 (cr_data_t *rd, /* {{{ */
62 const ros_interface_t *i)
63 {
64 if (i == NULL)
65 return;
67 if (!i->running)
68 {
69 submit_interface (rd, i->next);
70 return;
71 }
73 cr_submit_io (rd, "if_packets", i->name,
74 (counter_t) i->rx_packets, (counter_t) i->tx_packets);
75 cr_submit_io (rd, "if_octets", i->name,
76 (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
77 cr_submit_io (rd, "if_errors", i->name,
78 (counter_t) i->rx_errors, (counter_t) i->tx_errors);
79 cr_submit_io (rd, "if_dropped", i->name,
80 (counter_t) i->rx_drops, (counter_t) i->tx_drops);
82 submit_interface (rd, i->next);
83 } /* }}} void submit_interface */
85 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
86 const ros_interface_t *i, void *user_data)
87 {
88 if ((i == NULL) || (user_data == NULL))
89 return (EINVAL);
91 submit_interface (user_data, i);
92 return (0);
93 } /* }}} int handle_interface */
95 static void cr_submit_gauge (cr_data_t *rd, const char *type, /* {{{ */
96 const char *type_instance, gauge_t value)
97 {
98 value_t values[1];
99 value_list_t vl = VALUE_LIST_INIT;
101 values[0].gauge = value;
103 vl.values = values;
104 vl.values_len = STATIC_ARRAY_SIZE (values);
105 sstrncpy (vl.host, rd->node, sizeof (vl.host));
106 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
107 sstrncpy (vl.type, type, sizeof (vl.type));
108 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
110 plugin_dispatch_values (&vl);
111 } /* }}} void cr_submit_gauge */
113 static void submit_regtable (cr_data_t *rd, /* {{{ */
114 const ros_registration_table_t *r)
115 {
116 char type_instance[DATA_MAX_NAME_LEN];
118 if (r == NULL)
119 return;
121 /*** RX ***/
122 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
123 r->interface, r->radio_name);
124 cr_submit_gauge (rd, "bitrate", type_instance,
125 (gauge_t) (1000000.0 * r->rx_rate));
126 cr_submit_gauge (rd, "signal_power", type_instance,
127 (gauge_t) r->rx_signal_strength);
128 cr_submit_gauge (rd, "signal_quality", type_instance,
129 (gauge_t) r->rx_ccq);
131 /*** TX ***/
132 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
133 r->interface, r->radio_name);
134 cr_submit_gauge (rd, "bitrate", type_instance,
135 (gauge_t) (1000000.0 * r->tx_rate));
136 cr_submit_gauge (rd, "signal_power", type_instance,
137 (gauge_t) r->tx_signal_strength);
138 cr_submit_gauge (rd, "signal_quality", type_instance,
139 (gauge_t) r->tx_ccq);
141 /*** RX / TX ***/
142 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
143 r->interface, r->radio_name);
144 cr_submit_io (rd, "if_octets", type_instance,
145 (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
146 cr_submit_gauge (rd, "snr", type_instance, (gauge_t) r->signal_to_noise);
148 submit_regtable (rd, r->next);
149 } /* }}} void submit_regtable */
151 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
152 const ros_registration_table_t *r, void *user_data)
153 {
154 if ((r == NULL) || (user_data == NULL))
155 return (EINVAL);
157 submit_regtable (user_data, r);
158 return (0);
159 } /* }}} int handle_regtable */
161 static int cr_read (user_data_t *user_data) /* {{{ */
162 {
163 int status;
164 cr_data_t *rd;
166 if (user_data == NULL)
167 return (EINVAL);
169 rd = user_data->data;
170 if (rd == NULL)
171 return (EINVAL);
173 if (rd->connection == NULL)
174 {
175 rd->connection = ros_connect (rd->node, rd->service,
176 rd->username, rd->password);
177 if (rd->connection == NULL)
178 {
179 char errbuf[128];
180 ERROR ("routeros plugin: ros_connect failed: %s",
181 sstrerror (errno, errbuf, sizeof (errbuf)));
182 return (-1);
183 }
184 }
185 assert (rd->connection != NULL);
187 if (rd->collect_interface)
188 {
189 status = ros_interface (rd->connection, handle_interface,
190 /* user data = */ rd);
191 if (status != 0)
192 {
193 char errbuf[128];
194 ERROR ("routeros plugin: ros_interface failed: %s",
195 sstrerror (status, errbuf, sizeof (errbuf)));
196 ros_disconnect (rd->connection);
197 rd->connection = NULL;
198 return (-1);
199 }
200 }
202 if (rd->collect_regtable)
203 {
204 status = ros_registration_table (rd->connection, handle_regtable,
205 /* user data = */ rd);
206 if (status != 0)
207 {
208 char errbuf[128];
209 ERROR ("routeros plugin: ros_registration_table failed: %s",
210 sstrerror (status, errbuf, sizeof (errbuf)));
211 ros_disconnect (rd->connection);
212 rd->connection = NULL;
213 return (-1);
214 }
215 }
217 return (0);
218 } /* }}} int cr_read */
220 static void cr_free_data (cr_data_t *ptr) /* {{{ */
221 {
222 if (ptr == NULL)
223 return;
225 ros_disconnect (ptr->connection);
226 ptr->connection = NULL;
228 sfree (ptr->node);
229 sfree (ptr->service);
230 sfree (ptr->username);
231 sfree (ptr->password);
233 sfree (ptr);
234 } /* }}} void cr_free_data */
236 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
237 {
238 cr_data_t *router_data;
239 char read_name[128];
240 user_data_t user_data;
241 int status;
242 int i;
244 router_data = malloc (sizeof (*router_data));
245 if (router_data == NULL)
246 return (-1);
247 memset (router_data, 0, sizeof (router_data));
248 router_data->connection = NULL;
249 router_data->node = NULL;
250 router_data->service = NULL;
251 router_data->username = NULL;
252 router_data->password = NULL;
253 router_data->collect_interface = false;
254 router_data->collect_regtable = false;
256 status = 0;
257 for (i = 0; i < ci->children_num; i++)
258 {
259 oconfig_item_t *child = ci->children + i;
261 if (strcasecmp ("Host", child->key) == 0)
262 status = cf_util_get_string (child, &router_data->node);
263 else if (strcasecmp ("Port", child->key) == 0)
264 status = cf_util_get_string (child, &router_data->service);
265 else if (strcasecmp ("User", child->key) == 0)
266 status = cf_util_get_string (child, &router_data->username);
267 else if (strcasecmp ("Password", child->key) == 0)
268 status = cf_util_get_string (child, &router_data->password);
269 else if (strcasecmp ("CollectInterface", child->key) == 0)
270 cf_util_get_boolean (child, &router_data->collect_interface);
271 else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
272 cf_util_get_boolean (child, &router_data->collect_regtable);
273 else
274 {
275 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
276 }
278 if (status != 0)
279 break;
280 }
282 if (status == 0)
283 {
284 if (router_data->node == NULL)
285 {
286 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
287 "Where should I connect to?");
288 status = -1;
289 }
291 if (router_data->password == NULL)
292 {
293 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
294 "How should I authenticate?");
295 status = -1;
296 }
298 if (!router_data->collect_interface
299 && !router_data->collect_regtable)
300 {
301 ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
302 "What statistics should I collect?");
303 status = -1;
304 }
305 }
307 if ((status == 0) && (router_data->username == NULL))
308 {
309 router_data->username = sstrdup ("admin");
310 if (router_data->username == NULL)
311 {
312 ERROR ("routeros plugin: sstrdup failed.");
313 status = -1;
314 }
315 }
317 ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
318 user_data.data = router_data;
319 user_data.free_func = (void *) cr_free_data;
320 if (status == 0)
321 status = plugin_register_complex_read (read_name, cr_read,
322 /* interval = */ NULL, &user_data);
324 if (status != 0)
325 cr_free_data (router_data);
327 return (status);
328 } /* }}} int cr_config_router */
330 static int cr_config (oconfig_item_t *ci)
331 {
332 int i;
334 for (i = 0; i < ci->children_num; i++)
335 {
336 oconfig_item_t *child = ci->children + i;
338 if (strcasecmp ("Router", child->key) == 0)
339 cr_config_router (child);
340 else
341 {
342 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
343 }
344 }
346 return (0);
347 } /* }}} int cr_config */
349 void module_register (void)
350 {
351 plugin_register_complex_config ("routeros", cr_config);
352 } /* void module_register */
354 /* vim: set sw=2 noet fdm=marker : */