2 * collectd - src/routeros.c
3 * Copyright (C) 2009 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #include <routeros_api.h>
30 ros_connection_t *connection;
37 _Bool collect_interface;
38 _Bool collect_regtable;
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)
46 value_list_t vl = VALUE_LIST_INIT;
48 values[0].counter = rx;
49 values[1].counter = tx;
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) /* {{{ */
68 submit_interface (i->next);
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)
89 } /* }}} int handle_interface */
91 static void cr_submit_gauge (const char *type, /* {{{ */
92 const char *type_instance, gauge_t value)
95 value_list_t vl = VALUE_LIST_INIT;
97 values[0].gauge = value;
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) /* {{{ */
111 char type_instance[DATA_MAX_NAME_LEN];
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);
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);
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)
152 } /* }}} int handle_regtable */
154 static int cr_read (user_data_t *user_data) /* {{{ */
159 if (user_data == NULL)
162 rd = user_data->data;
166 if (rd->connection == NULL)
168 rd->connection = ros_connect (rd->node, rd->service,
169 rd->username, rd->password);
170 if (rd->connection == NULL)
173 ERROR ("routeros plugin: ros_connect failed: %s",
174 sstrerror (errno, errbuf, sizeof (errbuf)));
178 assert (rd->connection != NULL);
180 if (rd->collect_interface)
182 status = ros_interface (rd->connection, handle_interface,
183 /* user data = */ NULL);
187 ERROR ("routeros plugin: ros_interface failed: %s",
188 sstrerror (status, errbuf, sizeof (errbuf)));
189 ros_disconnect (rd->connection);
190 rd->connection = NULL;
195 if (rd->collect_regtable)
197 status = ros_registration_table (rd->connection, handle_regtable,
198 /* user data = */ NULL);
202 ERROR ("routeros plugin: ros_registration_table failed: %s",
203 sstrerror (status, errbuf, sizeof (errbuf)));
204 ros_disconnect (rd->connection);
205 rd->connection = NULL;
211 } /* }}} int cr_read */
213 static void cr_free_data (cr_data_t *ptr) /* {{{ */
218 ros_disconnect (ptr->connection);
219 ptr->connection = NULL;
222 sfree (ptr->service);
223 sfree (ptr->username);
224 sfree (ptr->password);
227 } /* }}} void cr_free_data */
229 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
231 cr_data_t *router_data;
233 user_data_t user_data;
237 router_data = malloc (sizeof (*router_data));
238 if (router_data == NULL)
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;
250 for (i = 0; i < ci->children_num; i++)
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);
268 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
277 if (router_data->node == NULL)
279 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
280 "Where should I connect to?");
284 if (router_data->password == NULL)
286 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
287 "How should I authenticate?");
291 if (!router_data->collect_interface
292 && !router_data->collect_regtable)
294 ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
295 "What statistics should I collect?");
300 if ((status == 0) && (router_data->username == NULL))
302 router_data->username = sstrdup ("admin");
303 if (router_data->username == NULL)
305 ERROR ("routeros plugin: sstrdup failed.");
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;
314 status = plugin_register_complex_read (read_name, cr_read,
315 /* interval = */ NULL, &user_data);
318 cr_free_data (router_data);
321 } /* }}} int cr_config_router */
323 static int cr_config (oconfig_item_t *ci)
327 for (i = 0; i < ci->children_num; i++)
329 oconfig_item_t *child = ci->children + i;
331 if (strcasecmp ("Router", child->key) == 0)
332 cr_config_router (child);
335 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
340 } /* }}} int cr_config */
342 void module_register (void)
344 plugin_register_complex_config ("routeros", cr_config);
345 } /* void module_register */
347 /* vim: set sw=2 noet fdm=marker : */