2 * collectd - src/load.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>
28 static ros_connection_t *connection = NULL;
30 static char *conf_node = "router.example.com";
31 static char *conf_service = NULL;
32 static char *conf_username = "collectd";
33 static char *conf_password = "secret";
35 static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */
36 counter_t rx, counter_t tx)
39 value_list_t vl = VALUE_LIST_INIT;
41 values[0].counter = rx;
42 values[1].counter = tx;
45 vl.values_len = STATIC_ARRAY_SIZE (values);
46 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
47 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
48 sstrncpy (vl.type, type, sizeof (vl.type));
49 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
51 plugin_dispatch_values (&vl);
52 } /* }}} void cr_submit_io */
54 static void submit_interface (const ros_interface_t *i) /* {{{ */
61 submit_interface (i->next);
65 cr_submit_io ("if_packets", i->name,
66 (counter_t) i->rx_packets, (counter_t) i->tx_packets);
67 cr_submit_io ("if_octets", i->name,
68 (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
69 cr_submit_io ("if_errors", i->name,
70 (counter_t) i->rx_errors, (counter_t) i->tx_errors);
71 cr_submit_io ("if_dropped", i->name,
72 (counter_t) i->rx_drops, (counter_t) i->tx_drops);
74 submit_interface (i->next);
75 } /* }}} void submit_interface */
77 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
78 const ros_interface_t *i, __attribute__((unused)) void *user_data)
82 } /* }}} int handle_interface */
84 static void cr_submit_gauge (const char *type, /* {{{ */
85 const char *type_instance, gauge_t value)
88 value_list_t vl = VALUE_LIST_INIT;
90 values[0].gauge = value;
93 vl.values_len = STATIC_ARRAY_SIZE (values);
94 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
95 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
96 sstrncpy (vl.type, type, sizeof (vl.type));
97 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
99 plugin_dispatch_values (&vl);
100 } /* }}} void cr_submit_gauge */
102 static void submit_regtable (const ros_registration_table_t *r) /* {{{ */
104 char type_instance[DATA_MAX_NAME_LEN];
110 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
111 r->interface, r->radio_name);
112 cr_submit_gauge ("bitrate", type_instance,
113 (gauge_t) (1000000.0 * r->rx_rate));
114 cr_submit_gauge ("signal_power", type_instance,
115 (gauge_t) r->rx_signal_strength);
116 cr_submit_gauge ("signal_quality", type_instance,
117 (gauge_t) r->rx_ccq);
120 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
121 r->interface, r->radio_name);
122 cr_submit_gauge ("bitrate", type_instance,
123 (gauge_t) (1000000.0 * r->tx_rate));
124 cr_submit_gauge ("signal_power", type_instance,
125 (gauge_t) r->tx_signal_strength);
126 cr_submit_gauge ("signal_quality", type_instance,
127 (gauge_t) r->tx_ccq);
130 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
131 r->interface, r->radio_name);
132 cr_submit_io ("if_octets", type_instance,
133 (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
134 cr_submit_gauge ("snr", type_instance, (gauge_t) r->signal_to_noise);
136 submit_regtable (r->next);
137 } /* }}} void submit_regtable */
139 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
140 const ros_registration_table_t *r,
141 __attribute__((unused)) void *user_data)
145 } /* }}} int handle_regtable */
147 static int cr_read (void) /* {{{ */
151 if (connection == NULL)
153 connection = ros_connect (conf_node, conf_service,
154 conf_username, conf_password);
155 if (connection == NULL)
158 ERROR ("routeros plugin: ros_connect failed: %s",
159 sstrerror (errno, errbuf, sizeof (errbuf)));
163 assert (connection != NULL);
165 status = ros_interface (connection, handle_interface,
166 /* user data = */ NULL);
170 ERROR ("routeros plugin: ros_interface failed: %s",
171 sstrerror (status, errbuf, sizeof (errbuf)));
172 ros_disconnect (connection);
177 status = ros_registration_table (connection, handle_regtable,
178 /* user data = */ NULL);
182 ERROR ("routeros plugin: ros_registration_table failed: %s",
183 sstrerror (status, errbuf, sizeof (errbuf)));
184 ros_disconnect (connection);
190 } /* }}} int cr_read */
192 void module_register (void)
194 plugin_register_read ("routeros", cr_read);
195 } /* void module_register */
197 /* vim: set sw=2 noet fdm=marker : */