Merge branch 'collectd-4.0'
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 24 Jun 2007 20:42:09 +0000 (22:42 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sun, 24 Jun 2007 20:42:09 +0000 (22:42 +0200)
22 files changed:
configure.in
contrib/PerlLib/Collectd/Unixsock.pm
contrib/SpamAssassin/Collectd.pm
contrib/SpamAssassin/example.cf
contrib/cussh.pl [new file with mode: 0755]
src/Makefile.am
src/collectd-snmp.pod [new file with mode: 0644]
src/collectd-unixsock.pod
src/collectd.conf.pod
src/collectd.h
src/configfile.c
src/configfile.h
src/email.c
src/network.c
src/network.h
src/plugin.c
src/plugin.h
src/rrdtool.c
src/snmp.c [new file with mode: 0644]
src/types.db
src/unixsock.c
src/xmms.c [new file with mode: 0644]

index 0c26355..cf423ed 100644 (file)
@@ -1238,6 +1238,32 @@ else
 fi
 AM_CONDITIONAL(BUILD_WITH_LIBIPTC, test "x$with_libiptc" = "xyes")
 
+AC_ARG_WITH(libnetsnmp, [AS_HELP_STRING([--with-libnetsnmp@<:@=PREFIX@:>@], [Path to the Net-SNMPD library.])],
+[
+       if test "x$withval" != "xno" && test "x$withval" != "xyes"
+       then
+               LDFLAGS="$LDFLAGS -L$withval/lib"
+               CPPFLAGS="$CPPFLAGS -I$withval/include"
+               with_libnetsnmp="yes"
+       fi
+],
+[with_libnetsnmp="yes"])
+if test "x$with_libnetsnmp" = "xyes"
+then
+       AC_CHECK_LIB(netsnmp, init_snmp,
+       [
+               AC_DEFINE(HAVE_LIBSNMP, 1, [Define to 1 if you have the Net-SNMP library (-lnetsnmp).])
+       ], [with_libnetsnmp="no (libnetsnmp not found)"])
+fi
+if test "x$with_libnetsnmp" = "xyes"
+then
+       AC_CHECK_HEADERS(net-snmp/net-snmp-config.h,
+       [
+               AC_DEFINE(HAVE_NET_SNMP_NET_SNMP_CONFIG_H, 1, [Define to 1 if you have the <net-snmp/net-snmp-config.h> header file.])
+       ], [with_libnetsnmp="no (net-snmp/net-snmp-config.h not found)"])
+fi
+AM_CONDITIONAL(BUILD_WITH_LIBNETSNMP, test "x$with_libnetsnmp" = "xyes")
+
 AC_ARG_WITH(libupsclient, [AS_HELP_STRING([--with-libupsclient@<:@=PREFIX@:>@], [Path to libupsclient.])],
 [
        if test "x$withval" != "xno" && test "x$withval" != "xyes"
@@ -1266,6 +1292,74 @@ then
 fi
 AM_CONDITIONAL(BUILD_WITH_LIBUPSCLIENT, test "x$with_libupsclient" = "xyes")
 
+### BEGIN of check for libxmms ###
+with_xmms_config="xmms-config"
+with_xmms_prefix=0
+with_xmms_cflags=""
+with_xmms_libs=""
+AC_ARG_WITH(libxmms, [AS_HELP_STRING([--with-libxmms@<:@=PREFIX@:>@], [Path to libxmms.])],
+[
+       if test "x$withval" != "xno" -a "x$withval" != "xyes"
+       then
+               if test -x "$withval/bin/xmms-config"
+               then
+                       with_xmms_config="$withval/bin/xmms-config"
+                       with_xmms_prefix=1
+               fi
+       fi
+       if test "x$withval" = "xno"
+       then
+               with_libxmms="no"
+       else
+               with_libxmms="yes"
+       fi
+],
+[
+       with_libxmms="yes"
+])
+if test "x$with_libxmms" = "xyes"
+then
+       with_xmms_cflags=`$with_xmms_config --cflags 2>/dev/null`
+       xmms_config_status=$?
+
+       if test $xmms_config_status -ne 0
+       then
+               with_libxmms="no"
+       fi
+fi
+if test "x$with_libxmms" = "xyes"
+then
+       with_xmms_libs=`$with_xmms_config --libs 2>/dev/null`
+       xmms_config_status=$?
+
+       if test $xmms_config_status -ne 0
+       then
+               with_libxmms="no"
+       fi
+fi
+if test "x$with_libxmms" = "xyes"
+then
+       AC_CHECK_LIB(xmms, xmms_remote_get_info,
+       [
+               BUILD_WITH_LIBXMMS_CFLAGS="$with_xmms_cflags"
+               BUILD_WITH_LIBXMMS_LIBS="$with_xmms_libs"
+               AC_SUBST(BUILD_WITH_LIBXMMS_CFLAGS)
+               AC_SUBST(BUILD_WITH_LIBXMMS_LIBS)
+       ],
+       [
+               with_libxmms="no"
+       ],
+       [$with_xmms_libs])
+fi
+with_libxmms_numeric=0
+if test "x$with_libxmms" = "xyes"
+then
+       with_libxmms_numeric=1
+fi
+AC_DEFINE_UNQUOTED(HAVE_LIBXMMS, [$with_libxmms_numeric], [Define to 1 if you have the 'xmms' library (-lxmms).])
+AM_CONDITIONAL(BUILD_WITH_LIBXMMS, test "x$with_libxmms" = "xyes")
+### END of check for libxmms ###
+
 # Check for enabled/disabled features
 #
 
@@ -1368,6 +1462,7 @@ AC_COLLECTD([ping],      [disable], [module], [ping statistics])
 AC_COLLECTD([processes], [disable], [module], [processes statistics])
 AC_COLLECTD([sensors],   [disable], [module], [lm_sensors statistics])
 AC_COLLECTD([serial],    [disable], [module], [serial statistics])
+AC_COLLECTD([snmp],      [disable], [module], [serial statistics])
 AC_COLLECTD([logfile],   [disable], [module], [logfile log facility])
 AC_COLLECTD([swap],      [disable], [module], [swap statistics])
 AC_COLLECTD([syslog],    [disable], [module], [syslog log facility])
@@ -1376,6 +1471,7 @@ AC_COLLECTD([unixsock],  [disable], [module], [UNIX socket plugin])
 AC_COLLECTD([users],     [disable], [module], [user count statistics])
 AC_COLLECTD([vserver],   [disable], [module], [vserver statistics])
 AC_COLLECTD([wireless],  [disable], [module], [wireless link statistics])
+AC_COLLECTD([xmms],      [disable], [module], [wireless link statistics])
 
 AC_OUTPUT(Makefile src/Makefile src/collectd.conf src/liboconfig/Makefile src/liboping/Makefile)
 
@@ -1395,6 +1491,7 @@ Configuration:
     libiptc . . . . . . $with_libiptc
     libkstat  . . . . . $with_kstat
     libmysql  . . . . . $with_libmysql
+    libnetsnmp  . . . . $with_libnetsnmp
     liboconfig  . . . . $with_liboconfig
     liboping  . . . . . $with_liboping
     libpcap . . . . . . $with_libpcap
@@ -1404,6 +1501,7 @@ Configuration:
     libsensors  . . . . $with_lm_sensors
     libstatgrab . . . . $with_libstatgrab
     libupsclient  . . . $with_libupsclient
+    libxmms . . . . . . $with_libxmms
 
   Features:
     daemon mode . . . . $enable_daemon
@@ -1442,6 +1540,7 @@ Configuration:
     processes . . . . . $enable_processes
     sensors . . . . . . $enable_sensors
     serial  . . . . . . $enable_serial
+    snmp  . . . . . . . $enable_snmp
     swap  . . . . . . . $enable_swap
     syslog  . . . . . . $enable_syslog
     tape  . . . . . . . $enable_tape
@@ -1449,5 +1548,6 @@ Configuration:
     users . . . . . . . $enable_users
     vserver . . . . . . $enable_vserver
     wireless  . . . . . $enable_wireless
+    xmms  . . . . . . . $enable_xmms
 
 EOF
index 92e8772..3b8a91c 100644 (file)
@@ -83,6 +83,33 @@ sub _create_identifier
        return ("$host/$plugin/$type");
 } # _create_identifier
 
+sub _parse_identifier
+{
+       my $string = shift;
+       my $host;
+       my $plugin;
+       my $plugin_instance;
+       my $type;
+       my $type_instance;
+       my $ident;
+
+       ($host, $plugin, $type) = split ('/', $string);
+
+       ($plugin, $plugin_instance) = split ('-', $plugin, 2);
+       ($type, $type_instance) = split ('-', $type, 2);
+
+       $ident =
+       {
+               host => $host,
+               plugin => $plugin,
+               type => $type
+       };
+       $ident->{'plugin_instance'} = $plugin_instance if (defined ($plugin_instance));
+       $ident->{'type_instance'} = $type_instance if (defined ($type_instance));
+
+       return ($ident);
+} # _parse_identifier
+
 =head1 PUBLIC METHODS
 
 =over 4
@@ -217,6 +244,52 @@ sub putval
        return;
 } # putval
 
+=item I<$res> = I<$obj>-E<gt>B<listval> ()
+
+Queries a list of values from the daemon. The list is returned as an array of
+hash references, where each hash reference is a valid identifier. The C<time>
+member of each hash holds the epoch value of the last update of that value.
+
+=cut
+
+sub listval
+{
+       my $obj = shift;
+       my $msg;
+       my @ret = ();
+       my $status;
+       my $fh = $obj->{'sock'} or confess;
+
+       $msg = "LISTVAL\n";
+       send ($fh, $msg, 0) or confess ("send: $!");
+
+       $msg = <$fh>;
+       ($status, $msg) = split (' ', $msg, 2);
+       if ($status < 0)
+       {
+               $obj->{'error'} = $msg;
+               return;
+       }
+
+       for (my $i = 0; $i < $status; $i++)
+       {
+               my $time;
+               my $ident;
+
+               $msg = <$fh>;
+               chomp ($msg);
+
+               ($time, $ident) = split (' ', $msg, 2);
+
+               $ident = _parse_identifier ($ident);
+               $ident->{'time'} = int ($time);
+
+               push (@ret, $ident);
+       } # for (i = 0 .. $status)
+
+       return (@ret);
+} # listval
+
 =item I<$obj>-E<gt>destroy ();
 
 Closes the socket before the object is destroyed. This function is also
index c76c44d..1edcfc6 100644 (file)
@@ -18,7 +18,7 @@ add a loadplugin call into your init.pre file.
 
 =over 4
 
-=item collectd_socket [ socket path ]      (default: /tmp/.collectd-email)
+=item collectd_socket [ socket path ]      (default: /var/run/collectd-email)
 
 Where the collectd socket is
 
@@ -116,7 +116,7 @@ sub set_config {
 
     push (@cmds, {
            setting => 'collectd_socket', 
-           default => '/tmp/.collectd-email',
+           default => '/var/run/collectd-email',
            type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
     });
 
index 0abfb49..7ffd708 100644 (file)
@@ -1,5 +1,5 @@
 collectd_buffersize 256
-collectd_socket /tmp/.collectd-email
+collectd_socket /var/run/collectd-email
 collectd_timeout 2
 collectd_retries 3 
 
diff --git a/contrib/cussh.pl b/contrib/cussh.pl
new file mode 100755 (executable)
index 0000000..65c634e
--- /dev/null
@@ -0,0 +1,185 @@
+#!/usr/bin/perl
+#
+# collectd - contrib/cussh.pl
+# Copyright (C) 2007  Sebastian Harl
+#
+# 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
+#
+# Author:
+#   Sebastian Harl <sh at tokkee.org>
+#
+
+=head1 NAME
+
+cussh - collectd UNIX socket shell
+
+=head1 SYNOPSIS
+
+B<cussh> [I<E<lt>pathE<gt>>]
+
+=head1 DESCRIPTION
+
+B<collectd>'s unixsock plugin allows external programs to access the values it
+has collected or received and to submit own values. This is a little
+interactive frontend for this plugin.
+
+=head1 OPTIONS
+
+=over 4
+
+=item I<E<lt>pathE<gt>>
+
+The path to the UNIX socket provided by collectd's unixsock plugin. (Default:
+F</var/run/collectd-unixsock>)
+
+=back
+
+=cut
+
+use strict;
+use warnings;
+
+use Collectd::Unixsock();
+
+{ # main
+       my $path = $ARGV[0] || "/var/run/collectd-unixsock";
+       my $sock = Collectd::Unixsock->new($path);
+
+       if (! $sock) {
+               print STDERR "Unable to connect to $path!\n";
+               exit 1;
+       }
+
+       print "cussh version 0.1, Copyright (C) 2007 Sebastian Harl\n"
+               . "cussh comes with ABSOLUTELY NO WARRANTY. This is free software,\n"
+               . "and you are welcome to redistribute it under certain conditions.\n"
+               . "See the GNU General Public License 2 for more details.\n\n";
+
+       while (1) {
+               print "cussh> ";
+               my $line = <STDIN>;
+
+               last if ((! $line) || ($line =~ m/^quit$/i));
+
+               my ($cmd) = $line =~ m/^(\w+)\s+/;
+               $line = $';
+
+               next if (! $cmd);
+               $cmd = uc $cmd;
+
+               my $f = undef;
+               if ($cmd eq "PUTVAL") {
+                       $f = \&putval;
+               }
+               elsif ($cmd eq "GETVAL") {
+                       $f = \&getval;
+               }
+               else {
+                       print STDERR "ERROR: Unknown command $cmd!\n";
+                       next;
+               }
+
+               if (! $f->($sock, $line)) {
+                       print STDERR "ERROR: Command failed!\n";
+                       next;
+               }
+       }
+
+       $sock->destroy();
+       exit 0;
+}
+
+sub getid {
+       my $string = shift || return;
+
+       print $$string . $/;
+       my ($h, $p, $pi, $t, $ti) =
+               $$string =~ m/^(\w+)\/(\w+)(?:-(\w+))?\/(\w+)(?:-(\w+))?\s+/;
+       $$string = $';
+
+       return if ((! $h) || (! $p) || (! $t));
+
+       my %id = ();
+
+       ($id{'host'}, $id{'plugin'}, $id{'type'}) = ($h, $p, $t);
+
+       $id{'plugin_instance'} = $pi if ($pi);
+       $id{'type_instance'} = $ti if ($ti);
+       return \%id;
+}
+
+=head1 COMMANDS
+
+=over 4
+
+=item B<GETVAL> I<Identifier>
+
+=item B<PUTVAL> I<Identifier> I<Valuelist>
+
+These commands follow the exact same syntax as described in
+L<collectd-unixsock(5)>.
+
+=cut
+
+sub putval {
+       my $sock = shift || return;
+       my $line = shift || return;
+
+       my $id = getid(\$line);
+
+       return if (! $id);
+
+       my ($time, @values) = split m/:/, $line;
+       return $sock->putval(%$id, $time, \@values);
+}
+
+sub getval {
+       my $sock = shift || return;
+       my $line = shift || return;
+
+       my $id = getid(\$line);
+
+       return if (! $id);
+
+       my $vals = $sock->getval(%$id);
+
+       return if (! $vals);
+
+       foreach my $key (keys %$vals) {
+               print "\t$key: $vals->{$key}\n";
+       }
+       return 1;
+}
+
+=head1 SEE ALSO
+
+L<collectd(1)>, L<collectd-unisock(5)>
+
+=head1 AUTHOR
+
+Written by Sebastian Harl E<lt>sh@tokkee.orgE<gt>.
+
+B<collectd> has been written by Florian Forster and others.
+
+=head1 COPYRIGHT
+
+Copyright (C) 2007 Sebastian Harl.
+
+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.
+
+=cut
+
+# vim: set sw=4 ts=4 tw=78 noexpandtab :
index f3f259b..d92ef96 100644 (file)
@@ -25,6 +25,7 @@ collectd_SOURCES = collectd.c collectd.h \
 collectd_CPPFLAGS = $(LTDLINCL)
 collectd_CPPFLAGS += -DPREFIX='"${prefix}"'
 collectd_CPPFLAGS += -DCONFIGFILE='"${sysconfdir}/${PACKAGE_NAME}.conf"'
+collectd_CPPFLAGS += -DLOCALSTATEDIR='"${localstatedir}"'
 collectd_CPPFLAGS += -DPKGLOCALSTATEDIR='"${localstatedir}/lib/${PACKAGE_NAME}"'
 if BUILD_FEATURE_DAEMON
 collectd_CPPFLAGS += -DPIDFILE='"${localstatedir}/run/${PACKAGE_NAME}.pid"'
@@ -471,6 +472,20 @@ collectd_LDADD += "-dlopen" serial.la
 collectd_DEPENDENCIES += serial.la
 endif
 
+if BUILD_MODULE_SNMP
+pkglib_LTLIBRARIES += snmp.la
+snmp_la_SOURCES = snmp.c
+snmp_la_LDFLAGS = -module -avoid-version
+if BUILD_WITH_LIBNETSNMP
+snmp_la_LDFLAGS += -lnetsnmp
+endif
+if BUILD_WITH_LIBPTHREAD
+snmp_la_LDFLAGS += -lpthread
+endif
+collectd_LDADD += "-dlopen" snmp.la
+collectd_DEPENDENCIES += snmp.la
+endif
+
 if BUILD_MODULE_SWAP
 pkglib_LTLIBRARIES += swap.la
 swap_la_SOURCES = swap.c
@@ -519,7 +534,7 @@ endif
 if BUILD_MODULE_UNIXSOCK
 pkglib_LTLIBRARIES += unixsock.la
 unixsock_la_SOURCES = unixsock.c
-unixsock_la_CPPFLAGS = -DPREFIX='"${prefix}"'
+unixsock_la_CPPFLAGS = -DLOCALSTATEDIR='"${localstatedir}"'
 unixsock_la_LDFLAGS = -module -avoid-version -lpthread
 collectd_LDADD += "-dlopen" unixsock.la
 collectd_DEPENDENCIES += unixsock.la
@@ -549,9 +564,23 @@ collectd_LDADD += "-dlopen" wireless.la
 collectd_DEPENDENCIES += wireless.la
 endif
 
+if BUILD_MODULE_XMMS
+pkglib_LTLIBRARIES += xmms.la
+xmms_la_SOURCES = xmms.c
+xmms_la_CFLAGS = 
+xmms_la_LDFLAGS = -module -avoid-version
+if BUILD_WITH_LIBXMMS
+xmms_la_CFLAGS += $(BUILD_WITH_LIBXMMS_CFLAGS)
+xmms_la_LDFLAGS += $(BUILD_WITH_LIBXMMS_LIBS)
+endif
+collectd_LDADD += "-dlopen" xmms.la
+collectd_DEPENDENCIES += xmms.la
+endif
+
+
 dist_man_MANS = collectd.1 collectd-nagios.1 collectd.conf.5 \
                collectd-email.5 collectd-exec.5 collectd-perl.5 \
-               collectd-unixsock.5
+               collectd-snmp.5 collectd-unixsock.5
 
 #collectd_1_SOURCES = collectd.pod
 
diff --git a/src/collectd-snmp.pod b/src/collectd-snmp.pod
new file mode 100644 (file)
index 0000000..22d6e37
--- /dev/null
@@ -0,0 +1,191 @@
+=head1 NAME
+
+collectd-snmp - Documentation of collectd's C<snmp plugin>
+
+=head1 SYNOPSIS
+
+  LoadPlugin snmp
+  # ...
+  <Plugin snmp>
+    <Data "powerplus_voltge_input">
+      Type "voltage"
+      Table false
+      Instance "input_line1"
+      Values "SNMPv2-SMI::enterprises.6050.5.4.1.1.2.1"
+    </Data>
+    <Data "hr_users">
+      Type "users"
+      Table false
+      Instance ""
+      Values "HOST-RESOURCES-MIB::hrSystemNumUsers.0"
+    </Data>
+    <Data "std_traffic">
+      Type "if_octets"
+      Table true
+      Instance "IF-MIB::ifDescr"
+      Values "IF-MIB::ifInOctets" "IF-MIB::ifOutOctets"
+    </Data>
+
+    <Host "some.switch.mydomain.org">
+      Address "192.168.0.2"
+      Version 1
+      Community "community_string"
+      Collect "std_traffic"
+      Inverval 120
+    </Host>
+    <Host "some.server.mydomain.org">
+      Address "192.168.0.42"
+      Version 2
+      Community "another_string"
+      Collect "std_traffic" "hr_users"
+    </Host>
+    <Host "some.ups.mydomain.org">
+      Address "192.168.0.3"
+      Version 1
+      Community "more_communities"
+      Collect "powerplus_voltge_input"
+      Interval 300
+    </Host>
+  </Plugin>
+
+=head1 DESCRIPTION
+
+The C<snmp plugin> queries other hosts using SNMP, the simple network
+management protocol, and translates the value it receives to collectd's
+internal format and dispatches them. Depending on the write plugins you have
+loaded they may be written to disk or submitted to another instance or
+whatever you configured.
+
+=head1 CONFIGURATION
+
+Since the aim of the C<snmp plugin> is to provide a generic interface to SNMP,
+it's configuration is not trivial and may take some time.
+
+There are two types of blocks that can be contained in the
+C<E<lt>PluginE<nbsp>snmpE<gt>> block: B<Data> and B<Host>:
+
+=head2 The B<Data> block
+
+The B<Data> block defines a list of values or a table of values that are to be
+queried. The following options can be set:
+
+=over 4
+
+=item B<Type> I<type>
+
+collectd's type that is to be used, e.E<nbsp>g. "if_octets" for interface
+traffic or "users" for a user count. The types are read from the B<TypesDB>
+(see L<collectd.conf(5)>), so you may want to check for which types are
+defined.
+
+=item B<Table> I<true|false>
+
+Define if this is a single list of values or a table of values. The difference
+is that when querying a list of values from SNMP that data is going to be
+dispatched as one value-list to the daemon (i.E<nbsp>e.  one RRD file will be
+created). If the correcponding data-set needs more than one value (has more
+than one data-source) you will still need to configure more than one B<Values>
+(see below).
+
+If B<Table> is set to I<true> then the plugin will search the entire subtree
+and dispatch all values it can find. This is handy for the typical SNMP
+tables, such as the interface table (C<IF-MIB::ifTable>).
+
+Since the semantic of B<Instance> and B<Values> depends on this setting you
+need to set it before setting them. Doing vice verse will result in undefined
+behavior.
+
+=item B<Instance> I<Instance>
+
+Sets the type-instance of the values that are dispatched. The meaning of this
+setting depends on wether B<Table> is set to I<true> or I<false>:
+
+If B<Table> is set to I<true>, I<Instance> is interpreted as an SNMP-prefix
+that will return a list of strings. Those strings are then used as the actual
+type-instance. An example would be the C<IF-MIB::ifDescr> subtree.
+L<variables(5)> from the SNMP distribution describes the format of OIDs.
+
+If B<Table> is set to I<false> the actual string configured for I<Instance> is
+copied into the value-list. In this case I<Instance> may be empty, i.E<nbsp>e.
+"".
+
+=item B<Values> I<OID> [I<OID> ...]
+
+Configures the values to be queried from the SNMP host. The meaning slightly
+changes with the B<Table> setting. L<variables(5)> from the SNMP distribution
+describes the format of OIDs.
+
+If B<Table> is set to I<true>, each I<OID> must be the prefix of all the
+values to query, e.E<nbsp>g. C<IF-MIB::ifInOctets> for all the counters of
+incoming traffic. This subtree is walked (using C<GETNEXT>) until a value from
+outside the subtree is returned.
+
+If B<Table> is set to I<false>, each I<OID> must be the OID of exactly one
+value, e.E<nbsp>g. C<IF-MIB::ifInOctets.3> for the third counter of incoming
+traffic.
+
+=back
+
+=head2 The Host block
+
+The B<Host> block defines which hosts to query, which SNMP community and
+version to use and which of the defined B<Data> to query.
+
+The argument passed to the B<Host> block is used as the hostname in the data
+stored by collectd.
+
+=over 4
+
+=item B<Address> I<IP-Address>|I<Hostname>
+
+Set the address to connect to.
+
+=item B<Version> B<1>|B<2>
+
+Set the SNMP version to use. When giving B<2> version 2c is actually used.
+Version 3 is not supported by this plugin.
+
+=item B<Community> I<Community>
+
+Pass I<Community> to the host.
+
+=item B<Collect> I<Data> [I<Data> ...]
+
+Defines which values to collect. I<Data> refers to one of the B<Data> block
+above. Since the config file is read top-down you need to define the data
+before using it here.
+
+=item B<Interval> I<Seconds>
+
+Collect data from this host every I<Seconds> seconds. This value needs to be a
+multiple of the global B<Interval> setting and, if it is not, will be rounded
+B<down> to one and a warning is logged in this case. So if your global
+B<Interval> is set to I<10> and you configure I<25> here, it's rounded down to
+I<20>. By default the global B<Interval> setting will be used.
+
+This option is meant for devices with not much CPU power, e.E<nbsp>g. network
+equipment such as switches, embedded devices, rack monitoring systems and so
+on. Since the B<Step> of generated RRD files depends on this setting it's
+wise to select a reasonable value once and never change it.
+
+=back
+
+=head1 BUGS
+
+All configured hosts are queried sequencially, so timeouts may cause gaps in
+graphs.
+
+=head1 SEE ALSO
+
+L<collectd(1)>,
+L<collectd.conf(5)>,
+L<snmpget(1)>,
+L<snmpgetnext(1)>,
+L<variables(5)>,
+L<unix(7)>
+
+=head1 AUTHOR
+
+Florian Forster E<lt>octo@verplant.orgE<gt>
+
+=cut
index 7c0e316..da4f2b2 100644 (file)
@@ -50,6 +50,25 @@ Example:
   -> | GETVAL myhost/cpu-0/cpu-user
   <- | 1 value=1.260000e+00
 
+=item B<LISTVAL>
+
+Returnes a list of the values available in the value cache together with the
+time of the last update, so that querying applications can issue a B<GETVAL>
+command for the values that have changed.
+
+The first line's status number is the number of identifiers returned or less
+than zero if an error occured. Each of the following lines containes the
+update time as an epoch value and the identifier, seperated by a space.
+
+Example:
+  -> | LISTVAL
+  <- | 69 Values found
+  <- | 1182204284 leeloo/cpu-0/cpu-idle
+  <- | 1182204284 leeloo/cpu-0/cpu-nice
+  <- | 1182204284 leeloo/cpu-0/cpu-system
+  <- | 1182204284 leeloo/cpu-0/cpu-user
+  ...
+
 =item B<PUTVAL> I<Identifier> I<Valuelist>
 
 Submits a value (identified by I<Identifier>, see below) to the daemon which
index f7c10ad..d6de96e 100644 (file)
@@ -214,6 +214,10 @@ Ignore packets that originate from this address.
 
 =over 4
 
+=item B<SocketFile> I<Path>
+
+Sets the socket-file which is to be created.
+
 =item B<SocketGroup> I<Group>
 
 If running as root change the group of the UNIX-socket after it has been 
index ac75516..282e632 100644 (file)
 #define CONFIGFILE SYSCONFDIR"/collectd.conf"
 #endif
 
+#ifndef LOCALSTATEDIR
+#define LOCALSTATEDIR PREFIX "/var"
+#endif
+
 #ifndef PKGLOCALSTATEDIR
 #define PKGLOCALSTATEDIR PREFIX "/var/lib/" PACKAGE_NAME
 #endif
index 4b6803e..f4e9c60 100644 (file)
@@ -42,6 +42,13 @@ typedef struct cf_callback
        struct cf_callback *next;
 } cf_callback_t;
 
