2 * collectd - src/routeros.c
3 * Copyright (C) 2009,2010 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 collectd.org>
26 #include <routeros_api.h>
30 ros_connection_t *connection;
37 _Bool collect_interface;
38 _Bool collect_regtable;
39 _Bool collect_cpu_load;
44 typedef struct cr_data_s cr_data_t;
46 static void cr_submit_io (cr_data_t *rd, const char *type, /* {{{ */
47 const char *type_instance, derive_t rx, derive_t tx)
50 value_list_t vl = VALUE_LIST_INIT;
52 values[0].derive = rx;
53 values[1].derive = tx;
56 vl.values_len = STATIC_ARRAY_SIZE (values);
57 sstrncpy (vl.host, rd->node, sizeof (vl.host));
58 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
59 sstrncpy (vl.type, type, sizeof (vl.type));
60 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
62 plugin_dispatch_values (&vl);
63 } /* }}} void cr_submit_io */
65 static void submit_interface (cr_data_t *rd, /* {{{ */
66 const ros_interface_t *i)
73 submit_interface (rd, i->next);
77 cr_submit_io (rd, "if_packets", i->name,
78 (derive_t) i->rx_packets, (derive_t) i->tx_packets);
79 cr_submit_io (rd, "if_octets", i->name,
80 (derive_t) i->rx_bytes, (derive_t) i->tx_bytes);
81 cr_submit_io (rd, "if_errors", i->name,
82 (derive_t) i->rx_errors, (derive_t) i->tx_errors);
83 cr_submit_io (rd, "if_dropped", i->name,
84 (derive_t) i->rx_drops, (derive_t) i->tx_drops);
86 submit_interface (rd, i->next);
87 } /* }}} void submit_interface */
89 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
90 const ros_interface_t *i, void *user_data)
92 if ((i == NULL) || (user_data == NULL))
95 submit_interface (user_data, i);
97 } /* }}} int handle_interface */
99 static void cr_submit_gauge (cr_data_t *rd, const char *type, /* {{{ */
100 const char *type_instance, gauge_t value)
103 value_list_t vl = VALUE_LIST_INIT;
105 values[0].gauge = value;
108 vl.values_len = STATIC_ARRAY_SIZE (values);
109 sstrncpy (vl.host, rd->node, sizeof (vl.host));
110 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
111 sstrncpy (vl.type, type, sizeof (vl.type));
112 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
114 plugin_dispatch_values (&vl);
115 } /* }}} void cr_submit_gauge */
117 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
118 static void cr_submit_counter (cr_data_t *rd, const char *type, /* {{{ */
119 const char *type_instance, derive_t value)
122 value_list_t vl = VALUE_LIST_INIT;
124 values[0].derive = value;
127 vl.values_len = STATIC_ARRAY_SIZE (values);
128 sstrncpy (vl.host, rd->node, sizeof (vl.host));
129 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
130 sstrncpy (vl.type, type, sizeof (vl.type));
131 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
133 plugin_dispatch_values (&vl);
134 } /* }}} void cr_submit_gauge */
137 static void submit_regtable (cr_data_t *rd, /* {{{ */
138 const ros_registration_table_t *r)
140 char type_instance[DATA_MAX_NAME_LEN];
146 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
147 r->interface, r->radio_name);
148 cr_submit_gauge (rd, "bitrate", type_instance,
149 (gauge_t) (1000000.0 * r->rx_rate));
150 cr_submit_gauge (rd, "signal_power", type_instance,
151 (gauge_t) r->rx_signal_strength);
152 cr_submit_gauge (rd, "signal_quality", type_instance,
153 (gauge_t) r->rx_ccq);
156 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
157 r->interface, r->radio_name);
158 cr_submit_gauge (rd, "bitrate", type_instance,
159 (gauge_t) (1000000.0 * r->tx_rate));
160 cr_submit_gauge (rd, "signal_power", type_instance,
161 (gauge_t) r->tx_signal_strength);
162 cr_submit_gauge (rd, "signal_quality", type_instance,
163 (gauge_t) r->tx_ccq);
166 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
167 r->interface, r->radio_name);
168 cr_submit_io (rd, "if_octets", type_instance,
169 (derive_t) r->rx_bytes, (derive_t) r->tx_bytes);
170 cr_submit_gauge (rd, "snr", type_instance, (gauge_t) r->signal_to_noise);
172 submit_regtable (rd, r->next);
173 } /* }}} void submit_regtable */
175 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
176 const ros_registration_table_t *r, void *user_data)
178 if ((r == NULL) || (user_data == NULL))
181 submit_regtable (user_data, r);
183 } /* }}} int handle_regtable */
185 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
186 static int handle_system_resource (__attribute__((unused)) ros_connection_t *c, /* {{{ */
187 const ros_system_resource_t *r,
188 __attribute__((unused)) void *user_data)
192 if ((r == NULL) || (user_data == NULL))
196 if (rd->collect_cpu_load)
197 cr_submit_gauge (rd, "gauge", "cpu_load", (gauge_t) r->cpu_load);
199 if (rd->collect_memory)
201 cr_submit_gauge (rd, "memory", "used",
202 (gauge_t) (r->total_memory - r->free_memory));
203 cr_submit_gauge (rd, "memory", "free", (gauge_t) r->free_memory);
208 cr_submit_gauge (rd, "df_complex", "used",
209 (gauge_t) (r->total_memory - r->free_memory));
210 cr_submit_gauge (rd, "df_complex", "free", (gauge_t) r->free_memory);
213 if (rd->collect_disk)
215 cr_submit_counter (rd, "counter", "secors_written", (derive_t) r->write_sect_total);
216 cr_submit_gauge (rd, "gauge", "bad_blocks", (gauge_t) r->bad_blocks);
220 } /* }}} int handle_system_resource */
223 static int cr_read (user_data_t *user_data) /* {{{ */
228 if (user_data == NULL)
231 rd = user_data->data;
235 if (rd->connection == NULL)
237 rd->connection = ros_connect (rd->node, rd->service,
238 rd->username, rd->password);
239 if (rd->connection == NULL)
242 ERROR ("routeros plugin: ros_connect failed: %s",
243 sstrerror (errno, errbuf, sizeof (errbuf)));
247 assert (rd->connection != NULL);
249 if (rd->collect_interface)
251 status = ros_interface (rd->connection, handle_interface,
252 /* user data = */ rd);
256 ERROR ("routeros plugin: ros_interface failed: %s",
257 sstrerror (status, errbuf, sizeof (errbuf)));
258 ros_disconnect (rd->connection);
259 rd->connection = NULL;
264 if (rd->collect_regtable)
266 status = ros_registration_table (rd->connection, handle_regtable,
267 /* user data = */ rd);
271 ERROR ("routeros plugin: ros_registration_table failed: %s",
272 sstrerror (status, errbuf, sizeof (errbuf)));
273 ros_disconnect (rd->connection);
274 rd->connection = NULL;
279 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
280 if (rd->collect_cpu_load
281 || rd->collect_memory
285 status = ros_system_resource (rd->connection, handle_system_resource,
286 /* user data = */ rd);
290 ERROR ("routeros plugin: ros_system_resource failed: %s",
291 sstrerror (status, errbuf, sizeof (errbuf)));
292 ros_disconnect (rd->connection);
293 rd->connection = NULL;
300 } /* }}} int cr_read */
302 static void cr_free_data (cr_data_t *ptr) /* {{{ */
307 ros_disconnect (ptr->connection);
308 ptr->connection = NULL;
311 sfree (ptr->service);
312 sfree (ptr->username);
313 sfree (ptr->password);
316 } /* }}} void cr_free_data */
318 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
320 cr_data_t *router_data;
322 user_data_t user_data;
326 router_data = malloc (sizeof (*router_data));
327 if (router_data == NULL)
329 memset (router_data, 0, sizeof (*router_data));
330 router_data->connection = NULL;
331 router_data->node = NULL;
332 router_data->service = NULL;
333 router_data->username = NULL;
334 router_data->password = NULL;
337 for (i = 0; i < ci->children_num; i++)
339 oconfig_item_t *child = ci->children + i;
341 if (strcasecmp ("Host", child->key) == 0)
342 status = cf_util_get_string (child, &router_data->node);
343 else if (strcasecmp ("Port", child->key) == 0)
344 status = cf_util_get_service (child, &router_data->service);
345 else if (strcasecmp ("User", child->key) == 0)
346 status = cf_util_get_string (child, &router_data->username);
347 else if (strcasecmp ("Password", child->key) == 0)
348 status = cf_util_get_string (child, &router_data->password);
349 else if (strcasecmp ("CollectInterface", child->key) == 0)
350 cf_util_get_boolean (child, &router_data->collect_interface);
351 else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
352 cf_util_get_boolean (child, &router_data->collect_regtable);
353 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
354 else if (strcasecmp ("CollectCPULoad", child->key) == 0)
355 cf_util_get_boolean (child, &router_data->collect_cpu_load);
356 else if (strcasecmp ("CollectMemory", child->key) == 0)
357 cf_util_get_boolean (child, &router_data->collect_memory);
358 else if (strcasecmp ("CollectDF", child->key) == 0)
359 cf_util_get_boolean (child, &router_data->collect_df);
360 else if (strcasecmp ("CollectDisk", child->key) == 0)
361 cf_util_get_boolean (child, &router_data->collect_disk);
365 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
374 if (router_data->node == NULL)
376 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
377 "Where should I connect to?");
381 if (router_data->password == NULL)
383 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
384 "How should I authenticate?");
388 if (!router_data->collect_interface
389 && !router_data->collect_regtable)
391 ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
392 "What statistics should I collect?");
397 if ((status == 0) && (router_data->username == NULL))
399 router_data->username = sstrdup ("admin");
400 if (router_data->username == NULL)
402 ERROR ("routeros plugin: sstrdup failed.");
407 ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
408 user_data.data = router_data;
409 user_data.free_func = (void *) cr_free_data;
411 status = plugin_register_complex_read (/* group = */ NULL, read_name,
412 cr_read, /* interval = */ NULL, &user_data);
415 cr_free_data (router_data);
418 } /* }}} int cr_config_router */
420 static int cr_config (oconfig_item_t *ci)
424 for (i = 0; i < ci->children_num; i++)
426 oconfig_item_t *child = ci->children + i;
428 if (strcasecmp ("Router", child->key) == 0)
429 cr_config_router (child);
432 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
437 } /* }}} int cr_config */
439 void module_register (void)
441 plugin_register_complex_config ("routeros", cr_config);
442 } /* void module_register */
444 /* vim: set sw=2 noet fdm=marker : */