From: Florian Forster Date: Sat, 28 Nov 2009 19:00:05 +0000 (+0100) Subject: routeros plugin: Added first version of a plugin for MikroTik's RouterOS. X-Git-Tag: collectd-4.9.0~36^2~6 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=f51548ec63c799d57422fc1f4560e82fdf8d86ab routeros plugin: Added first version of a plugin for MikroTik's RouterOS. --- diff --git a/configure.in b/configure.in index b3d9516e..0b41015f 100644 --- a/configure.in +++ b/configure.in @@ -2653,6 +2653,63 @@ AC_DEFINE_UNQUOTED(HAVE_LIBPTHREAD, [$collect_pthread], AM_CONDITIONAL(BUILD_WITH_LIBPTHREAD, test "x$with_libpthread" = "xyes") # }}} +# --with-librouteros {{{ +AC_ARG_WITH(librouteros, [AS_HELP_STRING([--with-librouteros@<:@=PREFIX@:>@], [Path to librouteros.])], +[ + if test "x$withval" = "xyes" + then + with_librouteros="yes" + else if test "x$withval" = "xno" + then + with_librouteros="no" + else + with_librouteros="yes" + LIBROUTEROS_CPPFLAGS="$LIBROUTEROS_CPPFLAGS -I$withval/include" + LIBROUTEROS_LDFLAGS="$LIBROUTEROS_LDFLAGS -L$withval/lib" + fi; fi +], +[with_librouteros="yes"]) + +SAVE_CPPFLAGS="$CPPFLAGS" +SAVE_LDFLAGS="$LDFLAGS" + +CPPFLAGS="$CPPFLAGS $LIBROUTEROS_CPPFLAGS" +LDFLAGS="$LDFLAGS $LIBROUTEROS_LDFLAGS" + +if test "x$with_librouteros" = "xyes" +then + if test "x$LIBROUTEROS_CPPFLAGS" != "x" + then + AC_MSG_NOTICE([librouteros CPPFLAGS: $LIBROUTEROS_CPPFLAGS]) + fi + AC_CHECK_HEADERS(routeros_api.h, + [with_librouteros="yes"], + [with_librouteros="no ('routeros_api.h' not found)"]) +fi +if test "x$with_librouteros" = "xyes" +then + if test "x$LIBROUTEROS_LDFLAGS" != "x" + then + AC_MSG_NOTICE([librouteros LDFLAGS: $LIBROUTEROS_LDFLAGS]) + fi + AC_CHECK_LIB(routeros, ros_interface, + [with_librouteros="yes"], + [with_librouteros="no (symbol 'ros_interface' not found)"]) +fi + +CPPFLAGS="$SAVE_CPPFLAGS" +LDFLAGS="$SAVE_LDFLAGS" + +if test "x$with_librouteros" = "xyes" +then + BUILD_WITH_LIBROUTEROS_CPPFLAGS="$LIBROUTEROS_CPPFLAGS" + BUILD_WITH_LIBROUTEROS_LDFLAGS="$LIBROUTEROS_LDFLAGS" + AC_SUBST(BUILD_WITH_LIBROUTEROS_CPPFLAGS) + AC_SUBST(BUILD_WITH_LIBROUTEROS_LDFLAGS) +fi +AM_CONDITIONAL(BUILD_WITH_LIBROUTEROS, test "x$with_librouteros" = "xyes") +# }}} + # --with-librrd {{{ # AC_ARG_WITH (package, help-string, [action-if-given], [action-if-not-given]) librrd_cflags="" @@ -3898,6 +3955,7 @@ AC_PLUGIN([postgresql], [$with_libpq], [PostgreSQL database statistics]) AC_PLUGIN([powerdns], [yes], [PowerDNS statistics]) AC_PLUGIN([processes], [$plugin_processes], [Process statistics]) AC_PLUGIN([protocols], [$plugin_protocols], [Protocol (IP, TCP, ...) statistics]) +AC_PLUGIN([routeros], [$with_librouteros], [RouterOS plugin]) AC_PLUGIN([rrdcached], [$librrd_rrdc_update], [RRDTool output plugin]) AC_PLUGIN([rrdtool], [$with_librrd], [RRDTool output plugin]) AC_PLUGIN([sensors], [$with_libsensors], [lm_sensors statistics]) @@ -4120,6 +4178,7 @@ Configuration: libperl . . . . . . . $with_libperl libpq . . . . . . . . $with_libpq libpthread . . . . . $with_libpthread + librouteros . . . . . $with_librouteros librrd . . . . . . . $with_librrd libsensors . . . . . $with_libsensors libstatgrab . . . . . $with_libstatgrab @@ -4203,6 +4262,7 @@ Configuration: powerdns . . . . . . $enable_powerdns processes . . . . . . $enable_processes protocols . . . . . . $enable_protocols + routeros . . . . . . $enable_routeros rrdcached . . . . . . $enable_rrdcached rrdtool . . . . . . . $enable_rrdtool sensors . . . . . . . $enable_sensors diff --git a/src/Makefile.am b/src/Makefile.am index 15609e47..19a04bcf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -810,6 +810,16 @@ collectd_LDADD += "-dlopen" protocols.la collectd_DEPENDENCIES += protocols.la endif +if BUILD_PLUGIN_ROUTEROS +pkglib_LTLIBRARIES += routeros.la +routeros_la_SOURCES = routeros.c +routeros_la_CPPFLAGS = $(BUILD_WITH_LIBROUTEROS_CPPFLAGS) +routeros_la_LDFLAGS = -module -avoid-version $(BUILD_WITH_LIBROUTEROS_LDFLAGS) +routeros_la_LIBADD = -lrouteros +collectd_LDADD += "-dlopen" routeros.la +collectd_DEPENDENCIES += routeros.la +endif + if BUILD_PLUGIN_RRDCACHED pkglib_LTLIBRARIES += rrdcached.la rrdcached_la_SOURCES = rrdcached.c utils_rrdcreate.c utils_rrdcreate.h diff --git a/src/routeros.c b/src/routeros.c new file mode 100644 index 00000000..3e5e6680 --- /dev/null +++ b/src/routeros.c @@ -0,0 +1,121 @@ +/** + * collectd - src/load.c + * Copyright (C) 2009 Florian octo Forster + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; only version 2 of the License is applicable. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + +#include "collectd.h" +#include "common.h" +#include "plugin.h" + +#include + +static ros_connection_t *connection = NULL; + +static char *conf_node = "router.example.com"; +static char *conf_service = NULL; +static char *conf_username = "collectd"; +static char *conf_password = "secret"; + +static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */ + counter_t rx, counter_t tx) +{ + value_t values[2]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].counter = rx; + values[1].counter = tx; + + vl.values = values; + vl.values_len = STATIC_ARRAY_SIZE (values); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin)); + sstrncpy (vl.type, type, sizeof (vl.type)); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + + plugin_dispatch_values (&vl); +} /* }}} void cr_submit_io */ + +static void submit_interface (const ros_interface_t *i) /* {{{ */ +{ + if (i == NULL) + return; + + if (!i->running) + { + submit_interface (i->next); + return; + } + + cr_submit_io ("if_packets", i->name, + (counter_t) i->rx_packets, (counter_t) i->tx_packets); + cr_submit_io ("if_octets", i->name, + (counter_t) i->rx_bytes, (counter_t) i->tx_bytes); + cr_submit_io ("if_errors", i->name, + (counter_t) i->rx_errors, (counter_t) i->tx_errors); + cr_submit_io ("if_dropped", i->name, + (counter_t) i->rx_drops, (counter_t) i->tx_drops); + + submit_interface (i->next); +} /* }}} void submit_interface */ + +static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */ + const ros_interface_t *i, __attribute__((unused)) void *user_data) +{ + submit_interface (i); + return (0); +} /* }}} int handle_interface */ + +static int cr_read (void) /* {{{ */ +{ + int status; + + if (connection == NULL) + { + connection = ros_connect (conf_node, conf_service, + conf_username, conf_password); + if (connection == NULL) + { + char errbuf[128]; + ERROR ("routeros plugin: ros_connect failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + } + assert (connection != NULL); + + status = ros_interface (connection, handle_interface, /* user data = */ NULL); + if (status != 0) + { + char errbuf[128]; + ERROR ("routeros plugin: ros_interface failed: %s", + sstrerror (status, errbuf, sizeof (errbuf))); + ros_disconnect (connection); + connection = NULL; + return (-1); + } + + return (0); +} /* }}} int cr_read */ + +void module_register (void) +{ + plugin_register_read ("routeros", cr_read); +} /* void module_register */ + +/* vim: set sw=2 noet fdm=marker : */