+typedef struct cf_complex_callback_s
+{
+       char *type;
+       int (*callback) (oconfig_item_t *);
+       struct cf_complex_callback_s *next;
+} cf_complex_callback_t;
+
 typedef struct cf_value_map_s
 {
        char *key;
@@ -65,6 +72,7 @@ static int dispatch_value_loadplugin (const oconfig_item_t *ci);
  * Private variables
  */
 static cf_callback_t *first_callback = NULL;
+static cf_complex_callback_t *complex_callback_head = NULL;
 
 static cf_value_map_t cf_value_map[] =
 {
@@ -257,21 +265,29 @@ static int dispatch_block_plugin (oconfig_item_t *ci)
        int i;
        char *name;
 
+       cf_complex_callback_t *cb;
+
        if (strcasecmp (ci->key, "Plugin") != 0)
                return (-1);
-       if (ci->values_num != 1)
+       if (ci->values_num < 1)
                return (-1);
        if (ci->values[0].type != OCONFIG_TYPE_STRING)
                return (-1);
 
        name = ci->values[0].value.string;
 
+       /* Check for a complex callback first */
+       for (cb = complex_callback_head; cb != NULL; cb = cb->next)
+               if (strcasecmp (name, cb->type) == 0)
+                       return (cb->callback (ci));
+
+       /* Hm, no complex plugin found. Dispatch the values one by one */
        for (i = 0; i < ci->children_num; i++)
        {
                if (ci->children[i].children == NULL)
                        dispatch_value_plugin (name, ci->children + i);
                else
-                       {DEBUG ("No nested config blocks allow for plugins. Yet.");}
+                       {DEBUG ("No nested config blocks allow for this plugin.");}
        }
 
        return (0);
@@ -347,6 +363,26 @@ void cf_unregister (const char *type)
                }
 } /* void cf_unregister */
 
+void cf_unregister_complex (const char *type)
+{
+       cf_complex_callback_t *this, *prev;
+
+       for (prev = NULL, this = complex_callback_head;
+                       this != NULL;
+                       prev = this, this = this->next)
+               if (strcasecmp (this->type, type) == 0)
+               {
+                       if (prev == NULL)
+                               complex_callback_head = this->next;
+                       else
+                               prev->next = this->next;
+
+                       sfree (this->type);
+                       sfree (this);
+                       break;
+               }
+} /* void cf_unregister */
+
 void cf_register (const char *type,
                int (*callback) (const char *, const char *),
                const char **keys, int keys_num)
@@ -369,6 +405,39 @@ void cf_register (const char *type,
        first_callback = cf_cb;
 } /* void cf_register */
 
+int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
+{
+       cf_complex_callback_t *new;
+
+       new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
+       if (new == NULL)
+               return (-1);
+
+       new->type = strdup (type);
+       if (new->type == NULL)
+       {
+               sfree (new);
+               return (-1);
+       }
+
+       new->callback = callback;
+       new->next = NULL;
+
+       if (complex_callback_head == NULL)
+       {
+               complex_callback_head = new;
+       }
+       else
+       {
+               cf_complex_callback_t *last = complex_callback_head;
+               while (last->next != NULL)
+                       last = last->next;
+               last->next = new;
+       }
+
+       return (0);
+} /* int cf_register_complex */
+
 int cf_read (char *filename)
 {
        oconfig_item_t *conf;
index 0ee8f33..3952c18 100644 (file)
@@ -1,3 +1,5 @@
+#ifndef CONFIGFILE_H
+#define CONFIGFILE_H
 /**
  * collectd - src/configfile.h
  * Copyright (C) 2005,2006  Florian octo Forster
@@ -20,8 +22,8 @@
  *   Florian octo Forster <octo at verplant.org>
  **/
 
-#ifndef CONFIGFILE_H
-#define CONFIGFILE_H
+#include "collectd.h"
+#include "liboconfig/oconfig.h"
 
 /*
  * DESCRIPTION
@@ -32,6 +34,7 @@
  *              `plugin_register'
  */
 void cf_unregister (const char *type);
+void cf_unregister_complex (const char *type);
 
 /*
  * DESCRIPTION
@@ -61,6 +64,8 @@ void cf_register (const char *type,
                int (*callback) (const char *, const char *),
                const char **keys, int keys_num);
 
+int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *));
+
 /*
  * DESCRIPTION
  *  `cf_read' reads the config file `filename' and dispatches the read
index 594ef96..f5a544a 100644 (file)
 /* 256 bytes ought to be enough for anybody ;-) */
 #define BUFSIZE 256
 
-#ifndef COLLECTD_SOCKET_PREFIX
-# define COLLECTD_SOCKET_PREFIX "/tmp/.collectd-"
-#endif /* COLLECTD_SOCKET_PREFIX */
-
-#define SOCK_PATH COLLECTD_SOCKET_PREFIX"email"
+#define SOCK_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-email"
 #define MAX_CONNS 5
 #define MAX_CONNS_LIMIT 16384
 
@@ -119,6 +115,7 @@ typedef struct {
 /* valid configuration file keys */
 static const char *config_keys[] =
 {
+       "SocketFile",
        "SocketGroup",
        "SocketPerms",
        "MaxConns"
@@ -126,6 +123,7 @@ static const char *config_keys[] =
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 /* socket configuration */
+static char *sock_file  = SOCK_PATH;
 static char *sock_group = COLLECTD_GRP_NAME;
 static int  sock_perms  = S_IRWXU | S_IRWXG;
 static int  max_conns   = MAX_CONNS;
@@ -171,7 +169,10 @@ static type_list_t check;
  */
 static int email_config (const char *key, const char *value)
 {
-       if (0 == strcasecmp (key, "SocketGroup")) {
+       if (0 == strcasecmp (key, "SocketFile")) {
+               sock_file = sstrdup (value);
+       }
+       else if (0 == strcasecmp (key, "SocketGroup")) {
                sock_group = sstrdup (value);
        }
        else if (0 == strcasecmp (key, "SocketPerms")) {
@@ -508,7 +509,7 @@ static void *open_connection (void *arg)
 
        addr.sun_family = AF_UNIX;
 
-       strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
+       strncpy (addr.sun_path, sock_file, (size_t)(UNIX_PATH_MAX - 1));
        addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
        unlink (addr.sun_path);
 
@@ -555,12 +556,12 @@ static void *open_connection (void *arg)
                }
                else
                {
-                       status = chown (SOCK_PATH, (uid_t) -1, grp->gr_gid);
+                       status = chown (sock_file, (uid_t) -1, grp->gr_gid);
                        if (status != 0)
                        {
                                char errbuf[1024];
                                log_warn ("chown (%s, -1, %i) failed: %s",
-                                               SOCK_PATH, (int) grp->gr_gid,
+                                               sock_file, (int) grp->gr_gid,
                                                sstrerror (errno, errbuf, sizeof (errbuf)));
                        }
                }
@@ -571,7 +572,7 @@ static void *open_connection (void *arg)
        }
 
        errno = 0;
-       if (0 != chmod (SOCK_PATH, sock_perms)) {
+       if (0 != chmod (sock_file, sock_perms)) {
                char errbuf[1024];
                log_warn ("chmod() failed: %s",
                                sstrerror (errno, errbuf, sizeof (errbuf)));
@@ -714,7 +715,7 @@ static int email_shutdown (void)
 
        pthread_mutex_unlock (&conns_mutex);
 
-       unlink (SOCK_PATH);
+       unlink (sock_file);
        errno = 0;
 
        return (0);
index 34385bf..ad8d530 100644 (file)
@@ -162,7 +162,7 @@ static int listen_loop = 0;
 static char         send_buffer[BUFF_SIZE];
 static char        *send_buffer_ptr;
 static int          send_buffer_fill;
-static value_list_t send_buffer_vl = VALUE_LIST_INIT;
+static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
 static char         send_buffer_type[DATA_MAX_NAME_LEN];
 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
 
@@ -557,6 +557,13 @@ static int parse_packet (void *buffer, int buffer_len)
                        if (status == 0)
                                vl.time = (time_t) tmp;
                }
+               else if (ntohs (header->type) == TYPE_INTERVAL)
+               {
+                       uint64_t tmp = 0;
+                       status = parse_part_number (&buffer, &buffer_len, &tmp);
+                       if (status == 0)
+                               vl.interval = (int) tmp;
+               }
                else if (ntohs (header->type) == TYPE_HOST)
                {
                        status = parse_part_string (&buffer, &buffer_len,
@@ -1097,6 +1104,16 @@ static int add_to_buffer (char *buffer, int buffer_size,
                                (unsigned int) vl->time);
        }
 
+       if (vl_def->interval != vl->interval)
+       {
+               if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
+                                       (uint64_t) vl->interval))
+                       return (-1);
+               vl_def->interval = vl->interval;
+               DEBUG ("network plugin: add_to_buffer: interval = %i",
+                               (int) vl->interval);
+       }
+
        if (strcmp (vl_def->plugin, vl->plugin) != 0)
        {
                if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
index b20fc75..6d8e966 100644 (file)
@@ -61,5 +61,6 @@
 #define TYPE_TYPE            0x0004
 #define TYPE_TYPE_INSTANCE   0x0005
 #define TYPE_VALUES          0x0006
+#define TYPE_INTERVAL        0x0007
 
 #endif /* NETWORK_H */
index da66204..6272581 100644 (file)
@@ -374,6 +374,12 @@ int plugin_register_config (const char *name,
        return (0);
 } /* int plugin_register_config */
 
+int plugin_register_complex_config (const char *type,
+               int (*callback) (oconfig_item_t *))
+{
+       return (cf_register_complex (type, callback));
+} /* int plugin_register_complex_config */
+
 int plugin_register_init (const char *name,
                int (*callback) (void))
 {
@@ -458,6 +464,12 @@ int plugin_unregister_config (const char *name)
        return (0);
 } /* int plugin_unregister_config */
 
+int plugin_unregister_complex_config (const char *name)
+{
+       cf_unregister_complex (name);
+       return (0);
+} /* int plugin_unregister_complex_config */
+
 int plugin_unregister_init (const char *name)
 {
        return (plugin_unregister (list_init, name));
index 83c2109..4ca6c77 100644 (file)
@@ -1,9 +1,8 @@
 #ifndef PLUGIN_H
 #define PLUGIN_H
-
 /**
  * collectd - src/plugin.h
- * Copyright (C) 2005,2006  Florian octo Forster
+ * Copyright (C) 2005-2007  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
@@ -22,6 +21,9 @@
  *   Florian octo Forster <octo at verplant.org>
  **/
 
+#include "collectd.h"
+#include "configfile.h"
+
 #define DATA_MAX_NAME_LEN 64
 
 #define DS_TYPE_COUNTER 0
@@ -61,6 +63,7 @@ struct value_list_s
        value_t *values;
        int      values_len;
        time_t   time;
+       int      interval;
        char     host[DATA_MAX_NAME_LEN];
        char     plugin[DATA_MAX_NAME_LEN];
        char     plugin_instance[DATA_MAX_NAME_LEN];
@@ -68,7 +71,8 @@ struct value_list_s
 };
 typedef struct value_list_s value_list_t;
 
-#define VALUE_LIST_INIT { NULL, 0, 0, "localhost", "", "", "" }
+#define VALUE_LIST_INIT { NULL, 0, 0, interval_g, "localhost", "", "", "" }
+#define VALUE_LIST_STATIC { NULL, 0, 0, 0, "localhost", "", "", "" }
 
 struct data_source_s
 {
@@ -143,6 +147,8 @@ void plugin_shutdown_all (void);
 int plugin_register_config (const char *name,
                int (*callback) (const char *key, const char *val),
                const char **keys, int keys_num);
+int plugin_register_complex_config (const char *type,
+               int (*callback) (oconfig_item_t *));
 int plugin_register_init (const char *name,
                int (*callback) (void));
 int plugin_register_read (const char *name,
@@ -156,6 +162,7 @@ int plugin_register_log (char *name,
                void (*callback) (int, const char *));
 
 int plugin_unregister_config (const char *name);
+int plugin_unregister_complex_config (const char *name);
 int plugin_unregister_init (const char *name);
 int plugin_unregister_read (const char *name);
 int plugin_unregister_write (const char *name);
@@ -163,6 +170,7 @@ int plugin_unregister_shutdown (const char *name);
 int plugin_unregister_data_set (const char *name);
 int plugin_unregister_log (const char *name);
 
+
 /*
  * NAME
  *  plugin_dispatch_values
index d20a814..4d4b87c 100644 (file)
@@ -37,9 +37,21 @@ struct rrd_cache_s
        char **values;
        time_t first_value;
        time_t last_value;
+       enum
+       {
+               FLAG_NONE   = 0x00,
+               FLAG_QUEUED = 0x01
+       } flags;
 };
 typedef struct rrd_cache_s rrd_cache_t;
 
+struct rrd_queue_s
+{
+       char *filename;
+       struct rrd_queue_s *next;
+};
+typedef struct rrd_queue_s rrd_queue_t;
+
 /*
  * Private variables
  */
@@ -77,25 +89,50 @@ static const char *config_keys[] =
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
+/* If datadir is zero, the daemon's basedir is used. If stepsize or heartbeat
+ * is zero a default, depending on the `interval' member of the value list is
+ * being used. */
 static char   *datadir   = NULL;
 static int     stepsize  = 0;
 static int     heartbeat = 0;
 static int     rrarows   = 1200;
 static double  xff       = 0.1;
 
+/* XXX: If you need to lock both, cache_lock and queue_lock, at the same time,
+ * ALWAYS lock `cache_lock' first! */
 static int         cache_timeout = 0;
 static int         cache_flush_timeout = 0;
 static time_t      cache_flush_last;
 static avl_tree_t *cache = NULL;
 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
 
+static rrd_queue_t    *queue_head = NULL;
+static rrd_queue_t    *queue_tail = NULL;
+static pthread_t       queue_thread = 0;
+static pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t  queue_cond = PTHREAD_COND_INITIALIZER;
+
+static int do_shutdown = 0;
+
 /* * * * * * * * * *
  * WARNING:  Magic *
  * * * * * * * * * */
-static int rra_get (char ***ret)
+
+static void rra_free (int rra_num, char **rra_def)
+{
+       int i;
+
+       for (i = 0; i < rra_num; i++)
+       {
+               sfree (rra_def[i]);
+       }
+       sfree (rra_def);
+} /* void rra_free */
+
+static int rra_get (char ***ret, const value_list_t *vl)
 {
-       static char **rra_def = NULL;
-       static int rra_num = 0;
+       char **rra_def;
+       int rra_num;
 
        int *rts;
        int  rts_num;
@@ -110,10 +147,21 @@ static int rra_get (char ***ret)
 
        char buffer[64];
 
-       if ((rra_num != 0) && (rra_def != NULL))
+       /* The stepsize we use here: If it is user-set, use it. If not, use the
+        * interval of the value-list. */
+       int ss;
+
+       if (rrarows <= 0)
+       {
+               *ret = NULL;
+               return (-1);
+       }
+
+       ss = (stepsize > 0) ? stepsize : vl->interval;
+       if (ss <= 0)
        {
-               *ret = rra_def;
-               return (rra_num);
+               *ret = NULL;
+               return (-1);
        }
 
        /* Use the configured timespans or fall back to the built-in defaults */
@@ -133,29 +181,24 @@ static int rra_get (char ***ret)
        if ((rra_def = (char **) malloc ((rra_max + 1) * sizeof (char *))) == NULL)
                return (-1);
        memset (rra_def, '\0', (rra_max + 1) * sizeof (char *));
-
-       if ((stepsize <= 0) || (rrarows <= 0))
-       {
-               *ret = NULL;
-               return (-1);
-       }
+       rra_num = 0;
 
        cdp_len = 0;
        for (i = 0; i < rts_num; i++)
        {
                span = rts[i];
 
-               if ((span / stepsize) < rrarows)
+               if ((span / ss) < rrarows)
                        continue;
 
                if (cdp_len == 0)
                        cdp_len = 1;
                else
                        cdp_len = (int) floor (((double) span)
-                                       / ((double) (rrarows * stepsize)));
+                                       / ((double) (rrarows * ss)));
 
                cdp_num = (int) ceil (((double) span)
-                               / ((double) (cdp_len * stepsize)));
+                               / ((double) (cdp_len * ss)));
 
                for (j = 0; j < rra_types_num; j++)
                {
@@ -182,7 +225,7 @@ static int rra_get (char ***ret)
 
        *ret = rra_def;
        return (rra_num);
-}
+} /* int rra_get */
 
 static void ds_free (int ds_num, char **ds_def)
 {
@@ -194,7 +237,7 @@ static void ds_free (int ds_num, char **ds_def)
        free (ds_def);
 }
 
-static int ds_get (char ***ret, const data_set_t *ds)
+static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
 {
        char **ds_def;
        int ds_num;
@@ -256,7 +299,8 @@ static int ds_get (char ***ret, const data_set_t *ds)
 
                status = snprintf (buffer, sizeof (buffer),
                                "DS:%s:%s:%i:%s:%s",
-                               d->name, type, heartbeat,
+                               d->name, type,
+                               (heartbeat > 0) ? heartbeat : (2 * vl->interval),
                                min, max);
                if ((status < 1) || (status >= sizeof (buffer)))
                        break;
@@ -283,7 +327,7 @@ static int ds_get (char ***ret, const data_set_t *ds)
        return (ds_num);
 }
 
-static int rrd_create_file (char *filename, const data_set_t *ds)
+static int rrd_create_file (char *filename, const data_set_t *ds, const value_list_t *vl)
 {
        char **argv;
        int argc;
@@ -298,13 +342,13 @@ static int rrd_create_file (char *filename, const data_set_t *ds)
        if (check_create_dir (filename))
                return (-1);
 
-       if ((rra_num = rra_get (&rra_def)) < 1)
+       if ((rra_num = rra_get (&rra_def, vl)) < 1)
        {
                ERROR ("rrd_create_file failed: Could not calculate RRAs");
                return (-1);
        }
 
-       if ((ds_num = ds_get (&ds_def, ds)) < 1)
+       if ((ds_num = ds_get (&ds_def, ds, vl)) < 1)
        {
                ERROR ("rrd_create_file failed: Could not calculate DSes");
                return (-1);
@@ -321,10 +365,13 @@ static int rrd_create_file (char *filename, const data_set_t *ds)
        }
 
        status = snprintf (stepsize_str, sizeof (stepsize_str),
-                       "%i", stepsize);
+                       "%i", (stepsize > 0) ? stepsize : vl->interval);
        if ((status < 1) || (status >= sizeof (stepsize_str)))
        {
                ERROR ("rrdtool plugin: snprintf failed.");
+               free (argv);
+               ds_free (ds_num, ds_def);
+               rra_free (rra_num, rra_def);
                return (-1);
        }
 
@@ -350,6 +397,7 @@ static int rrd_create_file (char *filename, const data_set_t *ds)
 
        free (argv);
        ds_free (ds_num, ds_def);
+       rra_free (rra_num, rra_def);
 
        return (status);
 }
@@ -434,138 +482,143 @@ static int value_list_to_filename (char *buffer, int buffer_len,
        return (0);
 } /* int value_list_to_filename */
 
-static rrd_cache_t *rrd_cache_insert (const char *filename,
-               const char *value, time_t value_time)
+static int rrd_write_to_file (char *filename, char **values, int values_num)
 {
-       rrd_cache_t *rc = NULL;
-       int new_rc = 0;
+       char **argv;
+       int argc;
+       int status;
 
-       if (cache != NULL)
-               avl_get (cache, filename, (void *) &rc);
+       if (values_num < 1)
+               return (0);
 
-       if (rc == NULL)
-       {
-               rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
-               if (rc == NULL)
-                       return (NULL);
-               rc->values_num = 0;
-               rc->values = NULL;
-               rc->first_value = 0;
-               rc->last_value = 0;
-               new_rc = 1;
-       }
+       argc = values_num + 2;
+       argv = (char **) malloc ((argc + 1) * sizeof (char *));
+       if (argv == NULL)
+               return (-1);
 
-       if (rc->last_value >= value_time)
+       argv[0] = "update";
+       argv[1] = filename;
+       memcpy (argv + 2, values, values_num * sizeof (char *));
+       argv[argc] = NULL;
+
+       DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
+
+       optind = 0; /* bug in librrd? */
+       rrd_clear_error ();
+       status = rrd_update (argc, argv);
+       if (status != 0)
        {
-               WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
-                               (unsigned int) rc->last_value,
-                               (unsigned int) value_time);
-               return (NULL);
+               WARNING ("rrd_update failed: %s: %s",
+                               filename, rrd_get_error ());
+               status = -1;
        }
 
-       rc->values = (char **) realloc ((void *) rc->values,
-                       (rc->values_num + 1) * sizeof (char *));
-       if (rc->values == NULL)
+       sfree (argv);
+
+       return (status);
+} /* int rrd_write_cache_entry */
+
+static void *rrd_queue_thread (void *data)
+{
+       while (42)
        {
-               char errbuf[1024];
-               ERROR ("rrdtool plugin: realloc failed: %s",
-                               sstrerror (errno, errbuf, sizeof (errbuf)));
-               if (cache != NULL)
+               rrd_queue_t *queue_entry;
+               rrd_cache_t *cache_entry;
+               char **values;
+               int    values_num;
+               int    i;
+
+               /* XXX: If you need to lock both, cache_lock and queue_lock, at
+                * the same time, ALWAYS lock `cache_lock' first! */
+
+               /* wait until an entry is available */
+               pthread_mutex_lock (&queue_lock);
+               while ((queue_head == NULL) && (do_shutdown == 0))
+                       pthread_cond_wait (&queue_cond, &queue_lock);
+
+               /* We're in the shutdown phase */
+               if (queue_head == NULL)
                {
-                       void *cache_key = NULL;
-                       avl_remove (cache, filename, &cache_key, NULL);
-                       sfree (cache_key);
+                       pthread_mutex_unlock (&queue_lock);
+                       break;
                }
-               free (rc);
-               return (NULL);
-       }
 
-       rc->values[rc->values_num] = strdup (value);
-       if (rc->values[rc->values_num] != NULL)
-               rc->values_num++;
+               /* Dequeue the first entry */
+               queue_entry = queue_head;
+               if (queue_head == queue_tail)
+                       queue_head = queue_tail = NULL;
+               else
+                       queue_head = queue_head->next;
 
-       if (rc->values_num == 1)
-               rc->first_value = value_time;
-       rc->last_value = value_time;
+               /* Unlock the queue again */
+               pthread_mutex_unlock (&queue_lock);
 
-       /* Insert if this is the first value */
-       if ((cache != NULL) && (new_rc == 1))
-       {
-               void *cache_key = strdup (filename);
+               /* We now need the cache lock so the entry isn't updated while
+                * we make a copy of it's values */
+               pthread_mutex_lock (&cache_lock);
 
-               if (cache_key == NULL)
-               {
-                       char errbuf[1024];
-                       ERROR ("rrdtool plugin: strdup failed: %s",
-                                       sstrerror (errno, errbuf, sizeof (errbuf)));
-                       sfree (rc->values[0]);
-                       sfree (rc->values);
-                       sfree (rc);
-                       return (NULL);
-               }
+               avl_get (cache, queue_entry->filename, (void *) &cache_entry);
 
-               avl_insert (cache, cache_key, rc);
-       }
+               values = cache_entry->values;
+               values_num = cache_entry->values_num;
 
-       DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
-                       (unsigned int) value_time, (void *) rc);
+               cache_entry->values = NULL;
+               cache_entry->values_num = 0;
+               cache_entry->flags = FLAG_NONE;
 
-       return (rc);
-} /* rrd_cache_t *rrd_cache_insert */
+               pthread_mutex_unlock (&cache_lock);
 
-static int rrd_write_cache_entry (const char *filename, rrd_cache_t *rc)
-{
-       char **argv;
-       int    argc;
+               /* Write the values to the RRD-file */
+               rrd_write_to_file (queue_entry->filename, values, values_num);
 
-       char *fn;
-       int status;
+               for (i = 0; i < values_num; i++)
+               {
+                       sfree (values[i]);
+               }
+               sfree (values);
+               sfree (queue_entry->filename);
+               sfree (queue_entry);
+       } /* while (42) */
 
-       int i;
+       pthread_mutex_lock (&cache_lock);
+       avl_destroy (cache);
+       cache = NULL;
+       pthread_mutex_unlock (&cache_lock);
 
-       if (rc->values_num < 1)
-               return (0);
+       pthread_exit ((void *) 0);
+       return ((void *) 0);
+} /* void *rrd_queue_thread */
 
-       argc = rc->values_num + 2;
-       argv = (char **) malloc ((argc + 1) * sizeof (char *));
-       if (argv == NULL)
+static int rrd_queue_cache_entry (const char *filename)
+{
+       rrd_queue_t *queue_entry;
+
+       queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
+       if (queue_entry == NULL)
                return (-1);
 
-       fn = strdup (filename);
-       if (fn == NULL)
+       queue_entry->filename = strdup (filename);
+       if (queue_entry->filename == NULL)
        {
-               free (argv);
+               free (queue_entry);
                return (-1);
        }
 
-       argv[0] = "update";
-       argv[1] = fn;
-       memcpy (argv + 2, rc->values, rc->values_num * sizeof (char *));
-       argv[argc] = NULL;
-
-       DEBUG ("rrd_update (argc = %i, argv = %p)", argc, (void *) argv);
+       queue_entry->next = NULL;
 
-       optind = 0; /* bug in librrd? */
-       rrd_clear_error ();
-       status = rrd_update (argc, argv);
-       if (status != 0)
-       {
-               WARNING ("rrd_update failed: %s: %s",
-                               filename, rrd_get_error ());
-               status = -1;
-       }
+       pthread_mutex_lock (&queue_lock);
+       if (queue_tail == NULL)
+               queue_head = queue_entry;
+       else
+               queue_tail->next = queue_entry;
+       queue_tail = queue_entry;
+       pthread_cond_signal (&queue_cond);
+       pthread_mutex_unlock (&queue_lock);
 
-       free (argv);
-       free (fn);
-       /* Free the value list of `rc' */
-       for (i = 0; i < rc->values_num; i++)
-               free (rc->values[i]);
-       free (rc->values);
-       rc->values = NULL;
-       rc->values_num = 0;
+       DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
 
-       return (status);
-} /* int rrd_write_cache_entry */
+       return (0);
+} /* int rrd_queue_cache_entry */
 
 static void rrd_cache_flush (int timeout)
 {
@@ -579,9 +632,6 @@ static void rrd_cache_flush (int timeout)
        avl_iterator_t *iter;
        int i;
 
-       if (cache == NULL)
-               return;
-
        DEBUG ("Flushing cache, timeout = %i", timeout);
 
        now = time (NULL);
@@ -591,7 +641,17 @@ static void rrd_cache_flush (int timeout)
        while (avl_iterator_next (iter, (void *) &key, (void *) &rc) == 0)
        {
                DEBUG ("key = %s; age = %i;", key, now - rc->first_value);
-               if ((now - rc->first_value) >= timeout)
+
+               if (rc->flags == FLAG_QUEUED)
+                       continue;
+               else if ((now - rc->first_value) < timeout)
+                       continue;
+               else if (rc->values_num > 0)
+               {
+                       if (rrd_queue_cache_entry (key) == 0)
+                               rc->flags = FLAG_QUEUED;
+               }
+               else /* ancient and no values -> waste of memory */
                {
                        keys = (char **) realloc ((void *) keys,
                                        (keys_num + 1) * sizeof (char *));
@@ -619,8 +679,9 @@ static void rrd_cache_flush (int timeout)
                        continue;
                }
 
-               rrd_write_cache_entry (keys[i], rc);
-               /* rc's value-list is free's by `rrd_write_cache_entry' */
+               assert (rc->values == NULL);
+               assert (rc->values_num == 0);
+
                sfree (rc);
                sfree (key);
                keys[i] = NULL;
@@ -632,13 +693,125 @@ static void rrd_cache_flush (int timeout)
        cache_flush_last = now;
 } /* void rrd_cache_flush */
 
+static int rrd_cache_insert (const char *filename,
+               const char *value, time_t value_time)
+{
+       rrd_cache_t *rc = NULL;
+       int new_rc = 0;
+       char **values_new;
+
+       pthread_mutex_lock (&cache_lock);
+
+       avl_get (cache, filename, (void *) &rc);
+
+       if (rc == NULL)
+       {
+               rc = (rrd_cache_t *) malloc (sizeof (rrd_cache_t));
+               if (rc == NULL)
+                       return (-1);
+               rc->values_num = 0;
+               rc->values = NULL;
+               rc->first_value = 0;
+               rc->last_value = 0;
+               rc->flags = FLAG_NONE;
+               new_rc = 1;
+       }
+
+       if (rc->last_value >= value_time)
+       {
+               pthread_mutex_unlock (&cache_lock);
+               WARNING ("rrdtool plugin: (rc->last_value = %u) >= (value_time = %u)",
+                               (unsigned int) rc->last_value,
+                               (unsigned int) value_time);
+               return (-1);
+       }
+
+       values_new = (char **) realloc ((void *) rc->values,
+                       (rc->values_num + 1) * sizeof (char *));
+       if (values_new == NULL)
+       {
+               char errbuf[1024];
+               void *cache_key = NULL;
+
+               sstrerror (errno, errbuf, sizeof (errbuf));
+
+               avl_remove (cache, filename, &cache_key, NULL);
+               pthread_mutex_unlock (&cache_lock);
+
+               ERROR ("rrdtool plugin: realloc failed: %s", errbuf);
+
+               sfree (cache_key);
+               sfree (rc->values);
+               sfree (rc);
+               return (-1);
+       }
+       rc->values = values_new;
+
+       rc->values[rc->values_num] = strdup (value);
+       if (rc->values[rc->values_num] != NULL)
+               rc->values_num++;
+
+       if (rc->values_num == 1)
+               rc->first_value = value_time;
+       rc->last_value = value_time;
+
+       /* Insert if this is the first value */
+       if (new_rc == 1)
+       {
+               void *cache_key = strdup (filename);
+
+               if (cache_key == NULL)
+               {
+                       char errbuf[1024];
+                       sstrerror (errno, errbuf, sizeof (errbuf));
+
+                       pthread_mutex_unlock (&cache_lock);
+
+                       ERROR ("rrdtool plugin: strdup failed: %s", errbuf);
+
+                       sfree (rc->values[0]);
+                       sfree (rc->values);
+                       sfree (rc);
+                       return (-1);
+               }
+
+               avl_insert (cache, cache_key, rc);
+       }
+
+       DEBUG ("rrd_cache_insert (%s, %s, %u) = %p", filename, value,
+                       (unsigned int) value_time, (void *) rc);
+
+       if ((rc->last_value - rc->first_value) >= cache_timeout)
+       {
+               /* XXX: If you need to lock both, cache_lock and queue_lock, at
+                * the same time, ALWAYS lock `cache_lock' first! */
+               if (rc->flags != FLAG_QUEUED)
+               {
+                       if (rrd_queue_cache_entry (filename) == 0)
+                               rc->flags = FLAG_QUEUED;
+               }
+               else
+               {
+                       DEBUG ("rrdtool plugin: `%s' is already queued.", filename);
+               }
+       }
+
+       if ((cache_timeout > 0) &&
+                       ((time (NULL) - cache_flush_last) > cache_flush_timeout))
+               rrd_cache_flush (cache_flush_timeout);
+
+
+       pthread_mutex_unlock (&cache_lock);
+
+       return (0);
+} /* int rrd_cache_insert */
+
 static int rrd_write (const data_set_t *ds, const value_list_t *vl)
 {
        struct stat  statbuf;
        char         filename[512];
        char         values[512];
-       rrd_cache_t *rc;
-       time_t       now;
+       int          status;
 
        if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
                return (-1);
@@ -650,7 +823,7 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl)
        {
                if (errno == ENOENT)
                {
-                       if (rrd_create_file (filename, ds))
+                       if (rrd_create_file (filename, ds, vl))
                                return (-1);
                }
                else
@@ -669,37 +842,9 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl)
                return (-1);
        }
 
-       pthread_mutex_lock (&cache_lock);
-       rc = rrd_cache_insert (filename, values, vl->time);
-       if (rc == NULL)
-       {
-               pthread_mutex_unlock (&cache_lock);
-               return (-1);
-       }
-
-       if (cache == NULL)
-       {
-               rrd_write_cache_entry (filename, rc);
-               /* rc's value-list is free's by `rrd_write_cache_entry' */
-               sfree (rc);
-               pthread_mutex_unlock (&cache_lock);
-               return (0);
-       }
-
-       now = time (NULL);
-
-       DEBUG ("age (%s) = %i", filename, now - rc->first_value);
+       status = rrd_cache_insert (filename, values, vl->time);
 
-       /* `rc' is not free'd here, because we'll likely reuse it. If not, then
-        * the next flush will remove this entry.  */
-       if ((now - rc->first_value) >= cache_timeout)
-               rrd_write_cache_entry (filename, rc);
-
-       if ((now - cache_flush_last) >= cache_flush_timeout)
-               rrd_cache_flush (cache_flush_timeout);
-
-       pthread_mutex_unlock (&cache_lock);
-       return (0);
+       return (status);
 } /* int rrd_write */
 
 static int rrd_config (const char *key, const char *value)
@@ -748,25 +893,15 @@ static int rrd_config (const char *key, const char *value)
        }
        else if (strcasecmp ("StepSize", key) == 0)
        {
-               int tmp = atoi (value);
-               if (tmp <= 0)
-               {
-                       fprintf (stderr, "rrdtool: `StepSize' must "
-                                       "be greater than 0.\n");
-                       return (1);
-               }
-               stepsize = tmp;
+               stepsize = atoi (value);
+               if (stepsize < 0)
+                       stepsize = 0;
        }
        else if (strcasecmp ("HeartBeat", key) == 0)
        {
-               int tmp = atoi (value);
-               if (tmp <= 0)
-               {
-                       fprintf (stderr, "rrdtool: `HeartBeat' must "
-                                       "be greater than 0.\n");
-                       return (1);
-               }
-               heartbeat = tmp;
+               heartbeat = atoi (value);
+               if (heartbeat < 0)
+                       heartbeat = 0;
        }
        else if (strcasecmp ("RRARows", key) == 0)
        {
@@ -833,47 +968,67 @@ static int rrd_shutdown (void)
 {
        pthread_mutex_lock (&cache_lock);
        rrd_cache_flush (-1);
-       if (cache != NULL)
-               avl_destroy (cache);
-       cache = NULL;
        pthread_mutex_unlock (&cache_lock);
 
+       pthread_mutex_lock (&queue_lock);
+       do_shutdown = 1;
+       pthread_cond_signal (&queue_cond);
+       pthread_mutex_unlock (&queue_lock);
+
        return (0);
 } /* int rrd_shutdown */
 
 static int rrd_init (void)
 {
-       if (stepsize <= 0)
-               stepsize = interval_g;
+       int status;
+
+       if (stepsize < 0)
+               stepsize = 0;
        if (heartbeat <= 0)
-               heartbeat = 2 * interval_g;
+       {
+               if (stepsize > 0)
+                       heartbeat = 2 * stepsize;
+               else
+                       heartbeat = 0;
+       }
 
-       if (heartbeat < interval_g)
+       if ((heartbeat > 0) && (heartbeat < interval_g))
                WARNING ("rrdtool plugin: Your `heartbeat' is "
                                "smaller than your `interval'. This will "
                                "likely cause problems.");
-       else if (stepsize < interval_g)
+       else if ((stepsize > 0) && (stepsize < interval_g))
                WARNING ("rrdtool plugin: Your `stepsize' is "
                                "smaller than your `interval'. This will "
                                "create needlessly big RRD-files.");
 
+       /* Set the cache up */
        pthread_mutex_lock (&cache_lock);
+
+       cache = avl_create ((int (*) (const void *, const void *)) strcmp);
+       if (cache == NULL)
+       {
+               ERROR ("rrdtool plugin: avl_create failed.");
+               return (-1);
+       }
+
+       cache_flush_last = time (NULL);
        if (cache_timeout < 2)
        {
                cache_timeout = 0;
                cache_flush_timeout = 0;
        }
-       else
-       {
-               if (cache_flush_timeout < cache_timeout)
-                       cache_flush_timeout = 10 * cache_timeout;
+       else if (cache_flush_timeout < cache_timeout)
+               cache_flush_timeout = 10 * cache_timeout;
 
-               cache = avl_create ((int (*) (const void *, const void *)) strcmp);
-               cache_flush_last = time (NULL);
-               plugin_register_shutdown ("rrdtool", rrd_shutdown);
-       }
        pthread_mutex_unlock (&cache_lock);
 
+       status = pthread_create (&queue_thread, NULL, rrd_queue_thread, NULL);
+       if (status != 0)
+       {
+               ERROR ("rrdtool plugin: Cannot create queue-thread.");
+               return (-1);
+       }
+
        DEBUG ("rrdtool plugin: rrd_init: datadir = %s; stepsize = %i;"
                        " heartbeat = %i; rrarows = %i; xff = %lf;",
                        (datadir == NULL) ? "(null)" : datadir,
@@ -888,4 +1043,5 @@ void module_register (void)
                        config_keys, config_keys_num);
        plugin_register_init ("rrdtool", rrd_init);
        plugin_register_write ("rrdtool", rrd_write);
+       plugin_register_shutdown ("rrdtool", rrd_shutdown);
 }
diff --git a/src/snmp.c b/src/snmp.c
new file mode 100644 (file)
index 0000000..2c4c930
--- /dev/null
@@ -0,0 +1,1314 @@
+/**
+ * collectd - src/snmp.c
+ * Copyright (C) 2007  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 <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#include <pthread.h>
+
+#include <net-snmp/net-snmp-config.h>
+#include <net-snmp/net-snmp-includes.h>
+
+/*
+ * Private data structes
+ */
+struct oid_s
+{
+  oid oid[MAX_OID_LEN];
+  size_t oid_len;
+};
+typedef struct oid_s oid_t;
+
+union instance_u
+{
+  char  string[DATA_MAX_NAME_LEN];
+  oid_t oid;
+};
+typedef union instance_u instance_t;
+
+struct data_definition_s
+{
+  char *name; /* used to reference this from the `Collect' option */
+  char *type; /* used to find the data_set */
+  int is_table;
+  instance_t instance;
+  oid_t *values;
+  int values_len;
+  struct data_definition_s *next;
+};
+typedef struct data_definition_s data_definition_t;
+
+struct host_definition_s
+{
+  char *name;
+  char *address;
+  char *community;
+  int version;
+  void *sess_handle;
+  int16_t skip_num;
+  int16_t skip_left;
+  data_definition_t **data_list;
+  int data_list_len;
+  enum          /****************************************************/
+  {             /* This host..                                      */
+    STATE_IDLE, /* - just sits there until `skip_left < interval_g' */
+    STATE_WAIT, /* - waits to be queried.                           */
+    STATE_BUSY  /* - is currently being queried.                    */
+  } state;      /****************************************************/
+  struct host_definition_s *next;
+};
+typedef struct host_definition_s host_definition_t;
+
+/* These two types are used to cache values in `csnmp_read_table' to handle
+ * gaps in tables. */
+struct csnmp_list_instances_s
+{
+  oid subid;
+  char instance[DATA_MAX_NAME_LEN];
+  struct csnmp_list_instances_s *next;
+};
+typedef struct csnmp_list_instances_s csnmp_list_instances_t;
+
+struct csnmp_table_values_s
+{
+  oid subid;
+  value_t value;
+  struct csnmp_table_values_s *next;
+};
+typedef struct csnmp_table_values_s csnmp_table_values_t;
+
+/*
+ * Private variables
+ */
+static int do_shutdown = 0;
+
+pthread_t *threads = NULL;
+int threads_num = 0;
+
+static data_definition_t *data_head = NULL;
+static host_definition_t *host_head = NULL;
+
+static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t  host_cond = PTHREAD_COND_INITIALIZER;
+
+/*
+ * Private functions
+ */
+/* First there are many functions which do configuration stuff. It's a big
+ * bloated and messy, I'm afraid. */
+
+/*
+ * Callgraph for the config stuff:
+ *  csnmp_config
+ *  +-> call_snmp_init_once
+ *  +-> csnmp_config_add_data
+ *  !   +-> csnmp_config_add_data_type
+ *  !   +-> csnmp_config_add_data_table
+ *  !   +-> csnmp_config_add_data_instance
+ *  !   +-> csnmp_config_add_data_values
+ *  +-> csnmp_config_add_host
+ *      +-> csnmp_config_add_host_address
+ *      +-> csnmp_config_add_host_community
+ *      +-> csnmp_config_add_host_version
+ *      +-> csnmp_config_add_host_collect
+ *      +-> csnmp_config_add_host_interval
+ */
+static void call_snmp_init_once (void)
+{
+  static int have_init = 0;
+
+  if (have_init == 0)
+    init_snmp (PACKAGE_NAME);
+  have_init = 1;
+} /* void call_snmp_init_once */
+
+static int csnmp_config_add_data_type (data_definition_t *dd, oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("snmp plugin: `Type' needs exactly one string argument.");
+    return (-1);
+  }
+
+  if (dd->type != NULL)
+    free (dd->type);
+
+  dd->type = strdup (ci->values[0].value.string);
+  if (dd->type == NULL)
+    return (-1);
+
+  return (0);
+} /* int csnmp_config_add_data_type */
+
+static int csnmp_config_add_data_table (data_definition_t *dd, oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+  {
+    WARNING ("snmp plugin: `Table' needs exactly one boolean argument.");
+    return (-1);
+  }
+
+  dd->is_table = ci->values[0].value.boolean ? 1 : 0;
+
+  return (0);
+} /* int csnmp_config_add_data_table */
+
+static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("snmp plugin: `Instance' needs exactly one string argument.");
+    return (-1);
+  }
+
+  if (dd->is_table)
+  {
+    /* Instance is an OID */
+    dd->instance.oid.oid_len = MAX_OID_LEN;
+
+    if (!read_objid (ci->values[0].value.string,
+         dd->instance.oid.oid, &dd->instance.oid.oid_len))
+    {
+      ERROR ("snmp plugin: read_objid (%s) failed.",
+         ci->values[0].value.string);
+      return (-1);
+    }
+  }
+  else
+  {
+    /* Instance is a simple string */
+    strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
+  }
+
+  return (0);
+} /* int csnmp_config_add_data_instance */
+
+static int csnmp_config_add_data_values (data_definition_t *dd, oconfig_item_t *ci)
+{
+  int i;
+
+  if (ci->values_num < 1)
+  {
+    WARNING ("snmp plugin: `Values' needs at least one argument.");
+    return (-1);
+  }
+
+  for (i = 0; i < ci->values_num; i++)
+    if (ci->values[i].type != OCONFIG_TYPE_STRING)
+    {
+      WARNING ("snmp plugin: `Values' needs only string argument.");
+      return (-1);
+    }
+
+  if (dd->values != NULL)
+    free (dd->values);
+  dd->values = (oid_t *) malloc (sizeof (oid_t) * ci->values_num);
+  if (dd->values == NULL)
+    return (-1);
+  dd->values_len = ci->values_num;
+
+  for (i = 0; i < ci->values_num; i++)
+  {
+    dd->values[i].oid_len = MAX_OID_LEN;
+
+    if (NULL == snmp_parse_oid (ci->values[i].value.string,
+         dd->values[i].oid, &dd->values[i].oid_len))
+    {
+      ERROR ("snmp plugin: snmp_parse_oid (%s) failed.",
+         ci->values[i].value.string);
+      free (dd->values);
+      dd->values = NULL;
+      dd->values_len = 0;
+      return (-1);
+    }
+  }
+
+  return (0);
+} /* int csnmp_config_add_data_instance */
+
+static int csnmp_config_add_data (oconfig_item_t *ci)
+{
+  data_definition_t *dd;
+  int status = 0;
+  int i;
+
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("snmp plugin: The `Data' config option needs exactly one string argument.");
+    return (-1);
+  }
+
+  dd = (data_definition_t *) malloc (sizeof (data_definition_t));
+  if (dd == NULL)
+    return (-1);
+  memset (dd, '\0', sizeof (data_definition_t));
+
+  dd->name = strdup (ci->values[0].value.string);
+  if (dd->name == NULL)
+  {
+    free (dd);
+    return (-1);
+  }
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *option = ci->children + i;
+    status = 0;
+
+    if (strcasecmp ("Type", option->key) == 0)
+      status = csnmp_config_add_data_type (dd, option);
+    else if (strcasecmp ("Table", option->key) == 0)
+      status = csnmp_config_add_data_table (dd, option);
+    else if (strcasecmp ("Instance", option->key) == 0)
+      status = csnmp_config_add_data_instance (dd, option);
+    else if (strcasecmp ("Values", option->key) == 0)
+      status = csnmp_config_add_data_values (dd, option);
+    else
+    {
+      WARNING ("snmp plugin: Option `%s' not allowed here.", option->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (ci->children) */
+
+  while (status == 0)
+  {
+    if (dd->type == NULL)
+    {
+      WARNING ("snmp plugin: `Type' not given for data `%s'", dd->name);
+      status = -1;
+      break;
+    }
+    if (dd->values == NULL)
+    {
+      WARNING ("snmp plugin: No `Value' given for data `%s'", dd->name);
+      status = -1;
+      break;
+    }
+
+    break;
+  } /* while (status == 0) */
+
+  if (status != 0)
+  {
+    sfree (dd->name);
+    sfree (dd->values);
+    sfree (dd);
+    return (-1);
+  }
+
+  DEBUG ("snmp plugin: dd = { name = %s, type = %s, is_table = %s, values_len = %i }",
+      dd->name, dd->type, (dd->is_table != 0) ? "true" : "false", dd->values_len);
+
+  if (data_head == NULL)
+    data_head = dd;
+  else
+  {
+    data_definition_t *last;
+    last = data_head;
+    while (last->next != NULL)
+      last = last->next;
+    last->next = dd;
+  }
+
+  return (0);
+} /* int csnmp_config_add_data */
+
+static int csnmp_config_add_host_address (host_definition_t *hd, oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("snmp plugin: The `Address' config option needs exactly one string argument.");
+    return (-1);
+  }
+
+  if (hd->address == NULL)
+    free (hd->address);
+
+  hd->address = strdup (ci->values[0].value.string);
+  if (hd->address == NULL)
+    return (-1);
+
+  DEBUG ("snmp plugin: host = %s; host->address = %s;",
+      hd->name, hd->address);
+
+  return (0);
+} /* int csnmp_config_add_host_address */
+
+static int csnmp_config_add_host_community (host_definition_t *hd, oconfig_item_t *ci)
+{
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("snmp plugin: The `Community' config option needs exactly one string argument.");
+    return (-1);
+  }
+
+  if (hd->community == NULL)
+    free (hd->community);
+
+  hd->community = strdup (ci->values[0].value.string);
+  if (hd->community == NULL)
+    return (-1);
+
+  DEBUG ("snmp plugin: host = %s; host->community = %s;",
+      hd->name, hd->community);
+
+  return (0);
+} /* int csnmp_config_add_host_community */
+
+static int csnmp_config_add_host_version (host_definition_t *hd, oconfig_item_t *ci)
+{
+  int version;
+
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
+  {
+    WARNING ("snmp plugin: The `Version' config option needs exactly one number argument.");
+    return (-1);
+  }
+
+  version = (int) ci->values[0].value.number;
+  if ((version != 1) && (version != 2))
+  {
+    WARNING ("snmp plugin: `Version' must either be `1' or `2'.");
+    return (-1);
+  }
+
+  hd->version = version;
+
+  return (0);
+} /* int csnmp_config_add_host_address */
+
+static int csnmp_config_add_host_collect (host_definition_t *host,
+    oconfig_item_t *ci)
+{
+  data_definition_t *data;
+  data_definition_t **data_list;
+  int data_list_len;
+  int i;
+
+  if (ci->values_num < 1)
+  {
+    WARNING ("snmp plugin: `Collect' needs at least one argument.");
+    return (-1);
+  }
+
+  for (i = 0; i < ci->values_num; i++)
+    if (ci->values[i].type != OCONFIG_TYPE_STRING)
+    {
+      WARNING ("snmp plugin: All arguments to `Collect' must be strings.");
+      return (-1);
+    }
+
+  data_list_len = host->data_list_len + ci->values_num;
+  data_list = (data_definition_t **) realloc (host->data_list,
+      sizeof (data_definition_t *) * data_list_len);
+  if (data_list == NULL)
+    return (-1);
+  host->data_list = data_list;
+
+  for (i = 0; i < ci->values_num; i++)
+  {
+    for (data = data_head; data != NULL; data = data->next)
+      if (strcasecmp (ci->values[i].value.string, data->name) == 0)
+       break;
+
+    if (data == NULL)
+    {
+      WARNING ("snmp plugin: No such data configured: `%s'",
+         ci->values[i].value.string);
+      continue;
+    }
+
+    DEBUG ("snmp plugin: Collect: host = %s, data[%i] = %s;",
+       host->name, host->data_list_len, data->name);
+
+    host->data_list[host->data_list_len] = data;
+    host->data_list_len++;
+  } /* for (values_num) */
+
+  return (0);
+} /* int csnmp_config_add_host_collect */
+
+static int csnmp_config_add_host_interval (host_definition_t *hd, oconfig_item_t *ci)
+{
+  int interval;
+
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
+  {
+    WARNING ("snmp plugin: The `Interval' config option needs exactly one number argument.");
+    return (-1);
+  }
+
+  interval = (int) ci->values[0].value.number;
+  hd->skip_num = interval;
+  if (hd->skip_num < 0)
+    hd->skip_num = 0;
+
+  return (0);
+} /* int csnmp_config_add_host_interval */
+
+static int csnmp_config_add_host (oconfig_item_t *ci)
+{
+  host_definition_t *hd;
+  int status = 0;
+  int i;
+
+  if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("snmp plugin: `Host' needs exactly one string argument.");
+    return (-1);
+  }
+
+  hd = (host_definition_t *) malloc (sizeof (host_definition_t));
+  if (hd == NULL)
+    return (-1);
+  memset (hd, '\0', sizeof (host_definition_t));
+  hd->version = 2;
+
+  hd->name = strdup (ci->values[0].value.string);
+  if (hd->name == NULL)
+  {
+    free (hd);
+    return (-1);
+  }
+
+  hd->sess_handle = NULL;
+  hd->skip_num = 0;
+  hd->skip_left = 0;
+  hd->state = STATE_IDLE;
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *option = ci->children + i;
+    status = 0;
+
+    if (strcasecmp ("Address", option->key) == 0)
+      status = csnmp_config_add_host_address (hd, option);
+    else if (strcasecmp ("Community", option->key) == 0)
+      status = csnmp_config_add_host_community (hd, option);
+    else if (strcasecmp ("Version", option->key) == 0)
+      status = csnmp_config_add_host_version (hd, option);
+    else if (strcasecmp ("Collect", option->key) == 0)
+      csnmp_config_add_host_collect (hd, option);
+    else if (strcasecmp ("Interval", option->key) == 0)
+      csnmp_config_add_host_interval (hd, option);
+    else
+    {
+      WARNING ("snmp plugin: csnmp_config_add_host: Option `%s' not allowed here.", option->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (ci->children) */
+
+  while (status == 0)
+  {
+    if (hd->address == NULL)
+    {
+      WARNING ("snmp plugin: `Address' not given for host `%s'", hd->name);
+      status = -1;
+      break;
+    }
+    if (hd->community == NULL)
+    {
+      WARNING ("snmp plugin: `Community' not given for host `%s'", hd->name);
+      status = -1;
+      break;
+    }
+
+    break;
+  } /* while (status == 0) */
+
+  if (status != 0)
+  {
+    sfree (hd->name);
+    sfree (hd);
+    return (-1);
+  }
+
+  DEBUG ("snmp plugin: hd = { name = %s, address = %s, community = %s, version = %i }",
+      hd->name, hd->address, hd->community, hd->version);
+
+  if (host_head == NULL)
+    host_head = hd;
+  else
+  {
+    host_definition_t *last;
+    last = host_head;
+    while (last->next != NULL)
+      last = last->next;
+    last->next = hd;
+  }
+
+  return (0);
+} /* int csnmp_config_add_host */
+
+static int csnmp_config (oconfig_item_t *ci)
+{
+  int i;
+
+  call_snmp_init_once ();
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+    if (strcasecmp ("Data", child->key) == 0)
+      csnmp_config_add_data (child);
+    else if (strcasecmp ("Host", child->key) == 0)
+      csnmp_config_add_host (child);
+    else
+    {
+      WARNING ("snmp plugin: Ignoring unknown config option `%s'.", child->key);
+    }
+  } /* for (ci->children) */
+
+  return (0);
+} /* int csnmp_config */
+
+/* End of the config stuff. Now the interesting part begins */
+
+static void csnmp_host_close_session (host_definition_t *host)
+{
+  int status;
+
+  if (host->sess_handle == NULL)
+    return;
+
+  status = snmp_sess_close (host->sess_handle);
+
+  if (status != 0)
+  {
+    char *errstr = NULL;
+
+    snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
+
+    ERROR ("snmp plugin: snmp_sess_close failed: %s",
+       (errstr == NULL) ? "Unknown problem" : errstr);
+    sfree (errstr);
+  }
+
+  host->sess_handle = NULL;
+} /* void csnmp_host_close_session */
+
+static void csnmp_host_open_session (host_definition_t *host)
+{
+  struct snmp_session sess;
+
+  if (host->sess_handle != NULL)
+    csnmp_host_close_session (host);
+
+  snmp_sess_init (&sess);
+  sess.peername = host->address;
+  sess.community = (u_char *) host->community;
+  sess.community_len = strlen (host->community);
+  sess.version = (host->version == 1) ? SNMP_VERSION_1 : SNMP_VERSION_2c;
+
+  /* snmp_sess_open will copy the `struct snmp_session *'. */
+  host->sess_handle = snmp_sess_open (&sess);
+
+  if (host->sess_handle == NULL)
+  {
+    char *errstr = NULL;
+
+    snmp_error (&sess, NULL, NULL, &errstr);
+
+    ERROR ("snmp plugin: snmp_sess_open failed: %s",
+       (errstr == NULL) ? "Unknown problem" : errstr);
+    sfree (errstr);
+  }
+} /* void csnmp_host_open_session */
+
+static value_t csnmp_value_list_to_value (struct variable_list *vl, int type)
+{
+  value_t ret;
+  uint64_t temp = 0;
+  int defined = 1;
+
+  if ((vl->type == ASN_INTEGER)
+      || (vl->type == ASN_UINTEGER)
+      || (vl->type == ASN_COUNTER)
+      || (vl->type == ASN_GAUGE))
+  {
+    temp = (uint32_t) *vl->val.integer;
+    DEBUG ("snmp plugin: Parsed int32 value is %llu.", temp);
+  }
+  else if (vl->type == ASN_COUNTER64)
+  {
+    temp = (uint32_t) vl->val.counter64->high;
+    temp = temp << 32;
+    temp += (uint32_t) vl->val.counter64->low;
+    DEBUG ("snmp plugin: Parsed int64 value is %llu.", temp);
+  }
+  else
+  {
+    WARNING ("snmp plugin: I don't know the ASN type `%i'", (int) vl->type);
+    defined = 0;
+  }
+
+  if (type == DS_TYPE_COUNTER)
+  {
+    ret.counter = temp;
+  }
+  else if (type == DS_TYPE_GAUGE)
+  {
+    ret.gauge = NAN;
+    if (defined != 0)
+      ret.gauge = temp;
+  }
+
+  return (ret);
+} /* value_t csnmp_value_list_to_value */
+
+static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *data,
+    csnmp_list_instances_t *instance_list,
+    csnmp_table_values_t **value_table)
+{
+  const data_set_t *ds;
+  value_list_t vl = VALUE_LIST_INIT;
+
+  csnmp_list_instances_t *instance_list_ptr;
+  csnmp_table_values_t **value_table_ptr;
+
+  int i;
+
+  ds = plugin_get_ds (data->type);
+  if (!ds)
+  {
+    ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
+    return (-1);
+  }
+  assert (ds->ds_num == data->values_len);
+
+  value_table_ptr = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
+      * data->values_len);
+  if (value_table_ptr == NULL)
+    return (-1);
+  for (i = 0; i < data->values_len; i++)
+    value_table_ptr[i] = value_table[i];
+
+  vl.values_len = ds->ds_num;
+  vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
+  if (vl.values == NULL)
+  {
+    sfree (value_table_ptr);
+    return (-1);
+  }
+
+  strncpy (vl.host, host->name, sizeof (vl.host));
+  vl.host[sizeof (vl.host) - 1] = '\0';
+  strcpy (vl.plugin, "snmp");
+
+  vl.interval = host->skip_num;
+  vl.time = time (NULL);
+
+  for (instance_list_ptr = instance_list;
+      instance_list_ptr != NULL;
+      instance_list_ptr = instance_list_ptr->next)
+  {
+    strncpy (vl.type_instance, instance_list_ptr->instance, sizeof (vl.type_instance));
+    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+
+    for (i = 0; i < data->values_len; i++)
+    {
+      while ((value_table_ptr[i] != NULL)
+         && (value_table_ptr[i]->subid < instance_list_ptr->subid))
+       value_table_ptr[i] = value_table_ptr[i]->next;
+      if ((value_table_ptr[i] == NULL)
+         || (value_table_ptr[i]->subid != instance_list_ptr->subid))
+       break;
+      vl.values[i] = value_table_ptr[i]->value;
+    } /* for (data->values_len) */
+
+    /* If the for-loop was aborted early, not all subid's match. */
+    if (i < data->values_len)
+    {
+      DEBUG ("snmp plugin: host = %s; data = %s; i = %i; "
+         "Skipping SUBID %i",
+         host->name, data->name, i, instance_list_ptr->subid);
+      continue;
+    }
+
+    /* If we get here `vl.type_instance' and all `vl.values' have been set */
+    plugin_dispatch_values (data->type, &vl);
+  } /* for (instance_list) */
+
+  sfree (vl.values);
+  sfree (value_table_ptr);
+
+  return (0);
+} /* int csnmp_dispatch_table */
+
+static int csnmp_read_table (host_definition_t *host, data_definition_t *data)
+{
+  struct snmp_pdu *req;
+  struct snmp_pdu *res;
+  struct variable_list *vb;
+
+  const data_set_t *ds;
+  oid_t *oid_list;
+  uint32_t oid_list_len;
+
+  int status;
+  int i;
+
+  /* `value_table' and `value_table_ptr' implement a linked list for each
+   * value. `instance_list' and `instance_list_ptr' implement a linked list of
+   * instance names. This is used to jump gaps in the table. */
+  csnmp_list_instances_t *instance_list;
+  csnmp_list_instances_t *instance_list_ptr;
+  csnmp_table_values_t **value_table;
+  csnmp_table_values_t **value_table_ptr;
+
+  DEBUG ("snmp plugin: csnmp_read_table (host = %s, data = %s)",
+      host->name, data->name);
+
+  ds = plugin_get_ds (data->type);
+  if (!ds)
+  {
+    ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
+    return (-1);
+  }
+
+  if (ds->ds_num != data->values_len)
+  {
+    ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
+       data->type, ds->ds_num, data->values_len);
+    return (-1);
+  }
+
+  /* We need a copy of all the OIDs, because GETNEXT will destroy them. */
+  oid_list_len = data->values_len + 1;
+  oid_list = (oid_t *) malloc (sizeof (oid_t) * (oid_list_len));
+  if (oid_list == NULL)
+    return (-1);
+  memcpy (oid_list, &data->instance.oid, sizeof (oid_t));
+  for (i = 0; i < data->values_len; i++)
+    memcpy (oid_list + (i + 1), data->values + i, sizeof (oid_t));
+
+  /* Allocate the `value_table' */
+  value_table = (csnmp_table_values_t **) malloc (sizeof (csnmp_table_values_t *)
+      * 2 * data->values_len);
+  if (value_table == NULL)
+  {
+    sfree (oid_list);
+    return (-1);
+  }
+  memset (value_table, '\0', sizeof (csnmp_table_values_t *) * 2 * data->values_len);
+  value_table_ptr = value_table + data->values_len;
+  
+  instance_list = NULL;
+  instance_list_ptr = NULL;
+
+  status = 0;
+  while (status == 0)
+  {
+    csnmp_list_instances_t *il;
+
+    req = snmp_pdu_create (SNMP_MSG_GETNEXT);
+    if (req == NULL)
+    {
+      ERROR ("snmp plugin: snmp_pdu_create failed.");
+      status = -1;
+      break;
+    }
+
+    for (i = 0; i < oid_list_len; i++)
+      snmp_add_null_var (req, oid_list[i].oid, oid_list[i].oid_len);
+
+    status = snmp_sess_synch_response (host->sess_handle, req, &res);
+
+    if (status != STAT_SUCCESS)
+    {
+      char *errstr = NULL;
+
+      snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
+      ERROR ("snmp plugin: snmp_sess_synch_response failed: %s",
+         (errstr == NULL) ? "Unknown problem" : errstr);
+      csnmp_host_close_session (host);
+
+      status = -1;
+      break;
+    }
+    status = 0;
+    assert (res != NULL);
+
+    vb = res->variables;
+    if (vb == NULL)
+    {
+      status = -1;
+      break;
+    }
+
+    /* Check if we left the subtree */
+    if (snmp_oid_ncompare (data->instance.oid.oid, data->instance.oid.oid_len,
+         vb->name, vb->name_length,
+         data->instance.oid.oid_len) != 0)
+      break;
+
+    /* Allocate a new `csnmp_list_instances_t', insert the instance name and
+     * add it to the list */
+    il = (csnmp_list_instances_t *) malloc (sizeof (csnmp_list_instances_t));
+    if (il == NULL)
+    {
+      status = -1;
+      break;
+    }
+    il->subid = vb->name[vb->name_length - 1];
+    il->next = NULL;
+
+    /* Get instance name */
+    if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
+    {
+      char *ptr;
+      size_t instance_len;
+
+      instance_len = sizeof (il->instance) - 1;
+      if (instance_len > vb->val_len)
+       instance_len = vb->val_len;
+
+      strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
+           ? vb->val.string
+           : vb->val.bitstring),
+         instance_len);
+      il->instance[instance_len] = '\0';
+
+      for (ptr = il->instance; *ptr != '\0'; ptr++)
+      {
+       if ((*ptr > 0) && (*ptr < 32))
+         *ptr = ' ';
+       else if (*ptr == '/')
+         *ptr = '_';
+      }
+      DEBUG ("snmp plugin: il->instance = `%s';", il->instance);
+    }
+    else
+    {
+      value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER);
+      snprintf (il->instance, sizeof (il->instance),
+         "%llu", val.counter);
+    }
+    il->instance[sizeof (il->instance) - 1] = '\0';
+    DEBUG ("snmp plugin: data = `%s'; il->instance = `%s';",
+       data->name, il->instance);
+
+    if (instance_list_ptr == NULL)
+      instance_list = il;
+    else
+      instance_list_ptr->next = il;
+    instance_list_ptr = il;
+
+    /* Copy OID to oid_list[0] */
+    memcpy (oid_list[0].oid, vb->name, sizeof (oid) * vb->name_length);
+    oid_list[0].oid_len = vb->name_length;
+
+    for (i = 0; i < data->values_len; i++)
+    {
+      csnmp_table_values_t *vt;
+
+      vb = vb->next_variable;
+      if (vb == NULL)
+      {
+       status = -1;
+       break;
+      }
+
+      /* Check if we left the subtree */
+      if (snmp_oid_ncompare (data->values[i].oid,
+           data->values[i].oid_len,
+           vb->name, vb->name_length,
+           data->values[i].oid_len) != 0)
+      {
+       DEBUG ("snmp plugin: host = %s; data = %s; Value %i left its subtree.",
+           host->name, data->name, i);
+       continue;
+      }
+
+      if ((value_table_ptr[i] != NULL)
+         && (vb->name[vb->name_length - 1] <= value_table_ptr[i]->subid))
+      {
+       DEBUG ("snmp plugin: host = %s; data = %s; i = %i; SUBID is not increasing.",
+           host->name, data->name, i);
+       continue;
+      }
+
+      vt = (csnmp_table_values_t *) malloc (sizeof (csnmp_table_values_t));
+      if (vt != NULL)
+      {
+       vt->subid = vb->name[vb->name_length - 1];
+       vt->value = csnmp_value_list_to_value (vb, ds->ds[i].type);
+       vt->next = NULL;
+
+       if (value_table_ptr[i] == NULL)
+         value_table[i] = vt;
+       else
+         value_table_ptr[i]->next = vt;
+       value_table_ptr[i] = vt;
+      }
+
+      /* Copy OID to oid_list[i + 1] */
+      memcpy (oid_list[i + 1].oid, vb->name, sizeof (oid) * vb->name_length);
+      oid_list[i + 1].oid_len = vb->name_length;
+    } /* for (i = data->values_len) */
+
+    if (res != NULL)
+      snmp_free_pdu (res);
+    res = NULL;
+  } /* while (status == 0) */
+
+  if (status == 0)
+    csnmp_dispatch_table (host, data, instance_list, value_table);
+
+  /* Free all allocated variables here */
+  while (instance_list != NULL)
+  {
+    instance_list_ptr = instance_list->next;
+    sfree (instance_list);
+    instance_list = instance_list_ptr;
+  }
+
+  for (i = 0; i < data->values_len; i++)
+  {
+    csnmp_table_values_t *tmp;
+    while (value_table[i] != NULL)
+    {
+      tmp = value_table[i]->next;
+      sfree (value_table[i]);
+      value_table[i] = tmp;
+    }
+  }
+
+  sfree (value_table);
+  sfree (oid_list);
+
+  return (0);
+} /* int csnmp_read_table */
+
+static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
+{
+  struct snmp_pdu *req;
+  struct snmp_pdu *res;
+  struct variable_list *vb;
+
+  const data_set_t *ds;
+  value_list_t vl = VALUE_LIST_INIT;
+
+  int status;
+  int i;
+
+  DEBUG ("snmp plugin: csnmp_read_value (host = %s, data = %s)",
+      host->name, data->name);
+
+  ds = plugin_get_ds (data->type);
+  if (!ds)
+  {
+    ERROR ("snmp plugin: DataSet `%s' not defined.", data->type);
+    return (-1);
+  }
+
+  if (ds->ds_num != data->values_len)
+  {
+    ERROR ("snmp plugin: DataSet `%s' requires %i values, but config talks about %i",
+       data->type, ds->ds_num, data->values_len);
+    return (-1);
+  }
+
+  vl.values_len = ds->ds_num;
+  vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
+  if (vl.values == NULL)
+    return (-1);
+  for (i = 0; i < vl.values_len; i++)
+  {
+    if (ds->ds[i].type == DS_TYPE_COUNTER)
+      vl.values[i].counter = 0;
+    else
+      vl.values[i].gauge = NAN;
+  }
+
+  strncpy (vl.host, host->name, sizeof (vl.host));
+  vl.host[sizeof (vl.host) - 1] = '\0';
+  strcpy (vl.plugin, "snmp");
+  strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
+  vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+
+  vl.interval = host->skip_num;
+
+  req = snmp_pdu_create (SNMP_MSG_GET);
+  if (req == NULL)
+  {
+    ERROR ("snmp plugin: snmp_pdu_create failed.");
+    sfree (vl.values);
+    return (-1);
+  }
+
+  for (i = 0; i < data->values_len; i++)
+    snmp_add_null_var (req, data->values[i].oid, data->values[i].oid_len);
+  status = snmp_sess_synch_response (host->sess_handle, req, &res);
+
+  if (status != STAT_SUCCESS)
+  {
+    char *errstr = NULL;
+
+    snmp_sess_error (host->sess_handle, NULL, NULL, &errstr);
+    ERROR ("snmp plugin: snmp_sess_synch_response failed: %s",
+       (errstr == NULL) ? "Unknown problem" : errstr);
+    csnmp_host_close_session (host);
+    sfree (errstr);
+
+    return (-1);
+  }
+
+  vl.time = time (NULL);
+
+  for (vb = res->variables; vb != NULL; vb = vb->next_variable)
+  {
+    char buffer[1024];
+    snprint_variable (buffer, sizeof (buffer),
+       vb->name, vb->name_length, vb);
+    DEBUG ("snmp plugin: Got this variable: %s", buffer);
+
+    for (i = 0; i < data->values_len; i++)
+      if (snmp_oid_compare (data->values[i].oid, data->values[i].oid_len,
+           vb->name, vb->name_length) == 0)
+       vl.values[i] = csnmp_value_list_to_value (vb, ds->ds[i].type);
+  } /* for (res->variables) */
+
+  snmp_free_pdu (res);
+
+  DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
+  plugin_dispatch_values (data->type, &vl);
+  sfree (vl.values);
+
+  return (0);
+} /* int csnmp_read_value */
+
+static int csnmp_read_host (host_definition_t *host)
+{
+  int i;
+
+  DEBUG ("snmp plugin: csnmp_read_host (%s);", host->name);
+
+  if (host->sess_handle == NULL)
+    csnmp_host_open_session (host);
+
+  if (host->sess_handle == NULL)
+    return (-1);
+
+  for (i = 0; i < host->data_list_len; i++)
+  {
+    data_definition_t *data = host->data_list[i];
+
+    if (data->is_table)
+      csnmp_read_table (host, data);
+    else
+      csnmp_read_value (host, data);
+  }
+
+  return (0);
+} /* int csnmp_read_host */
+
+static void *csnmp_read_thread (void *data)
+{
+  host_definition_t *host;
+
+  pthread_mutex_lock (&host_lock);
+  while (do_shutdown == 0)
+  {
+    pthread_cond_wait (&host_cond, &host_lock);
+
+    for (host = host_head; host != NULL; host = host->next)
+    {
+      if (do_shutdown != 0)
+       break;
+      if (host->state != STATE_WAIT)
+       continue;
+
+      host->state = STATE_BUSY;
+      pthread_mutex_unlock (&host_lock);
+      csnmp_read_host (host);
+      pthread_mutex_lock (&host_lock);
+      host->state = STATE_IDLE;
+    } /* for (host) */
+  } /* while (do_shutdown == 0) */
+  pthread_mutex_unlock (&host_lock);
+
+  pthread_exit ((void *) 0);
+  return ((void *) 0);
+} /* void *csnmp_read_thread */
+
+static int csnmp_init (void)
+{
+  host_definition_t *host;
+  int i;
+
+  if (host_head == NULL)
+    return (-1);
+
+  call_snmp_init_once ();
+
+  threads_num = 0;
+  for (host = host_head; host != NULL; host = host->next)
+  {
+    threads_num++;
+    /* We need to initialize `skip_num' here, because `interval_g' isn't
+     * initialized during `configure'. */
+    host->skip_left = interval_g;
+    if (host->skip_num == 0)
+    {
+      host->skip_num = interval_g;
+    }
+    else if (host->skip_num < interval_g)
+    {
+      host->skip_num = interval_g;
+      WARNING ("snmp plugin: Data for host `%s' will be collected every %i seconds.",
+         host->name, host->skip_num);
+    }
+
+    csnmp_host_open_session (host);
+  } /* for (host) */
+
+  /* Now start the reading threads */
+  if (threads_num > 3)
+  {
+    threads_num = 3 + ((threads_num - 3) / 10);
+    if (threads_num > 10)
+      threads_num = 10;
+  }
+
+  threads = (pthread_t *) malloc (threads_num * sizeof (pthread_t));
+  if (threads == NULL)
+    return (-1);
+  memset (threads, '\0', threads_num * sizeof (pthread_t));
+
+  for (i = 0; i < threads_num; i++)
+      pthread_create (threads + i, NULL, csnmp_read_thread, (void *) 0);
+
+  return (0);
+} /* int csnmp_init */
+
+static int csnmp_read (void)
+{
+  host_definition_t *host;
+  time_t now;
+
+  if (host_head == NULL)
+  {
+    INFO ("snmp plugin: No hosts configured.");
+    return (-1);
+  }
+
+  now = time (NULL);
+
+  pthread_mutex_lock (&host_lock);
+  for (host = host_head; host != NULL; host = host->next)
+  {
+    if (host->state != STATE_IDLE)
+      continue;
+
+    host->skip_left -= interval_g;
+    if (host->skip_left >= interval_g)
+      continue;
+
+    host->state = STATE_WAIT;
+
+    host->skip_left = host->skip_num;
+  } /* for (host) */
+
+  pthread_cond_broadcast (&host_cond);
+  pthread_mutex_unlock (&host_lock);
+
+  return (0);
+} /* int csnmp_read */
+
+static int csnmp_shutdown (void)
+{
+  host_definition_t *host_this;
+  host_definition_t *host_next;
+
+  data_definition_t *data_this;
+  data_definition_t *data_next;
+
+  int i;
+
+  pthread_mutex_lock (&host_lock);
+  do_shutdown = 1;
+  pthread_cond_broadcast (&host_cond);
+  pthread_mutex_unlock (&host_lock);
+
+  for (i = 0; i < threads_num; i++)
+    pthread_join (threads[i], NULL);
+
+  /* Now that all the threads have exited, let's free all the global variables.
+   * This isn't really neccessary, I guess, but I think it's good stile to do
+   * so anyway. */
+  host_this = host_head;
+  host_head = NULL;
+  while (host_this != NULL)
+  {
+    host_next = host_this->next;
+
+    csnmp_host_close_session (host_this);
+
+    sfree (host_this->name);
+    sfree (host_this->address);
+    sfree (host_this->community);
+    sfree (host_this->data_list);
+    sfree (host_this);
+
+    host_this = host_next;
+  }
+
+  data_this = data_head;
+  data_head = NULL;
+  while (data_this != NULL)
+  {
+    data_next = data_this->next;
+
+    sfree (data_this->name);
+    sfree (data_this->type);
+    sfree (data_this->values);
+    sfree (data_this);
+
+    data_this = data_next;
+  }
+
+  return (0);
+} /* int csnmp_shutdown */
+
+void module_register (void)
+{
+  plugin_register_complex_config ("snmp", csnmp_config);
+  plugin_register_init ("snmp", csnmp_init);
+  plugin_register_read ("snmp", csnmp_read);
+  plugin_register_shutdown ("snmp", csnmp_shutdown);
+} /* void module_register */
+
+/*
+ * vim: shiftwidth=2 softtabstop=2 tabstop=8
+ */
index 68b966b..e193205 100644 (file)
@@ -2,6 +2,7 @@ apache_bytes            count:COUNTER:0:134217728
 apache_connections     count:GAUGE:0:65535
 apache_requests                count:COUNTER:0:134217728
 apache_scoreboard      count:GAUGE:0:65535
+bitrate                        value:GAUGE:0:4294967295
 charge                 value:GAUGE:0:U
 counter                        value:COUNTER:U:U
 cpu                    value:COUNTER:0:4294967295
index 215abdd..82c15a8 100644 (file)
@@ -36,7 +36,7 @@
 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
 #endif
 
-#define US_DEFAULT_PATH PREFIX"/var/run/"PACKAGE_NAME"-unixsock"
+#define US_DEFAULT_PATH LOCALSTATEDIR"/run/"PACKAGE_NAME"-unixsock"
 
 /*
  * Private data structures
@@ -329,7 +329,7 @@ static void cache_flush (int max_age)
        } /* while (this != NULL) */
 
        pthread_mutex_unlock (&cache_lock);
-} /* int cache_flush */
+} /* void cache_flush */
 
 static int us_open_socket (void)
 {
@@ -630,6 +630,56 @@ static int us_handle_putval (FILE *fh, char **fields, int fields_num)
        return (0);
 } /* int us_handle_putval */
 
+static int us_handle_listval (FILE *fh, char **fields, int fields_num)
+{
+       char buffer[1024];
+       char **value_list = NULL;
+       int value_list_len = 0;
+       value_cache_t *entry;
+       int i;
+
+       if (fields_num != 1)
+       {
+               DEBUG ("unixsock plugin: us_handle_listval: "
+                               "Wrong number of fields: %i", fields_num);
+               fprintf (fh, "-1 Wrong number of fields: Got %i, expected 1.\n",
+                               fields_num);
+               fflush (fh);
+               return (-1);
+       }
+
+       pthread_mutex_lock (&cache_lock);
+
+       for (entry = cache_head; entry != NULL; entry = entry->next)
+       {
+               char **tmp;
+
+               snprintf (buffer, sizeof (buffer), "%u %s\n",
+                               (unsigned int) entry->time, entry->name);
+               buffer[sizeof (buffer) - 1] = '\0';
+               
+               tmp = realloc (value_list, sizeof (char *) * (value_list_len + 1));
+               if (tmp == NULL)
+                       continue;
+               value_list = tmp;
+
+               value_list[value_list_len] = strdup (buffer);
+
+               if (value_list[value_list_len] != NULL)
+                       value_list_len++;
+       } /* for (entry) */
+
+       pthread_mutex_unlock (&cache_lock);
+
+       DEBUG ("unixsock plugin: us_handle_listval: value_list_len = %i", value_list_len);
+       fprintf (fh, "%i Values found\n", value_list_len);
+       for (i = 0; i < value_list_len; i++)
+               fputs (value_list[i], fh);
+       fflush (fh);
+
+       return (0);
+} /* int us_handle_listval */
+
 static void *us_handle_client (void *arg)
 {
        int fd;
@@ -685,6 +735,10 @@ static void *us_handle_client (void *arg)
                {
                        us_handle_putval (fh, fields, fields_num);
                }
+               else if (strcasecmp (fields[0], "listval") == 0)
+               {
+                       us_handle_listval (fh, fields, fields_num);
+               }
                else
                {
                        fprintf (fh, "-1 Unknown command: %s\n", fields[0]);
diff --git a/src/xmms.c b/src/xmms.c
new file mode 100644 (file)
index 0000000..238a8ee
--- /dev/null
@@ -0,0 +1,73 @@
+/**
+ * collectd - src/xmms.c
+ * Copyright (C) 2007  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 <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "plugin.h"
+#include "common.h"
+
+#include <xmms/xmmsctrl.h>
+
+static gint xmms_session;
+
+static void cxmms_submit (const char *type, gauge_t value)
+{
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].gauge = value;
+
+       vl.values = values;
+       vl.values_len = 1;
+       vl.time = time (NULL);
+       strcpy (vl.host, hostname_g);
+       strcpy (vl.plugin, "xmms");
+
+       plugin_dispatch_values (type, &vl);
+} /* void cxmms_submit */
+
+int cxmms_read (void)
+{
+  gint rate;
+  gint freq;
+  gint nch;
+
+  if (!xmms_remote_is_running (xmms_session))
+    return (0);
+
+  xmms_remote_get_info (xmms_session, &rate, &freq, &nch);
+
+  if ((freq == 0) || (nch == 0))
+    return (0);
+
+  cxmms_submit ("bitrate", rate);
+  cxmms_submit ("frequency", freq);
+
+  return (0);
+} /* int read */
+
+void module_register (void)
+{
+  plugin_register_read ("xmms", cxmms_read);
+} /* void module_register */
+
+/*
+ * vim: shiftwidth=2:softtabstop=2:textwidth=78
+ */