Merge remote branch 'powdahound/master'
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 1 May 2010 09:02:57 +0000 (11:02 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Sat, 1 May 2010 09:02:57 +0000 (11:02 +0200)
125 files changed:
.mailmap
AUTHORS
ChangeLog
README
TODO
bindings/Makefile.am
bindings/java/Makefile.am
bindings/java/org/collectd/java/GenericJMXConfConnection.java
bindings/java/org/collectd/java/GenericJMXConfMBean.java
bindings/perl/Collectd.pm [deleted file]
bindings/perl/Collectd/Makefile.PL [deleted file]
bindings/perl/Collectd/Unixsock.pm [deleted file]
bindings/perl/lib/Collectd.pm [new file with mode: 0644]
bindings/perl/lib/Collectd/Plugins/Monitorus.pm [new file with mode: 0644]
bindings/perl/lib/Collectd/Plugins/OpenVZ.pm [new file with mode: 0644]
bindings/perl/lib/Collectd/Unixsock.pm [new file with mode: 0644]
configure.in
contrib/collection.cgi
contrib/collection3/README
contrib/examples/MyPlugin.pm
contrib/exec-munin.px
contrib/exec-nagios.px
contrib/python/getsigchld.py [new file with mode: 0644]
contrib/redhat/collectd.spec
contrib/snmp-data.conf
contrib/snmp-probe-host.px
debian/README.Debian [deleted file]
debian/changelog [deleted file]
debian/collectd.conf [deleted file]
debian/collectd.init.d [deleted file]
debian/collectd.postrm [deleted file]
debian/compat [deleted file]
debian/control [deleted file]
debian/copyright [deleted file]
debian/examples/myplugin.c [deleted file]
debian/rules [deleted file]
src/Makefile.am
src/apache.c
src/battery.c
src/collectd-nagios.c
src/collectd-perl.pod
src/collectd-python.pod [new file with mode: 0644]
src/collectd-snmp.pod
src/collectd.c
src/collectd.conf.in
src/collectd.conf.pod
src/collectd.h
src/common.c
src/common.h
src/configfile.c
src/configfile.h
src/contextswitch.c
src/cpu.c
src/cpython.h [new file with mode: 0644]
src/csv.c
src/curl.c
src/curl_json.c
src/curl_xml.c [new file with mode: 0644]
src/dbi.c
src/df.c
src/disk.c
src/exec.c
src/filecount.c
src/filter_chain.c
src/interface.c
src/java.c
src/libcollectdclient/client.h
src/liboconfig/parser.y
src/liboconfig/scanner.l
src/load.c
src/logfile.c
src/match_hashed.c
src/match_regex.c
src/memcachec.c
src/memcached.c
src/memory.c
src/meta_data.c
src/meta_data.h
src/modbus.c [new file with mode: 0644]
src/mysql.c
src/netapp.c
src/network.c
src/onewire.c
src/openvpn.c
src/oracle.c
src/owniptc/libip4tc.c
src/owniptc/libip6tc.c
src/owniptc/libiptc.c
src/pinba.c [new file with mode: 0644]
src/pinba.proto [new file with mode: 0644]
src/ping.c
src/plugin.c
src/plugin.h
src/postgresql.c
src/processes.c
src/protocols.c
src/pyconfig.c [new file with mode: 0644]
src/python.c [new file with mode: 0644]
src/pyvalues.c [new file with mode: 0644]
src/routeros.c [new file with mode: 0644]
src/rrdcached.c
src/rrdtool.c
src/snmp.c
src/swap.c
src/tail.c
src/target_scale.c
src/thermal.c
src/types.db
src/types.db.pod
src/utils_cache.c
src/utils_cmd_listval.c
src/utils_db_query.c
src/utils_db_query.h
src/utils_format_json.c
src/utils_format_json.h
src/utils_heap.c
src/utils_heap.h
src/utils_llist.c
src/utils_llist.h
src/utils_match.c
src/utils_match.h
src/utils_tail_match.c
src/utils_tail_match.h
src/write_http.c
version-gen.sh

index 9bb4399..a39a1d2 100644 (file)
--- a/.mailmap
+++ b/.mailmap
@@ -5,4 +5,5 @@ Luboš Staněk <kolektor@atlas.cz>
 Luboš Staněk <lubek@users.sourceforge.net>
 Niki W. Waibel <niki>
 Sebastian Harl <tokkee>
+Rodolphe Quiedeville <rquiedeville@bearstech.com>
 
diff --git a/AUTHORS b/AUTHORS
index 885d021..4568965 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -23,6 +23,7 @@ Alvaro Barcellos <alvaro.barcellos at gmail.com>
 
 Amit Gupta <amit.gupta221 at gmail.com>
  - Multiple servers in the apache plugin.
+ - curl_xml plugin.
 
 Anthony Dewhurst <dewhurst at gmail.com>
  - zfs_arc plugin.
@@ -82,9 +83,21 @@ Luke Herberling <collectd at c-ware.com>
 Lyonel Vincent <lyonel at ezix.org>
  - processes plugin.
 
+Manuel Sanmartin
+ - AIX port of the following plugins:
+   + cpu
+   + disk
+   + interface
+   + load
+   + memory
+   + processes
+   + swap
+ - Various AIX-related fixes and hacks.
+
 Marco Chiappero <marco at absence.it>
  - uptime plugin.
  - ip6tables support in the iptables plugin.
+ - openvpn plugin (support for more status file formats)
 
 Michael Stapelberg <michael+git at stapelberg.de>
  - OpenBSD port of the tcpconns plugin.
@@ -127,6 +140,9 @@ Peter Holik <peter at holik.at>
  - Some bugfixes in the exec plugin.
  - Notifications in the ipmi plugin.
 
+Phoenix Kayo <kayo.k11.4 at gmail.com>
+ - pinba plugin.
+
 Piotr Hosowicz <the55 at wp.pl>
  - SMF manifest for collectd.
 
@@ -154,6 +170,7 @@ Stefan Hacker <stefan.hacker at web.de>
 
 Sven Trenkel <collectd at semidefinite.de>
  - netapp plugin.
+ - python plugin.
 
 Tomasz Pala <gotar at pld-linux.org>
  - conntrack plugin.
index ecc3869..8696d46 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,213 @@
+2010-05-01, Version 4.10.0
+       * collectd: JSON output now includes the "dstypes" and "dsnames"
+         fields. This makes it easier for external applications to interpret
+         the data. Thanks to Chris Buben for his work.
+       * collectd: The new "Timeout" option can be used to specify a
+         "timeout" for missing values. This is used in the threshold checking
+         code to detect missing values. Thanks to Andrés J. Díaz for the
+         patch.
+       * apache plugin: Support for "IdleWorkers" (Apache 1.*: "IdleServers")
+         has been added.
+       * curl plugin: The new "ExcludeRegex" allows to easily exclude certain
+         lines from the match.
+       * curl_xml plugin: This new plugin allows to read XML files using cURL
+         and extract metrics included in the files. Thanks to Amit Gupta for
+         his work.
+       * filecount plugin: The new "IncludeHidden" option allows to include
+         "hidden" files and directories in the statistics. Thanks to Vaclav
+         Malek for the patch.
+       * logfile plugin: The new "PrintSeverity" option allows to include the
+         severity of a message in the output. Thanks to Clément Stenac for
+         his patch.
+       * memcachec plugin: The new "ExcludeRegex" allows to easily exclude
+         certain lines from the match.
+       * modbus plugin: This new plugin allows to read registers from
+         Modbus-TCP enabled devices.
+       * network plugin: The new "Interface" option allows to set the
+         interface to be used for multicast and, if supported, unicast
+         traffic. Thanks to Max Henkel for his work.
+       * openvpn plugin: The "CollectUserCount" and "CollectIndividualUsers"
+         options allow more detailed control over how to report sessions of
+         multiple users. Thanks to Fabian Schuh for his work.
+       * pinba plugin: This new plugin receives timing information from the
+         Pinba PHP extension, which can be used for profiling PHP code and
+         webserver performance.
+       * ping plugin: The new "MaxMissed" allows to re-resolve a hosts
+         address when it doesn't reply to a number of ping requests. Thanks
+         to Stefan Völkel for the patch.
+       * postgresql plugin: The "Interval" config option has been added. The
+         plugin has been relicensed under the 2-clause BSD license. Thanks to
+         Sebastian Harl for his work.
+       * processes plugin: Support for "code" and "data" virtual memory sizes
+         has been added. Thanks to Clément Stenac for his patch.
+       * python plugin: Support for Python 3 has been implemented. Thanks to
+         Sven Trenkel for his work.
+       * routeros plugin: Support for collecting CPU load, memory usage, used
+         and free disk space, sectors written and number of bad blocks from
+         MikroTik devices has been added.
+       * swap plugin: Support for Linux < 2.6 has been added. Thanks to Lorin
+         Scraba for his patch.
+       * tail plugin: The new "ExcludeRegex" allows to easily exclude certain
+         lines from the match. Thanks to Peter Warasin for his patch.
+       * write_http plugin: The "StoreRates" option has been added. Thanks to
+         Paul Sadauskas for his patch.
+       * regex match: The "Invert" option has been added. Thanks to Julien
+         Ammous for his patch.
+
+2010-04-22, Version 4.9.2
+       * Build system, various plugins: Fixes for AIX compatibility have been
+         added. Thanks to Manuel Sanmartin for his patches.
+       * Build system: Checking for "nanosleep" on old Solaris machines has
+         been fixed. Thanks to Vincent McIntyre and Sebastian Harl for
+         figuring out a way to make this work.
+       * collectd: Append a newline to messages written to STDERR.
+       * collectd: Serialization of NANs in JSON format has been fixed.
+         Thanks to Chris Buben for pointing out the resulting syntax error.
+       * collectd: Checks whether a "sleep" returned early have been added;
+         the cases are now handled correctly. Thanks to Michael Stapelberg
+         for the patch.
+       * collectd: Continue reading files in a directory when parsing one
+         file fails.
+       * apache plugin: Collection of the number of active connections has
+         been fixed for Apache 2.*.
+       * contextswitch plugin: Handle large counter/derive values correctly.
+         Thanks to Martin Merkel for reporting the bug.
+       * exec plugin: Error messages have been improved. The "running" flag
+         is now cleared correctly when forking a child fails.
+       * iptables plugin: Fix a violation of aliasing rules. This resolves a
+         warning / error with new GCC versions. Thanks to Jan Engelhardt for
+         the work-around.
+       * java plugin: The Java API files are now packaged into a .jar file.
+         Thanks to Amit Gupta for his patch.
+       * network plugin: Fix a segmentation fault when receiving packets with
+         an unknown data source type.
+       * network plugin: A memory leak when receiving encrypted network
+         packets has been fixed.
+       * openvpn plugin: Fix naming schema when reading "MULTI1" type status
+         files.
+       * oracle plugin: Fix checking for lost connections and reconnect in
+         this case. Thanks to Sven Trenkel for pointing out the problem.
+       * unixsock plugin: A memory leak in the "LISTVAL" command has been
+         fixed. Thanks to Peter Warasin for pointing it out.
+       * write_http plugin: Use the "any" authentication schema. This used to
+         be "digest". Thanks to Paul Sadauskas for the patch.
+
+2010-01-14, Version 4.9.1
+       * Documentation: Some manpage fixes.
+       * Default config: Added sample configuration for missing plugins.
+       * apache plugin: Fix a segmentation fault in the config handling of
+         VerifyPeer / VerifyHost. Thanks to "plazmus" for his or her patch.
+       * processes plugin: Fix handling of derive data sources.
+       * rrdtool plugin: Fix a bug with random write timeouts. Due to an
+         incorrect initialization some files may be suspended basically
+         indefinitely. After flushing the files they were written regularly
+         again.
+       * routeros plugin: Use the node name for the "host" field.
+       * Monitorus.pm: Put the plugin into the "Collectd::Plugins" namespace.
+       * Perl bindings: Fix a warning that was printed when building
+         debugging output.
+
+2009-12-21, Version 4.9.0
+       * contextswitch plugin: The new ContextSwitch plugin gathers the
+         number of context switches done by the CPU. Thanks to Patrik
+         Weiskircher for the patch.
+       * cpu plugin: Support for SMP (multiple processors) under FreeBSD has
+         been added. Thanks to Doug MacEachern for the patch.
+       * curl plugin: The “MeasureResponseTime” option has been added. Thanks
+         to Aman Gupta for the patch.
+       * df plugin: Collecting the inode count and reserved space has been
+         added. Thanks to Patrik Weiskircher for the patch.
+       * exec plugin: The environment variables “COLLECTD_INTERVAL” and
+         “COLLECTD_HOSTNAME” are now set before executing the application.
+       * Monitorus plugin: This Perl-based plugin to query statistics from
+         mon.itor.us has been added. Thanks to Jeff Green for the patch.
+       * netapp plugin: New plugin to collect statistics from NetApp filers.
+         Thanks to Sven Trenkel of the noris network AG for the patch.
+       * network plugin: Statistics collection about the plugin itself has
+         been implemented.
+       * openvpn plugin: Add support for more versions of the “status file”.
+         Thanks to Marco Chiappero for the patch.
+       * OpenVZ plugin: This Perl-based plugin to gather OpenVZ statistics
+         has been added. Thanks to Jonathan Kolb for the patch.
+       * ping plugin: The config options "SourceAddress" and "Device"
+         have been added. Thanks to Sebastian Harl for the patch.
+       * processes plugin: Collection of IO-metrics has been added. Thanks to
+         Andrés J. Díaz for the patch.
+       * python plugin: The new Python plugin integrates a Python interpreter
+         into collectd and allows to execute plugins written in the scripting
+         language. Thanks to Sven Trenkel for his work.
+       * routeros plugin: The new RouterOS plugin queries interface and
+         wireless registration statistics from RouterOS.
+       * Various plugins: AIX support has been added to the cpu, disk,
+         interface, load, memory, processes, and swap plugins. Thanks to
+         Manuel Sanmartin for his patches.
+       * hashed match: This match for simple load balancing and redundant
+         storage has been added.
+       * scale target: This target to scale (multiply) values by an arbitrary
+         value has been added.
+
+2010-04-22, Version 4.8.5
+       * collectd: Append a newline to messages written to STDERR.
+       * network plugin: Fix a segmentation fault when receiving packets with
+         an unknown data source type.
+
+2010-04-07, Version 4.8.4
+       * Build system, various plugins: Fixes for AIX compatibility have been
+         added. Thanks to Manuel Sanmartin for his patches.
+       * Build system: Checking for "nanosleep" on old Solaris machines has
+         been fixed. Thanks to Vincent McIntyre and Sebastian Harl for
+         figuring out a way to make this work.
+       * collectd: Serialization of NANs in JSON format has been fixed.
+         Thanks to Chris Buben for pointing out the resulting syntax error.
+       * collectd: Checks whether a "sleep" returned early have been added;
+         the cases are now handled correctly. Thanks to Michael Stapelberg
+         for the patch.
+       * collectd: Continue reading files in a directory when parsing one
+         file fails.
+       * apache plugin: Collection of the number of active connections has
+         been fixed for Apache 2.*.
+       * exec plugin: Error messages have been improved. The "running" flag
+         is now cleared correctly when forking a child fails.
+       * iptables plugin: Fix a violation of aliasing rules. This resolves a
+         warning / error with new GCC versions. Thanks to Jan Engelhardt for
+         the work-around.
+       * java plugin: The Java API files are now packaged into a .jar file.
+         Thanks to Amit Gupta for his patch.
+       * network plugin: A memory leak when receiving encrypted network
+         packets has been fixed.
+       * oracle plugin: Fix checking for lost connections and reconnect in
+         this case. Thanks to Sven Trenkel for pointing out the problem.
+       * unixsock plugin: A memory leak in the "LISTVAL" command has been
+         fixed. Thanks to Peter Warasin for pointing it out.
+       * write_http plugin: Use the "any" authentication schema. This used to
+         be "digest". Thanks to Paul Sadauskas for the patch.
+
+2010-01-14, Version 4.8.3
+       * Documentation: Some manpage fixes.
+       * rrdtool plugin: Fix a bug with random write timeouts. Due to an
+         incorrect initialization some files may be suspended basically
+         indefinitely. After flushing the files they were written regularly
+         again.
+
+2009-12-18, Version 4.8.2
+       * Build system, java plugin: Don't use “find -L” to search for Java
+         headers, because it's a GNU extension.
+       * Build system: Support for parallel builds has been improved. Thanks
+         Sebastian Harl and Stefan Völkel for looking into this.
+       * collectd: Print error messages to STDERR if no log plugin has been
+         loaded.
+       * genericjmx plugin: Close and re-open the connection upon I/O-errors.
+       * gmond plugin: Fix typos which caused syntax errors.
+       * memory plugin: Handling of >4 Gbyte of memory has been fixed.
+       * network plugin: The license has been changed to LGPL 2.1.
+       * oracle plugin: Reconnect to the database if the connection dies.
+       * rrdcached plugin: Work-around for a bug in RRDtool 1.4rc2 has been
+         added.
+       * snmp plugin: Handling of negative values has been fixed. Strings
+         containing control characters are now interpreted as hex-strings.
+       * unixsock plugin: A memory leak in the LISTVAL command has been
+         fixed. Thanks to Ben Knight for his patch.
+
 2009-10-04, Version 4.8.1
        * Build system: Issues when building the iptables plugin have been
          fixed.
          been added. “DERIVE” can be used for counters that are reset
          occasionally. Thanks to Mariusz Gronczewski for implementing this.
        * thresholds: The advanced threshold options “Percentage”, “Hits”, and
-         “Hysteresis” have been added. Thanks to Andrés J. Díaz for hit
+         “Hysteresis” have been added. Thanks to Andrés J. Díaz for his
          patches.
        * curl_json plugin: The new cURL-JSON plugin reads JSON files using
          the cURL library and parses the contents according to user
          lists, where at least one data source is of type COUNTER and the
          counter value of all counter data sources is zero.
 
+2009-12-18, Version 4.7.5
+       * Build system, java plugin: Don't use “find -L” to search for Java
+         headers, because it's a GNU extension.
+       * Build system: Support for parallel builds has been improved. Thanks
+         Sebastian Harl and Stefan Völkel for looking into this.
+       * collectd: Print error messages to STDERR if no log plugin has been
+         loaded.
+       * memory plugin: Handling of >4 Gbyte of memory has been fixed.
+       * network plugin: The license has been changed to LGPL 2.1.
+       * oracle plugin: Reconnect to the database if the connection dies.
+       * rrdcached plugin: Work-around for a bug in RRDtool 1.4rc2 has been
+         added.
+       * snmp plugin: Handling of negative values has been fixed. Strings
+         containing control characters are now interpreted as hex-strings.
+       * unixsock plugin: A memory leak in the LISTVAL command has been
+         fixed. Thanks to Ben Knight for his patch.
+
 2009-10-03, Version 4.7.4
        * Build system: Issues when building the iptables plugin have been
          fixed.
diff --git a/README b/README
index d40fd2f..ade430c 100644 (file)
--- a/README
+++ b/README
@@ -57,6 +57,10 @@ Features
       Retrieves JSON data via cURL and parses it according to user
       configuration.
 
+    - curl_xml
+      Retrieves XML data via cURL and parses it according to user
+      configuration.
+
     - dbi
       Executes SQL statements on various databases and interprets the returned
       data.
@@ -143,6 +147,10 @@ Features
       Memory utilization: Memory occupied by running processes, page cache,
       buffer cache and free.
 
+    - modbus
+      Reads values from Modbus/TCP enabled devices. Supports reading values
+      from multiple "slaves" so gateway devices can be used.
+
     - multimeter
       Information provided by serial multimeters, such as the `Metex
       M-4650CR'.
@@ -200,6 +208,10 @@ Features
       write your own plugins in Perl and return arbitrary values using this
       API. See collectd-perl(5).
 
+    - pinba
+      Receive and dispatch timing values from Pinba, a profiling extension for
+      PHP.
+
     - ping
       Network latency: Time to reach the default gateway or another given
       host.
@@ -217,6 +229,15 @@ Features
     - protocols
       Counts various aspects of network protocols such as IP, TCP, UDP, etc.
 
+    - python
+      The python plugin implements a Python interpreter into collectd. This
+      makes it possible to write plugins in Python which are executed by
+      collectd without the need to start a heavy interpreter every interval.
+      See collectd-python(5) for details.
+
+    - routeros
+      Query interface and wireless registration statistics from RouterOS.
+
     - rrdcached
       RRDtool caching daemon (RRDcacheD) statistics.
 
@@ -284,7 +305,7 @@ Features
     - zfs_arc
       Statistics for ZFS' “Adaptive Replacement Cache” (ARC).
 
-  * Output can be written or send to various destinations by the following
+  * Output can be written or sent to various destinations by the following
     plugins:
 
     - csv
@@ -301,6 +322,10 @@ Features
       you can easily do weird stuff with the plugins we didn't dare think of
       ;) See collectd-perl(5).
 
+    - python
+      It's possible to implement write plugins in Python using the python
+      plugin. See collectd-python(5) for details.
+
     - rrdcached
       Output to round-robin-database (RRD) files using the RRDtool caching
       daemon (RRDcacheD) - see rrdcached(1). That daemon provides a general
@@ -333,6 +358,10 @@ Features
       Log messages are propagated to plugins written in Perl as well.
       See collectd-perl(5).
 
+    - python
+      It's possible to implement log plugins in Python using the python plugin.
+      See collectd-python(5) for details.
+
     - syslog
       Logs to the standard UNIX logging mechanism, syslog.
 
@@ -362,12 +391,19 @@ Features
       Notifications are propagated to plugins written in Perl as well.
       See collectd-perl(5).
 
+    - python
+      It's possible to implement notification plugins in Python using the
+      python plugin. See collectd-python(5) for details.
+
   * Value processing can be controlled using the "filter chain" infrastructure
     and "matches" and "targets". The following plugins are available:
 
     - match_empty_counter
       Match counter values which are currently zero.
 
+    - match_hashed
+      Match values using a hash function of the hostname.
+
     - match_regex
       Match values by their identifier based on regular expressions.
 
@@ -383,6 +419,9 @@ Features
     - target_replace
       Replace parts of an identifier using regular expressions.
 
+    - target_scale
+      Scale (multiply) values by an arbitrary value.
+
     - target_set
       Set (overwrite) entire parts of an identifier.
 
@@ -399,7 +438,7 @@ Features
     network plugins, makes sure your resources are used efficiently. Also,
     since collectd is programmed multithreaded it benefits from hyperthreading
     and multicore processors and makes sure that the daemon isn't idle if only
-    one plugins waits for an IO-operation to complete.
+    one plugin waits for an IO-operation to complete.
 
   * Once set up, hardly any maintenance is necessary. Setup is kept as easy
     as possible and the default values should be okay for most users.
@@ -510,11 +549,15 @@ Prerequisites
     Used by the `memcachec' plugin to connect to a memcache daemon.
     <http://tangent.org/552/libmemcached.html>
 
+  * libmodbus (optional)
+    Used by the `modbus' plugin to communicate with Modbus/TCP devices.
+    <https://launchpad.net/libmodbus>
+
   * libmysqlclient (optional)
     Unsurprisingly used by the `mysql' plugin.
     <http://dev.mysql.com/>
 
-  * libnatapp (optional)
+  * libnetapp (optional)
     Required for the “netapp” plugin.
     This library is part of the “Manage ONTAP SDK” published by NetApp.
 
@@ -552,6 +595,19 @@ Prerequisites
     The PostgreSQL C client library used by the `postgresql' plugin.
     <http://www.postgresql.org/>
 
+  * libprotobuf-c, protoc-c (optional)
+    Used by the `pinba' plugin to generate a parser for the network packets
+    sent by the Pinba PHP extension.
+    <http://code.google.com/p/protobuf-c/>
+
+  * libpython (optional)
+    Used by the `python' plugin. Currently, only 2.3 ≦ Python < 3 is supported.
+    <http://www.python.org/>
+
+  * librouteros (optional)
+    Used by the `routeros' plugin to connect to a device running `RouterOS'.
+    <http://verplant.org/librouteros/>
+
   * librrd (optional)
     Used by the `rrdtool' and `rrdcached' plugins. The latter requires RRDtool
     client support which was added after version 1.3 of RRDtool. Versions 1.0,
diff --git a/TODO b/TODO
index 3c8f49e..009eb7f 100644 (file)
--- a/TODO
+++ b/TODO
@@ -5,9 +5,6 @@
 src/battery.c: commend not working code.
 
 Wishlist:
-* Update the RPM specfile to
-  - build `collectd-apache'
-  - be free of syntax errors.
 * Port nfs module to solaris
 * Port tape module to Linux
 * Port the apple_sensors plugin to Linux/PPC.
index fb68657..f39e9bb 100644 (file)
@@ -4,8 +4,11 @@ if BUILD_WITH_JAVA
 SUBDIRS += java
 endif
 
-EXTRA_DIST = perl/Collectd.pm perl/Makefile.PL perl/Collectd/Makefile.PL \
-               perl/Collectd/Unixsock.pm
+EXTRA_DIST = perl/Makefile.PL \
+            perl/lib/Collectd.pm \
+            perl/lib/Collectd/Unixsock.pm \
+            perl/lib/Collectd/Plugins/Monitorus.pm \
+            perl/lib/Collectd/Plugins/OpenVZ.pm
 
 all-local: @PERL_BINDINGS@
 
@@ -19,16 +22,16 @@ perl: perl/Makefile
        cd perl && $(MAKE)
 
 perl/Makefile: .perl-directory-stamp perl/Makefile.PL \
-       perl/Collectd/Makefile.PL $(top_builddir)/config.status
+       $(top_builddir)/config.status
        cd perl && @PERL@ Makefile.PL PREFIX=$(prefix) @PERL_BINDINGS_OPTIONS@
 
 .perl-directory-stamp:
        if test ! -d perl; then \
-         mkdir -p perl/Collectd; \
+         mkdir -p perl/Collectd/Plugins; \
          cp $(srcdir)/perl/Collectd.pm perl/; \
          cp $(srcdir)/perl/Makefile.PL perl/; \
          cp $(srcdir)/perl/Collectd/Unixsock.pm perl/Collectd/; \
-         cp $(srcdir)/perl/Collectd/Makefile.PL perl/Collectd/; \
+         cp $(srcdir)/perl/Collectd/Plugins/OpenVZ.pm perl/Collectd/Plugins/; \
        fi
        touch $@
 
index d3315ac..fa253f6 100644 (file)
@@ -27,19 +27,22 @@ EXTRA_DIST = org/collectd/api/CollectdConfigInterface.java \
 java-build-stamp: org/collectd/api/*.java
        $(JAVAC) -d "." "$(srcdir)/org/collectd/api"/*.java
        $(JAVAC) -d "." "$(srcdir)/org/collectd/java"/*.java
+       mkdir -p .libs
+       $(JAR) cf .libs/collectd-api.jar "org/collectd/api"/*.class
+       $(JAR) cf .libs/generic-jmx.jar "org/collectd/java"/*.class
        touch "$@"
 
 all-local: java-build-stamp
 
 install-exec-local: java-build-stamp
-       mkdir -p "$(DESTDIR)$(pkgdatadir)/java/org/collectd/api"
-       $(INSTALL) -m 644 "org/collectd/api"/*.class \
-               "$(DESTDIR)$(pkgdatadir)/java/org/collectd/api/"
-       mkdir -p "$(DESTDIR)$(pkgdatadir)/java/org/collectd/java"
-       $(INSTALL) -m 644 "org/collectd/java"/*.class \
-               "$(DESTDIR)$(pkgdatadir)/java/org/collectd/java/"
+       mkdir -p "$(DESTDIR)$(pkgdatadir)/java"
+       $(INSTALL) -m 644 .libs/collectd-api.jar \
+               "$(DESTDIR)$(pkgdatadir)/java"
+       $(INSTALL) -m 644 .libs/generic-jmx.jar \
+               "$(DESTDIR)$(pkgdatadir)/java"
 
 clean-local:
        rm -f "org/collectd/api"/*.class
        rm -f "org/collectd/java"/*.class
+       rm -f .libs
        rm -f "java-build-stamp"
index 7214fd7..ffa9ded 100644 (file)
@@ -208,7 +208,16 @@ private void connect () /* {{{ */
     pd.setPlugin ("GenericJMX");
 
     for (int i = 0; i < this._mbeans.size (); i++)
-      this._mbeans.get (i).query (this._jmx_connection, pd);
+    {
+      int status;
+
+      status = this._mbeans.get (i).query (this._jmx_connection, pd);
+      if (status != 0)
+      {
+        this._jmx_connection = null;
+        return;
+      }
+    } /* for */
   } /* }}} void query */
 
   public String toString ()
index 27e9e32..1587bd5 100644 (file)
@@ -170,7 +170,7 @@ class GenericJMXConfMBean
     return (this._name);
   } /* }}} */
 
-  public void query (MBeanServerConnection conn, PluginData pd) /* {{{ */
+  public int query (MBeanServerConnection conn, PluginData pd) /* {{{ */
   {
     Set<ObjectName> names;
     Iterator<ObjectName> iter;
@@ -182,7 +182,7 @@ class GenericJMXConfMBean
     catch (Exception e)
     {
       Collectd.logError ("GenericJMXConfMBean: queryNames failed: " + e);
-      return;
+      return (-1);
     }
 
     if (names.size () == 0)
@@ -236,6 +236,8 @@ class GenericJMXConfMBean
       for (int i = 0; i < this._values.size (); i++)
         this._values.get (i).query (conn, objName, pd_tmp);
     }
+
+    return (0);
   } /* }}} void query */
 }
 
diff --git a/bindings/perl/Collectd.pm b/bindings/perl/Collectd.pm
deleted file mode 100644 (file)
index 557950c..0000000
+++ /dev/null
@@ -1,648 +0,0 @@
-# collectd - Collectd.pm
-# Copyright (C) 2007-2009  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>
-
-package Collectd;
-
-use strict;
-use warnings;
-
-use Config;
-
-use threads;
-use threads::shared;
-
-BEGIN {
-       if (! $Config{'useithreads'}) {
-               die "Perl does not support ithreads!";
-       }
-}
-
-require Exporter;
-
-our @ISA = qw( Exporter );
-
-our %EXPORT_TAGS = (
-       'plugin' => [ qw(
-                       plugin_register
-                       plugin_unregister
-                       plugin_dispatch_values
-                       plugin_write
-                       plugin_flush
-                       plugin_flush_one
-                       plugin_flush_all
-                       plugin_dispatch_notification
-                       plugin_log
-       ) ],
-       'types' => [ qw(
-                       TYPE_INIT
-                       TYPE_READ
-                       TYPE_WRITE
-                       TYPE_SHUTDOWN
-                       TYPE_LOG
-                       TYPE_NOTIF
-                       TYPE_FLUSH
-                       TYPE_CONFIG
-                       TYPE_DATASET
-       ) ],
-       'ds_types' => [ qw(
-                       DS_TYPE_COUNTER
-                       DS_TYPE_GAUGE
-       ) ],
-       'log' => [ qw(
-                       ERROR
-                       WARNING
-                       NOTICE
-                       INFO
-                       DEBUG
-                       LOG_ERR
-                       LOG_WARNING
-                       LOG_NOTICE
-                       LOG_INFO
-                       LOG_DEBUG
-       ) ],
-       'filter_chain' => [ qw(
-                       fc_register
-                       FC_MATCH_NO_MATCH
-                       FC_MATCH_MATCHES
-                       FC_TARGET_CONTINUE
-                       FC_TARGET_STOP
-                       FC_TARGET_RETURN
-       ) ],
-       'fc_types' => [ qw(
-                       FC_MATCH
-                       FC_TARGET
-       ) ],
-       'notif' => [ qw(
-                       NOTIF_FAILURE
-                       NOTIF_WARNING
-                       NOTIF_OKAY
-       ) ],
-       'globals' => [ qw(
-                       $hostname_g
-                       $interval_g
-       ) ],
-);
-
-{
-       my %seen;
-       push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
-               foreach keys %EXPORT_TAGS;
-}
-
-# global variables
-our $hostname_g;
-our $interval_g;
-
-Exporter::export_ok_tags ('all');
-
-my @plugins : shared = ();
-my @fc_plugins : shared = ();
-my %cf_callbacks : shared = ();
-
-my %types = (
-       TYPE_INIT,     "init",
-       TYPE_READ,     "read",
-       TYPE_WRITE,    "write",
-       TYPE_SHUTDOWN, "shutdown",
-       TYPE_LOG,      "log",
-       TYPE_NOTIF,    "notify",
-       TYPE_FLUSH,    "flush"
-);
-
-my %fc_types = (
-       FC_MATCH,  "match",
-       FC_TARGET, "target"
-);
-
-my %fc_exec_names = (
-       FC_MATCH,  "match",
-       FC_TARGET, "invoke"
-);
-
-foreach my $type (keys %types) {
-       $plugins[$type] = &share ({});
-}
-
-foreach my $type (keys %fc_types) {
-       $fc_plugins[$type] = &share ({});
-}
-
-sub _log {
-       my $caller = shift;
-       my $lvl    = shift;
-       my $msg    = shift;
-
-       if ("Collectd" eq $caller) {
-               $msg = "perl: $msg";
-       }
-       return plugin_log ($lvl, $msg);
-}
-
-sub ERROR   { _log (scalar caller, LOG_ERR,     shift); }
-sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
-sub NOTICE  { _log (scalar caller, LOG_NOTICE,  shift); }
-sub INFO    { _log (scalar caller, LOG_INFO,    shift); }
-sub DEBUG   { _log (scalar caller, LOG_DEBUG,   shift); }
-
-sub plugin_call_all {
-       my $type = shift;
-
-       my %plugins;
-
-       our $cb_name = undef;
-
-       if (! defined $type) {
-               return;
-       }
-
-       if (TYPE_LOG != $type) {
-               DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
-       }
-
-       if (! defined $plugins[$type]) {
-               ERROR ("Collectd::plugin_call: unknown type \"$type\"");
-               return;
-       }
-
-       {
-               lock %{$plugins[$type]};
-               %plugins = %{$plugins[$type]};
-       }
-
-       foreach my $plugin (keys %plugins) {
-               my $p = $plugins{$plugin};
-
-               my $status = 0;
-
-               if ($p->{'wait_left'} > 0) {
-                       $p->{'wait_left'} -= $interval_g;
-               }
-
-               next if ($p->{'wait_left'} > 0);
-
-               $cb_name = $p->{'cb_name'};
-               $status = call_by_name (@_);
-
-               if (! $status) {
-                       my $err = undef;
-
-                       if ($@) {
-                               $err = $@;
-                       }
-                       else {
-                               $err = "callback returned false";
-                       }
-
-                       if (TYPE_LOG != $type) {
-                               ERROR ("Execution of callback \"$cb_name\" failed: $err");
-                       }
-
-                       $status = 0;
-               }
-
-               if ($status) {
-                       $p->{'wait_left'} = 0;
-                       $p->{'wait_time'} = $interval_g;
-               }
-               elsif (TYPE_READ == $type) {
-                       if ($p->{'wait_time'} < $interval_g) {
-                               $p->{'wait_time'} = $interval_g;
-                       }
-
-                       $p->{'wait_left'} = $p->{'wait_time'};
-                       $p->{'wait_time'} *= 2;
-
-                       if ($p->{'wait_time'} > 86400) {
-                               $p->{'wait_time'} = 86400;
-                       }
-
-                       WARNING ("${plugin}->read() failed with status $status. "
-                               . "Will suspend it for $p->{'wait_left'} seconds.");
-               }
-               elsif (TYPE_INIT == $type) {
-                       ERROR ("${plugin}->init() failed with status $status. "
-                               . "Plugin will be disabled.");
-
-                       foreach my $type (keys %types) {
-                               plugin_unregister ($type, $plugin);
-                       }
-               }
-               elsif (TYPE_LOG != $type) {
-                       WARNING ("${plugin}->$types{$type}() failed with status $status.");
-               }
-       }
-       return 1;
-}
-
-# Collectd::plugin_register (type, name, data).
-#
-# type:
-#   init, read, write, shutdown, data set
-#
-# name:
-#   name of the plugin
-#
-# data:
-#   reference to the plugin's subroutine that does the work or the data set
-#   definition
-sub plugin_register {
-       my $type = shift;
-       my $name = shift;
-       my $data = shift;
-
-       DEBUG ("Collectd::plugin_register: "
-               . "type = \"$type\", name = \"$name\", data = \"$data\"");
-
-       if (! ((defined $type) && (defined $name) && (defined $data))) {
-               ERROR ("Usage: Collectd::plugin_register (type, name, data)");
-               return;
-       }
-
-       if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
-                       && (TYPE_CONFIG != $type)) {
-               ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
-               return;
-       }
-
-       if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
-               return plugin_register_data_set ($name, $data);
-       }
-       elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
-               my $pkg = scalar caller;
-
-               if ($data !~ m/^$pkg\:\:/) {
-                       $data = $pkg . "::" . $data;
-               }
-
-               lock %cf_callbacks;
-               $cf_callbacks{$name} = $data;
-       }
-       elsif ((TYPE_DATASET != $type) && (! ref $data)) {
-               my $pkg = scalar caller;
-
-               my %p : shared;
-
-               if ($data !~ m/^$pkg\:\:/) {
-                       $data = $pkg . "::" . $data;
-               }
-
-               %p = (
-                       wait_time => $interval_g,
-                       wait_left => 0,
-                       cb_name   => $data,
-               );
-
-               lock %{$plugins[$type]};
-               $plugins[$type]->{$name} = \%p;
-       }
-       else {
-               ERROR ("Collectd::plugin_register: Invalid data.");
-               return;
-       }
-       return 1;
-}
-
-sub plugin_unregister {
-       my $type = shift;
-       my $name = shift;
-
-       DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
-
-       if (! ((defined $type) && (defined $name))) {
-               ERROR ("Usage: Collectd::plugin_unregister (type, name)");
-               return;
-       }
-
-       if (TYPE_DATASET == $type) {
-               return plugin_unregister_data_set ($name);
-       }
-       elsif (TYPE_CONFIG == $type) {
-               lock %cf_callbacks;
-               delete $cf_callbacks{$name};
-       }
-       elsif (defined $plugins[$type]) {
-               lock %{$plugins[$type]};
-               delete $plugins[$type]->{$name};
-       }
-       else {
-               ERROR ("Collectd::plugin_unregister: Invalid type.");
-               return;
-       }
-}
-
-sub plugin_write {
-       my %args = @_;
-
-       my @plugins    = ();
-       my @datasets   = ();
-       my @valuelists = ();
-
-       if (! defined $args{'valuelists'}) {
-               ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
-               return;
-       }
-
-       DEBUG ("Collectd::plugin_write:"
-               . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
-               . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
-               . " valueslists = $args{'valuelists'}");
-
-       if (defined ($args{'plugins'})) {
-               if ("ARRAY" eq ref ($args{'plugins'})) {
-                       @plugins = @{$args{'plugins'}};
-               }
-               else {
-                       @plugins = ($args{'plugins'});
-               }
-       }
-       else {
-               @plugins = (undef);
-       }
-
-       if ("ARRAY" eq ref ($args{'valuelists'})) {
-               @valuelists = @{$args{'valuelists'}};
-       }
-       else {
-               @valuelists = ($args{'valuelists'});
-       }
-
-       if (defined ($args{'datasets'})) {
-               if ("ARRAY" eq ref ($args{'datasets'})) {
-                       @datasets = @{$args{'datasets'}};
-               }
-               else {
-                       @datasets = ($args{'datasets'});
-               }
-       }
-       else {
-               @datasets = (undef) x scalar (@valuelists);
-       }
-
-       if ($#datasets != $#valuelists) {
-               ERROR ("Collectd::plugin_write: Invalid number of datasets.");
-               return;
-       }
-
-       foreach my $plugin (@plugins) {
-               for (my $i = 0; $i < scalar (@valuelists); ++$i) {
-                       _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
-               }
-       }
-}
-
-sub plugin_flush {
-       my %args = @_;
-
-       my $timeout = -1;
-       my @plugins = ();
-       my @ids     = ();
-
-       DEBUG ("Collectd::plugin_flush:"
-               . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
-               . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
-               . (defined ($args{'identifiers'})
-                       ? " identifiers = $args{'identifiers'}" : ""));
-
-       if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
-               $timeout = $args{'timeout'};
-       }
-
-       if (defined ($args{'plugins'})) {
-               if ("ARRAY" eq ref ($args{'plugins'})) {
-                       @plugins = @{$args{'plugins'}};
-               }
-               else {
-                       @plugins = ($args{'plugins'});
-               }
-       }
-       else {
-               @plugins = (undef);
-       }
-
-       if (defined ($args{'identifiers'})) {
-               if ("ARRAY" eq ref ($args{'identifiers'})) {
-                       @ids = @{$args{'identifiers'}};
-               }
-               else {
-                       @ids = ($args{'identifiers'});
-               }
-       }
-       else {
-               @ids = (undef);
-       }
-
-       foreach my $plugin (@plugins) {
-               foreach my $id (@ids) {
-                       _plugin_flush($plugin, $timeout, $id);
-               }
-       }
-}
-
-sub plugin_flush_one {
-       my $timeout = shift;
-       my $name    = shift;
-
-       WARNING ("Collectd::plugin_flush_one is deprecated - "
-               . "use Collectd::plugin_flush instead.");
-
-       if (! (defined ($timeout) && defined ($name))) {
-               ERROR ("Usage: Collectd::plugin_flush_one(timeout, name)");
-               return;
-       }
-
-       plugin_flush (plugins => $name, timeout => $timeout);
-}
-
-sub plugin_flush_all {
-       my $timeout = shift;
-
-       WARNING ("Collectd::plugin_flush_all is deprecated - "
-               . "use Collectd::plugin_flush instead.");
-
-       if (! defined ($timeout)) {
-               ERROR ("Usage: Collectd::plugin_flush_all(timeout)");
-               return;
-       }
-
-       plugin_flush (timeout => $timeout);
-}
-
-sub fc_call {
-       my $type    = shift;
-       my $name    = shift;
-       my $cb_type = shift;
-
-       my %proc;
-
-       our $cb_name = undef;
-       my  $status;
-
-       if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
-               ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
-               return;
-       }
-
-       if (! defined $fc_plugins[$type]) {
-               ERROR ("Collectd::fc_call: Invalid type \"$type\"");
-               return;
-       }
-
-       if (! defined $fc_plugins[$type]->{$name}) {
-               ERROR ("Collectd::fc_call: Unknown "
-                       . ($type == FC_MATCH ? "match" : "target")
-                       . " \"$name\"");
-               return;
-       }
-
-       DEBUG ("Collectd::fc_call: "
-               . "type = \"$type\", name = \"$name\", cb_type = \"$cb_type\"");
-
-       {
-               lock %{$fc_plugins[$type]};
-               %proc = %{$fc_plugins[$type]->{$name}};
-       }
-
-       if (FC_CB_EXEC == $cb_type) {
-               $cb_name = $proc{$fc_exec_names{$type}};
-       }
-       elsif (FC_CB_CREATE == $cb_type) {
-               if (defined $proc{'create'}) {
-                       $cb_name = $proc{'create'};
-               }
-               else {
-                       return 1;
-               }
-       }
-       elsif (FC_CB_DESTROY == $cb_type) {
-               if (defined $proc{'destroy'}) {
-                       $cb_name = $proc{'destroy'};
-               }
-               else {
-                       return 1;
-               }
-       }
-
-       $status = call_by_name (@_);
-
-       if ($status < 0) {
-               my $err = undef;
-
-               if ($@) {
-                       $err = $@;
-               }
-               else {
-                       $err = "callback returned false";
-               }
-
-               ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
-               return;
-       }
-       return $status;
-}
-
-sub fc_register {
-       my $type = shift;
-       my $name = shift;
-       my $proc = shift;
-
-       my %fc : shared;
-
-       DEBUG ("Collectd::fc_register: "
-               . "type = \"$type\", name = \"$name\", proc = \"$proc\"");
-
-       if (! ((defined $type) && (defined $name) && (defined $proc))) {
-               ERROR ("Usage: Collectd::fc_register(type, name, proc)");
-               return;
-       }
-
-       if (! defined $fc_plugins[$type]) {
-               ERROR ("Collectd::fc_register: Invalid type \"$type\"");
-               return;
-       }
-
-       if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
-                       || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
-               ERROR ("Collectd::fc_register: Invalid proc.");
-               return;
-       }
-
-       for my $p (qw( create destroy )) {
-               if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
-                       ERROR ("Collectd::fc_register: Invalid proc.");
-                       return;
-               }
-       }
-
-       %fc = %$proc;
-
-       foreach my $p (keys %fc) {
-               my $pkg = scalar caller;
-
-               if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
-                       next;
-               }
-
-               if ($fc{$p} !~ m/^$pkg\:\:/) {
-                       $fc{$p} = $pkg . "::" . $fc{$p};
-               }
-       }
-
-       lock %{$fc_plugins[$type]};
-       if (defined $fc_plugins[$type]->{$name}) {
-               WARNING ("Collectd::fc_register: Overwriting previous "
-                       . "definition of match \"$name\".");
-       }
-
-       if (! _fc_register ($type, $name)) {
-               ERROR ("Collectd::fc_register: Failed to register \"$name\".");
-               return;
-       }
-
-       $fc_plugins[$type]->{$name} = \%fc;
-       return 1;
-}
-
-sub _plugin_dispatch_config {
-       my $plugin = shift;
-       my $config = shift;
-
-       our $cb_name = undef;
-
-       if (! (defined ($plugin) && defined ($config))) {
-               return;
-       }
-
-       if (! defined $cf_callbacks{$plugin}) {
-               WARNING ("Found a configuration for the \"$plugin\" plugin, but "
-                       . "the plugin isn't loaded or didn't register "
-                       . "a configuration callback.");
-               return;
-       }
-
-       {
-               lock %cf_callbacks;
-               $cb_name = $cf_callbacks{$plugin};
-       }
-       call_by_name ($config);
-}
-
-1;
-
-# vim: set sw=4 ts=4 tw=78 noexpandtab :
-
diff --git a/bindings/perl/Collectd/Makefile.PL b/bindings/perl/Collectd/Makefile.PL
deleted file mode 100644 (file)
index baf7166..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-use ExtUtils::MakeMaker;
-
-WriteMakefile(
-       'NAME'          => 'Collectd::Unixsock',
-       'AUTHOR'        => 'Florian Forster <octo@verplant.org>',
-);
-
-# vim: set sw=4 ts=4 tw=78 noexpandtab :
diff --git a/bindings/perl/Collectd/Unixsock.pm b/bindings/perl/Collectd/Unixsock.pm
deleted file mode 100644 (file)
index 199a47c..0000000
+++ /dev/null
@@ -1,656 +0,0 @@
-#
-# collectd - Collectd::Unixsock
-# Copyright (C) 2007,2008  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
-#
-# Author:
-#   Florian octo Forster <octo at verplant.org>
-#
-
-package Collectd::Unixsock;
-
-=head1 NAME
-
-Collectd::Unixsock - Abstraction layer for accessing the functionality by
-collectd's unixsock plugin.
-
-=head1 SYNOPSIS
-
-  use Collectd::Unixsock ();
-
-  my $sock = Collectd::Unixsock->new ($path);
-
-  my $value = $sock->getval (%identifier);
-  $sock->putval (%identifier,
-                 time => time (),
-                values => [123, 234, 345]);
-
-  $sock->destroy ();
-
-=head1 DESCRIPTION
-
-collectd's unixsock plugin allows external programs to access the values it has
-collected or received and to submit own values. This Perl-module is simply a
-little abstraction layer over this interface to make it even easier for
-programmers to interact with the daemon.
-
-=cut
-
-use strict;
-use warnings;
-
-#use constant { NOTIF_FAILURE => 1, NOTIF_WARNING => 2, NOTIF_OKAY => 4 };
-
-use Carp (qw(cluck confess));
-use IO::Socket::UNIX;
-use Regexp::Common (qw(number));
-
-our $Debug = 0;
-
-return (1);
-
-sub _debug
-{
-       if (!$Debug)
-       {
-               return;
-       }
-       print @_;
-}
-
-sub _create_socket
-{
-       my $path = shift;
-       my $sock = IO::Socket::UNIX->new (Type => SOCK_STREAM, Peer => $path);
-       if (!$sock)
-       {
-               cluck ("Cannot open UNIX-socket $path: $!");
-               return;
-       }
-       return ($sock);
-} # _create_socket
-
-=head1 VALUE IDENTIFIERS
-
-The values in the collectd are identified using an five-tuple (host, plugin,
-plugin-instance, type, type-instance) where only plugin-instance and
-type-instance may be NULL (or undefined). Many functions expect an
-I<%identifier> hash that has at least the members B<host>, B<plugin>, and
-B<type>, possibly completed by B<plugin_instance> and B<type_instance>.
-
-Usually you can pass this hash as follows:
-
-  $obj->method (host => $host, plugin => $plugin, type => $type, %other_args);
-
-=cut
-
-sub _create_identifier
-{
-       my $args = shift;
-       my $host;
-       my $plugin;
-       my $type;
-
-       if (!$args->{'host'} || !$args->{'plugin'} || !$args->{'type'})
-       {
-               cluck ("Need `host', `plugin' and `type'");
-               return;
-       }
-
-       $host = $args->{'host'};
-       $plugin = $args->{'plugin'};
-       $plugin .= '-' . $args->{'plugin_instance'} if (defined ($args->{'plugin_instance'}));
-       $type = $args->{'type'};
-       $type .= '-' . $args->{'type_instance'} if (defined ($args->{'type_instance'}));
-
-       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
-
-sub _escape_argument
-{
-       my $string = shift;
-
-       if ($string =~ m/^\w+$/)
-       {
-               return ("$string");
-       }
-
-       $string =~ s#\\#\\\\#g;
-       $string =~ s#"#\\"#g;
-       $string = "\"$string\"";
-
-       return ($string);
-}
-
-=head1 PUBLIC METHODS
-
-=over 4
-
-=item I<$obj> = Collectd::Unixsock->B<new> ([I<$path>]);
-
-Creates a new connection to the daemon. The optional I<$path> argument gives
-the path to the UNIX socket of the C<unixsock plugin> and defaults to
-F</var/run/collectd-unixsock>. Returns the newly created object on success and
-false on error.
-
-=cut
-
-sub new
-{
-       my $pkg = shift;
-       my $path = @_ ? shift : '/var/run/collectd-unixsock';
-       my $sock = _create_socket ($path) or return;
-       my $obj = bless (
-               {
-                       path => $path,
-                       sock => $sock,
-                       error => 'No error'
-               }, $pkg);
-       return ($obj);
-} # new
-
-=item I<$res> = I<$obj>-E<gt>B<getval> (I<%identifier>);
-
-Requests a value-list from the daemon. On success a hash-ref is returned with
-the name of each data-source as the key and the according value as, well, the
-value. On error false is returned.
-
-=cut
-
-sub getval # {{{
-{
-       my $obj = shift;
-       my %args = @_;
-
-       my $status;
-       my $fh = $obj->{'sock'} or confess ('object has no filehandle');
-       my $msg;
-       my $identifier;
-
-       my $ret = {};
-
-       $identifier = _create_identifier (\%args) or return;
-
-       $msg = 'GETVAL ' . _escape_argument ($identifier) . "\n";
-       _debug "-> $msg";
-       print $fh $msg;
-
-       $msg = <$fh>;
-       chomp ($msg);
-       _debug "<- $msg\n";
-
-       ($status, $msg) = split (' ', $msg, 2);
-       if ($status <= 0)
-       {
-               $obj->{'error'} = $msg;
-               return;
-       }
-
-       for (my $i = 0; $i < $status; $i++)
-       {
-               my $entry = <$fh>;
-               chomp ($entry);
-               _debug "<- $entry\n";
-
-               if ($entry =~ m/^(\w+)=NaN$/)
-               {
-                       $ret->{$1} = undef;
-               }
-               elsif ($entry =~ m/^(\w+)=($RE{num}{real})$/)
-               {
-                       $ret->{$1} = 0.0 + $2;
-               }
-       }
-
-       return ($ret);
-} # }}} sub getval
-
-=item I<$res> = I<$obj>-E<gt>B<getthreshold> (I<%identifier>);
-
-Requests a threshold from the daemon. On success a hash-ref is returned with
-the threshold data. On error false is returned.
-
-=cut
-
-sub getthreshold # {{{
-{
-       my $obj = shift;
-       my %args = @_;
-
-       my $status;
-       my $fh = $obj->{'sock'} or confess ('object has no filehandle');
-       my $msg;
-       my $identifier;
-
-       my $ret = {};
-
-       $identifier = _create_identifier (\%args) or return;
-
-       $msg = 'GETTHRESHOLD ' . _escape_argument ($identifier) . "\n";
-       _debug "-> $msg";
-       print $fh $msg;
-
-       $msg = <$fh>;
-       chomp ($msg);
-       _debug "<- $msg\n";
-
-       ($status, $msg) = split (' ', $msg, 2);
-       if ($status <= 0)
-       {
-               $obj->{'error'} = $msg;
-               return;
-       }
-
-       for (my $i = 0; $i < $status; $i++)
-       {
-               my $entry = <$fh>;
-               chomp ($entry);
-               _debug "<- $entry\n";
-
-               if ($entry =~ m/^([^:]+):\s*(\S.*)$/)
-               {
-                       my $key = $1;
-                       my $value = $2;
-
-                       $key =~ s/^\s+//;
-                       $key =~ s/\s+$//;
-
-                       $ret->{$key} = $value;
-               }
-       }
-
-       return ($ret);
-} # }}} sub getthreshold
-
-=item I<$obj>-E<gt>B<putval> (I<%identifier>, B<time> =E<gt> I<$time>, B<values> =E<gt> [...]);
-
-Submits a value-list to the daemon. If the B<time> argument is omitted
-C<time()> is used. The required argument B<values> is a reference to an array
-of values that is to be submitted. The number of values must match the number
-of values expected for the given B<type> (see L<VALUE IDENTIFIERS>), though
-this is checked by the daemon, not the Perl module. Also, gauge data-sources
-(e.E<nbsp>g. system-load) may be C<undef>. Returns true upon success and false
-otherwise.
-
-=cut
-
-sub putval
-{
-       my $obj = shift;
-       my %args = @_;
-
-       my $status;
-       my $fh = $obj->{'sock'} or confess;
-       my $msg;
-       my $identifier;
-       my $values;
-       my $interval = "";
-
-       if (defined $args{'interval'})
-       {
-               $interval = ' interval='
-               . _escape_argument ($args{'interval'});
-       }
-
-       $identifier = _create_identifier (\%args) or return;
-       if (!$args{'values'})
-       {
-               cluck ("Need argument `values'");
-               return;
-       }
-
-       if (!ref ($args{'values'}))
-       {
-               $values = $args{'values'};
-       }
-       else
-       {
-               my $time;
-
-               if ("ARRAY" ne ref ($args{'values'}))
-               {
-                       cluck ("Invalid `values' argument (expected an array ref)");
-                       return;
-               }
-
-               if (! scalar @{$args{'values'}})
-               {
-                       cluck ("Empty `values' array");
-                       return;
-               }
-
-               $time = $args{'time'} ? $args{'time'} : time ();
-               $values = join (':', $time, map { defined ($_) ? $_ : 'U' } (@{$args{'values'}}));
-       }
-
-       $msg = 'PUTVAL '
-       . _escape_argument ($identifier)
-       . $interval
-       . ' ' . _escape_argument ($values) . "\n";
-       _debug "-> $msg";
-       print $fh $msg;
-
-       $msg = <$fh>;
-       chomp ($msg);
-       _debug "<- $msg\n";
-
-       ($status, $msg) = split (' ', $msg, 2);
-       return (1) if ($status == 0);
-
-       $obj->{'error'} = $msg;
-       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;
-
-       _debug "LISTVAL\n";
-       print $fh "LISTVAL\n";
-
-       $msg = <$fh>;
-       chomp ($msg);
-       _debug "<- $msg\n";
-       ($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);
-               _debug "<- $msg\n";
-
-               ($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<$res> = I<$obj>-E<gt>B<putnotif> (B<severity> =E<gt> I<$severity>, B<message> =E<gt> I<$message>, ...);
-
-Submits a notification to the daemon.
-
-Valid options are:
-
-=over 4
-
-=item B<severity>
-
-Sets the severity of the notification. The value must be one of the following
-strings: C<failure>, C<warning>, or C<okay>. Case does not matter. This option
-is mandatory.
-
-=item B<message>
-
-Sets the message of the notification. This option is mandatory.
-
-=item B<time>
-
-Sets the time. If omitted, C<time()> is used.
-
-=item I<Value identifier>
-
-All the other fields of the value identifiers, B<host>, B<plugin>,
-B<plugin_instance>, B<type>, and B<type_instance>, are optional. When given,
-the notification is associated with the performance data of that identifier.
-For more details, please see L<collectd-unixsock(5)>.
-
-=back
-
-=cut
-
-sub putnotif
-{
-       my $obj = shift;
-       my %args = @_;
-
-       my $status;
-       my $fh = $obj->{'sock'} or confess;
-
-       my $msg; # message sent to the socket
-       
-       if (!$args{'message'})
-       {
-               cluck ("Need argument `message'");
-               return;
-       }
-       if (!$args{'severity'})
-       {
-               cluck ("Need argument `severity'");
-               return;
-       }
-       $args{'severity'} = lc ($args{'severity'});
-       if (($args{'severity'} ne 'failure')
-               && ($args{'severity'} ne 'warning')
-               && ($args{'severity'} ne 'okay'))
-       {
-               cluck ("Invalid `severity: " . $args{'severity'});
-               return;
-       }
-
-       if (!$args{'time'})
-       {
-               $args{'time'} = time ();
-       }
-       
-       $msg = 'PUTNOTIF '
-       . join (' ', map { $_ . '=' . _escape_argument ($args{$_}) } (keys %args))
-       . "\n";
-
-       _debug "-> $msg";
-       print $fh $msg;
-
-       $msg = <$fh>;
-       chomp ($msg);
-       _debug "<- $msg\n";
-
-       ($status, $msg) = split (' ', $msg, 2);
-       return (1) if ($status == 0);
-
-       $obj->{'error'} = $msg;
-       return;
-} # putnotif
-
-=item I<$obj>-E<gt>B<flush> (B<timeout> =E<gt> I<$timeout>, B<plugins> =E<gt> [...], B<identifier>  =E<gt> [...]);
-
-Flush cached data.
-
-Valid options are:
-
-=over 4
-
-=item B<timeout>
-
-If this option is specified, only data older than I<$timeout> seconds is
-flushed.
-
-=item B<plugins>
-
-If this option is specified, only the selected plugins will be flushed. The
-argument is a reference to an array of strings.
-
-=item B<identifier>
-
-If this option is specified, only the given identifier(s) will be flushed. The
-argument is a reference to an array of identifiers. Identifiers, in this case,
-are hash references and have the members as outlined in L<VALUE IDENTIFIERS>.
-
-=back
-
-=cut
-
-sub flush
-{
-       my $obj  = shift;
-       my %args = @_;
-
-       my $fh = $obj->{'sock'} or confess;
-
-       my $status = 0;
-       my $msg    = "FLUSH";
-
-       if (defined ($args{'timeout'}))
-       {
-               $msg .= " timeout=" . $args{'timeout'};
-       }
-
-       if ($args{'plugins'})
-       {
-               foreach my $plugin (@{$args{'plugins'}})
-               {
-                       $msg .= " plugin=" . $plugin;
-               }
-       }
-
-       if ($args{'identifier'})
-       {
-               for (@{$args{'identifier'}})
-               {
-                       my $identifier = $_;
-                       my $ident_str;
-
-                       if (ref ($identifier) ne 'HASH')
-                       {
-                               cluck ("The argument of the `identifier' "
-                                       . "option must be an array reference "
-                                       . "of hash references.");
-                               return;
-                       }
-
-                       $ident_str = _create_identifier ($identifier);
-                       if (!$ident_str)
-                       {
-                               return;
-                       }
-
-                       $msg .= ' identifier=' . _escape_argument ($ident_str);
-               }
-       }
-
-       $msg .= "\n";
-
-       _debug "-> $msg";
-       print $fh $msg;
-
-       $msg = <$fh>;
-       chomp ($msg);
-       _debug "<- $msg\n";
-
-       ($status, $msg) = split (' ', $msg, 2);
-       return (1) if ($status == 0);
-
-       $obj->{'error'} = $msg;
-       return;
-}
-
-sub error
-{
-       my $obj = shift;
-       if ($obj->{'error'})
-       {
-               return ($obj->{'error'});
-       }
-       return;
-}
-
-=item I<$obj>-E<gt>destroy ();
-
-Closes the socket before the object is destroyed. This function is also
-automatically called then the object goes out of scope.
-
-=back
-
-=cut
-
-sub destroy
-{
-       my $obj = shift;
-       if ($obj->{'sock'})
-       {
-               close ($obj->{'sock'});
-               delete ($obj->{'sock'});
-       }
-}
-
-sub DESTROY
-{
-       my $obj = shift;
-       $obj->destroy ();
-}
-
-=head1 SEE ALSO
-
-L<collectd(1)>,
-L<collectd.conf(5)>,
-L<collectd-unixsock(5)>
-
-=head1 AUTHOR
-
-Florian octo Forster E<lt>octo@verplant.orgE<gt>
-
-=cut
-
-# vim: set fdm=marker :
diff --git a/bindings/perl/lib/Collectd.pm b/bindings/perl/lib/Collectd.pm
new file mode 100644 (file)
index 0000000..f1b5d85
--- /dev/null
@@ -0,0 +1,662 @@
+# collectd - Collectd.pm
+# Copyright (C) 2007-2009  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>
+
+package Collectd;
+
+use strict;
+use warnings;
+
+use Config;
+
+use threads;
+use threads::shared;
+
+BEGIN {
+       if (! $Config{'useithreads'}) {
+               die "Perl does not support ithreads!";
+       }
+}
+
+require Exporter;
+
+our @ISA = qw( Exporter );
+
+our %EXPORT_TAGS = (
+       'plugin' => [ qw(
+                       plugin_register
+                       plugin_unregister
+                       plugin_dispatch_values
+                       plugin_write
+                       plugin_flush
+                       plugin_flush_one
+                       plugin_flush_all
+                       plugin_dispatch_notification
+                       plugin_log
+       ) ],
+       'types' => [ qw(
+                       TYPE_INIT
+                       TYPE_READ
+                       TYPE_WRITE
+                       TYPE_SHUTDOWN
+                       TYPE_LOG
+                       TYPE_NOTIF
+                       TYPE_FLUSH
+                       TYPE_CONFIG
+                       TYPE_DATASET
+       ) ],
+       'ds_types' => [ qw(
+                       DS_TYPE_COUNTER
+                       DS_TYPE_GAUGE
+       ) ],
+       'log' => [ qw(
+                       ERROR
+                       WARNING
+                       NOTICE
+                       INFO
+                       DEBUG
+                       LOG_ERR
+                       LOG_WARNING
+                       LOG_NOTICE
+                       LOG_INFO
+                       LOG_DEBUG
+       ) ],
+       'filter_chain' => [ qw(
+                       fc_register
+                       FC_MATCH_NO_MATCH
+                       FC_MATCH_MATCHES
+                       FC_TARGET_CONTINUE
+                       FC_TARGET_STOP
+                       FC_TARGET_RETURN
+       ) ],
+       'fc_types' => [ qw(
+                       FC_MATCH
+                       FC_TARGET
+       ) ],
+       'notif' => [ qw(
+                       NOTIF_FAILURE
+                       NOTIF_WARNING
+                       NOTIF_OKAY
+       ) ],
+       'globals' => [ qw(
+                       $hostname_g
+                       $interval_g
+       ) ],
+);
+
+{
+       my %seen;
+       push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
+               foreach keys %EXPORT_TAGS;
+}
+
+# global variables
+our $hostname_g;
+our $interval_g;
+
+Exporter::export_ok_tags ('all');
+
+my @plugins : shared = ();
+my @fc_plugins : shared = ();
+my %cf_callbacks : shared = ();
+
+my %types = (
+       TYPE_CONFIG,   "config",
+       TYPE_INIT,     "init",
+       TYPE_READ,     "read",
+       TYPE_WRITE,    "write",
+       TYPE_SHUTDOWN, "shutdown",
+       TYPE_LOG,      "log",
+       TYPE_NOTIF,    "notify",
+       TYPE_FLUSH,    "flush"
+);
+
+my %fc_types = (
+       FC_MATCH,  "match",
+       FC_TARGET, "target"
+);
+
+my %fc_exec_names = (
+       FC_MATCH,  "match",
+       FC_TARGET, "invoke"
+);
+
+my %fc_cb_types = (
+       FC_CB_EXEC, "exec",
+       FC_CB_CREATE, "create",
+       FC_CB_DESTROY, "destroy"
+);
+
+foreach my $type (keys %types) {
+       $plugins[$type] = &share ({});
+}
+
+foreach my $type (keys %fc_types) {
+       $fc_plugins[$type] = &share ({});
+}
+
+sub _log {
+       my $caller = shift;
+       my $lvl    = shift;
+       my $msg    = shift;
+
+       if ("Collectd" eq $caller) {
+               $msg = "perl: $msg";
+       }
+       return plugin_log ($lvl, $msg);
+}
+
+sub ERROR   { _log (scalar caller, LOG_ERR,     shift); }
+sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
+sub NOTICE  { _log (scalar caller, LOG_NOTICE,  shift); }
+sub INFO    { _log (scalar caller, LOG_INFO,    shift); }
+sub DEBUG   { _log (scalar caller, LOG_DEBUG,   shift); }
+
+sub plugin_call_all {
+       my $type = shift;
+
+       my %plugins;
+
+       our $cb_name = undef;
+
+       if (! defined $type) {
+               return;
+       }
+
+       if (TYPE_LOG != $type) {
+               DEBUG ("Collectd::plugin_call: type = \"$type\" ("
+                       . $types{$type} . "), args=\""
+                       . join(', ', map { defined($_) ? $_ : '<undef>' } @_) . "\"");
+       }
+
+       if (! defined $plugins[$type]) {
+               ERROR ("Collectd::plugin_call: unknown type \"$type\"");
+               return;
+       }
+
+       {
+               lock %{$plugins[$type]};
+               %plugins = %{$plugins[$type]};
+       }
+
+       foreach my $plugin (keys %plugins) {
+               my $p = $plugins{$plugin};
+
+               my $status = 0;
+
+               if ($p->{'wait_left'} > 0) {
+                       $p->{'wait_left'} -= $interval_g;
+               }
+
+               next if ($p->{'wait_left'} > 0);
+
+               $cb_name = $p->{'cb_name'};
+               $status = call_by_name (@_);
+
+               if (! $status) {
+                       my $err = undef;
+
+                       if ($@) {
+                               $err = $@;
+                       }
+                       else {
+                               $err = "callback returned false";
+                       }
+
+                       if (TYPE_LOG != $type) {
+                               ERROR ("Execution of callback \"$cb_name\" failed: $err");
+                       }
+
+                       $status = 0;
+               }
+
+               if ($status) {
+                       $p->{'wait_left'} = 0;
+                       $p->{'wait_time'} = $interval_g;
+               }
+               elsif (TYPE_READ == $type) {
+                       if ($p->{'wait_time'} < $interval_g) {
+                               $p->{'wait_time'} = $interval_g;
+                       }
+
+                       $p->{'wait_left'} = $p->{'wait_time'};
+                       $p->{'wait_time'} *= 2;
+
+                       if ($p->{'wait_time'} > 86400) {
+                               $p->{'wait_time'} = 86400;
+                       }
+
+                       WARNING ("${plugin}->read() failed with status $status. "
+                               . "Will suspend it for $p->{'wait_left'} seconds.");
+               }
+               elsif (TYPE_INIT == $type) {
+                       ERROR ("${plugin}->init() failed with status $status. "
+                               . "Plugin will be disabled.");
+
+                       foreach my $type (keys %types) {
+                               plugin_unregister ($type, $plugin);
+                       }
+               }
+               elsif (TYPE_LOG != $type) {
+                       WARNING ("${plugin}->$types{$type}() failed with status $status.");
+               }
+       }
+       return 1;
+}
+
+# Collectd::plugin_register (type, name, data).
+#
+# type:
+#   init, read, write, shutdown, data set
+#
+# name:
+#   name of the plugin
+#
+# data:
+#   reference to the plugin's subroutine that does the work or the data set
+#   definition
+sub plugin_register {
+       my $type = shift;
+       my $name = shift;
+       my $data = shift;
+
+       DEBUG ("Collectd::plugin_register: "
+               . "type = \"$type\" (" . $types{$type}
+               . "), name = \"$name\", data = \"$data\"");
+
+       if (! ((defined $type) && (defined $name) && (defined $data))) {
+               ERROR ("Usage: Collectd::plugin_register (type, name, data)");
+               return;
+       }
+
+       if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
+                       && (TYPE_CONFIG != $type)) {
+               ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
+               return;
+       }
+
+       if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
+               return plugin_register_data_set ($name, $data);
+       }
+       elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
+               my $pkg = scalar caller;
+
+               if ($data !~ m/^$pkg\:\:/) {
+                       $data = $pkg . "::" . $data;
+               }
+
+               lock %cf_callbacks;
+               $cf_callbacks{$name} = $data;
+       }
+       elsif ((TYPE_DATASET != $type) && (! ref $data)) {
+               my $pkg = scalar caller;
+
+               my %p : shared;
+
+               if ($data !~ m/^$pkg\:\:/) {
+                       $data = $pkg . "::" . $data;
+               }
+
+               %p = (
+                       wait_time => $interval_g,
+                       wait_left => 0,
+                       cb_name   => $data,
+               );
+
+               lock %{$plugins[$type]};
+               $plugins[$type]->{$name} = \%p;
+       }
+       else {
+               ERROR ("Collectd::plugin_register: Invalid data.");
+               return;
+       }
+       return 1;
+}
+
+sub plugin_unregister {
+       my $type = shift;
+       my $name = shift;
+
+       DEBUG ("Collectd::plugin_unregister: type = \"$type\" ("
+               . $types{$type} . "), name = \"$name\"");
+
+       if (! ((defined $type) && (defined $name))) {
+               ERROR ("Usage: Collectd::plugin_unregister (type, name)");
+               return;
+       }
+
+       if (TYPE_DATASET == $type) {
+               return plugin_unregister_data_set ($name);
+       }
+       elsif (TYPE_CONFIG == $type) {
+               lock %cf_callbacks;
+               delete $cf_callbacks{$name};
+       }
+       elsif (defined $plugins[$type]) {
+               lock %{$plugins[$type]};
+               delete $plugins[$type]->{$name};
+       }
+       else {
+               ERROR ("Collectd::plugin_unregister: Invalid type.");
+               return;
+       }
+}
+
+sub plugin_write {
+       my %args = @_;
+
+       my @plugins    = ();
+       my @datasets   = ();
+       my @valuelists = ();
+
+       if (! defined $args{'valuelists'}) {
+               ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
+               return;
+       }
+
+       DEBUG ("Collectd::plugin_write:"
+               . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
+               . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
+               . " valueslists = $args{'valuelists'}");
+
+       if (defined ($args{'plugins'})) {
+               if ("ARRAY" eq ref ($args{'plugins'})) {
+                       @plugins = @{$args{'plugins'}};
+               }
+               else {
+                       @plugins = ($args{'plugins'});
+               }
+       }
+       else {
+               @plugins = (undef);
+       }
+
+       if ("ARRAY" eq ref ($args{'valuelists'})) {
+               @valuelists = @{$args{'valuelists'}};
+       }
+       else {
+               @valuelists = ($args{'valuelists'});
+       }
+
+       if (defined ($args{'datasets'})) {
+               if ("ARRAY" eq ref ($args{'datasets'})) {
+                       @datasets = @{$args{'datasets'}};
+               }
+               else {
+                       @datasets = ($args{'datasets'});
+               }
+       }
+       else {
+               @datasets = (undef) x scalar (@valuelists);
+       }
+
+       if ($#datasets != $#valuelists) {
+               ERROR ("Collectd::plugin_write: Invalid number of datasets.");
+               return;
+       }
+
+       foreach my $plugin (@plugins) {
+               for (my $i = 0; $i < scalar (@valuelists); ++$i) {
+                       _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
+               }
+       }
+}
+
+sub plugin_flush {
+       my %args = @_;
+
+       my $timeout = -1;
+       my @plugins = ();
+       my @ids     = ();
+
+       DEBUG ("Collectd::plugin_flush:"
+               . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
+               . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
+               . (defined ($args{'identifiers'})
+                       ? " identifiers = $args{'identifiers'}" : ""));
+
+       if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
+               $timeout = $args{'timeout'};
+       }
+
+       if (defined ($args{'plugins'})) {
+               if ("ARRAY" eq ref ($args{'plugins'})) {
+                       @plugins = @{$args{'plugins'}};
+               }
+               else {
+                       @plugins = ($args{'plugins'});
+               }
+       }
+       else {
+               @plugins = (undef);
+       }
+
+       if (defined ($args{'identifiers'})) {
+               if ("ARRAY" eq ref ($args{'identifiers'})) {
+                       @ids = @{$args{'identifiers'}};
+               }
+               else {
+                       @ids = ($args{'identifiers'});
+               }
+       }
+       else {
+               @ids = (undef);
+       }
+
+       foreach my $plugin (@plugins) {
+               foreach my $id (@ids) {
+                       _plugin_flush($plugin, $timeout, $id);
+               }
+       }
+}
+
+sub plugin_flush_one {
+       my $timeout = shift;
+       my $name    = shift;
+
+       WARNING ("Collectd::plugin_flush_one is deprecated - "
+               . "use Collectd::plugin_flush instead.");
+
+       if (! (defined ($timeout) && defined ($name))) {
+               ERROR ("Usage: Collectd::plugin_flush_one(timeout, name)");
+               return;
+       }
+
+       plugin_flush (plugins => $name, timeout => $timeout);
+}
+
+sub plugin_flush_all {
+       my $timeout = shift;
+
+       WARNING ("Collectd::plugin_flush_all is deprecated - "
+               . "use Collectd::plugin_flush instead.");
+
+       if (! defined ($timeout)) {
+               ERROR ("Usage: Collectd::plugin_flush_all(timeout)");
+               return;
+       }
+
+       plugin_flush (timeout => $timeout);
+}
+
+sub fc_call {
+       my $type    = shift;
+       my $name    = shift;
+       my $cb_type = shift;
+
+       my %proc;
+
+       our $cb_name = undef;
+       my  $status;
+
+       if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
+               ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
+               return;
+       }
+
+       if (! defined $fc_plugins[$type]) {
+               ERROR ("Collectd::fc_call: Invalid type \"$type\"");
+               return;
+       }
+
+       if (! defined $fc_plugins[$type]->{$name}) {
+               ERROR ("Collectd::fc_call: Unknown "
+                       . ($type == FC_MATCH ? "match" : "target")
+                       . " \"$name\"");
+               return;
+       }
+
+       DEBUG ("Collectd::fc_call: "
+               . "type = \"$type\" (" . $fc_types{$type}
+               . "), name = \"$name\", cb_type = \"$cb_type\" ("
+               . $fc_cb_types{$cb_type} . ")");
+
+       {
+               lock %{$fc_plugins[$type]};
+               %proc = %{$fc_plugins[$type]->{$name}};
+       }
+
+       if (FC_CB_EXEC == $cb_type) {
+               $cb_name = $proc{$fc_exec_names{$type}};
+       }
+       elsif (FC_CB_CREATE == $cb_type) {
+               if (defined $proc{'create'}) {
+                       $cb_name = $proc{'create'};
+               }
+               else {
+                       return 1;
+               }
+       }
+       elsif (FC_CB_DESTROY == $cb_type) {
+               if (defined $proc{'destroy'}) {
+                       $cb_name = $proc{'destroy'};
+               }
+               else {
+                       return 1;
+               }
+       }
+
+       $status = call_by_name (@_);
+
+       if ($status < 0) {
+               my $err = undef;
+
+               if ($@) {
+                       $err = $@;
+               }
+               else {
+                       $err = "callback returned false";
+               }
+
+               ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
+               return;
+       }
+       return $status;
+}
+
+sub fc_register {
+       my $type = shift;
+       my $name = shift;
+       my $proc = shift;
+
+       my %fc : shared;
+
+       DEBUG ("Collectd::fc_register: "
+               . "type = \"$type\" (" . $fc_types{$type}
+               . "), name = \"$name\", proc = \"$proc\"");
+
+       if (! ((defined $type) && (defined $name) && (defined $proc))) {
+               ERROR ("Usage: Collectd::fc_register(type, name, proc)");
+               return;
+       }
+
+       if (! defined $fc_plugins[$type]) {
+               ERROR ("Collectd::fc_register: Invalid type \"$type\"");
+               return;
+       }
+
+       if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
+                       || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
+               ERROR ("Collectd::fc_register: Invalid proc.");
+               return;
+       }
+
+       for my $p (qw( create destroy )) {
+               if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
+                       ERROR ("Collectd::fc_register: Invalid proc.");
+                       return;
+               }
+       }
+
+       %fc = %$proc;
+
+       foreach my $p (keys %fc) {
+               my $pkg = scalar caller;
+
+               if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
+                       next;
+               }
+
+               if ($fc{$p} !~ m/^$pkg\:\:/) {
+                       $fc{$p} = $pkg . "::" . $fc{$p};
+               }
+       }
+
+       lock %{$fc_plugins[$type]};
+       if (defined $fc_plugins[$type]->{$name}) {
+               WARNING ("Collectd::fc_register: Overwriting previous "
+                       . "definition of match \"$name\".");
+       }
+
+       if (! _fc_register ($type, $name)) {
+               ERROR ("Collectd::fc_register: Failed to register \"$name\".");
+               return;
+       }
+
+       $fc_plugins[$type]->{$name} = \%fc;
+       return 1;
+}
+
+sub _plugin_dispatch_config {
+       my $plugin = shift;
+       my $config = shift;
+
+       our $cb_name = undef;
+
+       if (! (defined ($plugin) && defined ($config))) {
+               return;
+       }
+
+       if (! defined $cf_callbacks{$plugin}) {
+               WARNING ("Found a configuration for the \"$plugin\" plugin, but "
+                       . "the plugin isn't loaded or didn't register "
+                       . "a configuration callback.");
+               return;
+       }
+
+       {
+               lock %cf_callbacks;
+               $cb_name = $cf_callbacks{$plugin};
+       }
+       call_by_name ($config);
+}
+
+1;
+
+# vim: set sw=4 ts=4 tw=78 noexpandtab :
+
diff --git a/bindings/perl/lib/Collectd/Plugins/Monitorus.pm b/bindings/perl/lib/Collectd/Plugins/Monitorus.pm
new file mode 100644 (file)
index 0000000..7054fbf
--- /dev/null
@@ -0,0 +1,104 @@
+#
+# collectd - mon.itor.us collectd plugin
+# Copyright (C) 2009  Jeff Green
+#
+# 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:
+#   Jeff Green <jeff at kikisoso.org>
+#
+
+package Collectd::Plugins::Monitorus;
+
+use strict;
+use warnings;
+
+use Collectd qw( :all );
+use LWP;
+use threads::shared;
+
+use constant NUM_OF_INTERVALS => 90;
+
+my $intervalcnt :shared;
+$intervalcnt=NUM_OF_INTERVALS;
+my $prev_value :shared;
+$prev_value=0;
+
+plugin_register (TYPE_READ, "monitorus", "monitorus_read");
+
+sub monitorus_read
+{
+        my $vl = { plugin => 'monitorus', type => 'gauge' };
+
+        # Only retrieve a value occasionally in order to not overload mon.itor.us
+        if (++$intervalcnt<NUM_OF_INTERVALS) { # e.g. 180 * 10 secs / 60 seconds/min = 30 minutes
+                $vl->{'values'} = [ $prev_value ];
+                plugin_dispatch_values ($vl);
+                return 1;
+        }
+
+        $intervalcnt=0;
+
+        my $site = 'http://mon.itor.us';
+        my $username = 'me@example.org';
+        my $target = $site.'/user/api/'.$username.'/secretpassword';
+
+        my $ua = LWP::UserAgent->new;
+        my $req = HTTP::Request->new(GET => "$target");
+        $req->header('Accept' => 'text/html');          #Accept HTML Page
+
+        my $key;
+        my $res = $ua->get($target);
+        if ($res->is_success) {# Success....all content of page has been received
+                $res->content() =~ m/\[CDATA\[(.*)\]\]/;
+                $key = $1;
+        } else {
+                INFO("monitorus: Error in retrieving login page.");
+        }
+
+        $target = $site.'/test/api/'.$key.'/testNames';
+        my $testid;
+        $res = $ua->get($target);
+        if ($res->is_success) {# Success....all content of page has been received
+                $res->content() =~ m/<test id='(.*)'><!\[CDATA\[sitetest_http\]\]/;
+                $testid = $1;
+        } else {
+                INFO("monitorus: Error in retrieving testNames page.");
+        }
+
+        #$target = $site.'/test/api/'.$key.'/testinfo/'.$testid.'/-240';
+        #$target = $site.'/test/api/'.$key.'/test/'.$testid.'/27/5/2009/1/3/-240';
+        $target = $site.'/test/api/'.$key.'/testsLastValues/1/3';
+
+        my $result;
+        my $value;
+        $res = $ua->get($target);
+        if ($res->is_success) {# Success....all content of page has been received
+                $res->content() =~ m/\<\/row\>\s*(\<row\>.*?sitetest_http.*?\<\/row\>)/s;
+                $result = $1;
+                $result =~ s/\<cell\>.*?CDATA.*?\<\/cell\>//g;
+                $result =~ m|\<cell\>([0-9]*)\<\/cell\>|;
+                $value = $1;
+        } else {
+                INFO("monitorus: Error in retrieving testsLastValues page.");
+        }
+
+        $prev_value = $value;
+        $vl->{'values'} = [ $value ];
+        plugin_dispatch_values ($vl);
+
+        return 1;
+}
+
+1;
diff --git a/bindings/perl/lib/Collectd/Plugins/OpenVZ.pm b/bindings/perl/lib/Collectd/Plugins/OpenVZ.pm
new file mode 100644 (file)
index 0000000..2944157
--- /dev/null
@@ -0,0 +1,190 @@
+#
+# collectd - OpenVZ collectd plugin
+# Copyright (C) 2009  Jonathan Kolb
+#
+# 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; either version 2 of the License, or (at your option) any later
+# version.
+#
+# 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:
+#   Jonathan Kolb <jon at b0g.us>
+#
+
+package Collectd::Plugins::OpenVZ;
+
+use strict;
+use warnings;
+
+use Collectd qw( :all );
+
+my @cpu_instances = ('user', 'nice', 'system', 'idle', 'wait', 'interrupt', 'softirq', 'steal');
+my @if_instances = ('if_octets', 'if_packets', 'if_errors');
+my $vzctl = '/usr/sbin/vzctl';
+my $vzlist = '/usr/sbin/vzlist';
+
+my $last_stat = {};
+
+sub openvz_read
+{
+    my %v = (time => time(), interval => $interval_g);
+    my (@veids, $veid, $name, $key, $val, $i, @lines, @parts, @counters);
+
+    @veids = map { s/ //g; $_; } split(/\n/, `$vzlist -Ho veid`);
+
+    foreach $veid (@veids)
+    {
+        ($name = `$vzlist -Ho name $veid`) =~ s/^\s*(.*?)\s*$/$1/;
+        $name = $veid if ($name =~ /^-$/);
+
+        $v{'host'} = $name;
+
+        #####################################################################
+        # interface
+
+        $v{'plugin'} = 'interface';
+        delete $v{'plugin_instance'};
+
+        @lines = split(/\n/, `$vzctl exec $veid cat /proc/net/dev`);
+        foreach (@lines)
+        {
+            next if (!/:/);                
+
+            @parts = split(/:/);
+            ($key = $parts[0]) =~ s/^\s*(.*?)\s*$/$1/;
+            ($val = $parts[1]) =~ s/^\s*(.*?)\s*$/$1/;
+            @counters = split(/ +/, $val);
+
+            $v{'type_instance'} = $key;
+            for ($key = 0; $key <= $#if_instances; ++$key)
+            {
+                $v{'type'} = $if_instances[$key];
+                $v{'values'} = [ $counters[$key], $counters[$key + 8] ];
+                plugin_dispatch_values(\%v);
+            }
+        }
+
+        #####################################################################
+        # cpu
+
+        $v{'plugin'} = 'cpu';
+        $v{'type'} = 'cpu';
+
+        $i = 0;
+        @lines = split(/\n/, `$vzctl exec $veid cat /proc/stat`);
+        foreach (@lines)
+        {
+            next if (!/^cpu[0-9]/);
+
+            @counters = split(/ +/);
+            shift(@counters);
+
+            # Remove once OpenVZ bug 1376 is resolved
+            if (48485 == $counters[3])
+            {
+                $counters[3] = $last_stat->{"$veid-$i-idle"};
+                $counters[4] = $last_stat->{"$veid-$i-wait"};
+            }
+            else
+            {
+                $last_stat->{"$veid-$i-idle"} = $counters[3];
+                $last_stat->{"$veid-$i-wait"} = $counters[4];
+            }
+
+            $v{'plugin_instance'} = $i++;
+            for ($key = 0; $key <= $#counters; ++$key)
+            {
+                $v{'type_instance'} = $cpu_instances[$key];
+                $v{'values'} = [ $counters[$key] ];
+                plugin_dispatch_values(\%v);
+            }
+        }
+
+        #####################################################################
+        # df
+
+        $v{'plugin'} = 'df';
+        delete $v{'plugin_instance'};
+        $v{'type'} = 'df';
+
+        $val = join(' ', map { (split)[1] } split(/\n/, `$vzctl exec $veid cat /proc/mounts`));
+        @lines = split(/\n/, `$vzctl exec $veid stat -tf $val`);
+        foreach (@lines)
+        {
+            @parts = split(/ /);
+            next if (0 == $parts[7]);
+
+            $val = substr($parts[0], 1);
+            $val = 'root' if ($val =~ /^$/);
+            $val =~ s#/#-#g;
+
+            $v{'type_instance'} = $val;
+            $v{'values'} = [ $parts[5] * ($parts[6] - $parts[7]), $parts[5] * $parts[7] ];
+            plugin_dispatch_values(\%v);
+        }
+
+        #####################################################################
+        # load
+
+        $v{'plugin'} = 'load';
+        delete $v{'plugin_instance'};
+        $v{'type'} = 'load';
+        delete $v{'type_instance'};
+
+        @parts = split(/ +/, `$vzctl exec $veid cat /proc/loadavg`);
+        $v{'values'} = [ $parts[0], $parts[1], $parts[2] ];
+        plugin_dispatch_values(\%v);
+
+        #####################################################################
+        # processes
+
+        my $ps_states = { 'paging' => 0, 'blocked' => 0, 'zombies' => 0, 'stopped' => 0,
+            'running' => 0, 'sleeping' => 0 };
+        my $state_map = { 'R' => 'running', 'S' => 'sleeping', 'D' => 'blocked',
+            'Z' => 'zombies', 'T' => 'stopped', 'W' => 'paging' };
+
+        $v{'plugin'} = 'processes';
+        delete $v{'plugin_instance'};
+        $v{'type'} = 'ps_state';
+
+        @lines = map { (split)[2] } split(/\n/, `$vzctl exec $veid cat '/proc/[0-9]*/stat'`);
+        foreach $key (@lines)
+        {
+            ++$ps_states->{$state_map->{$key}};
+        }
+
+        foreach $key (keys %{$ps_states})
+        {
+            $v{'type_instance'} = $key;
+            $v{'values'} = [ $ps_states->{$key} ];
+            plugin_dispatch_values(\%v);
+        }
+
+        #####################################################################
+        # users
+
+        $v{'plugin'} = 'users';
+        delete $v{'plugin_instance'};
+        $v{'type'} = 'users';
+        delete $v{'type_instance'};
+
+        @lines = split(/\n/, `$vzctl exec $veid w -h`);
+        $v{'values'} = [ scalar(@lines) ];
+        plugin_dispatch_values(\%v);
+    }
+
+    return 1;
+}
+
+plugin_register(TYPE_READ, 'OpenVZ', 'openvz_read');
+
+return 1;
diff --git a/bindings/perl/lib/Collectd/Unixsock.pm b/bindings/perl/lib/Collectd/Unixsock.pm
new file mode 100644 (file)
index 0000000..199a47c
--- /dev/null
@@ -0,0 +1,656 @@
+#
+# collectd - Collectd::Unixsock
+# Copyright (C) 2007,2008  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
+#
+# Author:
+#   Florian octo Forster <octo at verplant.org>
+#
+
+package Collectd::Unixsock;
+
+=head1 NAME
+
+Collectd::Unixsock - Abstraction layer for accessing the functionality by
+collectd's unixsock plugin.
+
+=head1 SYNOPSIS
+
+  use Collectd::Unixsock ();
+
+  my $sock = Collectd::Unixsock->new ($path);
+
+  my $value = $sock->getval (%identifier);
+  $sock->putval (%identifier,
+                 time => time (),
+                values => [123, 234, 345]);
+
+  $sock->destroy ();
+
+=head1 DESCRIPTION
+
+collectd's unixsock plugin allows external programs to access the values it has
+collected or received and to submit own values. This Perl-module is simply a
+little abstraction layer over this interface to make it even easier for
+programmers to interact with the daemon.
+
+=cut
+
+use strict;
+use warnings;
+
+#use constant { NOTIF_FAILURE => 1, NOTIF_WARNING => 2, NOTIF_OKAY => 4 };
+
+use Carp (qw(cluck confess));
+use IO::Socket::UNIX;
+use Regexp::Common (qw(number));
+
+our $Debug = 0;
+
+return (1);
+
+sub _debug
+{
+       if (!$Debug)
+       {
+               return;
+       }
+       print @_;
+}
+
+sub _create_socket
+{
+       my $path = shift;
+       my $sock = IO::Socket::UNIX->new (Type => SOCK_STREAM, Peer => $path);
+       if (!$sock)
+       {
+               cluck ("Cannot open UNIX-socket $path: $!");
+               return;
+       }
+       return ($sock);
+} # _create_socket
+
+=head1 VALUE IDENTIFIERS
+
+The values in the collectd are identified using an five-tuple (host, plugin,
+plugin-instance, type, type-instance) where only plugin-instance and
+type-instance may be NULL (or undefined). Many functions expect an
+I<%identifier> hash that has at least the members B<host>, B<plugin>, and
+B<type>, possibly completed by B<plugin_instance> and B<type_instance>.
+
+Usually you can pass this hash as follows:
+
+  $obj->method (host => $host, plugin => $plugin, type => $type, %other_args);
+
+=cut
+
+sub _create_identifier
+{
+       my $args = shift;
+       my $host;
+       my $plugin;
+       my $type;
+
+       if (!$args->{'host'} || !$args->{'plugin'} || !$args->{'type'})
+       {
+               cluck ("Need `host', `plugin' and `type'");
+               return;
+       }
+
+       $host = $args->{'host'};
+       $plugin = $args->{'plugin'};
+       $plugin .= '-' . $args->{'plugin_instance'} if (defined ($args->{'plugin_instance'}));
+       $type = $args->{'type'};
+       $type .= '-' . $args->{'type_instance'} if (defined ($args->{'type_instance'}));
+
+       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
+
+sub _escape_argument
+{
+       my $string = shift;
+
+       if ($string =~ m/^\w+$/)
+       {
+               return ("$string");
+       }
+
+       $string =~ s#\\#\\\\#g;
+       $string =~ s#"#\\"#g;
+       $string = "\"$string\"";
+
+       return ($string);
+}
+
+=head1 PUBLIC METHODS
+
+=over 4
+
+=item I<$obj> = Collectd::Unixsock->B<new> ([I<$path>]);
+
+Creates a new connection to the daemon. The optional I<$path> argument gives
+the path to the UNIX socket of the C<unixsock plugin> and defaults to
+F</var/run/collectd-unixsock>. Returns the newly created object on success and
+false on error.
+
+=cut
+
+sub new
+{
+       my $pkg = shift;
+       my $path = @_ ? shift : '/var/run/collectd-unixsock';
+       my $sock = _create_socket ($path) or return;
+       my $obj = bless (
+               {
+                       path => $path,
+                       sock => $sock,
+                       error => 'No error'
+               }, $pkg);
+       return ($obj);
+} # new
+
+=item I<$res> = I<$obj>-E<gt>B<getval> (I<%identifier>);
+
+Requests a value-list from the daemon. On success a hash-ref is returned with
+the name of each data-source as the key and the according value as, well, the
+value. On error false is returned.
+
+=cut
+
+sub getval # {{{
+{
+       my $obj = shift;
+       my %args = @_;
+
+       my $status;
+       my $fh = $obj->{'sock'} or confess ('object has no filehandle');
+       my $msg;
+       my $identifier;
+
+       my $ret = {};
+
+       $identifier = _create_identifier (\%args) or return;
+
+       $msg = 'GETVAL ' . _escape_argument ($identifier) . "\n";
+       _debug "-> $msg";
+       print $fh $msg;
+
+       $msg = <$fh>;
+       chomp ($msg);
+       _debug "<- $msg\n";
+
+       ($status, $msg) = split (' ', $msg, 2);
+       if ($status <= 0)
+       {
+               $obj->{'error'} = $msg;
+               return;
+       }
+
+       for (my $i = 0; $i < $status; $i++)
+       {
+               my $entry = <$fh>;
+               chomp ($entry);
+               _debug "<- $entry\n";
+
+               if ($entry =~ m/^(\w+)=NaN$/)
+               {
+                       $ret->{$1} = undef;
+               }
+               elsif ($entry =~ m/^(\w+)=($RE{num}{real})$/)
+               {
+                       $ret->{$1} = 0.0 + $2;
+               }
+       }
+
+       return ($ret);
+} # }}} sub getval
+
+=item I<$res> = I<$obj>-E<gt>B<getthreshold> (I<%identifier>);
+
+Requests a threshold from the daemon. On success a hash-ref is returned with
+the threshold data. On error false is returned.
+
+=cut
+
+sub getthreshold # {{{
+{
+       my $obj = shift;
+       my %args = @_;
+
+       my $status;
+       my $fh = $obj->{'sock'} or confess ('object has no filehandle');
+       my $msg;
+       my $identifier;
+
+       my $ret = {};
+
+       $identifier = _create_identifier (\%args) or return;
+
+       $msg = 'GETTHRESHOLD ' . _escape_argument ($identifier) . "\n";
+       _debug "-> $msg";
+       print $fh $msg;
+
+       $msg = <$fh>;
+       chomp ($msg);
+       _debug "<- $msg\n";
+
+       ($status, $msg) = split (' ', $msg, 2);
+       if ($status <= 0)
+       {
+               $obj->{'error'} = $msg;
+               return;
+       }
+
+       for (my $i = 0; $i < $status; $i++)
+       {
+               my $entry = <$fh>;
+               chomp ($entry);
+               _debug "<- $entry\n";
+
+               if ($entry =~ m/^([^:]+):\s*(\S.*)$/)
+               {
+                       my $key = $1;
+                       my $value = $2;
+
+                       $key =~ s/^\s+//;
+                       $key =~ s/\s+$//;
+
+                       $ret->{$key} = $value;
+               }
+       }
+
+       return ($ret);
+} # }}} sub getthreshold
+
+=item I<$obj>-E<gt>B<putval> (I<%identifier>, B<time> =E<gt> I<$time>, B<values> =E<gt> [...]);
+
+Submits a value-list to the daemon. If the B<time> argument is omitted
+C<time()> is used. The required argument B<values> is a reference to an array
+of values that is to be submitted. The number of values must match the number
+of values expected for the given B<type> (see L<VALUE IDENTIFIERS>), though
+this is checked by the daemon, not the Perl module. Also, gauge data-sources
+(e.E<nbsp>g. system-load) may be C<undef>. Returns true upon success and false
+otherwise.
+
+=cut
+
+sub putval
+{
+       my $obj = shift;
+       my %args = @_;
+
+       my $status;
+       my $fh = $obj->{'sock'} or confess;
+       my $msg;
+       my $identifier;
+       my $values;
+       my $interval = "";
+
+       if (defined $args{'interval'})
+       {
+               $interval = ' interval='
+               . _escape_argument ($args{'interval'});
+       }
+
+       $identifier = _create_identifier (\%args) or return;
+       if (!$args{'values'})
+       {
+               cluck ("Need argument `values'");
+               return;
+       }
+
+       if (!ref ($args{'values'}))
+       {
+               $values = $args{'values'};
+       }
+       else
+       {
+               my $time;
+
+               if ("ARRAY" ne ref ($args{'values'}))
+               {
+                       cluck ("Invalid `values' argument (expected an array ref)");
+                       return;
+               }
+
+               if (! scalar @{$args{'values'}})
+               {
+                       cluck ("Empty `values' array");
+                       return;
+               }
+
+               $time = $args{'time'} ? $args{'time'} : time ();
+               $values = join (':', $time, map { defined ($_) ? $_ : 'U' } (@{$args{'values'}}));
+       }
+
+       $msg = 'PUTVAL '
+       . _escape_argument ($identifier)
+       . $interval
+       . ' ' . _escape_argument ($values) . "\n";
+       _debug "-> $msg";
+       print $fh $msg;
+
+       $msg = <$fh>;
+       chomp ($msg);
+       _debug "<- $msg\n";
+
+       ($status, $msg) = split (' ', $msg, 2);
+       return (1) if ($status == 0);
+
+       $obj->{'error'} = $msg;
+       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;
+
+       _debug "LISTVAL\n";
+       print $fh "LISTVAL\n";
+
+       $msg = <$fh>;
+       chomp ($msg);
+       _debug "<- $msg\n";
+       ($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);
+               _debug "<- $msg\n";
+
+               ($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<$res> = I<$obj>-E<gt>B<putnotif> (B<severity> =E<gt> I<$severity>, B<message> =E<gt> I<$message>, ...);
+
+Submits a notification to the daemon.
+
+Valid options are:
+
+=over 4
+
+=item B<severity>
+
+Sets the severity of the notification. The value must be one of the following
+strings: C<failure>, C<warning>, or C<okay>. Case does not matter. This option
+is mandatory.
+
+=item B<message>
+
+Sets the message of the notification. This option is mandatory.
+
+=item B<time>
+
+Sets the time. If omitted, C<time()> is used.
+
+=item I<Value identifier>
+
+All the other fields of the value identifiers, B<host>, B<plugin>,
+B<plugin_instance>, B<type>, and B<type_instance>, are optional. When given,
+the notification is associated with the performance data of that identifier.
+For more details, please see L<collectd-unixsock(5)>.
+
+=back
+
+=cut
+
+sub putnotif
+{
+       my $obj = shift;
+       my %args = @_;
+
+       my $status;
+       my $fh = $obj->{'sock'} or confess;
+
+       my $msg; # message sent to the socket
+       
+       if (!$args{'message'})
+       {
+               cluck ("Need argument `message'");
+               return;
+       }
+       if (!$args{'severity'})
+       {
+               cluck ("Need argument `severity'");
+               return;
+       }
+       $args{'severity'} = lc ($args{'severity'});
+       if (($args{'severity'} ne 'failure')
+               && ($args{'severity'} ne 'warning')
+               && ($args{'severity'} ne 'okay'))
+       {
+               cluck ("Invalid `severity: " . $args{'severity'});
+               return;
+       }
+
+       if (!$args{'time'})
+       {
+               $args{'time'} = time ();
+       }
+       
+       $msg = 'PUTNOTIF '
+       . join (' ', map { $_ . '=' . _escape_argument ($args{$_}) } (keys %args))
+       . "\n";
+
+       _debug "-> $msg";
+       print $fh $msg;
+
+       $msg = <$fh>;
+       chomp ($msg);
+       _debug "<- $msg\n";
+
+       ($status, $msg) = split (' ', $msg, 2);
+       return (1) if ($status == 0);
+
+       $obj->{'error'} = $msg;
+       return;
+} # putnotif
+
+=item I<$obj>-E<gt>B<flush> (B<timeout> =E<gt> I<$timeout>, B<plugins> =E<gt> [...], B<identifier>  =E<gt> [...]);
+
+Flush cached data.
+
+Valid options are:
+
+=over 4
+
+=item B<timeout>
+
+If this option is specified, only data older than I<$timeout> seconds is
+flushed.
+
+=item B<plugins>
+
+If this option is specified, only the selected plugins will be flushed. The
+argument is a reference to an array of strings.
+
+=item B<identifier>
+
+If this option is specified, only the given identifier(s) will be flushed. The
+argument is a reference to an array of identifiers. Identifiers, in this case,
+are hash references and have the members as outlined in L<VALUE IDENTIFIERS>.
+
+=back
+
+=cut
+
+sub flush
+{
+       my $obj  = shift;
+       my %args = @_;
+
+       my $fh = $obj->{'sock'} or confess;
+
+       my $status = 0;
+       my $msg    = "FLUSH";
+
+       if (defined ($args{'timeout'}))
+       {
+               $msg .= " timeout=" . $args{'timeout'};
+       }
+
+       if ($args{'plugins'})
+       {
+               foreach my $plugin (@{$args{'plugins'}})
+               {
+                       $msg .= " plugin=" . $plugin;
+               }
+       }
+
+       if ($args{'identifier'})
+       {
+               for (@{$args{'identifier'}})
+               {
+                       my $identifier = $_;
+                       my $ident_str;
+
+                       if (ref ($identifier) ne 'HASH')
+                       {
+                               cluck ("The argument of the `identifier' "
+                                       . "option must be an array reference "
+                                       . "of hash references.");
+                               return;
+                       }
+
+                       $ident_str = _create_identifier ($identifier);
+                       if (!$ident_str)
+                       {
+                               return;
+                       }
+
+                       $msg .= ' identifier=' . _escape_argument ($ident_str);
+               }
+       }
+
+       $msg .= "\n";
+
+       _debug "-> $msg";
+       print $fh $msg;
+
+       $msg = <$fh>;
+       chomp ($msg);
+       _debug "<- $msg\n";
+
+       ($status, $msg) = split (' ', $msg, 2);
+       return (1) if ($status == 0);
+
+       $obj->{'error'} = $msg;
+       return;
+}
+
+sub error
+{
+       my $obj = shift;
+       if ($obj->{'error'})
+       {
+               return ($obj->{'error'});
+       }
+       return;
+}
+
+=item I<$obj>-E<gt>destroy ();
+
+Closes the socket before the object is destroyed. This function is also
+automatically called then the object goes out of scope.
+
+=back
+
+=cut
+
+sub destroy
+{
+       my $obj = shift;
+       if ($obj->{'sock'})
+       {
+               close ($obj->{'sock'});
+               delete ($obj->{'sock'});
+       }
+}
+
+sub DESTROY
+{
+       my $obj = shift;
+       $obj->destroy ();
+}
+
+=head1 SEE ALSO
+
+L<collectd(1)>,
+L<collectd.conf(5)>,
+L<collectd-unixsock(5)>
+
+=head1 AUTHOR
+
+Florian octo Forster E<lt>octo@verplant.orgE<gt>
+
+=cut
+
+# vim: set fdm=marker :
index b3d9516..c435027 100644 (file)
@@ -9,6 +9,7 @@ m4_ifdef([LT_PACKAGE_VERSION],
         LT_CONFIG_LTDL_DIR([libltdl])
         LT_INIT([dlopen])
         LTDL_INIT([convenience])
+        AC_DEFINE(LIBTOOL_VERSION, 2, [Define to used libtool version.])
        ]
 ,
        # libtool <= 1.5
@@ -18,6 +19,7 @@ m4_ifdef([LT_PACKAGE_VERSION],
         AC_SUBST(LIBLTDL)
         AC_LIBTOOL_DLOPEN
         AC_CONFIG_SUBDIRS(libltdl)
+        AC_DEFINE(LIBTOOL_VERSION, 1, [Define to used libtool version.])
        ]
 )
 
@@ -45,6 +47,9 @@ AC_PROG_LEX
 AC_PROG_YACC
 PKG_PROG_PKG_CONFIG
 
+AC_CHECK_PROG([have_protoc_c], [protoc-c], [yes], [no])
+AM_CONDITIONAL(HAVE_PROTOC_C, test "x$have_protoc_c" = "xyes")
+
 AC_MSG_CHECKING([for kernel type ($host_os)])
 case $host_os in
        *linux*)
@@ -61,6 +66,10 @@ case $host_os in
        *openbsd*)
        ac_system="OpenBSD"
        ;;
+       *aix*)
+       AC_DEFINE([KERNEL_AIX], 1, [True if program is to be compiled for a AIX kernel])
+       ac_system="AIX"
+       ;;
        *)
        ac_system="unknown"
 esac
@@ -109,7 +118,7 @@ AC_HEADER_SYS_WAIT
 AC_HEADER_DIRENT
 AC_HEADER_STDBOOL
 
-AC_CHECK_HEADERS(stdio.h stdint.h errno.h math.h stdarg.h syslog.h fcntl.h signal.h assert.h sys/types.h sys/socket.h sys/select.h poll.h netdb.h arpa/inet.h sys/resource.h sys/param.h kstat.h regex.h sys/ioctl.h endian.h sys/isa_defs.h)
+AC_CHECK_HEADERS(stdio.h errno.h math.h stdarg.h syslog.h fcntl.h signal.h assert.h sys/types.h sys/socket.h sys/select.h poll.h netdb.h arpa/inet.h sys/resource.h sys/param.h kstat.h regex.h sys/ioctl.h endian.h sys/isa_defs.h)
 
 # For ping library
 AC_CHECK_HEADERS(netinet/in_systm.h, [], [],
@@ -471,10 +480,17 @@ AC_HEADER_TIME
 # Checks for library functions.
 #
 AC_PROG_GCC_TRADITIONAL
-AC_CHECK_FUNCS(gettimeofday select strdup strtol getaddrinfo getnameinfo strchr memcpy strstr strcmp strncmp strncpy strlen strncasecmp strcasecmp openlog closelog sysconf)
+AC_CHECK_FUNCS(gettimeofday select strdup strtol getaddrinfo getnameinfo strchr memcpy strstr strcmp strncmp strncpy strlen strncasecmp strcasecmp openlog closelog sysconf setenv if_indextoname)
 
 AC_FUNC_STRERROR_R
 
+SAVE_CFLAGS="$CFLAGS"
+# Emulate behavior of src/Makefile.am
+if test "x$GCC" = "xyes"
+then
+       CFLAGS="$CFLAGS -Wall -Werror"
+fi
+
 AC_CACHE_CHECK([for strtok_r],
   [c_cv_have_strtok_r_default],
   AC_LINK_IFELSE(
@@ -505,7 +521,6 @@ AC_CACHE_CHECK([for strtok_r],
 
 if test "x$c_cv_have_strtok_r_default" = "xno"
 then
-  SAVE_CFLAGS="$CFLAGS"
   CFLAGS="$CFLAGS -D_REENTRANT=1"
 
   AC_CACHE_CHECK([if strtok_r needs _REENTRANT],
@@ -537,6 +552,12 @@ then
   )
 fi
 
+CFLAGS="$SAVE_CFLAGS"
+if test "x$c_cv_have_strtok_r_reentrant" = "xyes"
+then
+       CFLAGS="$CFLAGS -D_REENTRANT=1"
+fi
+
 AC_CHECK_FUNCS(getpwnam_r getgrnam_r setgroups regcomp regerror regexec regfree)
 
 socket_needs_socket="no"
@@ -544,8 +565,16 @@ AC_CHECK_FUNCS(socket, [], AC_CHECK_LIB(socket, socket, [socket_needs_socket="ye
 AM_CONDITIONAL(BUILD_WITH_LIBSOCKET, test "x$socket_needs_socket" = "xyes")
 
 nanosleep_needs_rt="no"
-AC_CHECK_FUNCS(nanosleep, [], AC_CHECK_LIB(rt, nanosleep, [nanosleep_needs_rt="yes"], AC_MSG_ERROR(cannot find nanosleep)))
+nanosleep_needs_posix4="no"
+AC_CHECK_FUNCS(nanosleep,
+    [],
+    AC_CHECK_LIB(rt, nanosleep,
+        [nanosleep_needs_rt="yes"],
+        AC_CHECK_LIB(posix4, nanosleep,
+            [nanosleep_needs_posix4="yes"],
+            AC_MSG_ERROR(cannot find nanosleep))))
 AM_CONDITIONAL(BUILD_WITH_LIBRT, test "x$nanosleep_needs_rt" = "xyes")
+AM_CONDITIONAL(BUILD_WITH_LIBPOSIX4, test "x$nanosleep_needs_posix4" = "xyes")
 
 AC_CHECK_FUNCS(sysctl, [have_sysctl="yes"], [have_sysctl="no"])
 AC_CHECK_FUNCS(sysctlbyname, [have_sysctlbyname="yes"], [have_sysctlbyname="no"])
@@ -627,6 +656,8 @@ static float foo = NAN;
   fi
 fi
 if test "x$nan_type" = "xnone"; then
+  SAVE_LDFLAGS=$LDFLAGS
+  LDFLAGS="$LDFLAGS -lm"
   AC_CACHE_CHECK([whether NAN can be defined by 0/0],
     [c_cv_have_nan_zero],
     AC_RUN_IFELSE(
@@ -653,6 +684,7 @@ static float foo = NAN;
       [c_cv_have_nan_zero="no"]
     )
   )
+  LDFLAGS=$SAVE_LDFLAGS
   if test "x$c_cv_have_nan_zero" = "xyes"
   then
     nan_type="zero"
@@ -698,6 +730,9 @@ if test "x$fp_layout_type" = "xunknown"; then
 #if HAVE_STDINT_H
 # include <stdint.h>
 #endif
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
 #if HAVE_STDBOOL_H
 # include <stdbool.h>
 #endif
@@ -742,6 +777,9 @@ if test "x$fp_layout_type" = "xunknown"; then
 #if HAVE_STDINT_H
 # include <stdint.h>
 #endif
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
 #if HAVE_STDBOOL_H
 # include <stdbool.h>
 #endif
@@ -794,6 +832,9 @@ if test "x$fp_layout_type" = "xunknown"; then
 #if HAVE_STDINT_H
 # include <stdint.h>
 #endif
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
 #if HAVE_STDBOOL_H
 # include <stdbool.h>
 #endif
@@ -922,6 +963,27 @@ if test "x$have_getmntent" = "xgen"; then
                  [Define if the function getmntent exists. It's the version from libgen.])
 fi
 
+# Check for htonll
+AC_MSG_CHECKING([if have htonll defined])
+
+    have_htonll="no"
+    AC_RUN_IFELSE([
+       AC_LANG_PROGRAM([
+#include <sys/types.h>
+#include <netinet/in.h>
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+       ], [
+          return htonll(0);
+       ])
+    ], [
+      have_htonll="yes"
+      AC_DEFINE(HAVE_HTONLL, 1, [Define if the function htonll exists.])
+    ])
+AC_MSG_RESULT([$have_htonll])
+
 # Check for structures
 AC_CHECK_MEMBERS([struct if_data.ifi_ibytes, struct if_data.ifi_opackets, struct if_data.ifi_ierrors],
        [AC_DEFINE(HAVE_STRUCT_IF_DATA, 1, [Define if struct if_data exists and is usable.])],
@@ -941,6 +1003,13 @@ AC_CHECK_MEMBERS([struct net_device_stats.rx_bytes, struct net_device_stats.tx_p
        #include <linux/netdevice.h>
        ])
 
+AC_CHECK_MEMBERS([struct ip_mreqn.imr_ifindex], [],
+       [],
+       [
+       #include <netinet/in.h>
+       #include <net/if.h>
+       ])
+
 AC_CHECK_MEMBERS([struct kinfo_proc.ki_pid, struct kinfo_proc.ki_rssize, struct kinfo_proc.ki_rusage],
        [
                AC_DEFINE(HAVE_STRUCT_KINFO_PROC_FREEBSD, 1,
@@ -1052,6 +1121,38 @@ fi
 m4_divert_once([HELP_WITH], [
 collectd additional packages:])
 
+AM_CONDITIONAL([BUILD_AIX],[test "x$x$ac_system" = "xAIX"]) 
+
+if test "x$ac_system" = "xAIX"
+then
+       with_perfstat="yes"
+       with_procinfo="yes"
+else
+       with_perfstat="no (AIX only)"
+       with_procinfo="no (AIX only)"
+fi
+
+if test "x$with_perfstat" = "xyes"
+then
+       AC_CHECK_LIB(perfstat, perfstat_reset, [with_perfstat="yes"], [with_perfstat="no (perfstat not found)"], [])
+#      AC_CHECK_HEADERS(sys/protosw.h libperfstat.h,, [with_perfstat="no (perfstat not found)"])
+fi
+if test "x$with_perfstat" = "xyes"
+then
+        AC_DEFINE(HAVE_PERFSTAT, 1, [Define to 1 if you have the 'perfstat' library (-lperfstat)])
+fi
+AM_CONDITIONAL(BUILD_WITH_PERFSTAT, test "x$with_perfstat" = "xyes")
+
+# Processes plugin under AIX.
+if test "x$with_procinfo" = "xyes"
+then
+       AC_CHECK_HEADERS(procinfo.h,, [with_procinfo="no (procinfo.h not found)"])
+fi
+if test "x$with_procinfo" = "xyes"
+then
+        AC_DEFINE(HAVE_PROCINFO_H, 1, [Define to 1 if you have the procinfo.h])
+fi
+
 if test "x$ac_system" = "xSolaris"
 then
        with_kstat="yes"
@@ -1570,6 +1671,7 @@ with_java_vmtype="client"
 with_java_cflags=""
 with_java_libs=""
 JAVAC="$JAVAC"
+JAR="$JAR"
 AC_ARG_WITH(java, [AS_HELP_STRING([--with-java@<:@=PREFIX@:>@], [Path to Java home.])],
 [
        if test "x$withval" = "xno"
@@ -1630,6 +1732,18 @@ then
                                AC_MSG_RESULT([not found])
                        fi
                fi
+               if test "x$JAR" = "x"
+               then
+                       AC_MSG_CHECKING([for jar])
+                       TMPDIR=`find "$with_java_home" -name jar -type f | head -n 1`
+                       if test "x$TMPDIR" != "x"
+                       then
+                               JAR="$TMPDIR"
+                               AC_MSG_RESULT([$JAR])
+                       else
+                               AC_MSG_RESULT([not found])
+                       fi
+               fi
        else if test "x$with_java_home" != "x"
        then
                AC_MSG_WARN([JAVA_HOME: No such directory: $with_java_home])
@@ -1666,6 +1780,24 @@ if test "x$JAVAC" = "x"
 then
        with_java="no (javac not found)"
 fi
+if test "x$JAR" = "x"
+then
+       with_jar_path="$PATH"
+       if test "x$with_java_home" != "x"
+       then
+               with_jar_path="$with_java_home:$with_jar_path"
+               if test -d "$with_java_home/bin"
+               then
+                       with_jar_path="$with_java_home/bin:$with_jar_path"
+               fi
+       fi
+
+       AC_PATH_PROG(JAR, jar, [], "$with_jar_path")
+fi
+if test "x$JAR" = "x"
+then
+       with_java="no (jar not found)"
+fi
 
 SAVE_CPPFLAGS="$CPPFLAGS"
 SAVE_CFLAGS="$CFLAGS"
@@ -1753,6 +1885,98 @@ fi
 AM_CONDITIONAL(BUILD_WITH_LIBMEMCACHED, test "x$with_libmemcached" = "xyes")
 # }}}
 
+# --with-libmodbus {{{
+with_libmodbus_config=""
+with_libmodbus_cflags=""
+with_libmodbus_libs=""
+AC_ARG_WITH(libmodbus, [AS_HELP_STRING([--with-libmodbus@<:@=PREFIX@:>@], [Path to the modbus library.])],
+[
+       if test "x$withval" = "xno"
+       then
+               with_libmodbus="no"
+       else if test "x$withval" = "xyes"
+       then
+               with_libmodbus="use_pkgconfig"
+       else if test -d "$with_libmodbus/lib"
+       then
+               AC_MSG_NOTICE([Not checking for libmodbus: Manually configured])
+               with_libmodbus_cflags="-I$withval/include"
+               with_libmodbus_libs="-L$withval/lib -lmodbus"
+               with_libmodbus="yes"
+       fi; fi; fi
+],
+[with_libmodbus="use_pkgconfig"])
+
+# configure using pkg-config
+if test "x$with_libmodbus" = "xuse_pkgconfig"
+then
+       if test "x$PKG_CONFIG" = "x"
+       then
+               with_libmodbus="no (Don't have pkg-config)"
+       fi
+fi
+if test "x$with_libmodbus" = "xuse_pkgconfig"
+then
+       AC_MSG_NOTICE([Checking for modbus using $PKG_CONFIG])
+       $PKG_CONFIG --exists 'modbus' 2>/dev/null
+       if test $? -ne 0
+       then
+               with_libmodbus="no (pkg-config doesn't know library)"
+       fi
+fi
+if test "x$with_libmodbus" = "xuse_pkgconfig"
+then
+       with_libmodbus_cflags="`$PKG_CONFIG --cflags 'modbus'`"
+       if test $? -ne 0
+       then
+               with_libmodbus="no ($PKG_CONFIG failed)"
+       fi
+       with_libmodbus_libs="`$PKG_CONFIG --libs 'modbus'`"
+       if test $? -ne 0
+       then
+               with_libmodbus="no ($PKG_CONFIG failed)"
+       fi
+fi
+if test "x$with_libmodbus" = "xuse_pkgconfig"
+then
+       with_libmodbus="yes"
+fi
+
+# with_libmodbus_cflags and with_libmodbus_libs are set up now, let's do
+# the actual checks.
+if test "x$with_libmodbus" = "xyes"
+then
+       SAVE_CPPFLAGS="$CPPFLAGS"
+       CPPFLAGS="$CPPFLAGS $with_libmodbus_cflags"
+
+       AC_CHECK_HEADERS(modbus/modbus.h, [], [with_libmodbus="no (modbus/modbus.h not found)"])
+
+       CPPFLAGS="$SAVE_CPPFLAGS"
+fi
+if test "x$with_libmodbus" = "xyes"
+then
+       SAVE_CPPFLAGS="$CPPFLAGS"
+       SAVE_LDFLAGS="$LDFLAGS"
+
+       CPPFLAGS="$CPPFLAGS $with_libmodbus_cflags"
+       LDFLAGS="$LDFLAGS $with_libmodbus_libs"
+
+       AC_CHECK_LIB(modbus, modbus_init_tcp,
+                    [with_libmodbus="yes"],
+                    [with_libmodbus="no (symbol modbus_init_tcp not found)"])
+
+       CPPFLAGS="$SAVE_CPPFLAGS"
+       LDFLAGS="$SAVE_LDFLAGS"
+fi
+if test "x$with_libmodbus" = "xyes"
+then
+       BUILD_WITH_LIBMODBUS_CFLAGS="$with_libmodbus_cflags"
+       BUILD_WITH_LIBMODBUS_LIBS="$with_libmodbus_libs"
+       AC_SUBST(BUILD_WITH_LIBMODBUS_CFLAGS)
+       AC_SUBST(BUILD_WITH_LIBMODBUS_LIBS)
+fi
+# }}}
+
 # --with-libmysql {{{
 with_mysql_config="mysql_config"
 with_mysql_cflags=""
@@ -2653,6 +2877,184 @@ AC_DEFINE_UNQUOTED(HAVE_LIBPTHREAD, [$collect_pthread],
 AM_CONDITIONAL(BUILD_WITH_LIBPTHREAD, test "x$with_libpthread" = "xyes")
 # }}}
 
+# --with-python {{{
+with_python_prog=""
+with_python_path="$PATH"
+AC_ARG_WITH(python, [AS_HELP_STRING([--with-python@<:@=PREFIX@:>@], [Path to the python interpreter.])],
+[
+ if test "x$withval" = "xyes" || test "x$withval" = "xno"
+ then
+        with_python="$withval"
+ else if test -x "$withval"
+ then
+        with_python_prog="$withval"
+        with_python_path="`dirname \"$withval\"`$PATH_SEPARATOR$with_python_path"
+        with_python="yes"
+ else if test -d "$withval"
+ then
+        with_python_path="$withval$PATH_SEPARATOR$with_python_path"
+        with_python="yes"
+ else
+        AC_MSG_WARN([Argument not recognized: $withval])
+ fi; fi; fi
+], [with_python="yes"])
+
+SAVE_PATH="$PATH"
+SAVE_CPPFLAGS="$CPPFLAGS"
+SAVE_LDFLAGS="$LDFLAGS"
+SAVE_LIBS="$LIBS"
+
+PATH="$with_python_path"
+
+if test "x$with_python" = "xyes" && test "x$with_python_prog" = "x"
+then
+       AC_MSG_CHECKING([for python])
+       with_python_prog="`which python 2>/dev/null`"
+       if test "x$with_python_prog" = "x"
+       then
+               AC_MSG_RESULT([not found])
+               with_python="no (interpreter not found)"
+       else
+               AC_MSG_RESULT([$with_python_prog])
+       fi
+fi
+
+if test "x$with_python" = "xyes"
+then
+       AC_MSG_CHECKING([for Python CPPFLAGS])
+       python_include_path=`echo "import distutils.sysconfig;import sys;sys.stdout.write(distutils.sysconfig.get_python_inc())" | "$with_python_prog" 2>&1`
+       python_config_status=$?
+
+       if test "$python_config_status" -ne 0 || test "x$python_include_path" = "x"
+       then
+               AC_MSG_RESULT([failed with status $python_config_status (output: $python_include_path)])
+               with_python="no"
+       else
+               AC_MSG_RESULT([$python_include_path])
+       fi
+fi
+
+if test "x$with_python" = "xyes"
+then
+       CPPFLAGS="-I$python_include_path $CPPFLAGS"
+       AC_CHECK_HEADERS(Python.h,
+                        [with_python="yes"],
+                        [with_python="no ('Python.h' not found)"])
+fi
+
+if test "x$with_python" = "xyes"
+then
+       AC_MSG_CHECKING([for Python LDFLAGS])
+       python_library_path=`echo "import distutils.sysconfig;import sys;sys.stdout.write(distutils.sysconfig.get_config_vars(\"LIBDIR\").__getitem__(0))" | "$with_python_prog" 2>&1`
+       python_config_status=$?
+
+       if test "$python_config_status" -ne 0 || test "x$python_library_path" = "x"
+       then
+               AC_MSG_RESULT([failed with status $python_config_status (output: $python_library_path)])
+               with_python="no"
+       else
+               AC_MSG_RESULT([$python_library_path])
+       fi
+fi
+
+if test "x$with_python" = "xyes"
+then
+       AC_MSG_CHECKING([for Python LIBS])
+       python_library_flags=`echo "import distutils.sysconfig;import sys;sys.stdout.write(distutils.sysconfig.get_config_vars(\"BLDLIBRARY\").__getitem__(0))" | "$with_python_prog" 2>&1`
+       python_config_status=$?
+
+       if test "$python_config_status" -ne 0 || test "x$python_library_flags" = "x"
+       then
+               AC_MSG_RESULT([failed with status $python_config_status (output: $python_library_flags)])
+               with_python="no"
+       else
+               AC_MSG_RESULT([$python_library_flags])
+       fi
+fi
+
+if test "x$with_python" = "xyes"
+then
+       LDFLAGS="-L$python_library_path $LDFLAGS"
+       LIBS="$python_library_flags $LIBS"
+
+       AC_CHECK_FUNC(PyObject_CallFunction,
+                     [with_python="yes"],
+                     [with_python="no (Symbol 'PyObject_CallFunction' not found)"])
+fi
+
+PATH="$SAVE_PATH"
+CPPFLAGS="$SAVE_CPPFLAGS"
+LDFLAGS="$SAVE_LDFLAGS"
+LIBS="$SAVE_LIBS"
+
+if test "x$with_python" = "xyes"
+then
+       BUILD_WITH_PYTHON_CPPFLAGS="-I$python_include_path"
+       BUILD_WITH_PYTHON_LDFLAGS="-L$python_library_path"
+       BUILD_WITH_PYTHON_LIBS="$python_library_flags"
+       AC_SUBST(BUILD_WITH_PYTHON_CPPFLAGS)
+       AC_SUBST(BUILD_WITH_PYTHON_LDFLAGS)
+       AC_SUBST(BUILD_WITH_PYTHON_LIBS)
+fi
+# }}} --with-python
+
+# --with-librouteros {{{
+AC_ARG_WITH(librouteros, [AS_HELP_STRING([--with-librouteros@<:@=PREFIX@:>@], [Path to librouteros.])],
+[
+ if test "x$withval" = "xyes"
+ then
+        with_librouteros="yes"
+ else if test "x$withval" = "xno"
+ then
+        with_librouteros="no"
+ else
+        with_librouteros="yes"
+        LIBROUTEROS_CPPFLAGS="$LIBROUTEROS_CPPFLAGS -I$withval/include"
+        LIBROUTEROS_LDFLAGS="$LIBROUTEROS_LDFLAGS -L$withval/lib"
+ fi; fi
+],
+[with_librouteros="yes"])
+
+SAVE_CPPFLAGS="$CPPFLAGS"
+SAVE_LDFLAGS="$LDFLAGS"
+
+CPPFLAGS="$CPPFLAGS $LIBROUTEROS_CPPFLAGS"
+LDFLAGS="$LDFLAGS $LIBROUTEROS_LDFLAGS"
+
+if test "x$with_librouteros" = "xyes"
+then
+       if test "x$LIBROUTEROS_CPPFLAGS" != "x"
+       then
+               AC_MSG_NOTICE([librouteros CPPFLAGS: $LIBROUTEROS_CPPFLAGS])
+       fi
+       AC_CHECK_HEADERS(routeros_api.h,
+       [with_librouteros="yes"],
+       [with_librouteros="no ('routeros_api.h' not found)"])
+fi
+if test "x$with_librouteros" = "xyes"
+then
+       if test "x$LIBROUTEROS_LDFLAGS" != "x"
+       then
+               AC_MSG_NOTICE([librouteros LDFLAGS: $LIBROUTEROS_LDFLAGS])
+       fi
+       AC_CHECK_LIB(routeros, ros_interface,
+       [with_librouteros="yes"],
+       [with_librouteros="no (symbol 'ros_interface' not found)"])
+fi
+
+CPPFLAGS="$SAVE_CPPFLAGS"
+LDFLAGS="$SAVE_LDFLAGS"
+
+if test "x$with_librouteros" = "xyes"
+then
+       BUILD_WITH_LIBROUTEROS_CPPFLAGS="$LIBROUTEROS_CPPFLAGS"
+       BUILD_WITH_LIBROUTEROS_LDFLAGS="$LIBROUTEROS_LDFLAGS"
+       AC_SUBST(BUILD_WITH_LIBROUTEROS_CPPFLAGS)
+       AC_SUBST(BUILD_WITH_LIBROUTEROS_LDFLAGS)
+fi
+AM_CONDITIONAL(BUILD_WITH_LIBROUTEROS, test "x$with_librouteros" = "xyes")
+# }}}
+
 # --with-librrd {{{
 # AC_ARG_WITH (package, help-string, [action-if-given], [action-if-not-given])
 librrd_cflags=""
@@ -3000,7 +3402,7 @@ AC_ARG_WITH(libupsclient, [AS_HELP_STRING([--with-libupsclient@<:@=PREFIX@:>@],
                        with_libupsclient="use_libupsclient_config"
                else if test -x "$withval/bin/libupsclient-config"
                then
-                       with_libupsclient_config="$withval/bin/net-snmp-config"
+                       with_libupsclient_config="$withval/bin/libupsclient-config"
                        with_libupsclient="use_libupsclient_config"
                else
                        AC_MSG_NOTICE([Not checking for libupsclient: Manually configured])
@@ -3518,7 +3920,7 @@ AC_DEFUN(
             enable_plugin="yes"
             force="yes"
      else
-            enable_plugin="no"
+            enable_plugin="no (disabled on command line)"
      fi; fi
     ],
     [
@@ -3571,6 +3973,7 @@ plugin_contextswitch="no"
 plugin_cpu="no"
 plugin_cpufreq="no"
 plugin_curl_json="no"
+plugin_curl_xml="no"
 plugin_df="no"
 plugin_disk="no"
 plugin_entropy="no"
@@ -3645,6 +4048,22 @@ then
        plugin_disk="yes"
 fi
 
+# AIX
+if test "x$with_perfstat" = "xyes"
+then
+       plugin_cpu="yes"
+       plugin_disk="yes"
+       plugin_memory="yes"
+       plugin_swap="yes"
+       plugin_interface="yes"
+       plugin_load="yes"
+fi
+
+if test "x$with_procinfo" = "xyes"
+then
+       plugin_processes="yes"
+fi
+
 # Solaris
 if test "x$with_kstat" = "xyes"
 then
@@ -3694,6 +4113,11 @@ then
        plugin_curl_json="yes"
 fi
 
+if test "x$with_libcurl" = "xyes" && test "x$with_libxml2" = "xyes"
+then
+       plugin_curl_xml="yes"
+fi
+
 if test "x$have_processor_info" = "xyes"
 then
        plugin_cpu="yes"
@@ -3847,6 +4271,7 @@ AC_PLUGIN([cpu],         [$plugin_cpu],        [CPU usage statistics])
 AC_PLUGIN([csv],         [yes],                [CSV output plugin])
 AC_PLUGIN([curl],        [$with_libcurl],      [CURL generic web statistics])
 AC_PLUGIN([curl_json],   [$plugin_curl_json],    [CouchDB statistics])
+AC_PLUGIN([curl_xml],   [$plugin_curl_xml],    [CURL generic xml statistics])
 AC_PLUGIN([dbi],         [$with_libdbi],       [General database statistics])
 AC_PLUGIN([df],          [$plugin_df],         [Filesystem usage statistics])
 AC_PLUGIN([disk],        [$plugin_disk],       [Disk usage statistics])
@@ -3877,6 +4302,7 @@ AC_PLUGIN([mbmon],       [yes],                [Query mbmond])
 AC_PLUGIN([memcachec],   [$with_libmemcached], [memcachec statistics])
 AC_PLUGIN([memcached],   [yes],                [memcached statistics])
 AC_PLUGIN([memory],      [$plugin_memory],     [Memory usage])
+AC_PLUGIN([modbus],      [$with_libmodbus],    [Modbus plugin])
 AC_PLUGIN([multimeter],  [$plugin_multimeter], [Read multimeter values])
 AC_PLUGIN([mysql],       [$with_libmysql],     [MySQL statistics])
 AC_PLUGIN([netapp],      [$with_libnetapp],    [NetApp plugin])
@@ -3893,11 +4319,15 @@ AC_PLUGIN([onewire],     [$with_libowcapi],    [OneWire sensor statistics])
 AC_PLUGIN([openvpn],     [yes],                [OpenVPN client statistics])
 AC_PLUGIN([oracle],      [$with_oracle],       [Oracle plugin])
 AC_PLUGIN([perl],        [$plugin_perl],       [Embed a Perl interpreter])
+# FIXME: Check for libevent, too.
+AC_PLUGIN([pinba],       [$have_protoc_c],     [Pinba statistics])
 AC_PLUGIN([ping],        [$with_liboping],     [Network latency statistics])
 AC_PLUGIN([postgresql],  [$with_libpq],        [PostgreSQL database statistics])
 AC_PLUGIN([powerdns],    [yes],                [PowerDNS statistics])
 AC_PLUGIN([processes],   [$plugin_processes],  [Process statistics])
 AC_PLUGIN([protocols],   [$plugin_protocols],  [Protocol (IP, TCP, ...) statistics])
+AC_PLUGIN([python],      [$with_python],       [Embed a Python interpreter])
+AC_PLUGIN([routeros],    [$with_librouteros],  [RouterOS plugin])
 AC_PLUGIN([rrdcached],   [$librrd_rrdc_update], [RRDTool output plugin])
 AC_PLUGIN([rrdtool],     [$with_librrd],       [RRDTool output plugin])
 AC_PLUGIN([sensors],     [$with_libsensors],   [lm_sensors statistics])
@@ -4108,6 +4538,7 @@ Configuration:
     libkstat  . . . . . . $with_kstat
     libkvm  . . . . . . . $with_libkvm
     libmemcached  . . . . $with_libmemcached
+    libmodbus . . . . . . $with_libmodbus
     libmysql  . . . . . . $with_libmysql
     libnetapp . . . . . . $with_libnetapp
     libnetlink  . . . . . $with_libnetlink
@@ -4117,9 +4548,11 @@ Configuration:
     libopenipmi . . . . . $with_libopenipmipthread
     liboping  . . . . . . $with_liboping
     libpcap . . . . . . . $with_libpcap
+    libperfstat . . . . . $with_perfstat
     libperl . . . . . . . $with_libperl
     libpq . . . . . . . . $with_libpq
     libpthread  . . . . . $with_libpthread
+    librouteros . . . . . $with_librouteros
     librrd  . . . . . . . $with_librrd
     libsensors  . . . . . $with_libsensors
     libstatgrab . . . . . $with_libstatgrab
@@ -4129,7 +4562,10 @@ Configuration:
     libxml2 . . . . . . . $with_libxml2
     libxmms . . . . . . . $with_libxmms
     libyajl . . . . . . . $with_libyajl
+    libevent  . . . . . . $with_libevent
+    protobuf-c  . . . . . $have_protoc_c
     oracle  . . . . . . . $with_oracle
+    python  . . . . . . . $with_python
 
   Features:
     daemon mode . . . . . $enable_daemon
@@ -4152,6 +4588,7 @@ Configuration:
     csv . . . . . . . . . $enable_csv
     curl  . . . . . . . . $enable_curl
     curl_json . . . . . . $enable_curl_json
+    curl_xml  . . . . . . $enable_curl_xml
     dbi . . . . . . . . . $enable_dbi
     df  . . . . . . . . . $enable_df
     disk  . . . . . . . . $enable_disk
@@ -4182,6 +4619,7 @@ Configuration:
     memcachec . . . . . . $enable_memcachec
     memcached . . . . . . $enable_memcached
     memory  . . . . . . . $enable_memory
+    modbus  . . . . . . . $enable_modbus
     multimeter  . . . . . $enable_multimeter
     mysql . . . . . . . . $enable_mysql
     netapp  . . . . . . . $enable_netapp
@@ -4198,11 +4636,14 @@ Configuration:
     openvpn . . . . . . . $enable_openvpn
     oracle  . . . . . . . $enable_oracle
     perl  . . . . . . . . $enable_perl
+    pinba . . . . . . . . $enable_pinba
     ping  . . . . . . . . $enable_ping
     postgresql  . . . . . $enable_postgresql
     powerdns  . . . . . . $enable_powerdns
     processes . . . . . . $enable_processes
     protocols . . . . . . $enable_protocols
+    python  . . . . . . . $enable_python
+    routeros  . . . . . . $enable_routeros
     rrdcached . . . . . . $enable_rrdcached
     rrdtool . . . . . . . $enable_rrdtool
     sensors . . . . . . . $enable_sensors
index cbdb76e..af64fb1 100755 (executable)
@@ -13,6 +13,7 @@ use Data::Dumper ();
 
 our $Config = "/etc/collection.conf";
 our @DataDirs = ();
+our @DontShowTypes = ();
 our $LibDir;
 
 our $ValidTimespan =
@@ -78,6 +79,10 @@ sub read_config
                        $value =~ s#/*$##;
                        $LibDir = $value;
                }
+               elsif ($key eq 'dontshowtype')
+               {
+                 push (@DontShowTypes, $value);
+               }
                else
                {
                        print STDERR "Unknown key: $key\n";
@@ -239,6 +244,7 @@ sub _find_types
       my $name = "$_";
       $name =~ s/\.rrd$//i;
       my ($type, $instance) = split (m/-/, $name, 2);
+      if (grep { $_ eq $type } @DontShowTypes) { next; }
       $types{$type} = [] if (!$types{$type});
       push (@{$types{$type}}, $instance) if (defined ($instance));
     }
@@ -970,6 +976,28 @@ sub load_graph_definitions
     'GPRINT:avg:LAST:%5.1lf%s Last',
     'GPRINT:avg_sum:LAST:(ca. %5.1lf%sB Total)\l'
     ],
+   apache_connections => ['DEF:min={file}:count:MIN',
+    'DEF:avg={file}:count:AVERAGE',
+    'DEF:max={file}:count:MAX',
+    "AREA:max#$HalfBlue",
+    "AREA:min#$Canvas",
+    "LINE1:avg#$FullBlue:Connections",
+    'GPRINT:min:MIN:%6.2lf Min,',
+    'GPRINT:avg:AVERAGE:%6.2lf Avg,',
+    'GPRINT:max:MAX:%6.2lf Max,',
+    'GPRINT:avg:LAST:%6.2lf Last'
+    ],
+    apache_idle_workers => ['DEF:min={file}:count:MIN',
+    'DEF:avg={file}:count:AVERAGE',
+    'DEF:max={file}:count:MAX',
+    "AREA:max#$HalfBlue",
+    "AREA:min#$Canvas",
+    "LINE1:avg#$FullBlue:Idle Workers",
+    'GPRINT:min:MIN:%6.2lf Min,',
+    'GPRINT:avg:AVERAGE:%6.2lf Avg,',
+    'GPRINT:max:MAX:%6.2lf Max,',
+    'GPRINT:avg:LAST:%6.2lf Last'
+    ],
     apache_requests => ['DEF:min={file}:count:MIN',
     'DEF:avg={file}:count:AVERAGE',
     'DEF:max={file}:count:MAX',
index 01d01bb..c890042 100644 (file)
@@ -29,6 +29,7 @@ Dependencies
   distribution itself:
 
   * Config::General
+  * Regexp::Common
   * HTML::Entities
   * RRDs
 
index 13806ee..b1a8a6d 100644 (file)
@@ -40,7 +40,7 @@ my $dataset =
 # This code is executed after loading the plugin to register it with collectd.
 plugin_register (TYPE_LOG, 'myplugin', 'my_log');
 plugin_register (TYPE_NOTIF, 'myplugin', 'my_notify');
-plugin_register (TYPE_DATASET, 'myplugin', $dataset);
+plugin_register (TYPE_DATASET, 'mytype', $dataset);
 plugin_register (TYPE_INIT, 'myplugin', 'my_init');
 plugin_register (TYPE_READ, 'myplugin', 'my_read');
 plugin_register (TYPE_WRITE, 'myplugin', 'my_write');
@@ -68,16 +68,17 @@ sub my_read
 
        # do the magic to read the data:
        # the number of values has to match the number of data sources defined in
-       # the registered data set
+       # the registered data set. The type used here (in this example:
+       # "mytype") must be defined in the types.db, see types.db(5) for
+       # details, or registered as "TYPE_DATASET".
        $vl->{'values'} = [ rand(65535) ];
        $vl->{'plugin'} = 'myplugin';
+       $vl->{'type'}   = 'mytype';
        # any other elements are optional
 
        # dispatch the values to collectd which passes them on to all registered
-       # write functions - the first argument is used to lookup the data set
-       # definition (it is strongly recommended to use a type defined in the
-       # types.db file)
-       plugin_dispatch_values ('myplugin', $vl);
+       # write functions
+       plugin_dispatch_values ($vl);
 
        # A false return value indicates an error and the plugin will be skipped
        # for an increasing amount of time.
index 9163730..907ea9b 100755 (executable)
@@ -1,5 +1,26 @@
 #!/usr/bin/perl
 
+#
+# collectd - contrib/exec-munin.px
+# Copyright (C) 2007,2008  Florian 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>
+#
+
 use strict;
 use warnings;
 
index a9f4663..938721f 100755 (executable)
@@ -88,6 +88,8 @@ with the C<exec-plugin>).
 
 =back
 
+=back
+
 =cut
 
 sub handle_config_addtype
@@ -293,7 +295,7 @@ sub execute_script
       
       if ($perfdata)
       {
-       push (@serviceperfdata, split (' ', $perfdata));
+        push (@serviceperfdata, split (' ', $perfdata));
       }
 
       $state = 1;
@@ -308,8 +310,8 @@ sub execute_script
 
       if ($perfdata)
       {
-       push (@serviceperfdata, split (' ', $perfdata));
-       $state = 2;
+        push (@serviceperfdata, split (' ', $perfdata));
+        $state = 2;
       }
     }
     else # ($state == 2)
@@ -347,7 +349,7 @@ sub execute_script
     for (@serviceperfdata)
     {
       handle_performance_data ($host, 'nagios', $pinst, $script->{'type'},
-       $time, $_);
+        $time, $_);
     }
   }
 } # execute_script
diff --git a/contrib/python/getsigchld.py b/contrib/python/getsigchld.py
new file mode 100644 (file)
index 0000000..557adc0
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/python
+
+###############################################################################
+#         WARNING! Importing this script will break the exec plugin!          #
+###############################################################################
+# Use this if you want to create new processes from your python scripts.      #
+# Normally you will get a OSError exception when the new process terminates   #
+# because collectd will ignore the SIGCHLD python is waiting for.             #
+# This script will restore the default SIGCHLD behavior so python scripts can #
+# create new processes without errors.                                        #
+###############################################################################
+#         WARNING! Importing this script will break the exec plugin!          #
+###############################################################################
+
+import signal
+import collectd
+
+def init():
+       signal.signal(signal.SIGCHLD, signal.SIG_DFL)
+
+collectd.register_init(init)
index 012fc48..4617f6c 100644 (file)
@@ -1,12 +1,17 @@
+
+%define with_java %(test -z "$JAVA_HOME" ; echo $?)
+
 Summary:       Statistics collection daemon for filling RRD files.
 Name:          collectd
-Version:       4.3.1
-Release:       0.centos5
+Version:       4.9.0
+Release:       1%{?dist}
 Source:                http://collectd.org/files/%{name}-%{version}.tar.gz
 License:       GPL
 Group:         System Environment/Daemons
 BuildRoot:     %{_tmppath}/%{name}-%{version}-root
-BuildPrereq:   lm_sensors-devel, mysql-devel, rrdtool-devel, curl-devel, libpcap-devel, net-snmp-devel, libstatgrab-devel, mysql-devel, libxml2-devel, libiptcdata-devel
+BuildPrereq:   lm_sensors-devel, rrdtool-devel, libpcap-devel, net-snmp-devel, libstatgrab-devel, libxml2-devel, libiptcdata-devel
+# libcurl deps
+BuildPrereq:   curl-devel,libidn-devel,openssl-devel
 Requires:      rrdtool, perl-Regexp-Common, libstatgrab
 Packager:      RightScale <support@rightscale.com>
 Vendor:                collectd development team <collectd@verplant.org>
@@ -19,6 +24,7 @@ every time it wants to update the values it's very fast and easy on the
 system. Also, the statistics are very fine grained since the files are updated
 every 10 seconds.
 
+
 %package apache
 Summary:       apache-plugin for collectd.
 Group:         System Environment/Daemons
@@ -62,12 +68,25 @@ Requires:   collectd = %{version}, net-snmp
 %description snmp
 This plugin for collectd allows querying of network equipment using SNMP.
 
+%if %with_java
+%package java
+Summary:       java-module for collectd.
+Group:         System Environment/Daemons
+Requires:      collectd = %{version}, jdk >= 1.6
+BuildPrereq:   jdk >= 1.6
+%description java
+This plugin for collectd allows plugins to be written in Java and executed
+in an embedded JVM.
+%endif
+
 %prep
 rm -rf $RPM_BUILD_ROOT
 %setup
 
 %build
-./configure CFLAGS=-"DLT_LAZY_OR_NOW='RTLD_LAZY|RTLD_GLOBAL'" --prefix=%{_prefix} --sbindir=%{_sbindir} --mandir=%{_mandir} --libdir=%{_libdir} --sysconfdir=%{_sysconfdir} --enable-apache --enable-email --enable-mysql --enable-dns
+./configure CFLAGS=-"DLT_LAZY_OR_NOW='RTLD_LAZY|RTLD_GLOBAL'" --prefix=%{_prefix} --sbindir=%{_sbindir} --mandir=%{_mandir} --libdir=%{_libdir} --sysconfdir=%{_sysconfdir} \
+    %{!?with_java:"--with-java=$JAVA_HOME --enable-java"} \
+    --disable-battery
 make
 
 %install
@@ -80,14 +99,17 @@ mkdir -p $RPM_BUILD_ROOT/etc/collectd.d
 mkdir -p $RPM_BUILD_ROOT/var/lib/collectd
 ### Clean up docs
 find contrib/ -type f -exec %{__chmod} a-x {} \;
+
 ###Modify Config for Redhat Based Distros
-cp contrib/redhat/collectd.conf $RPM_BUILD_ROOT/etc/collectd.conf
 sed -i 's:#BaseDir     "/usr/var/lib/collectd":BaseDir     "/var/lib/collectd":' $RPM_BUILD_ROOT/etc/collectd.conf
 sed -i 's:#PIDFile     "/usr/var/run/collectd.pid":PIDFile     "/var/run/collectd.pid":' $RPM_BUILD_ROOT/etc/collectd.conf
-sed -i 's:#PluginDir   "/usr/lib/collectd":PluginDir   "/usr/lib/collectd":' $RPM_BUILD_ROOT/etc/collectd.conf
+sed -i 's:#PluginDir   "/usr/lib/collectd":PluginDir   "%{_libdir}/collectd":' $RPM_BUILD_ROOT/etc/collectd.conf
 sed -i 's:#TypesDB     "/usr/share/collectd/types.db":TypesDB     "/usr/share/collectd/types.db":' $RPM_BUILD_ROOT/etc/collectd.conf
-sed -i 's:#Interval     10:Interval     10:' $RPM_BUILD_ROOT/etc/collectd.conf
+sed -i 's:#Interval     10:Interval     30:' $RPM_BUILD_ROOT/etc/collectd.conf
 sed -i 's:#ReadThreads  5:ReadThreads  5:' $RPM_BUILD_ROOT/etc/collectd.conf
+###Include broken out config directory
+echo -e '\nInclude "/etc/collectd.d"' >> $RPM_BUILD_ROOT/etc/collectd.conf
+
 ##Move config contribs
 cp contrib/redhat/apache.conf $RPM_BUILD_ROOT/etc/collectd.d/apache.conf
 cp contrib/redhat/email.conf $RPM_BUILD_ROOT/etc/collectd.d/email.conf
@@ -120,7 +142,7 @@ exit 0
 %files
 %defattr(-,root,root)
 %doc AUTHORS COPYING ChangeLog INSTALL NEWS README contrib/
-%attr(0644,root,root) %config(noreplace) /etc/collectd.conf
+%config %attr(0644,root,root) /etc/collectd.conf
 %attr(0755,root,root) /etc/rc.d/init.d/collectd
 %attr(0755,root,root) /var/www/cgi-bin/collection.cgi
 %attr(0755,root,root) %{_sbindir}/collectd
@@ -130,167 +152,142 @@ exit 0
 %attr(0644,root,root) %{_mandir}/man5/*
 %dir /etc/collectd.d
 
-%attr(0644,root,root) %{_libdir}/%{name}/apcups.so*
-%attr(0644,root,root) %{_libdir}/%{name}/apcups.la
-
-#%attr(0644,root,root) %{_libdir}/%{name}/apple_sensors.so*
-#%attr(0644,root,root) %{_libdir}/%{name}/apple_sensors.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/battery.so*
-%attr(0644,root,root) %{_libdir}/%{name}/battery.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/conntrack.so*
-%attr(0644,root,root) %{_libdir}/%{name}/conntrack.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/cpufreq.so*
-%attr(0644,root,root) %{_libdir}/%{name}/cpufreq.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/cpu.so*
-%attr(0644,root,root) %{_libdir}/%{name}/cpu.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/csv.so*
-%attr(0644,root,root) %{_libdir}/%{name}/csv.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/df.so*
-%attr(0644,root,root) %{_libdir}/%{name}/df.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/disk.so*
-%attr(0644,root,root) %{_libdir}/%{name}/disk.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/dns.so*
-%attr(0644,root,root) %{_libdir}/%{name}/dns.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/entropy.so*
-%attr(0644,root,root) %{_libdir}/%{name}/entropy.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/exec.so*
-%attr(0644,root,root) %{_libdir}/%{name}/exec.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/hddtemp.so*
-%attr(0644,root,root) %{_libdir}/%{name}/hddtemp.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/interface.so*
-%attr(0644,root,root) %{_libdir}/%{name}/interface.la
-
-#%attr(0644,root,root) %{_libdir}/%{name}/iptables.so*
-#%attr(0644,root,root) %{_libdir}/%{name}/iptables.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/irq.so*
-%attr(0644,root,root) %{_libdir}/%{name}/irq.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/load.so*
-%attr(0644,root,root) %{_libdir}/%{name}/load.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/logfile.so*
-%attr(0644,root,root) %{_libdir}/%{name}/logfile.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/mbmon.so
-%attr(0644,root,root) %{_libdir}/%{name}/mbmon.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/memcached.so*
-%attr(0644,root,root) %{_libdir}/%{name}/memcached.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/memory.so*
-%attr(0644,root,root) %{_libdir}/%{name}/memory.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/multimeter.so*
-%attr(0644,root,root) %{_libdir}/%{name}/multimeter.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/network.so*
-%attr(0644,root,root) %{_libdir}/%{name}/network.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/nfs.so*
-%attr(0644,root,root) %{_libdir}/%{name}/nfs.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/ntpd.so*
-%attr(0644,root,root) %{_libdir}/%{name}/ntpd.la
-
-#%attr(0644,root,root) %{_libdir}/%{name}/nut.so*
-#%attr(0644,root,root) %{_libdir}/%{name}/nut.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/perl.so*
-%attr(0644,root,root) %{_libdir}/%{name}/perl.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/ping.so*
-%attr(0644,root,root) %{_libdir}/%{name}/ping.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/processes.so*
-%attr(0644,root,root) %{_libdir}/%{name}/processes.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/rrdtool.so*
-%attr(0644,root,root) %{_libdir}/%{name}/rrdtool.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/serial.so*
-%attr(0644,root,root) %{_libdir}/%{name}/serial.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/swap.so*
-%attr(0644,root,root) %{_libdir}/%{name}/swap.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/syslog.so*
-%attr(0644,root,root) %{_libdir}/%{name}/syslog.la
-
-#%attr(0644,root,root) %{_libdir}/%{name}/tape.so*
-#%attr(0644,root,root) %{_libdir}/%{name}/tape.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/tcpconns.so*
-%attr(0644,root,root) %{_libdir}/%{name}/tcpconns.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/unixsock.so*
-%attr(0644,root,root) %{_libdir}/%{name}/unixsock.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/users.so*
-%attr(0644,root,root) %{_libdir}/%{name}/users.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/uuid.so*
-%attr(0644,root,root) %{_libdir}/%{name}/uuid.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/vserver.so*
-%attr(0644,root,root) %{_libdir}/%{name}/vserver.la
-
-%attr(0644,root,root) %{_libdir}/%{name}/wireless.so*
-%attr(0644,root,root) %{_libdir}/%{name}/wireless.la
+# client
+%attr(0644,root,root) /usr/include/collectd/client.h
+%attr(0644,root,root) /usr/include/collectd/lcc_features.h
+
+%attr(0644,root,root) %{_libdir}/libcollectdclient.*
+%attr(0644,root,root) %{_libdir}/pkgconfig/libcollectdclient.pc
+
+# macro to grab binaries for a plugin, given a name
+%define plugin_macro() \
+%attr(0644,root,root) %{_libdir}/%{name}/%1.a \
+%attr(0644,root,root) %{_libdir}/%{name}/%1.so* \
+%attr(0644,root,root) %{_libdir}/%{name}/%1.la
+
+%plugin_macro apcups
+%plugin_macro ascent
+%plugin_macro bind
+%plugin_macro conntrack
+%plugin_macro contextswitch
+%plugin_macro cpufreq
+%plugin_macro cpu
+%plugin_macro csv
+%plugin_macro curl
+%plugin_macro df
+%plugin_macro disk
+%plugin_macro dns
+%plugin_macro entropy
+%plugin_macro email
+%plugin_macro exec
+%plugin_macro filecount
+%plugin_macro fscache
+%plugin_macro hddtemp
+%plugin_macro interface
+%plugin_macro iptables
+%plugin_macro irq
+%plugin_macro load
+%plugin_macro logfile
+%plugin_macro madwifi
+
+%plugin_macro match_empty_counter
+%plugin_macro match_hashed
+%plugin_macro match_regex
+%plugin_macro match_timediff
+%plugin_macro match_value
+
+%plugin_macro mbmon
+%plugin_macro memcached
+%plugin_macro memory
+%plugin_macro multimeter
+%plugin_macro network
+%plugin_macro nfs
+%plugin_macro ntpd
+%plugin_macro openvpn
+%plugin_macro olsrd
+%plugin_macro perl
+%plugin_macro powerdns
+%plugin_macro processes
+%plugin_macro protocols
+%plugin_macro python
+%plugin_macro rrdtool
+%plugin_macro serial
+%plugin_macro sensors
+%plugin_macro swap
+%plugin_macro syslog
+%plugin_macro table
+%plugin_macro tail
+
+%plugin_macro target_notification
+%plugin_macro target_replace
+%plugin_macro target_scale
+%plugin_macro target_set
+
+%plugin_macro tcpconns
+%plugin_macro teamspeak2
+%plugin_macro ted
+%plugin_macro thermal
+%plugin_macro unixsock
+%plugin_macro uptime
+%plugin_macro users
+%plugin_macro uuid
+%plugin_macro vmem
+%plugin_macro vserver
+%plugin_macro wireless
+%plugin_macro write_http
 
 %attr(0644,root,root) %{_datadir}/%{name}/types.db
 
 %exclude %{_libdir}/perl5/5.8.8/%{_arch}-linux-thread-multi/perllocal.pod
-%attr(0644,root,root) %{_libdir}/perl5/site_perl/5.8.8/Collectd.pm
-%attr(0644,root,root) %{_libdir}/perl5/site_perl/5.8.8/Collectd/Unixsock.pm
 %attr(0644,root,root) %{_libdir}/perl5/site_perl/5.8.8/%{_arch}-linux-thread-multi/auto/Collectd/.packlist
+%attr(0644,root,root) /usr/lib/perl5/site_perl/5.8.8/Collectd.pm
+%attr(0644,root,root) /usr/lib/perl5/site_perl/5.8.8/Collectd/Unixsock.pm
+%attr(0644,root,root) /usr/lib/perl5/site_perl/5.8.8/Collectd/Plugins/OpenVZ.pm
 %attr(0644,root,root) /usr/share/man/man3/Collectd::Unixsock.3pm.gz
 
+%exclude /usr/share/collectd/postgresql_default.conf
+
 %dir /var/lib/collectd
 
+%if %with_java
+%files java
+%attr(0644,root,root) /usr/share/%{name}/java/org/collectd/api/*.class
+%attr(0644,root,root) /usr/share/%{name}/java/org/collectd/java/*.class
+%plugin_macro java
+%endif
+
 %files apache
-%attr(0644,root,root) %{_libdir}/%{name}/apache.so*
-%attr(0644,root,root) %{_libdir}/%{name}/apache.la
-%attr(0644,root,root) /etc/collectd.d/apache.conf
+%config %attr(0644,root,root) /etc/collectd.d/apache.conf
+%plugin_macro apache
 
 %files email
 %attr(0644,root,root) %{_libdir}/%{name}/email.so*
 %attr(0644,root,root) %{_libdir}/%{name}/email.la
-%attr(0644,root,root) /etc/collectd.d/email.conf
+%config %attr(0644,root,root) /etc/collectd.d/email.conf
 
 %files mysql
-%attr(0644,root,root) %{_libdir}/%{name}/mysql.so*
-%attr(0644,root,root) %{_libdir}/%{name}/mysql.la
-%attr(0644,root,root) /etc/collectd.d/mysql.conf
+%config %attr(0644,root,root) /etc/collectd.d/mysql.conf
+%plugin_macro mysql
 
 %files nginx
-%attr(0644,root,root) %{_libdir}/%{name}/nginx.so*
-%attr(0644,root,root) %{_libdir}/%{name}/nginx.la
-%attr(0644,root,root) /etc/collectd.d/nginx.conf
+%config %attr(0644,root,root) /etc/collectd.d/nginx.conf
+%plugin_macro nginx
 
 %files sensors
 %attr(0644,root,root) %{_libdir}/%{name}/sensors.so*
 %attr(0644,root,root) %{_libdir}/%{name}/sensors.la
-%attr(0644,root,root) /etc/collectd.d/sensors.conf
+%config %attr(0644,root,root) /etc/collectd.d/sensors.conf
 
 %files snmp
-%attr(0644,root,root) %{_libdir}/%{name}/snmp.so*
-%attr(0644,root,root) %{_libdir}/%{name}/snmp.la
 %attr(0644,root,root) /etc/collectd.d/snmp.conf
+%plugin_macro snmp
 
 %changelog
+* Tue Jan 04 2010 Rackspace <stu.hood@rackspace.com> 4.9.0
+- New upstream version
+- Changes to support 4.9.0
+- Added support for Java/GenericJMX plugin
+
 * Mon Mar 17 2008 RightScale <support@rightscale.com> 4.3.1
 - New upstream version
 - Changes to support 4.3.1
index fd8d404..07381db 100644 (file)
        InstancePrefix "input"
        Values ".1.3.6.1.4.1.1909.10.6.1.1.3"
     </Data>
+
+    #
+    # Mikrotik RouterBoards
+    #
+    # Wireless statistics: station mode
+    <Data "mikrotik_wl_sta_bitrate_tx">
+        tYPE "Bitrate"
+        Table true
+        InstancePrefix "st-tx-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.1.1.5"
+        Values ".1.3.6.1.4.1.14988.1.1.1.1.1.2"
+    </Data>
+
+    <Data "mikrotik_wl_sta_bitrate_rx">
+        Type "bitrate"
+        Table true
+        InstancePrefix "st-rx-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.1.1.5"
+        Values ".1.3.6.1.4.1.14988.1.1.1.1.1.3"
+    </Data>
+
+    <Data "mikrotik_wl_sta_signal">
+        Type "signal_power"
+        Table true
+        InstancePrefix "st-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.1.1.5"
+        Values ".1.3.6.1.4.1.14988.1.1.1.1.1.4"
+    </Data>
+
+    # Wireless statistics: AP mode / registration table
+    <Data "mikrotik_wl_reg_signal">
+        Type "signal_power"
+        Table true
+        InstancePrefix "ap-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.2.1.1"
+        Values ".1.3.6.1.4.1.14988.1.1.1.2.1.3"
+    </Data>
+
+    <Data "mikrotik_wl_reg_octets">
+        Type "if_octets"
+        Table true
+        InstancePrefix "ap-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.2.1.1"
+        Values ".1.3.6.1.4.1.14988.1.1.1.2.1.5" ".1.3.6.1.4.1.14988.1.1.1.2.1.4"
+    </Data>
+
+    <Data "mikrotik_wl_reg_packets">
+        Type "if_packets"
+        Table true
+        InstancePrefix "ap-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.2.1.1"
+        Values ".1.3.6.1.4.1.14988.1.1.1.2.1.7" ".1.3.6.1.4.1.14988.1.1.1.2.1.6"
+    </Data>
+
+    <Data "mikrotik_wl_reg_bitrate_tx">
+        Type "bitrate"
+        Table true
+        InstancePrefix "ap-tx-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.2.1.1"
+        Values ".1.3.6.1.4.1.14988.1.1.1.2.1.8"
+    </Data>
+
+    <Data "mikrotik_wl_reg_bitrate_rx">
+        Type "bitrate"
+        Table true
+        InstancePrefix "ap-rx-"
+        Instance ".1.3.6.1.4.1.14988.1.1.1.2.1.1"
+        Values ".1.3.6.1.4.1.14988.1.1.1.2.1.9"
+    </Data>
 </Plugin>
index 1d8f975..d1a7a88 100755 (executable)
@@ -1,7 +1,8 @@
 #!/usr/bin/perl
 #
 # collectd - snmp-probe-host.px
-# Copyright (C) 2008  Florian octo Forster
+# Copyright (C) 2008,2009  Florian octo Forster
+# Copyright (C) 2009       noris network AG
 #
 # 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
@@ -27,6 +28,12 @@ use Config::General ('ParseConfig');
 use Getopt::Long ('GetOptions');
 use Socket6;
 
+our %ExcludeOptions =
+(
+  'IF-MIB64' => qr/^\.?1\.3\.6\.1\.2\.1\.31/,
+  'IF-MIB32' => qr/^\.?1\.3\.6\.1\.2\.1\.2/
+);
+
 sub get_config
 {
   my %conf;
@@ -50,6 +57,7 @@ sub probe_one
 {
   my $sess = shift;
   my $conf = shift;
+  my $excludes = @_ ? shift : [];
   my @oids;
   my $cmd = 'GET';
   my $vl;
@@ -90,6 +98,14 @@ sub probe_one
       $oid_orig = $tmp;
     }
 
+    for (@$excludes)
+    {
+      if ($oid_orig =~ $_)
+      {
+        return;
+      }
+    }
+
     $vb = SNMP::Varbind->new ([$oid_orig]);
 
     if ($cmd eq 'GET')
@@ -148,6 +164,7 @@ sub probe_all
   my $host = shift;
   my $community = shift;
   my $data = shift;
+  my $excludes = @_ ? shift : [];
   my $version = 2;
   my @valid_data = ();
   my $begin;
@@ -203,7 +220,7 @@ sub probe_all
     for (keys %$data)
     {
       my $name = $_;
-      if (probe_one ($sess, $data->{$name}))
+      if (probe_one ($sess, $data->{$name}, $excludes))
       {
         push (@valid_data, $name);
       }
@@ -258,11 +275,24 @@ Options are:
   -C | --config        Path to config file holding the SNMP data blocks.
   -c | --community     SNMP community to use. Default: `public'.
   -h | --help          Print this information and exit.
+  -x | --exclude       Exclude a specific MIB. Call with "help" for more
+                       information.
 
 USAGE
   exit (1);
 }
 
+sub exit_usage_exclude
+{
+  print "Available exclude MIBs:\n\n";
+  for (sort (keys %ExcludeOptions))
+  {
+    print "  $_\n";
+  }
+  print "\n";
+  exit (1);
+}
+
 =head1 NAME
 
 snmp-probe-host.px - Find out what information an SNMP device provides.
@@ -294,6 +324,7 @@ my $file = '/etc/collectd/collectd.conf';
 my $community = 'public';
 my $conf;
 my $working_data;
+my @excludes = ();
 
 =head1 OPTIONS
 
@@ -318,6 +349,19 @@ Defaults to F</etc/collectd/collectd.conf>.
 
 SNMP community to use. Should be pretty straight forward.
 
+=item B<--exclude> I<MIB>
+
+This option can be used to exclude specific data from being enabled in the
+generated config. Currently the following MIBs are understood:
+
+=over 4
+
+=item B<IF-MIB>
+
+Exclude interface information, such as I<ifOctets> and I<ifPackets>.
+
+=back
+
 =back
 
 =cut
@@ -325,6 +369,7 @@ SNMP community to use. Should be pretty straight forward.
 GetOptions ('H|host|hostname=s' => \$host,
   'C|conf|config=s' => \$file,
   'c|community=s' => \$community,
+  'x|exclude=s' => \@excludes,
   'h|help' => \&exit_usage) or die;
 
 if (!$host)
@@ -333,6 +378,28 @@ if (!$host)
   exit (1);
 }
 
+if (@excludes)
+{
+  my $tmp = join (',', @excludes);
+  my @tmp = split (/\s*,\s*/, $tmp);
+
+  @excludes = ();
+  for (@tmp)
+  {
+    my $mib = uc ($_);
+    if ($mib eq 'HELP')
+    {
+      exit_usage_exclude ();
+    }
+    elsif (!exists ($ExcludeOptions{$mib}))
+    {
+      print STDERR "No such MIB: $mib\n";
+      exit_usage_exclude ();
+    }
+    push (@excludes, $ExcludeOptions{$mib});
+  }
+}
+
 $conf = get_config ($file) or die ("Cannot read config");
 
 if (!defined ($conf->{'plugin'})
@@ -343,7 +410,7 @@ if (!defined ($conf->{'plugin'})
   exit (1);
 }
 
-probe_all ($host, $community, $conf->{'plugin'}{'snmp'}{'data'});
+probe_all ($host, $community, $conf->{'plugin'}{'snmp'}{'data'}, \@excludes);
 
 exit (0);
 
diff --git a/debian/README.Debian b/debian/README.Debian
deleted file mode 100644 (file)
index b0b701b..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-collectd on Debian
-==================
-
-General notes:
---------------
-
-- This package is split up into several packages to prevent you from having to
-  install dependencies (or recommended packages) that you don't actually need.
-  Any plugin that has dependencies other than libc gets its own package.
-
-Configuring collectd:
----------------------
-
-- collectd uses a similar configuration layout as openvpn does. That is to
-  say that one daemon process is started for each configuration file found in
-  /etc/collectd/.
-
-- See collectd.conf(5) for details about configuring collectd.
-
-Building your own plugins:
---------------------------
-
-- If you want to contribute plugins to the official distribution you should
-  read http://collectd.org/dev-info.shtml.
-
-- If you want to build plugins for your personal use only simply install the
-  collectd-dev package and use /usr/share/doc/collectd-dev/examples/myplugin.c
-  as a starting point (Note: This is already a working example, though it does
-  not collect any useful data).
-  
-  The resulting file can be compiled as follows:
-
-    gcc -shared -o myplugin.so myplugin.c
-
-  Copy myplugin.so to /usr/lib/collectd and add the following line to your
-  collectd config file:
-
-    LoadPlugin myplugin
-
-  Restart collectd and you're done.
-
diff --git a/debian/changelog b/debian/changelog
deleted file mode 100644 (file)
index 14e450b..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-collectd (3.11.5-0octo1) unstable; urgency=low
-
-  * New upstream release.
-
- -- Florian Forster <octo@leeloo.home.verplant.org>  Tue, 29 May 2007 22:50:59 +0200
-
-collectd (3.11.4-0octo1) unstable; urgency=low
-
-  * New upstream release.
-
- -- Florian Forster <octo@leeloo.home.verplant.org>  Tue, 10 Apr 2007 17:18:11 +0200
-
-collectd (3.11.3-0octo1) unstable; urgency=low
-
-  * New upstream release.
-
- -- Florian octo Forster <octo@alice.lan.home.verplant.org>  Fri, 30 Mar 2007 22:08:40 +0200
-
-collectd (3.11.0-0octo1) unstable; urgency=low
-
-  * New upstream release.
-  * The package collectd-mbmon has been added.
-
- -- Florian octo Forster <octo@alice.lan.home.verplant.org>  Fri, 22 Dec 2006 15:10:37 +0100
-
-collectd (3.10.4-0octo1) unstable; urgency=low
-
-  * New upstream release.
-
- -- Florian Forster <octo@leeloo.home.verplant.org>  Thu, 21 Dec 2006 20:36:37 +0100
-
-collectd (3.10.3-0octo1) unstable; urgency=low
-
-  * New upstream release.
-
- -- Florian Forster <octo@leeloo.home.verplant.org>  Sat,  4 Nov 2006 13:46:49 +0100
-
-collectd (3.10.1-4) unstable; urgency=low
-
-  * Changed collectd-dbg's section and priority to "devel" and "extra"
-    respectively.
-  * Set init start sequence code to 95 to be sure to start after any daemons
-    that data is collected from.
-
- -- Sebastian Harl <sh@tokkee.org>  Thu,  5 Oct 2006 10:25:07 +0000
-
-collectd (3.10.1-3) unstable; urgency=low
-
-  * Added --oknodo to start-stop-daemon in the init script (Closes: #379703).
-
- -- Sebastian Harl <sh@tokkee.org>  Tue, 25 Jul 2006 18:34:55 +0200
-
-collectd (3.10.1-2) unstable; urgency=low
-
-  * Added collectd-dbg package.
-
- -- Sebastian Harl <sh@tokkee.org>  Sun, 23 Jul 2006 23:39:42 +0200
-
-collectd (3.10.1-1) unstable; urgency=low
-
-  * New upstream release.
-  * Dynamically link against external liboping.
-    - New binary package collectd-ping.
-    - Added collectd-ping to suggested packages.
-  * Moved config file from /usr/share/doc/collectd/examples/ to
-    /etc/collectd/.
-
- -- Sebastian Harl <sh@tokkee.org>  Sat, 22 Jul 2006 21:43:37 +0200
-
-collectd (3.10.0-1) unstable; urgency=low
-
-  * New upstream release.
-    New plugins:
-    - APC UPS's charge, load, input/output/battery voltage, etc.: apcups
-    - NTP daemon's local clock drift, offset to peers, etc.: ntpd
-  * Upstream no longer provides a debian/ directory. Thus no repackaging is
-    required any longer.
-  * Not using getifaddrs() is now the default in upstream. getifaddrs.dpatch
-    no longer needed.
-  * Added collectd-hddtemp as a suggestion to the collectd package.
-
- -- Sebastian Harl <sh@tokkee.org>  Sun,  9 Jul 2006 21:52:13 +0200
-
-collectd (3.9.4+debian-1) unstable; urgency=low
-
-  * Initial release (Closes: #373008).
-  * Removed upstream's debian/ directory from .orig.tar.gz.
-  * getifaddrs.dpatch: Patching src/traffic.c to read data from /proc instead
-    of using getifaddrs(). getifaddrs() does not seem to work correctly on 
-    AMD64.
-
- -- Sebastian Harl <sh@tokkee.org>  Fri,  7 Jul 2006 15:49:42 +0200
-
diff --git a/debian/collectd.conf b/debian/collectd.conf
deleted file mode 100644 (file)
index 91eef47..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-# Config file for collectd(1).
-#
-# Some plugins need additional configuration and are disabled by default.
-# Please read collectd.conf(5) for details.
-
-Mode Local
-
-# If in "Client" mode you have to specify which server to send datagrams to.
-#Mode Client
-#Server 123.123.123.123 12345
-
-#Mode Server
-
-#Mode Log
-
-#DataDir    /var/lib/collectd
-#PIDFILE    /var/run/collectd.pid
-#PluginDir  /usr/lib/collectd
-
-#LoadPlugin apache
-#LoadPlugin apcups
-#LoadPlugin apple_sensors
-LoadPlugin battery
-LoadPlugin cpu
-#LoadPlugin cpufreq
-LoadPlugin df
-LoadPlugin disk
-#LoadPlugin hddtemp
-LoadPlugin load
-#LoadPlugin mbmon
-LoadPlugin memory
-#LoadPlugin mysql
-#LoadPlugin nfs
-#LoadPlugin ntpd
-#LoadPlugin ping
-LoadPlugin processes
-#LoadPlugin sensors
-#LoadPlugin serial
-LoadPlugin swap
-#LoadPlugin tape
-LoadPlugin traffic
-LoadPlugin users
-#LoadPlugin vserver
-#LoadPlugin wireless
-
-#<Plugin apache>
-#   URL http://localhost/status?auto
-#   User www-user
-#   Password secret
-#   CACert /etc/ssl/ca.crt
-#</Plugin>
-
-#<Plugin apcups>
-#   Host localhost
-#   Port 3551
-#</Plugin>
-
-#<Plugin hddtemp>
-#   Host 127.0.0.1
-#   Port 7634
-#</Plugin>
-
-#<Plugin mbmon>
-#      Host 127.0.0.1
-#      Port 411
-#</Plugin>
-
-#<Plugin ntpd>
-#   Host localhost
-#   Port 123
-#</Plugin>
-
-#<Plugin mysql>
-#   Host localhost
-#   User db_user
-#   Password secret
-#   Database db_name
-#</Plugin>
-
-#<Plugin ping>
-#   Host host.foo.bar
-#   Host host.baz.qux
-#</Plugin>
-
-#<Plugin traffic>
-#   Interface eth0
-#   IgnoreSelected false
-#</Plugin>
-
diff --git a/debian/collectd.init.d b/debian/collectd.init.d
deleted file mode 100755 (executable)
index 7c8ac29..0000000
+++ /dev/null
@@ -1,115 +0,0 @@
-#!/bin/bash
-#
-# collectd     Initscript for collectd
-#              http://collectd.org/
-# Authors:     Florian Forster <octo@verplant.org>
-#              Sebastian Harl <sh@tokkee.org>
-#
-
-set -e
-
-PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
-DESC="Statistics collection daemon"
-NAME=collectd
-DAEMON=/usr/sbin/$NAME
-SCRIPTNAME=/etc/init.d/$NAME
-ARGS=""
-
-CONFIGDIR=/etc/collectd
-# for backward compatibility
-FALLBACKCONF=/etc/collectd.conf
-
-# Gracefully exit if the package has been removed.
-test -x $DAEMON || exit 0
-
-if [ -r /etc/default/$NAME ]
-then
-       . /etc/default/$NAME
-fi
-
-#
-#      Function that starts the daemon/service.
-#
-d_start() {
-       i=0
-       
-       if [ ! -d "$CONFIGDIR" ]
-       then
-               if [ -e "$FALLBACKCONF" ]
-               then
-                       $DAEMON -C "$FALLBACKCONF" 2> /dev/null
-               else
-                       echo ""
-                       echo "This package is not configured yet. Please refer"
-                       echo "to /usr/share/doc/collectd/README.Debian for"
-                       echo "details."
-                       echo ""
-                       exit 0
-               fi
-       else
-               for FILE in `ls $CONFIGDIR/*.conf 2>/dev/null`
-               do
-                       NAME=`basename "$FILE" .conf`
-
-                       if [ $i == 0 ]
-                       then
-                               echo -n " ("
-                       else
-                               echo -n ", "
-                       fi
-                       
-                       $DAEMON -C "$FILE" 2> /dev/null
-                       if [ $? == 0 ]
-                       then
-                               echo -n "$NAME"
-                       else
-                               echo -n "$NAME failed"
-                       fi
-
-                       i=$(($i+1))
-               done
-
-               if [ $i == 0 ]
-               then
-                       echo -n "[no config found]"
-                       exit 1
-               else
-                       echo -n ")"
-               fi
-       fi
-}
-
-#
-#      Function that stops the daemon/service.
-#
-d_stop() {
-       start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
-}
-
-case "$1" in
-  start)
-       echo -n "Starting $DESC: $NAME"
-       d_start
-       echo "."
-       ;;
-  stop)
-       echo -n "Stopping $DESC: $NAME"
-       d_stop
-       echo "."
-       ;;
-  restart|force-reload)
-       echo -n "Restarting $DESC: $NAME"
-       d_stop
-       sleep 1
-       d_start
-       echo "."
-       ;;
-  *)
-       echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
-       exit 1
-       ;;
-esac
-
-exit 0
-
-# vim: syntax=sh noexpandtab sw=8 ts=8 :
diff --git a/debian/collectd.postrm b/debian/collectd.postrm
deleted file mode 100755 (executable)
index 84b8386..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-#!/bin/bash
-# postrm script for collectd
-#
-# see: dh_installdeb(1)
-
-set -e
-
-# summary of how this script can be called:
-#        * <postrm> `remove'
-#        * <postrm> `purge'
-#        * <old-postrm> `upgrade' <new-version>
-#        * <new-postrm> `failed-upgrade' <old-version>
-#        * <new-postrm> `abort-install'
-#        * <new-postrm> `abort-install' <old-version>
-#        * <new-postrm> `abort-upgrade' <old-version>
-#        * <disappearer's-postrm> `disappear' <r>overwrit>r> <new-version>
-# for details, see http://www.debian.org/doc/debian-policy/ or
-# the debian-policy package
-
-
-case "$1" in
-    purge)
-        rm -rf /var/lib/collectd
-        rm -rf /etc/collectd
-        ;;
-    
-    remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
-        ;;
-
-    *)
-        echo "postrm called with unknown argument \`$1'" >&2
-        exit 1
-        ;;
-
-esac
-
-# dh_installdeb will replace this with shell code automatically
-# generated by other debhelper scripts.
-
-#DEBHELPER#
-
-exit 0
-
diff --git a/debian/compat b/debian/compat
deleted file mode 100644 (file)
index 7ed6ff8..0000000
+++ /dev/null
@@ -1 +0,0 @@
-5
diff --git a/debian/control b/debian/control
deleted file mode 100644 (file)
index 56e8463..0000000
+++ /dev/null
@@ -1,149 +0,0 @@
-Source: collectd
-Section: utils
-Priority: optional
-Maintainer: Sebastian Harl <sh@tokkee.org>
-Build-Depends: debhelper (>= 5), autotools-dev, libcurl3-dev,
- libmysqlclient15-dev | libmysqlclient14-dev, librrd2-dev | librrd0-dev,
- libsensors-dev, liboping-dev (> 0.3.3)
-Standards-Version: 3.7.2
-
-Package: collectd
-Architecture: any
-Depends: ${shlibs:Depends}
-Suggests: collectd-apache, collectd-mysql, collectd-sensors, collectd-dev,
- collectd-hddtemp, collectd-mbmon, collectd-ping, librrds-perl
-Description: statistics collection daemon
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the main program file and the following plugins:
-   * APC UPS's charge, load, input/output/battery voltage, etc.: apcups
-   * Apple computer's sensors information: apple_sensors (server mode only)
-   * battery status: battery
-   * CPU utilization: cpu
-   * CPU frequency: cpufreq
-   * disk space usage: df
-   * disk and partition throughput: disk
-   * system load averages: load
-   * memory usage: memory
-   * NFS utilization: nfs
-   * NTP daemon's local clock drift, offset to peers, etc.: ntpd
-   * number of processes: processes
-   * serial port traffic: serial
-   * swap usage: swap
-   * tape drive usage: tape (server mode only)
-   * network traffic: traffic
-   * number of users logged into the system: users
-   * system ressources used by Linux-VServers: vserver
-   * wireless network stats: wireless
-
-Package: collectd-dbg
-Section: devel
-Architecture: any
-Priority: extra
-Depends: collectd (= ${Source-Version}), collectd-apache (= ${Source-Version}),
- collectd-hddtemp (= ${Source-Version}), collectd-mysql (= ${Source-Version}),
- collectd-ping (= ${Source-Version}), collectd-sensors (= ${Source-Version})
-Description: statistics collection daemon (debugging symbols)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the debugging symbols.
-
-Package: collectd-apache
-Architecture: any
-Depends: collectd (= ${Source-Version}), ${shlibs:Depends}
-Description: statistics collection daemon (Apache plugin)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the Apache plugin which collects Apache statistics
- provided by Apache's mod_status.
-
-Package: collectd-hddtemp
-Architecture: any
-Depends: collectd (= ${Source-Version}), ${shlibs:Depends}
-Recommends: hddtemp
-Description: statistics collection daemon (hddtemp plugin)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the hddtemp plugin which collects harddisk temperatures.
-
-Package: collectd-mbmon
-Architecture: any
-Depends: collectd (= ${Source-Version}), ${shlibs:Depends}
-Recommends: mbmon (>= 2.01)
-Description: statistics collection daemon (mbmon plugin)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the mbmon plugin which collects motherboard status
- information: temperatures, voltages and cooling fan speeds.
-
-Package: collectd-mysql
-Architecture: any
-Depends: collectd (= ${Source-Version}), ${shlibs:Depends}
-Description: statistics collection daemon (MySQL plugin)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the MySQL plugin which collects MySQL statistics
- provided by MySQL's "show status" command.
-
-Package: collectd-ping
-Architecture: any
-Depends: collectd (= ${Source-Version}), ${shlibs:Depends}
-Description: statistics collection daemon (ping plugin)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the ping plugin which collects network latency
- statistics.
-
-Package: collectd-sensors
-Architecture: any
-Depends: collectd (= ${Source-Version}), ${shlibs:Depends}
-Description: statistics collection daemon (sensors plugin)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the sensors plugin which collects lm_sensors
- information (e.g. CPU temperature, fan speeds).
-
-Package: collectd-dev
-Architecture: all
-Depends: collectd (= ${Source-Version})
-Description: statistics collection daemon (development files)
- collectd is a small daemon written in C for performance. It reads various
- system statistics and updates RRD files, creating them if necessary. Since
- the daemon doesn't need to startup every time it wants to update the files
- it's very fast and easy on the system. Also, the statistics are very fine
- grained since the files are updated every 10 seconds.
- .
- This package contains the development files needed to create your own
- plugins.
diff --git a/debian/copyright b/debian/copyright
deleted file mode 100644 (file)
index c75c8ee..0000000
+++ /dev/null
@@ -1,148 +0,0 @@
-This package was debianized by Sebastian Harl <sh@tokkee.org> on
-Wed, 10 May 2006 09:20:39 +0200.
-
-It was downloaded from <http://collectd.org/files/>.
-
-Upstream Authors:
-       Core Developer:
-       Florian Forster <octo@verplant.org>
-
-       For individual credits, see AUTHORS.
-
-Copyright Holders (in alphabetical order):
-       Andre M. Hedrick <andre@suse.com>
-       Anthony Gialluca <tonyabg@charter.net>
-       David Bacher <drbacher@gmail.com>
-       Florian Forster <octo@verplant.org>
-       Free Software Foundation, Inc.
-       Jason Pepas <cell@ices.utexas.edu>
-       Kern Sibbald
-       Lyonel Vincent <lyonel@ezix.org>
-       Niki W. Waibel <niki.waibel@newlogic.com>
-       Peter Holik <peter@holik.at>
-       Roy Keene, Keene Enterprises <libconfig@rkeene.org>
-       Scott Garrett <sgarrett@technomancer.com>
-       Sebastian Harl <sh@tokkee.org>
-       Vincent Stehlé <vincent.stehle@free.fr>
-
-License:
-
-       Files src/apache.c
-             src/apple_sensors.c
-             src/mysql.c
-             src/ntpd.c
-             src/battery.c
-             src/wireless.c
-       Copyright (C) 2006 Florian Forster <octo@verplant.org>
-
-       File src/apcups.c
-       Copyright (C) 2006 Anthony Gialluca <tonyabg@charter.net>
-       Copyright (C) 2000-2004 Kern Sibbald
-       Copyright (C) 1996-99 Andre M. Hedrick <andre@suse.com>
-
-       File src/cpufreq.c
-       Copyright (C) 2005, 2006 Peter Holik <peter@holik.at>
-
-       File src/hddtemp.c
-       Copyright (C) 2005, 2006 Vincent Stehlé <vincent.stehle@free.fr>
-
-       File src/mbmon.c
-       Copyright (C) 2006 Flavio Stanchina <flavio@stanchina.net>
-
-       File src/nfs.c
-       Copyright (C) 2005, 2006 Jason Pepas <cell@ices.utexas.edu>
-
-       File src/processes.c
-       Copyright (C) 2005 Lyonel Vincent <lyonel@ezix.org>
-       Copyright (C) 2006 Florian Forster <octo@verplant.org> (Mach code)
-
-       File src/serial.c
-       Copyright (C) 2005, 2006 David Bacher <drbacher@gmail.com>
-
-       File src/tape.c
-       Copyright (C) 2005, 2006 Scott Garrett <sgarrett@technomancer.com>
-
-       File src/users.c
-       Copyright (C) 2005, 2006 Sebastian Harl <sh@tokkee.org>
-
-       Files src/utils_debug.c
-             src/utils_debug.h
-             src/utils_mount.c
-             src/utils_mount.h
-       Copyright (C) 2005, 2006 Niki W. Waibel <niki.waibel@gmx.net>
-
-       File src/vserver.c
-       Copyright (C) 2006 Sebastian Harl <sh@tokkee.org>
-
-       All other files (except libltdl/* and src/libconfig/*):
-       Copyright (C) 2005, 2006 Florian Forster <octo@verplant.org>
-
-       This package 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; either version 2 of the License, or
-       (at your option) any later version.
-
-       This package 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 package; if not, write to the Free Software
-       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 
-       USA.
-
-On Debian systems, the complete text of the GNU General Public License 
-can be found in `/usr/share/common-licenses/GPL'.
-
-       File libltdl/ltdl.c
-       Copyright (C) 1998-2000, 2004, 2005 Free Software Foundation, Inc.
-       Originally by Thomas Tanner <tanner@ffii.org>
-
-       File libltdl/ltdl.h
-       Copyright (C) 1998-2000 Free Software Foundation, Inc.
-       Originally by Thomas Tanner <tanner@ffii.org>
-       
-       This library is free software; you can redistribute it and/or
-       modify it under the terms of the GNU Lesser General Public
-       License as published by the Free Software Foundation; either
-       version 2 of the License, or (at your option) any later version.
-
-       This library 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
-       Lesser General Public License for more details.
-
-       You should have received a copy of the GNU Lesser General Public
-       License along with this library; if not, write to the Free Software
-       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 
-       USA.
-
-On Debian systems, the complete text of the GNU Lesser General Public License 
-can be found in `/usr/share/common-licenses/LGPL'.
-
-       Files src/libconfig/*
-       Copyright (C) Roy Keene <libconfig@rkeene.org>
-
-       Permission is hereby granted, free of charge, to any person
-       obtaining a copy of this software and associated documentation 
-       files (the "Software"), to deal in the Software without restriction, 
-       including without limitation the rights to use, copy, modify, merge, 
-       publish, distribute, sublicense, and/or sell copies of the Software, 
-       and to permit persons to whom the Software is furnished to do so, 
-       subject to the following conditions:
-
-       The above copyright notice and this permission notice shall be 
-       included in all copies or substantial portions of the Software.
-
-       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
-       EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
-       MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
-       IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
-       CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
-       TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
-       SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-The Debian packaging is (C) 2006, Sebastian Harl <sh@tokkee.org> and
-is licensed under the GPL, see above.
-
diff --git a/debian/examples/myplugin.c b/debian/examples/myplugin.c
deleted file mode 100644 (file)
index 1bd8e42..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * /usr/share/doc/collectd/examples/sample_plugin.c
- *
- * A sample plugin for collectd.
- *
- * Written by Sebastian Harl <sh@tokkee.org>
- *
- * This 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; either version 2 of the License, or (at your 
- * option) any later version.
- */
-
-#include <collectd/common.h>    /* rrd_update_file */
-#include <collectd/plugin.h>    /* plugin_* */
-
-#include <stdio.h>
-#include <stdlib.h>
-
-/* Optional config file support */
-/* #include <collectd/configfile.h> */
-
-/* Optional debugging support 
- * (only available if collectd was compiled with debugging support) */
-/* #include <collectd/utils_debug.h> */
-
-#define MODULE_NAME "myplugin"
-
-/* Name of the rrd file under DataDir (/var/lib/collectd by default)
- *
- * The name may contain slashes to create subdirectories. */
-static char *my_rrd = "myplugin.rrd";
-
-/* DS definitions for the rrd file
- *
- * See the rrdcreate(1) manpage for details. The heartbeat is configurable in
- * collectd. It defaults to 25. */
-static char *ds_def[] =
-{
-    "DS:my_ds:GAUGE:25:0:U",
-    NULL
-};
-
-/* DS count */
-static int ds_num = 1;
-
-/* Time at which the read function is called */
-extern time_t curtime;
-
-/* Initialize the plugin
- *
- * This function is called to set up a plugin before using it. */
-static void my_init(void)
-{
-    /* we have nothing to do here :-) */
-    return;
-}
-
-/* Get the data
- *
- * This function implements the magic used to get the desired values that
- * should be stored in the rrd file. It uses plugin_submit to transfer the
- * data to whatever place is configured in the config file. If there are more
- * than one instances you should pass a uniq identifier as seconds argument to
- * the plugin_submit function. */
-#define BUFSIZE 256
-static void my_read(void)
-{
-    long int data = 0;
-    char buf[BUFSIZE] = "";
-
-    /* magic ;-) */
-    data = random();
-
-    if (snprintf(buf, BUFSIZE, "%u:%li", 
-                (unsigned int)curtime, data) >= BUFSIZE)
-        return;
-
-    plugin_submit(MODULE_NAME, NULL, buf);
-    return;
-}
-#undef BUFSIZE
-
-/* Save the data
- *
- * This function saves the data to the appropriate location by calling
- * rrd_update_file. It is used to "calculate" the filename and DS definition
- * appropriate for the given instance. */
-static void my_write(host, inst, val)
-    char *host;
-    char *inst;
-    char *val;
-{
-    rrd_update_file(host, my_rrd, val, ds_def, ds_num);
-    return;
-}
-
-/* Register the plugin
- *
- * This function registers the plugin with collectd. It has to be named
- * "module_register". */
-void module_register(void)
-{
-    plugin_register(MODULE_NAME, my_init, my_read, my_write);
-    return;
-}
-
diff --git a/debian/rules b/debian/rules
deleted file mode 100755 (executable)
index 602efd5..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-#!/usr/bin/make -f
-# debian/rules for collectd
-#
-# Written by Sebastian Harl <sh@tokkee.org>.
-
-# Uncomment this to turn on verbose mode.
-#export DH_VERBOSE=1
-
-# These are used for cross-compiling and for saving the configure script
-# from having to guess our platform (since we know it already)
-DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
-DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
-
-CFLAGS = -Wall -g
-
-ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
-       CFLAGS += -O0
-else
-       CFLAGS += -O2
-endif
-
-config.status: configure
-       dh_testdir
-       CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) \
-                       --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr \
-                       --mandir=\$${prefix}/share/man \
-                       --localstatedir=/var --sysconfdir=/etc
-
-build: build-stamp
-
-build-stamp: config.status
-       dh_testdir
-       
-       $(MAKE)
-       
-       touch build-stamp
-
-clean:
-       dh_testdir
-       dh_testroot
-       rm -f build-stamp
-       
-       -$(MAKE) distclean
-       
-       dh_clean 
-
-install-indep:
-       dh_testdir
-       dh_testroot
-       dh_clean -k
-       dh_installdirs -i
-       
-       include_dir=$(CURDIR)/debian/collectd-dev/usr/include/collectd/ \
-               && mkdir -p $$include_dir \
-               && cp src/*.h $$include_dir
-
-install-arch: build
-       dh_testdir
-       dh_testroot
-       dh_clean -k 
-       dh_installdirs -a
-       
-       $(MAKE) install DESTDIR=$(CURDIR)/debian/collectd
-       
-       rm -f $(CURDIR)/debian/collectd/usr/lib/collectd/*.la
-       
-       rm -f $(CURDIR)/debian/collectd/etc/collectd.conf
-       install -D -m 0644 $(CURDIR)/debian/collectd.conf \
-               $(CURDIR)/debian/collectd/etc/collectd/collectd.conf
-       
-       for PLUGIN in apache hddtemp mbmon mysql ping sensors; do \
-               plugin_dir=$(CURDIR)/debian/collectd-$$PLUGIN/usr/lib/collectd/; \
-               mkdir -p $$plugin_dir; \
-               mv $(CURDIR)/debian/collectd/usr/lib/collectd/$$PLUGIN.so \
-                       $$plugin_dir; \
-       done    
-
-binary-indep: install-indep
-       dh_testdir
-       dh_testroot
-       dh_installchangelogs -i ChangeLog
-       dh_installdocs -A -i debian/README.Debian AUTHORS README TODO
-       dh_installexamples -i debian/examples/myplugin.c
-       dh_compress -i
-       dh_fixperms -i
-       dh_installdeb -i
-       dh_gencontrol -i
-       dh_md5sums -i
-       dh_builddeb -i
-
-binary-arch: build install-arch
-       dh_testdir
-       dh_testroot
-       dh_installchangelogs -a ChangeLog
-       dh_installdocs -A -a debian/README.Debian AUTHORS README TODO
-       dh_installexamples -a contrib/collectd2html.pl contrib/collection.cgi
-       dh_installinit -a -- defaults 95
-       dh_installman -a src/collectd.1 src/collectd.conf.5
-       dh_link -a
-       dh_strip -a --dbg-package=collectd-dbg
-       dh_compress -a
-       dh_fixperms -a
-       dh_installdeb -a
-       dh_shlibdeps -a
-       dh_gencontrol -a
-       dh_md5sums -a
-       dh_builddeb -a
-
-binary: binary-arch binary-indep
-.PHONY: build clean binary-indep binary-arch binary install-indep install-arch
-
index d0cd99d..8d26d19 100644 (file)
@@ -53,6 +53,9 @@ collectd_DEPENDENCIES =
 if BUILD_WITH_LIBRT
 collectd_LDADD += -lrt
 endif
+if BUILD_WITH_LIBPOSIX4
+collectd_LDADD += -lposix4
+endif
 if BUILD_WITH_LIBSOCKET
 collectd_LDADD += -lsocket
 endif
@@ -68,6 +71,10 @@ endif
 if BUILD_WITH_LIBDEVINFO
 collectd_LDADD += -ldevinfo
 endif
+if BUILD_AIX
+collectd_LDFLAGS += -Wl,-bexpall,-brtllib
+collectd_LDADD += -lm
+endif
 
 # The daemon needs to call sg_init, so we need to link it against libstatgrab,
 # too. -octo
@@ -91,6 +98,10 @@ collectd_nagios_LDADD =
 if BUILD_WITH_LIBSOCKET
 collectd_nagios_LDADD += -lsocket
 endif
+if BUILD_AIX
+collectd_nagios_LDADD += -lm
+endif
+
 collectd_nagios_LDADD += libcollectdclient/libcollectdclient.la
 collectd_nagios_DEPENDENCIES = libcollectdclient/libcollectdclient.la
 
@@ -197,6 +208,9 @@ if BUILD_WITH_LIBSTATGRAB
 cpu_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS)
 cpu_la_LIBADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS)
 endif
+if BUILD_WITH_PERFSTAT
+cpu_la_LIBADD += -lperfstat
+endif
 collectd_LDADD += "-dlopen" cpu.la
 collectd_DEPENDENCIES += cpu.la
 endif
@@ -246,6 +260,17 @@ collectd_LDADD += "-dlopen" curl_json.la
 collectd_DEPENDENCIES += curl_json.la
 endif
 
+if BUILD_PLUGIN_CURL_XML
+pkglib_LTLIBRARIES += curl_xml.la
+curl_xml_la_SOURCES = curl_xml.c
+curl_xml_la_LDFLAGS = -module -avoid-version
+curl_xml_la_CFLAGS = $(AM_CFLAGS) \
+               $(BUILD_WITH_LIBCURL_CFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS)
+curl_xml_la_LIBADD = $(BUILD_WITH_LIBCURL_LIBS) $(BUILD_WITH_LIBXML2_LIBS)
+collectd_LDADD += "-dlopen" curl_xml.la
+collectd_DEPENDENCIES += curl_xml.la
+endif
+
 if BUILD_PLUGIN_DBI
 pkglib_LTLIBRARIES += dbi.la
 dbi_la_SOURCES = dbi.c \
@@ -284,6 +309,9 @@ if BUILD_WITH_LIBSTATGRAB
 disk_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS)  
 disk_la_LIBADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS)
 endif
+if BUILD_WITH_PERFSTAT
+disk_la_LIBADD += -lperfstat
+endif
 collectd_LDADD += "-dlopen" disk.la
 collectd_DEPENDENCIES += disk.la
 endif
@@ -374,6 +402,9 @@ if BUILD_WITH_LIBDEVINFO
 interface_la_LIBADD += -ldevinfo
 endif # BUILD_WITH_LIBDEVINFO
 endif # !BUILD_WITH_LIBSTATGRAB
+if BUILD_WITH_PERFSTAT
+interface_la_LIBADD += -lperfstat
+endif
 endif # BUILD_PLUGIN_INTERFACE
 
 if BUILD_PLUGIN_IPTABLES
@@ -452,6 +483,9 @@ if BUILD_WITH_LIBSTATGRAB
 load_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS)
 load_la_LIBADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS)
 endif # BUILD_WITH_LIBSTATGRAB
+if BUILD_WITH_PERFSTAT
+load_la_LIBADD += -lperfstat
+endif
 endif # BUILD_PLUGIN_LOAD
 
 if BUILD_PLUGIN_LOGFILE
@@ -562,6 +596,19 @@ if BUILD_WITH_LIBSTATGRAB
 memory_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS)
 memory_la_LIBADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS)
 endif
+if BUILD_WITH_PERFSTAT
+memory_la_LIBADD += -lperfstat
+endif
+endif
+
+if BUILD_PLUGIN_MODBUS
+pkglib_LTLIBRARIES += modbus.la
+modbus_la_SOURCES = modbus.c
+modbus_la_LDFLAGS = -module -avoid-version
+modbus_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBMODBUS_CFLAGS)
+modbus_la_LIBADD = $(BUILD_WITH_LIBMODBUS_LIBS)
+collectd_LDADD += "-dlopen" modbus.la
+collectd_DEPENDENCIES += modbus.la
 endif
 
 if BUILD_PLUGIN_MULTIMETER
@@ -760,6 +807,15 @@ collectd_LDADD += "-dlopen" perl.la
 collectd_DEPENDENCIES += perl.la
 endif
 
+if BUILD_PLUGIN_PINBA
+pkglib_LTLIBRARIES += pinba.la
+pinba_la_SOURCES = pinba.c pinba.pb-c.c pinba.pb-c.h
+pinba_la_LDFLAGS = -module -avoid-version
+pinba_la_LIBADD = -lprotobuf-c
+collectd_LDADD += "-dlopen" pinba.la
+collectd_DEPENDENCIES += pinba.la
+endif
+
 if BUILD_PLUGIN_PING
 pkglib_LTLIBRARIES += ping.la
 ping_la_SOURCES = ping.c
@@ -790,6 +846,16 @@ collectd_LDADD += "-dlopen" powerdns.la
 collectd_DEPENDENCIES += powerdns.la
 endif
 
+if BUILD_PLUGIN_PYTHON
+pkglib_LTLIBRARIES += python.la
+python_la_SOURCES = python.c pyconfig.c pyvalues.c cpython.h
+python_la_CPPFLAGS = $(AM_CPPFLAGS) $(BUILD_WITH_PYTHON_CPPFLAGS)
+python_la_LDFLAGS = -module -avoid-version $(BUILD_WITH_PYTHON_LDFLAGS)
+python_la_LIBADD = $(BUILD_WITH_PYTHON_LIBS)
+collectd_LDADD += "-dlopen" python.la
+collectd_DEPENDENCIES += python.la
+endif
+
 if BUILD_PLUGIN_PROCESSES
 pkglib_LTLIBRARIES += processes.la
 processes_la_SOURCES = processes.c
@@ -810,6 +876,16 @@ collectd_LDADD += "-dlopen" protocols.la
 collectd_DEPENDENCIES += protocols.la
 endif
 
+if BUILD_PLUGIN_ROUTEROS
+pkglib_LTLIBRARIES += routeros.la
+routeros_la_SOURCES = routeros.c
+routeros_la_CPPFLAGS = $(BUILD_WITH_LIBROUTEROS_CPPFLAGS)
+routeros_la_LDFLAGS = -module -avoid-version $(BUILD_WITH_LIBROUTEROS_LDFLAGS)
+routeros_la_LIBADD = -lrouteros
+collectd_LDADD += "-dlopen" routeros.la
+collectd_DEPENDENCIES += routeros.la
+endif
+
 if BUILD_PLUGIN_RRDCACHED
 pkglib_LTLIBRARIES += rrdcached.la
 rrdcached_la_SOURCES = rrdcached.c utils_rrdcreate.c utils_rrdcreate.h
@@ -886,6 +962,10 @@ if BUILD_WITH_LIBSTATGRAB
 swap_la_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS)
 swap_la_LIBADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS)
 endif
+if BUILD_WITH_PERFSTAT
+swap_la_LIBADD += -lperfstat
+endif
+
 endif
 
 if BUILD_PLUGIN_SYSLOG
@@ -1113,7 +1193,6 @@ collectd_LDADD += "-dlopen" zfs_arc.la
 collectd_DEPENDENCIES += zfs_arc.la
 endif
 
-
 dist_man_MANS = collectd.1 \
                collectd.conf.5 \
                collectd-email.5 \
@@ -1122,13 +1201,14 @@ dist_man_MANS = collectd.1 \
                collectdmon.1 \
                collectd-nagios.1 \
                collectd-perl.5 \
+               collectd-python.5 \
                collectd-snmp.5 \
                collectd-unixsock.5 \
                types.db.5
 
 #collectd_1_SOURCES = collectd.pod
 
-EXTRA_DIST = types.db
+EXTRA_DIST = types.db pinba.proto
 
 EXTRA_DIST +=   collectd.conf.pod \
                collectd-email.pod \
@@ -1137,6 +1217,7 @@ EXTRA_DIST +=   collectd.conf.pod \
                collectdmon.pod \
                collectd-nagios.pod \
                collectd-perl.pod \
+               collectd-python.pod \
                collectd.pod \
                collectd-snmp.pod \
                collectd-unixsock.pod \
@@ -1145,7 +1226,7 @@ EXTRA_DIST +=   collectd.conf.pod \
 
 .pod.1:
        pod2man --release=$(VERSION) --center=$(PACKAGE) $< \
-               >.pod2man.tmp 2>/dev/null && mv -f .pod2man.tmp $@ || true
+               >.pod2man.tmp.$$$$ 2>/dev/null && mv -f .pod2man.tmp.$$$$ $@ || true
        @if grep '\<POD ERRORS\>' $@ >/dev/null 2>&1; \
        then \
                echo "$@ has some POD errors!"; false; \
@@ -1153,12 +1234,17 @@ EXTRA_DIST +=   collectd.conf.pod \
 
 .pod.5:
        pod2man --section=5 --release=$(VERSION) --center=$(PACKAGE) $< \
-               >.pod2man.tmp 2>/dev/null && mv -f .pod2man.tmp $@ || true
+               >.pod2man.tmp.$$$$ 2>/dev/null && mv -f .pod2man.tmp.$$$$ $@ || true
        @if grep '\<POD ERRORS\>' $@ >/dev/null 2>&1; \
        then \
                echo "$@ has some POD errors!"; false; \
        fi
 
+if HAVE_PROTOC_C
+pinba.pb-c.c pinba.pb-c.h: pinba.proto
+       protoc-c --c_out $(builddir) pinba.proto
+endif
+
 install-exec-hook:
        $(mkinstalldirs) $(DESTDIR)$(sysconfdir)
        if test -e $(DESTDIR)$(sysconfdir)/collectd.conf; \
index df1b560..3d6d957 100644 (file)
@@ -202,7 +202,7 @@ static int config_set_boolean (int *ret_boolean, /* {{{ */
                return (-1);
        }
 
-       if (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
+       if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
        {
                if (ci->values[0].value.boolean)
                        *ret_boolean = 1;
@@ -313,7 +313,8 @@ static int config_add (oconfig_item_t *ci)
                                (st->host != NULL) ? st->host : hostname_g,
                                (st->name != NULL) ? st->name : "default"),
 
-               status = plugin_register_complex_read (callback_name,
+               status = plugin_register_complex_read (/* group = */ NULL,
+                               /* name      = */ callback_name,
                                /* callback  = */ apache_read_host,
                                /* interval  = */ NULL,
                                /* user_data = */ &ud);
@@ -685,8 +686,12 @@ static int apache_read_host (user_data_t *user_data) /* {{{ */
                {
                        if (strcmp (fields[0], "Scoreboard:") == 0)
                                submit_scoreboard (fields[1], st);
-                       else if (strcmp (fields[0], "BusyServers:") == 0)
+                       else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */
+                                       || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */)
                                submit_gauge ("apache_connections", NULL, atol (fields[1]), st);
+                       else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */
+                                       || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */)
+                               submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st);
                }
        }
 
index b62ad81..4178d8b 100644 (file)
@@ -514,7 +514,8 @@ static int battery_read (void)
 
        if (0 == access (battery_acpi_dir, R_OK))
                walk_directory (battery_acpi_dir, battery_read_acpi,
-                               /* user_data = */ NULL);
+                               /* user_data = */ NULL,
+                               /* include hidden */ 0);
        else
        {
                char errbuf[1024];
index 29e34a4..45162bd 100644 (file)
@@ -316,7 +316,7 @@ static int do_check_con_none (size_t values_num,
        {
                printf (" |");
                for (i = 0; i < values_num; i++)
-                       printf (" %s=%g;;;;", values_names[i], values[i]);
+                       printf (" %s=%f;;;;", values_names[i], values[i]);
        }
        printf ("\n");
 
@@ -370,7 +370,7 @@ static int do_check_con_average (size_t values_num,
 
        printf ("%s: %g average |", status_str, average);
        for (i = 0; i < values_num; i++)
-               printf (" %s=%g;;;;", values_names[i], values[i]);
+               printf (" %s=%f;;;;", values_names[i], values[i]);
        printf ("\n");
 
        return (status_code);
@@ -420,7 +420,7 @@ static int do_check_con_sum (size_t values_num,
 
        printf ("%s: %g sum |", status_str, total);
        for (i = 0; i < values_num; i++)
-               printf (" %s=%g;;;;", values_names[i], values[i]);
+               printf (" %s=%f;;;;", values_names[i], values[i]);
        printf ("\n");
 
        return (status_code);
index b8d72f0..5637053 100644 (file)
@@ -8,7 +8,7 @@ collectd-perl - Documentation of collectd's C<perl plugin>
   # ...
   <Plugin perl>
     IncludeDir "/path/to/perl/plugins"
-    BaseName "Collectd::Plugin"
+    BaseName "Collectd::Plugins"
     EnableDebugger ""
     LoadPlugin "FooBar"
 
@@ -38,7 +38,9 @@ causes the Perl-interpreter to be initialized.
 =item B<BaseName> I<Name>
 
 Prepends I<Name>B<::> to all plugin names loaded after this option. This is
-provided for convenience to keep plugin names short.
+provided for convenience to keep plugin names short. All Perl-based plugins
+provided with the I<collectd> distributions reside in the C<Collectd::Plugins>
+namespace.
 
 =item E<lt>B<Plugin> I<Name>E<gt> block
 
@@ -163,7 +165,7 @@ and collectd:
 
 =item Config-Item
 
-A config-item is one structure which keeps the informations provided in the
+A config-item is one structure which keeps the information provided in the
 configuration file. The array of children keeps one entry for each
 configuration option. Each such entry is another config-item structure, which
 may nest further if nested blocks are used.
@@ -713,7 +715,7 @@ types used by the read, write and match functions.
 
 =item
 
-Please feel free to send in new plugins to collectd's mailinglist at
+Please feel free to send in new plugins to collectd's mailing list at
 E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
 inclusion in the main distribution. In the latter case, we will take care of
 keeping the plugin up to date and adapting it to new versions of collectd.
diff --git a/src/collectd-python.pod b/src/collectd-python.pod
new file mode 100644 (file)
index 0000000..36d2be3
--- /dev/null
@@ -0,0 +1,726 @@
+=head1 NAME
+
+collectd-python - Documentation of collectd's C<python plugin>
+
+=head1 SYNOPSIS
+
+  <LoadPlugin python>
+    Globals true
+  </LoadPlugin>
+  # ...
+  <Plugin python>
+    ModulePath "/path/to/your/python/modules"
+    LogTraces true
+    Interactive true
+    Import "spam"
+
+    <Module spam>
+      spam "wonderful" "lovely"
+    </Module>
+  </Plugin>
+
+=head1 DESCRIPTION
+
+The C<python plugin> embeds a Python-interpreter into collectd and provides an
+interface to collectd's plugin system. This makes it possible to write plugins
+for collectd in Python. This is a lot more efficient than executing a
+Python-script every time you want to read a value with the C<exec plugin> (see
+L<collectd-exec(5)>) and provides a lot more functionality, too.
+
+At least python I<version 2.3> is required.
+
+=head1 CONFIGURATION
+
+=over 4
+
+=item B<LoadPlugin> I<Plugin>
+
+Loads the Python plugin I<Plugin>. Unlike most other LoadPlugin lines, this one
+should be a block containing the line "Globals true". This will cause collectd
+to export the name of all objects in the python interpreter for all plugins to
+see. If you don't do this or your platform does not support it, the embedded
+interpreter will start anyway but you won't be able to load certain python
+modules, e.g. "time".
+
+=item B<Encoding> I<Name>
+
+The default encoding for Unicode objects you pass to collectd. If you omit this
+option it will default to B<ascii> on I<Python 2> and B<utf-8> on I<Python 3>.
+This is hardcoded in Python and will ignore everything else, including your
+locale.
+
+=item B<ModulePath> I<Name>
+
+Appends I<Name> to B<sys.path>. You won't be able to import any scripts you
+wrote unless they are located in one of the directories in this list. Please
+note that it only has effect on plugins loaded after this option. You can
+use multiple B<ModulePath> lines to add more than one directory.
+
+=item B<LogTraces> I<bool>
+
+If a python script throws an exception it will be logged by collectd with the
+name of the exception and the message. If you set this option to true it will
+also log the full stacktrace just like the default output of an interactive
+python interpreter. This should probably be set to false most of the time but
+is very useful for development and debugging of new modules.
+
+=item B<Interactive> I<bool>
+
+This option will cause the module to launch an interactive python interpreter
+that reads from and writes to the terminal. Note that collectd will terminate
+right after starting up if you try to run it as a daemon while this option is
+enabled to make sure to start collectd with the B<-f> option.
+
+The B<collectd> module is I<not> imported into the interpreter's globals. You
+have to do it manually. Be sure to read the help text of the module, it can be
+used as a reference guide during coding.
+
+This interactive session will behave slightly differently from a daemonized
+collectd script as well as from a normal python interpreter:
+
+=over 4
+
+=item
+
+B<1.> collectd will try to import the B<readline> module to give you a decent
+way of entering your commands. The daemonized collectd won't do that.
+
+=item
+
+B<2.> collectd will block I<SIGINT>. Pressing I<Ctrl+C> will usually cause
+collectd to shut down. This would be problematic in an interactive session,
+therefore this signal will be blocked. You can still use it to interrupt
+syscalls like sleep and pause but it won't generate a I<KeyboardInterrupt>
+exception either.
+
+To quit collectd send I<EOF> (press I<Ctrl+D> at the beginning of a new line).
+
+=item
+
+B<3.> collectd handles I<SIGCHLD>. This means that python won't be able to
+determine the return code of spawned processes with system(), popen() and
+subprocess. This will result in python not using external programs like less
+to display help texts. You can override this behavior with the B<PAGER>
+environment variable, e.g. I<export PAGER=less> before starting collectd.
+Depending on your version of python this might or might not result in an
+B<OSError> exception which can be ignored.
+
+If you really need to spawn new processes from python you can register an init
+callback and reset the action for SIGCHLD to the default behavior. Please note
+that this I<will> break the exec plugin. Do not even load the exec plugin if
+you intend to do this!
+
+There is an example script located in B<contrib/python/getsigchld.py>  to do
+this. If you import this from I<collectd.conf> SIGCHLD will be handled
+normally and spawning processes from python will work as intended.
+
+=back
+
+=item E<lt>B<Module> I<Name>E<gt> block
+
+This block may be used to pass on configuration settings to a Python module.
+The configuration is converted into an instance of the B<Config> class which is
+passed to the registered configuration callback. See below for details about
+the B<Config> class and how to register callbacks.
+
+The I<name> identifies the callback.
+
+=back
+
+=head1 STRINGS
+
+There are a lot of places where strings are send from collectd to python and
+from python to collectd. How exactly this works depends on wheather byte or
+unicode strings or python2 or python3 are used.
+
+Python2 has I<str>, which is just bytes, and I<unicode>. Python3 has I<str>,
+which is a unicode object, and I<bytes>.
+
+When passing strings from python to collectd all of these object are supported
+in all places, however I<str> should be used if possible. These strings must
+not contain a NUL byte. Ignoring this will result in a I<TypeError> exception.
+If a byte string was used it will be used as is by collectd. If a unicode
+object was used it will be encoded using the default encoding (see above). If
+this is not possible python will raise a I<UnicodeEncodeError> exception.
+
+Wenn passing strings from collectd to python the behavior depends on the
+python version used. Python2 will always receive a I<str> object. Python3 will
+usually receive a I<str> object as well, however the original string will be
+decoded to unicode using the default encoding. If this fails because the
+string is not a valid sequence for this encoding a I<bytes> object will be
+returned instead.
+
+=head1 WRITING YOUR OWN PLUGINS
+
+Writing your own plugins is quite simple. collectd manages plugins by means of
+B<dispatch functions> which call the appropriate B<callback functions>
+registered by the plugins. Any plugin basically consists of the implementation
+of these callback functions and initializing code which registers the
+functions with collectd. See the section "EXAMPLES" below for a really basic
+example. The following types of B<callback functions> are known to collectd
+(all of them are optional):
+
+=over 4
+
+=item configuration functions
+
+This type of functions is called during configuration if an appropriate
+B<Module> block has been encountered. It is called once for each B<Module>
+block which matches the name of the callback as provided with the
+B<register_config> method - see below.
+
+Python thread support has not been initialized at this point so do not use any
+threading functions here!
+
+=item init functions
+
+This type of functions is called once after loading the module and before any
+calls to the read and write functions. It should be used to initialize the
+internal state of the plugin (e.E<nbsp>g. open sockets, ...). This is the
+earliest point where you may use threads.
+
+=item read functions
+
+This type of function is used to collect the actual data. It is called once
+per interval (see the B<Interval> configuration option of collectd). Usually
+it will call B<plugin_dispatch_values> to dispatch the values to collectd
+which will pass them on to all registered B<write functions>. If this function
+throws any kind of exception the plugin will be skipped for an increasing
+amount of time until it returns normally again.
+
+=item write functions
+
+This type of function is used to write the dispatched values. It is called
+once for every value that was dispatched by any plugin.
+
+=item flush functions
+
+This type of function is used to flush internal caches of plugins. It is
+usually triggered by the user only. Any plugin which caches data before
+writing it to disk should provide this kind of callback function.
+
+=item log functions
+
+This type of function is used to pass messages of plugins or the daemon itself
+to the user.
+
+=item notification function
+
+This type of function is used to act upon notifications. In general, a
+notification is a status message that may be associated with a data instance.
+Usually, a notification is generated by the daemon if a configured threshold
+has been exceeded (see the section "THRESHOLD CONFIGURATION" in
+L<collectd.conf(5)> for more details), but any plugin may dispatch
+notifications as well.
+
+=item shutdown functions
+
+This type of function is called once before the daemon shuts down. It should
+be used to clean up the plugin (e.g. close sockets, ...).
+
+=back
+
+Any function (except log functions) may set throw an exception in case of any
+errors. The exception will be passed on to the user using collectd's logging
+mechanism. If a log callback throws an exception it will be printed to standard
+error instead.
+
+See the documentation of the various B<register_> methods in the section
+"FUNCTIONS" below for the number and types of arguments passed to each
+B<callback function>. This section also explains how to register B<callback
+functions> with collectd.
+
+To enable a module, copy it to a place where Python can find it (i.E<nbsp>e. a
+directory listed in B<sys.path>) just as any other Python plugin and add
+an appropriate B<Import> option to the configuration file. After restarting
+collectd you're done.
+
+=head1 CLASSES
+
+The following complex types are used to pass values between the Python plugin
+and collectd:
+
+=head2 Signed
+
+The Signed class is just a long. It has all its methods and behaves exactly
+like any other long object. It is used to indicate if an integer was or should
+be stored as a signed or unsigned integer object.
+
+ class Signed(long)
+
+This is a long by another name. Use it in meta data dicts
+to choose the way it is stored in the meta data.
+
+=head2 Unsigned
+
+The Unsigned class is just a long. It has all its methods and behaves exactly
+like any other long object. It is used to indicate if an integer was or should
+be stored as a signed or unsigned integer object.
+
+ class Unsigned(long)
+
+This is a long by another name. Use it in meta data dicts
+to choose the way it is stored in the meta data.
+
+=head2 Config
+
+The Config class is an object which keeps the information provided in the
+configuration file. The sequence of children keeps one entry for each
+configuration option. Each such entry is another Config instance, which
+may nest further if nested blocks are used.
+
+ class Config(object)
+
+This represents a piece of collectd's config file. It is passed to scripts with
+config callbacks (see B<register_config>) and is of little use if created
+somewhere else.
+
+It has no methods beyond the bare minimum and only exists for its data members.
+
+Data descriptors defined here:
+
+=over 4
+
+=item parent
+
+This represents the parent of this node. On the root node
+of the config tree it will be None.
+
+=item key
+
+This is the keyword of this item, i.e. the first word of any given line in the
+config file. It will always be a string.
+
+=item values
+
+This is a tuple (which might be empty) of all value, i.e. words following the
+keyword in any given line in the config file.
+
+Every item in this tuple will be either a string or a float or a boolean,
+depending on the contents of the configuration file.
+
+=item children
+
+This is a tuple of child nodes. For most nodes this will be empty. If this node
+represents a block instead of a single line of the config file it will contain
+all nodes in this block.
+
+=back
+
+=head2 PluginData
+
+This should not be used directly but it is the base class for both Values and
+Notification. It is used to identify the source of a value or notification.
+
+ class PluginData(object)
+
+This is an internal class that is the base for Values and Notification. It is
+pretty useless by itself and was therefore not exported to the collectd module.
+
+Data descriptors defined here:
+
+=over 4
+
+=item host
+
+The hostname of the host this value was read from. For dispatching this can be
+set to an empty string which means the local hostname as defined in
+collectd.conf.
+
+=item plugin
+
+The name of the plugin that read the data. Setting this member to an empty
+string will insert "python" upon dispatching.
+
+=item plugin_instance
+
+Plugin instance string. May be empty.
+
+=item time
+
+This is the Unix timestamp of the time this value was read. For dispatching
+values this can be set to zero which means "now". This means the time the value
+is actually dispatched, not the time it was set to 0.
+
+=item type
+
+The type of this value. This type has to be defined in your I<types.db>.
+Attempting to set it to any other value will raise a I<TypeError> exception.
+Assigning a type is mandatory, calling dispatch without doing so will raise a
+I<RuntimeError> exception.
+
+=item type_instance
+
+Type instance string. May be empty.
+
+=back
+
+=head2 Values
+
+A Value is an object which features a sequence of values. It is based on then
+I<PluginData> type and uses its members to identify the values.
+
+ class Values(PluginData)
+
+A Values object used for dispatching values to collectd and receiving values
+from write callbacks.
+
+Method resolution order:
+
+=over 4
+
+=item Values
+
+=item PluginData
+
+=item object
+
+=back
+
+Methods defined here:
+
+=over 4
+
+=item B<dispatch>([type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None.
+
+Dispatch this instance to the collectd process. The object has members for each
+of the possible arguments for this method. For a detailed explanation of these
+parameters see the member of the same same.
+
+If you do not submit a parameter the value saved in its member will be
+submitted. If you do provide a parameter it will be used instead, without
+altering the member.
+
+=item B<write>([destination][, type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None.
+
+Write this instance to a single plugin or all plugins if "destination" is
+omitted. This will bypass the main collectd process and all filtering and
+caching. Other than that it works similar to "dispatch". In most cases
+"dispatch" should be used instead of "write".
+
+=back
+
+Data descriptors defined here:
+
+=over 4
+
+=item interval
+
+The interval is the timespan in seconds between two submits for the same data
+source. This value has to be a positive integer, so you can't submit more than
+one value per second. If this member is set to a non-positive value, the
+default value as specified in the config file will be used (default: 10).
+
+If you submit values more often than the specified interval, the average will
+be used. If you submit less values, your graphs will have gaps.
+
+=item values
+
+These are the actual values that get dispatched to collectd. It has to be a
+sequence (a tuple or list) of numbers. The size of the sequence and the type of
+its content depend on the type member your I<types.db> file. For more
+information on this read the L<types.db(5)> manual page.
+
+If the sequence does not have the correct size upon dispatch a I<RuntimeError>
+exception will be raised. If the content of the sequence is not a number, a
+I<TypeError> exception will be raised.
+
+=item meta
+These are the meta data for this Value object.
+It has to be a dictionary of numbers, strings or bools. All keys must be
+strings. I<int> and <long> objects will be dispatched as signed integers unless
+they are between 2**63 and 2**64-1, which will result in a unsigned integer.
+You can force one of these storage classes by using the classes
+B<collectd.Signed> and B<collectd.Unsigned>. A meta object received by a write
+callback will always contain B<Signed> or B<Unsigned> objects.
+
+=back
+
+=head2 Notification
+
+A notification is an object defining the severity and message of the status
+message as well as an identification of a data instance by means of the members
+of I<PluginData> on which it is based.
+
+class Notification(PluginData)
+The Notification class is a wrapper around the collectd notification.
+It can be used to notify other plugins about bad stuff happening. It works
+similar to Values but has a severity and a message instead of interval
+and time.
+Notifications can be dispatched at any time and can be received with
+register_notification.
+
+Method resolution order:
+
+=over 4
+
+=item Notification
+
+=item PluginData
+
+=item object
+
+=back
+
+Methods defined here:
+
+=over 4
+
+=item B<dispatch>([type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None.  Dispatch a value list.
+
+Dispatch this instance to the collectd process. The object has members for each
+of the possible arguments for this method. For a detailed explanation of these
+parameters see the member of the same same.
+
+If you do not submit a parameter the value saved in its member will be
+submitted. If you do provide a parameter it will be used instead, without
+altering the member.
+
+=back
+
+Data descriptors defined here:
+
+=over 4
+
+=item message
+
+Some kind of description what's going on and why this Notification was
+generated.
+
+=item severity
+
+The severity of this notification. Assign or compare to I<NOTIF_FAILURE>,
+I<NOTIF_WARNING> or I<NOTIF_OKAY>.
+
+=back
+
+=head1 FUNCTIONS
+
+The following functions provide the C-interface to Python-modules.
+
+=over 4
+
+=item B<register_*>(I<callback>[, I<data>][, I<name>]) -> identifier
+
+There are eight different register functions to get callback for eight
+different events. With one exception all of them are called as shown above.
+
+=over 4
+
+=item
+
+I<callback> is a callable object that will be called every time the event is
+triggered.
+
+=item
+
+I<data> is an optional object that will be passed back to the callback function
+every time it is called. If you omit this parameter no object is passed back to
+your callback, not even None.
+
+=item
+
+I<name> is an optional identifier for this callback. The default name is
+B<python>.I<module>. I<module> is taken from the B<__module__> attribute of
+your callback function. Every callback needs a unique identifier, so if you
+want to register the same callback multiple time in the same module you need to
+specify a name here. Otherwise it's save to ignore this parameter I<identifier>
+is the full identifier assigned to this callback.
+
+=back
+
+These functions are called in the various stages of the daemon (see the section
+L<"WRITING YOUR OWN PLUGINS"> above) and are passed the following arguments:
+
+=over 4
+
+=item register_config
+
+The only argument passed is a I<Config> object. See above for the layout of this
+data type.
+Note that you can not receive the whole config files this way, only B<Module>
+blocks inside the Python configuration block. Additionally you will only
+receive blocks where your callback identifier matches B<python.>I<blockname>.
+
+=item register_init
+
+The callback will be called without arguments.
+
+=item register_read(callback[, interval][, data][, name]) -> identifier
+
+This function takes an additional parameter: I<interval>. It specifies the
+time between calls to the callback function.
+
+The callback will be called without arguments.
+
+=item register_shutdown
+
+The callback will be called without arguments.
+
+=item register_write
+
+The callback function will be called with one arguments passed, which will be a
+I<Values> object. For the layout of I<Values> see above.
+If this callback function throws an exception the next call will be delayed by
+an increasing interval.
+
+=item register_flush
+
+Like B<register_config> is important for this callback because it determines
+what flush requests the plugin will receive.
+
+The arguments passed are I<timeout> and I<identifier>. I<timeout> indicates
+that only data older than I<timeout> seconds is to be flushed. I<identifier>
+specifies which values are to be flushed.
+
+=item register_log
+
+The arguments are I<severity> and I<message>. The severity is an integer and
+small for important messages and high for less important messages. The least
+important level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In
+between there are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>,
+and B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the
+end.
+
+If this callback throws an exception it will B<not> be logged. It will just be
+printed to B<sys.stderr> which usually means silently ignored.
+
+=item register_notification
+
+The only argument passed is a I<Notification> object. See above for the layout of this
+data type.
+
+=back
+
+=item B<unregister_*>(I<identifier>) -> None
+
+Removes a callback or data-set from collectd's internal list of callback
+functions. Every I<register_*> function has an I<unregister_*> function.
+I<identifier> is either the string that was returned by the register function
+or a callback function. The identifier will be constructed in the same way as
+for the register functions.
+
+=item B<flush>(I<plugin[, I<timeout>][, I<identifier>]) -> None
+
+Flush one or all plugins. I<timeout> and the specified I<identifiers> are
+passed on to the registered flush-callbacks. If omitted, the timeout defaults
+to C<-1>. The identifier defaults to None. If the B<plugin> argument has been
+specified, only named plugin will be flushed.
+
+=item B<error>, B<warning>, B<notice>, B<info>, B<debug>(I<message>)
+
+Log a message with the specified severity.
+
+=back
+
+=head1 EXAMPLES
+
+Any Python module will start similar to:
+
+  import collectd
+
+A very simple read function might look like:
+
+  def read(data=None):
+    vl = collectd.Values(type='gauge')
+    vl.plugin='python.spam'
+    vl.dispatch(values=[random.random() * 100])
+
+A very simple write function might look like:
+
+  def write(vl, data=None):
+    for i in vl.values:
+      print "%s (%s): %f" % (vl.plugin, vl.type, i)
+
+To register those functions with collectd:
+
+  collectd.register_read(read);
+  collectd.register_write(write);
+
+See the section L<"CLASSES"> above for a complete documentation of the data
+types used by the read, write and match functions.
+
+=head1 NOTES
+
+=over 4
+
+=item
+
+Please feel free to send in new plugins to collectd's mailinglist at
+E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
+inclusion in the main distribution. In the latter case, we will take care of
+keeping the plugin up to date and adapting it to new versions of collectd.
+
+Before submitting your plugin, please take a look at
+L<http://collectd.org/dev-info.shtml>.
+
+=back
+
+=head1 CAVEATS
+
+=over 4
+
+=item
+
+collectd is heavily multi-threaded. Each collectd thread accessing the python
+plugin will be mapped to a Python interpreter thread. Any such thread will be
+created and destroyed transparently and on-the-fly.
+
+Hence, any plugin has to be thread-safe if it provides several entry points
+from collectd (i.E<nbsp>e. if it registers more than one callback or if a
+registered callback may be called more than once in parallel).
+
+=item
+
+The Python thread module is initialized just before calling the init callbacks.
+This means you must not use Python's threading module prior to this point. This
+includes all config and possibly other callback as well.
+
+=item
+
+The python plugin exports the internal API of collectd which is considered
+unstable and subject to change at any time. We try hard to not break backwards
+compatibility in the Python API during the life cycle of one major release.
+However, this cannot be guaranteed at all times. Watch out for warnings
+dispatched by the python plugin after upgrades.
+
+=back
+
+=head1 KNOWN BUGS
+
+=over 4
+
+=item
+
+This plugin is not compatible with python3. Trying to compile it with python3
+will fail because of the ways string, unicode and bytearray behavior was
+changed.
+
+=item
+
+Not all aspects of the collectd API are accessible from python. This includes
+but is not limited to meta-data, filters and data sets.
+
+=back
+
+=head1 SEE ALSO
+
+L<collectd(1)>,
+L<collectd.conf(5)>,
+L<collectd-perl(5)>,
+L<collectd-exec(5)>,
+L<types.db(5)>,
+L<python(1)>,
+
+=head1 AUTHOR
+
+The C<python plugin> has been written by
+Sven Trenkel E<lt>collectdE<nbsp>atE<nbsp>semidefinite.deE<gt>.
+
+This manpage has been written by Sven Trenkel
+E<lt>collectdE<nbsp>atE<nbsp>semidefinite.deE<gt>.
+It is based on the L<collectd-perl(5)> manual page by
+Florian Forster E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and
+Sebastian Harl E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
+
+=cut
index 51e0481..3c6e799 100644 (file)
@@ -175,7 +175,8 @@ traffic.
 The gauge-values returned by the SNMP-agent are multiplied by I<Value>.  This
 is useful when values are transfered as a fixed point real number. For example,
 thermometers may transfer B<243> but actually mean B<24.3>, so you can specify
-a scale value of B<0.1> to correct this. The default value is of course B<1.0>.
+a scale value of B<0.1> to correct this. The default value is, of course,
+B<1.0>.
 
 This value is not applied to counter-values.
 
@@ -184,7 +185,7 @@ This value is not applied to counter-values.
 I<Value> is added to gauge-values returned by the SNMP-agent after they have
 been multiplied by any B<Scale> value. If, for example, a thermometer returns
 degrees Kelvin you could specify a shift of B<273.15> here to store values in
-degrees Celsius. The default value is of course B<0.0>.
+degrees Celsius. The default value is, of course, B<0.0>.
 
 This value is not applied to counter-values.
 
index bc69a3b..277d3b0 100644 (file)
@@ -41,6 +41,7 @@
  */
 char hostname_g[DATA_MAX_NAME_LEN];
 int  interval_g;
+int  timeout_g;
 #if HAVE_LIBKSTAT
 kstat_ctl_t *kc;
 #endif /* HAVE_LIBKSTAT */
@@ -148,6 +149,18 @@ static int init_global_variables (void)
        }
        DEBUG ("interval_g = %i;", interval_g);
 
+       str = global_option_get ("Timeout");
+       if (str == NULL)
+               str = "2";
+       timeout_g = atoi (str);
+       if (timeout_g <= 1)
+       {
+               fprintf (stderr, "Cannot set the timeout to a correct value.\n"
+                               "Please check your settings.\n");
+               return (-1);
+       }
+       DEBUG ("timeout_g = %i;", timeout_g);
+
        if (init_hostname () != 0)
                return (-1);
        DEBUG ("hostname_g = %s;", hostname_g);
@@ -259,9 +272,10 @@ static void exit_usage (int status)
 #endif
                        "    -h              Display help (this message)\n"
                        "\nBuiltin defaults:\n"
-                       "  Config-File       "CONFIGFILE"\n"
-                       "  PID-File          "PIDFILE"\n"
-                       "  Data-Directory    "PKGLOCALSTATEDIR"\n"
+                       "  Config file       "CONFIGFILE"\n"
+                       "  PID file          "PIDFILE"\n"
+                       "  Plugin directory  "PLUGINDIR"\n"
+                       "  Data directory    "PKGLOCALSTATEDIR"\n"
                        "\n"PACKAGE" "VERSION", http://collectd.org/\n"
                        "by Florian octo Forster <octo@verplant.org>\n"
                        "for contributions see `AUTHORS'\n");
index ad13353..c564e1f 100644 (file)
@@ -63,6 +63,7 @@ FQDNLookup   true
 @LOAD_PLUGIN_CSV@LoadPlugin csv
 #@BUILD_PLUGIN_CURL_TRUE@LoadPlugin curl
 #@BUILD_PLUGIN_CURL_JSON_TRUE@LoadPlugin curl_json
+#@BUILD_PLUGIN_CURL_XML_TRUE@LoadPlugin curl_xml
 #@BUILD_PLUGIN_DBI_TRUE@LoadPlugin dbi
 #@BUILD_PLUGIN_DF_TRUE@LoadPlugin df
 #@BUILD_PLUGIN_DISK_TRUE@LoadPlugin disk
@@ -89,6 +90,7 @@ FQDNLookup   true
 @BUILD_PLUGIN_MEMORY_TRUE@@BUILD_PLUGIN_MEMORY_TRUE@LoadPlugin memory
 #@BUILD_PLUGIN_MULTIMETER_TRUE@LoadPlugin multimeter
 #@BUILD_PLUGIN_MYSQL_TRUE@LoadPlugin mysql
+#@BUILD_PLUGIN_NETAPP_TRUE@LoadPlugin netapp
 #@BUILD_PLUGIN_NETLINK_TRUE@LoadPlugin netlink
 @LOAD_PLUGIN_NETWORK@LoadPlugin network
 #@BUILD_PLUGIN_NFS_TRUE@LoadPlugin nfs
@@ -102,11 +104,14 @@ FQDNLookup   true
 #@BUILD_PLUGIN_OPENVPN_TRUE@LoadPlugin openvpn
 #@BUILD_PLUGIN_ORACLE_TRUE@LoadPlugin oracle
 #@BUILD_PLUGIN_PERL_TRUE@LoadPlugin perl
+#@BUILD_PLUGIN_PINBA_TRUE@LoadPlugin pinba
 #@BUILD_PLUGIN_PING_TRUE@LoadPlugin ping
 #@BUILD_PLUGIN_POSTGRESQL_TRUE@LoadPlugin postgresql
 #@BUILD_PLUGIN_POWERDNS_TRUE@LoadPlugin powerdns
 #@BUILD_PLUGIN_PROCESSES_TRUE@LoadPlugin processes
 #@BUILD_PLUGIN_PROTOCOLS_TRUE@LoadPlugin protocols
+#@BUILD_PLUGIN_PYTHON_TRUE@LoadPlugin python
+#@BUILD_PLUGIN_ROUTEROS_TRUE@LoadPlugin routeros
 #@BUILD_PLUGIN_RRDCACHED_TRUE@LoadPlugin rrdcached
 @LOAD_PLUGIN_RRDTOOL@LoadPlugin rrdtool
 #@BUILD_PLUGIN_SENSORS_TRUE@LoadPlugin sensors
@@ -187,6 +192,7 @@ FQDNLookup   true
 #    URL "http://finance.google.com/finance?q=NYSE%3AAMD"
 #    User "foo"
 #    Password "bar"
+#    MeasureResponseTime false
 #    <Match>
 #      Regex "<span +class=\"pr\"[^>]*> *([0-9]*\\.[0-9]+) *</span>"
 #      DSType "GaugeAverage"
@@ -227,6 +233,25 @@ FQDNLookup   true
 #  </URL>
 #</Plugin>
 
+#<Plugin "curl_xml">
+#  <URL "http://localhost/stats.xml">
+#    Host "my_host"
+#    Instance "some_instance"
+#    User "collectd"
+#    Password "thaiNg0I"
+#    VerifyPeer true
+#    VerifyHost true
+#    CACert "/path/to/ca.crt"
+#
+#    <XPath "table[@id=\"magic_level\"]/tr">
+#      Type "magic_level"
+#      #InstancePrefix "prefix-"
+#      InstanceFrom "td[1]"
+#      ValuesFrom "td[2]/span[@class=\"level\"]"
+#    </XPath>
+#  </URL>
+#</Plugin>
+
 #<Plugin dbi>
 #      <Query "num_of_customers">
 #              Statement "SELECT 'customers' AS c_key, COUNT(*) AS c_value FROM customers_tbl"
@@ -255,6 +280,8 @@ FQDNLookup   true
 #      FSType "ext3"
 #      IgnoreSelected false
 #      ReportByDevice false
+#      ReportReserved false
+#      ReportInodes false
 #</Plugin>
 
 #<Plugin disk>
@@ -343,7 +370,7 @@ FQDNLookup   true
 
 #<Plugin "java">
 #      JVMArg "-verbose:jni"
-#      JVMArg "-Djava.class.path=@prefix@/share/collectd/java"
+#      JVMArg "-Djava.class.path=@prefix@/share/collectd/java/collectd-api.jar"
 #
 #      LoadPlugin "org.collectd.java.Foobar"
 #      <Plugin "org.collectd.java.Foobar">
@@ -412,6 +439,58 @@ FQDNLookup   true
 #      </Database>
 #</Plugin>
 
+#<Plugin netapp>
+#      <Host "netapp1.example.com">
+#              Protocol      "https"
+#              Address       "10.0.0.1"
+#              Port          443
+#              User          "username"
+#              Password      "aef4Aebe"
+#              Interval      30
+#
+#              <WAFL>
+#                      Interval 30
+#                      GetNameCache   true
+#                      GetDirCache    true
+#                      GetBufferCache true
+#                      GetInodeCache  true
+#              </WAFL>
+#
+#              <Disks>
+#                      Interval 30
+#                      GetBusy true
+#              </Disks>
+#
+#              <VolumePerf>
+#                      Interval 30
+#                      GetIO      "volume0"
+#                      IgnoreSelectedIO      false
+#                      GetOps     "volume0"
+#                      IgnoreSelectedOps     false
+#                      GetLatency "volume0"
+#                      IgnoreSelectedLatency false
+#              </VolumePerf>
+#
+#              <VolumeUsage>
+#                      Interval 30
+#                      GetCapacity "vol0"
+#                      GetCapacity "vol1"
+#                      IgnoreSelectedCapacity false
+#                      GetSnapshot "vol1"
+#                      GetSnapshot "vol3"
+#                      IgnoreSelectedSnapshot false
+#              </VolumeUsage>
+#
+#              <System>
+#                      Interval 30
+#                      GetCPULoad     true
+#                      GetInterfaces  true
+#                      GetDiskOps     true
+#                      GetDiskIO      true
+#              </System>
+#      </Host>
+#</Plugin>
+
 #<Plugin netlink>
 #      Interface "All"
 #      VerboseInterface "All"
@@ -504,9 +583,10 @@ FQDNLookup   true
 
 #<Plugin perl>
 #      IncludeDir "/my/include/path"
-#      BaseName "Collectd::Plugin"
+#      BaseName "Collectd::Plugins"
 #      EnableDebugger ""
-#      LoadPlugin foo
+#      LoadPlugin Monitorus
+#      LoadPlugin OpenVZ
 #
 #      <Plugin foo>
 #              Foo "Bar"
@@ -514,9 +594,24 @@ FQDNLookup   true
 #      </Plugin>
 #</Plugin>
 
+#<Plugin pinba>
+#      Address "::0"
+#      Port "30002"
+#      <View "name">
+#              Host "host name"
+#              Server "server name"
+#              Script "script name"
+#      </View>
+#</Plugin>
+
 #<Plugin ping>
 #      Host "host.foo.bar"
+#      Interval 1.0
+#      Timeout 0.9
 #      TTL 255
+#      SourceAddress "1.2.3.4"
+#      Device "eth0"
+#      MaxMissed -1
 #</Plugin>
 
 #<Plugin postgresql>
@@ -582,6 +677,32 @@ FQDNLookup   true
 #      IgnoreSelected false
 #</Plugin>
 
+#<Plugin python>
+#      ModulePath "/path/to/your/python/modules"
+#      LogTraces true
+#      Interactive true
+#      Import "spam"
+#
+#      <Module spam>
+#              spam "wonderful" "lovely"
+#      </Module>
+#</Plugin>
+
+#<Plugin routeros>
+#      <Router>
+#              Host "router.example.com"
+#              Port "8728"
+#              User "admin"
+#              Password "dozaiTh4"
+#              CollectInterface true
+#              CollectRegistrationTable true
+#              CollectCPULoad true
+#              CollectMemory true
+#              CollectDF true
+#              CollectDisk true
+#      </Router>
+#</Plugin>
+
 #<Plugin rrdcached>
 #      DaemonAddress "unix:/tmp/rrdcached.sock"
 #      DataDir "@prefix@/var/lib/@PACKAGE_NAME@/rrd"
@@ -674,6 +795,7 @@ FQDNLookup   true
 #    </Match>
 #    <Match>
 #      Regex "\\<R=local_user\\>"
+#      ExcludeRegex "\\<R=local_user\\>.*mail_spool defer"
 #      DSType "CounterInc"
 #      Type "counter"
 #      Instance "local_user"
@@ -740,6 +862,7 @@ FQDNLookup   true
 
 # Load required matches:
 #@BUILD_PLUGIN_MATCH_EMPTY_COUNTER_TRUE@LoadPlugin match_empty_counter
+#@BUILD_PLUGIN_MATCH_HASHED_TRUE@LoadPlugin match_hashed
 #@BUILD_PLUGIN_MATCH_REGEX_TRUE@LoadPlugin match_regex
 #@BUILD_PLUGIN_MATCH_VALUE_TRUE@LoadPlugin match_value
 #@BUILD_PLUGIN_MATCH_TIMEDIFF_TRUE@LoadPlugin match_timediff
@@ -747,6 +870,7 @@ FQDNLookup   true
 # Load required targets:
 #@BUILD_PLUGIN_TARGET_NOTIFICATION_TRUE@LoadPlugin target_notification
 #@BUILD_PLUGIN_TARGET_REPLACE_TRUE@LoadPlugin target_replace
+#@BUILD_PLUGIN_TARGET_SCALE_TRUE@LoadPlugin target_scale
 #@BUILD_PLUGIN_TARGET_SET_TRUE@LoadPlugin target_set
 
 #----------------------------------------------------------------------------#
index 6bae339..773fc36 100644 (file)
@@ -102,6 +102,16 @@ Configures the interval in which to query the read plugins. Obviously smaller
 values lead to a higher system load produced by collectd, while higher values
 lead to more coarse statistics.
 
+=item B<Timeout> I<Iterations>
+
+Consider a value list "missing" when no update has been read or received for
+I<Iterations> iterations. By default, I<collectd> considers a value list
+missing when no update has been received for twice the update interval. Since
+this setting uses iterations, the maximum allowed time without update depends
+on the I<Interval> information contained in each value list. This is used in
+the I<Threshold> configuration to dispatch notifications about missing values,
+see L<"THRESHOLD CONFIGURATION"> below.
+
 =item B<ReadThreads> I<Num>
 
 Number of threads to start for reading plugins. The default value is B<5>, but
@@ -112,7 +122,7 @@ a value higher than the number of plugins you've loaded is totally useless.
 =item B<Hostname> I<Name>
 
 Sets the hostname that identifies a host. If you omit this setting, the
-hostname will be determinded using the L<gethostname(2)> system call.
+hostname will be determined using the L<gethostname(2)> system call.
 
 =item B<FQDNLookup> B<true|false>
 
@@ -443,7 +453,7 @@ The curl plugin uses the B<libcurl> (L<http://curl.haxx.se/>) to read web pages
 and the match infrastructure (the same code used by the tail plugin) to use
 regular expressions with the received data.
 
-The following example will read the current value of AMD stock from google's
+The following example will read the current value of AMD stock from Google's
 finance page and dispatch the value to collectd.
 
   <Plugin curl>
@@ -503,14 +513,16 @@ and are checked by default depends on the distribution you use.
 
 =item B<MeasureResponseTime> B<true>|B<false>
 
-Measure response time for the request. Disabled by default.
+Measure response time for the request. If this setting is enabled, B<Match>
+blocks (see below) are optional. Disabled by default.
 
 =item B<E<lt>MatchE<gt>>
 
 One or more B<Match> blocks that define how to match information in the data
 returned by C<libcurl>. The C<curl> plugin uses the same infrastructure that's
 used by the C<tail> plugin, so please see the documentation of the C<tail>
-plugin below on how matches are defined.
+plugin below on how matches are defined. If the B<MeasureResponseTime> option
+is set to B<true>, B<Match> blocks are optional.
 
 =back
 
@@ -560,9 +572,9 @@ The following example will collect the status values from each database:
 
 In the B<Plugin> block, there may be one or more B<URL> blocks, each defining
 a URL to be fetched via HTTP (using libcurl) and one or more B<Key> blocks.
-The B<Key> string argument must be in a path format, of which is used to collect
-a value from a JSON map object.  If a B<Key> path element is that of a I<*> wildcard,
-the values for all keys will be collectd.
+The B<Key> string argument must be in a path format, which is used to collect a
+value from a JSON map object. If a path element of B<Key> is the
+I<*>E<nbsp>wildcard, the values for all keys will be collectd.
 
 The following options are valid within B<URL> blocks:
 
@@ -617,6 +629,110 @@ Type-instance to use. Defaults to the current map key or current string array el
 
 =back
 
+=head2 Plugin C<curl_xml>
+
+The B<curl_xml plugin> uses B<libcurl> (L<http://curl.haxx.se/>) and B<libxml2>
+(L<http://xmlsoft.org/>) to retrieve XML data via cURL.
+
+ <Plugin "curl_xml">
+   <URL "http://localhost/stats.xml">
+     Host "my_host"
+     Instance "some_instance"
+     User "collectd"
+     Password "thaiNg0I"
+     VerifyPeer true
+     VerifyHost true
+     CACert "/path/to/ca.crt"
+
+     <XPath "table[@id=\"magic_level\"]/tr">
+       Type "magic_level"
+       #InstancePrefix "prefix-"
+       InstanceFrom "td[1]"
+       ValuesFrom "td[2]/span[@class=\"level\"]"
+     </XPath>
+   </URL>
+ </Plugin>
+
+In the B<Plugin> block, there may be one or more B<URL> blocks, each defining a
+URL to be fetched via HTTP (using libcurl). Within each B<URL> block there are
+options which specify the connection parameters, for example authentication
+information, and one or more B<XPath> blocks.
+
+Each B<XPath> block specifies how to get one type of information. The
+string argument must be a valid XPath expression which returns a list
+of "base elements". One value is dispatched for each "base element". The
+I<type instance> and values are looked up using further I<XPath> expressions
+that should be relative to the base element.
+
+Within the B<URL> block the following options are accepted:
+
+=over 4
+
+=item B<Host> I<Name>
+
+Use I<Name> as the host name when submitting values. Defaults to the global
+host name setting.
+
+=item B<Instance> I<Instance>
+
+Use I<Instance> as the plugin instance when submitting values. Defaults to an
+empty string (no plugin instance).
+
+=item B<User> I<User>
+=item B<Password> I<Password>
+=item B<VerifyPeer> B<true>|B<false>
+=item B<VerifyHost> B<true>|B<false>
+=item B<CACert> I<CA Cert File>
+
+These options behave exactly equivalent to the appropriate options of the
+I<cURL> and I<cURL-JSON> plugins. Please see there for a detailed description.
+
+=item E<lt>B<XPath> I<XPath-expression>E<gt>
+
+Within each B<URL> block, there must be one or more B<XPath> blocks. Each
+B<XPath> block specifies how to get one type of information. The string
+argument must be a valid XPath expression which returns a list of "base
+elements". One value is dispatched for each "base element".
+
+Within the B<XPath> block the following options are accepted:
+
+=over 4
+
+=item B<Type> I<Type>
+
+Specifies the I<Type> used for submitting patches. This determines the number
+of values that are required / expected and whether the strings are parsed as
+signed or unsigned integer or as double values. See L<types.db(5)> for details.
+This option is required.
+
+=item B<InstancePrefix> I<InstancePrefix>
+
+Prefix the I<type instance> with I<InstancePrefix>. The values are simply
+concatenated together without any separator.
+This option is optional.
+
+=item B<InstanceFrom> I<InstanceFrom>
+
+Specifies a XPath expression to use for determining the I<type instance>. The
+XPath expression must return exactly one element. The element's value is then
+used as I<type instance>, possibly prefixed with I<InstancePrefix> (see above).
+
+This value is required. As a special exception, if the "base XPath expression"
+(the argument to the B<XPath> block) returns exactly one argument, then this
+option may be omitted.
+
+=item B<ValuesFrom> I<ValuesFrom> [I<ValuesFrom> ...]
+
+Specifies one or more XPath expression to use for reading the values. The
+number of XPath expressions must match the number of data sources in the
+I<type> specified with B<Type> (see above). Each XPath expression must return
+exactly one element. The element's value is then parsed as a number and used as
+value for the appropriate value in the value list dispatched to the daemon.
+
+=back
+
+=back
+
 =head2 Plugin C<dbi>
 
 This plugin uses the B<dbi> library (L<http://libdbi.sourceforge.net/>) to
@@ -667,7 +783,7 @@ The following is a complete list of options:
 Query blocks define I<SQL> statements and how the returned data should be
 interpreted. They are identified by the name that is given in the opening line
 of the block. Thus the name needs to be unique. Other than that, the name is
-not used in collectd. 
+not used in collectd.
 
 In each B<Query> block, there is one or more B<Result> blocks. B<Result> blocks
 define which column holds which value or instance information. You can use
@@ -725,7 +841,7 @@ options to provide multiple queries with the same name but with a slightly
 different syntax. The plugin will use only those queries, where the specified
 minimum and maximum versions fit the version of the database in use.
 
-The database version is determined by C<dbi_conn_get_engine_version>, see the 
+The database version is determined by C<dbi_conn_get_engine_version>, see the
 L<libdbi documentation|http://libdbi.sourceforge.net/docs/programmers-guide/reference-conn.html#DBI-CONN-GET-ENGINE-VERSION>
 for details. Basically, each part of the version is assumed to be in the range
 from B<00> to B<99> and all dots are removed. So version "4.1.2" becomes
@@ -950,7 +1066,7 @@ is set to B<true>, all disks are collected B<except> the ones matched.
 
 =item B<Interface> I<Interface>
 
-The dns plugin uses B<libpcap> to capture dns traffic and analyses it. This
+The dns plugin uses B<libpcap> to capture dns traffic and analyzes it. This
 option sets the interface that should be used. If this option is not set, or
 set to "any", the plugin will try to get packets from B<all> interfaces. This
 may not work on certain platforms, such as MacE<nbsp>OSE<nbsp>X.
@@ -1102,6 +1218,12 @@ note that there are 1000 bytes in a kilobyte, not 1024.
 
 Controls whether or not to recurse into subdirectories. Enabled by default.
 
+=item B<IncludeHidden> I<true>|I<false>
+
+Controls whether or not to include "hidden" files and directories in the count.
+"Hidden" files and directories are those, whose name begins with a dot.
+Defaults to I<false>, i.e. by default hidden files and directories are ignored.
+
 =back
 
 =head2 Plugin C<GenericJMX>
@@ -1471,6 +1593,11 @@ running in foreground- or non-daemon-mode.
 
 Prefix all lines printed by the current time. Defaults to B<true>.
 
+=item B<PrintSeverity> B<true>|B<false>
+
+When enabled, all lines are prefixed by the severity of the log message, for
+example "warning". Defaults to B<false>.
+
 =back
 
 B<Note>: There is no need to notify the daemon after moving or removing the
@@ -1571,6 +1698,132 @@ TCP-Port to connect to. Defaults to B<11211>.
 
 =back
 
+=head2 Plugin C<modbus>
+
+The B<modbus plugin> connects to a Modbus "slave" via Modbus/TCP and reads
+register values. It supports reading single registers (unsigned 16E<nbsp>bit
+values), large integer values (unsigned 32E<nbsp>bit values) and floating point
+values (two registers interpreted as IEEE floats in big endian notation).
+
+Synopsis:
+
+ <Data "voltage-input-1">
+   RegisterBase 0
+   RegisterType float
+   Type voltage
+   Instance "input-1"
+ </Data>
+ <Data "voltage-input-2">
+   RegisterBase 2
+   RegisterType float
+   Type voltage
+   Instance "input-2"
+ </Data>
+ <Host "modbus.example.com">
+   Address "192.168.0.42"
+   Port    "502"
+   Interval 60
+   
+   <Slave 1>
+     Instance "power-supply"
+     Collect  "voltage-input-1"
+     Collect  "voltage-input-2"
+   </Slave>
+ </Host>
+
+=over 4
+
+=item E<lt>B<Data> I<Name>E<gt> blocks
+
+Data blocks define a mapping between register numbers and the "types" used by
+I<collectd>.
+
+Within E<lt>DataE<nbsp>/E<gt> blocks, the following options are allowed:
+
+=over 4
+
+=item B<RegisterBase> I<Number>
+
+Configures the base register to read from the device. If the option
+B<RegisterType> has been set to B<Uint32> or B<Float>, this and the next
+register will be read (the register number is increased by one).
+
+=item B<RegisterType> B<Uint16>|B<Uint32>|B<Float>
+
+Specifies what kind of data is returned by the device. If the type is B<Uint32>
+or B<Float>, two 16E<nbsp>bit registers will be read and the data is combined
+into one value. Defaults to B<Uint16>.
+
+=item B<Type> I<Type>
+
+Specifies the "type" (data set) to use when dispatching the value to
+I<collectd>. Currently, only data sets with exactly one data source are
+supported.
+
+=item B<Instance> I<Instance>
+
+Sets the type instance to use when dispatching the value to I<collectd>. If
+unset, an empty string (no type instance) is used.
+
+=back
+
+=item E<lt>B<Host> I<Name>E<gt> blocks
+
+Host blocks are used to specify to which hosts to connect and what data to read
+from their "slaves". The string argument I<Name> is used as hostname when
+dispatching the values to I<collectd>.
+
+Within E<lt>HostE<nbsp>/E<gt> blocks, the following options are allowed:
+
+=over 4
+
+=item B<Address> I<Hostname>
+
+Specifies the node name (the actual network address) used to connect to the
+host. This may be an IP address or a hostname. Please note that the used
+I<libmodbus> library only supports IPv4 at the moment.
+
+=item B<Port> I<Service>
+
+Specifies the port used to connect to the host. The port can either be given as
+a number or as a service name. Please note that the I<Service> argument must be
+a string, even if ports are given in their numerical form. Defaults to "502".
+
+=item B<Interval> I<Interval>
+
+Sets the interval (in seconds) in which the values will be collected from this
+host. By default the global B<Interval> setting will be used.
+
+=item E<lt>B<Slave> I<ID>E<gt>
+
+Over each TCP connection, multiple Modbus devices may be reached. The slave ID
+is used to specify which device should be addressed. For each device you want
+to query, one B<Slave> block must be given.
+
+Within E<lt>SlaveE<nbsp>/E<gt> blocks, the following options are allowed:
+
+=over 4
+
+=item B<Instance> I<Instance>
+
+Specify the plugin instance to use when dispatching the values to I<collectd>.
+By default "slave_I<ID>" is used.
+
+=item B<Collect> I<DataName>
+
+Specifies which data to retrieve from the device. I<DataName> must be the same
+string as the I<Name> argument passed to a B<Data> block. You can specify this
+option multiple times to collect more than one value from a slave. At least one
+B<Collect> option is mandatory.
+
+=back
+
+=back
+
+=back
+
 =head2 Plugin C<mysql>
 
 The C<mysql plugin> requires B<mysqlclient> to be installed. It connects to
@@ -1671,9 +1924,18 @@ or SQL threads are not running.
 
 =head2 Plugin C<netapp>
 
-The netapp plugin can collect various performance and capacity informations
+The netapp plugin can collect various performance and capacity information
 from a NetApp filer using the NetApp API.
 
+Please note that NetApp has a wide line of products and a lot of different
+software versions for each of these products. This plugin was developed for a
+NetApp FAS3040 running OnTap 7.2.3P8 and tested on FAS2050 7.3.1.1L1,
+FAS3140 7.2.5.1 and FAS3020 7.2.4P9. It I<should> work for most combinations of
+model and software version but it is very hard to test this.
+If you have used this plugin with other models and/or software version, feel
+free to send us a mail to tell us about the results, even if it's just a short
+"It works".
+
 To collect these data collectd will log in to the NetApp via HTTP(S) and HTTP
 basic authentication.
 
@@ -2186,7 +2448,7 @@ Here are some examples to help you understand the above text more easily:
 
 =item B<IgnoreSelected>
 
-The behaviour is the same as with all other similar plugins: If nothing is
+The behavior is the same as with all other similar plugins: If nothing is
 selected at all, everything is collected. If some things are selected using the
 options described above, only these statistics are collected. If you set
 B<IgnoreSelected> to B<true>, this behavior is inverted, i.E<nbsp>e. the
@@ -2198,7 +2460,7 @@ specified statistics will not be collected.
 
 The Network plugin sends data to a remote instance of collectd, receives data
 from a remote instance, or both at the same time. Data which has been received
-from the network is usually not transmitted again, but this can be actived, see
+from the network is usually not transmitted again, but this can be activated, see
 the B<Forward> option below.
 
 The default IPv6 multicast group is C<ff18::efc0:4a42>. The default IPv4
@@ -2260,6 +2522,15 @@ B<None> require this setting.
 This feature is only available if the I<network> plugin was linked with
 I<libgcrypt>.
 
+=item B<Interface> I<Interface name>
+
+Set the outgoing interface for IP packets. This applies at least
+to IPv6 packets and if possible to IPv4. If this option is not applicable,
+undefined or a non-existent interface name is specified, the default
+behaviour is to let the kernel choose the appropriate interface. Be warned
+that the manual selection of an interface for unicast traffic is only
+necessary in rare cases.
+
 =back
 
 =item B<E<lt>Listen> I<Host> [I<Port>]B<E<gt>>
@@ -2308,6 +2579,14 @@ Each time a packet is received, the modification time of the file is checked
 using L<stat(2)>. If the file has been changed, the contents is re-read. While
 the file is being read, it is locked using L<fcntl(2)>.
 
+=item B<Interface> I<Interface name>
+
+Set the incoming interface for IP packets explicitly. This applies at least
+to IPv6 packets and if possible to IPv4. If this option is not applicable,
+undefined or a non-existent interface name is specified, the default
+behaviour is, to let the kernel choose the appropriate interface. Thus incoming
+traffic gets only accepted, if it arrives on the given interface.
+
 =back
 
 =item B<TimeToLive> I<1-255>
@@ -2492,7 +2771,7 @@ UDP-Port to connect to. Defaults to B<123>.
 
 =item B<ReverseLookups> B<true>|B<false>
 
-Sets wether or not to perform reverse lookups on peers. Since the name or
+Sets whether or not to perform reverse lookups on peers. Since the name or
 IP-address may be used in a filename it is recommended to disable reverse
 lookups. The default is to do reverse lookups to preserve backwards
 compatibility, though.
@@ -2650,6 +2929,31 @@ Available options:
 
 Specifies the location of the status file.
 
+=item B<ImprovedNamingSchema> B<true>|B<false>
+
+When enabled, the filename of the status file will be used as plugin instance
+and the client's "common name" will be used as type instance. This is required
+when reading multiple status files. Enabling this option is recommended, but to
+maintain backwards compatibility this option is disabled by default.
+
+=item B<CollectCompression> B<true>|B<false>
+
+Sets whether or not statistics about the compression used by OpenVPN should be
+collected. This information is only available in I<single> mode. Enabled by
+default.
+
+=item B<CollectIndividualUsers> B<true>|B<false>
+
+Sets whether or not traffic information is collected for each connected client
+individually. If set to false, currently no traffic data is collected at all
+because aggregating this data in a save manner is tricky. Defaults to B<true>.
+
+=item B<CollectUserCount> B<true>|B<false>
+
+When enabled, the number of currently connected clients or users is collected.
+This is expecially interesting when B<CollectIndividualUsers> is disabled, but
+can be configured independently from that option. Defaults to B<false>.
+
 =back
 
 =head2 Plugin C<oracle>
@@ -2759,6 +3063,24 @@ Default: B<0.9>
 
 Sets the Time-To-Live of generated ICMP packets.
 
+=item B<SourceAddress> I<host>
+
+Sets the source address to use. I<host> may either be a numerical network
+address or a network hostname.
+
+=item B<Device> I<name>
+
+Sets the outgoing network device to be used. I<name> has to specify an
+interface name (e.E<nbsp>g. C<eth0>). This might not be supported by all
+operating systems.
+
+=item B<MaxMissed> I<Packets>
+
+Trigger a DNS resolv after the host has not replied to I<Packets> packets. This
+enables the use of dynamic DNS services (like dyndns.org) with the ping plugin.
+
+Default: B<-1> (disabled)
+
 =back
 
 =head2 Plugin C<postgresql>
@@ -2819,6 +3141,7 @@ L<http://www.postgresql.org/docs/manuals/>.
     </Database>
 
     <Database bar>
+      Interval 300
       Service "service_name"
       Query backend # predefined
       Query rt36_tickets
@@ -2882,7 +3205,8 @@ The username used to connect to the database.
 
 =item I<interval>
 
-The interval collectd is using (as specified by the B<Interval> option).
+The interval with which this database is queried (as specified by the database
+specific or global B<Interval> options).
 
 =back
 
@@ -3020,6 +3344,11 @@ for details.
 
 =over 4
 
+=item B<Interval> I<seconds>
+
+Specify the interval with which the database should be queried. The default is
+to use the global B<Interval> setting.
+
 =item B<Host> I<hostname>
 
 Specify the hostname or IP of the PostgreSQL server to connect to. If the
@@ -3281,6 +3610,98 @@ matching values will be ignored.
 
 =back
 
+=head2 Plugin C<python>
+
+This plugin embeds a Python-interpreter into collectd and provides an interface
+to collectd's plugin system. See L<collectd-python(5)> for its documentation.
+
+=head2 Plugin C<routeros>
+
+The C<routeros> plugin connects to a device running I<RouterOS>, the
+Linux-based operating system for routers by I<MikroTik>. The plugin uses
+I<librouteros> to connect and reads information about the interfaces and
+wireless connections of the device. The configuration supports querying
+multiple routers:
+
+  <Plugin "routeros">
+    <Router>
+      Host "router0.example.com"
+      User "collectd"
+      Password "secr3t"
+      CollectInterface true
+      CollectCPULoad true
+      CollectMemory true
+    </Router>
+    <Router>
+      Host "router1.example.com"
+      User "collectd"
+      Password "5ecret"
+      CollectInterface true
+      CollectRegistrationTable true
+      CollectDF true
+      CollectDisk true
+    </Router>
+  </Plugin>
+
+As you can see above, the configuration of the I<routeros> plugin consists of
+one or more B<E<lt>RouterE<gt>> blocks. Within each block, the following
+options are understood:
+
+=over 4
+
+=item B<Host> I<Host>
+
+Hostname or IP-address of the router to connect to.
+
+=item B<Port> I<Port>
+
+Port name or port number used when connecting. If left unspecified, the default
+will be chosen by I<librouteros>, currently "8728". This option expects a
+string argument, even when a numeric port number is given.
+
+=item B<User> I<User>
+
+Use the user name I<User> to authenticate. Defaults to "admin".
+
+=item B<Password> I<Password>
+
+Set the password used to authenticate.
+
+=item B<CollectInterface> B<true>|B<false>
+
+When set to B<true>, interface statistics will be collected for all interfaces
+present on the device. Defaults to B<false>.
+
+=item B<CollectRegistrationTable> B<true>|B<false>
+
+When set to B<true>, information about wireless LAN connections will be
+collected. Defaults to B<false>.
+
+=item B<CollectCPULoad> B<true>|B<false>
+
+When set to B<true>, information about the CPU usage will be collected. The
+number is a dimensionless value where zero indicates no CPU usage at all.
+Defaults to B<false>.
+
+=item B<CollectMemory> B<true>|B<false>
+
+When enabled, the amount of used and free memory will be collected. How used
+memory is calculated is unknown, for example whether or not caches are counted
+as used space.
+Defaults to B<false>.
+
+=item B<CollectDF> B<true>|B<false>
+
+When enabled, the amount of used and free disk space will be collected.
+Defaults to B<false>.
+
+=item B<CollectDisk> B<true>|B<false>
+
+When enabled, the number of sectors written and bad blocks will be collected.
+Defaults to B<false>.
+
+=back
+
 =head2 Plugin C<rrdcached>
 
 The C<rrdcached> plugin uses the RRDtool accelerator daemon, L<rrdcached(1)>,
@@ -3611,6 +4032,7 @@ user using (extended) regular expressions, as described in L<regex(7)>.
       </Match>
       <Match>
         Regex "\\<R=local_user\\>"
+        ExcludeRegex "\\<R=local_user\\>.*mail_spool defer"
         DSType "CounterInc"
         Type "counter"
         Instance "local_user"
@@ -3645,6 +4067,13 @@ want to match literal parentheses you need to do the following:
 
   Regex "SPAM \\(Score: (-?[0-9]+\\.[0-9]+)\\)"
 
+=item B<ExcludeRegex> I<regex>
+
+Sets an optional regular expression to use for excluding lines from the match.
+An example which excludes all connections from localhost from the match:
+
+  ExcludeRegex "127\\.0\\.0\\.1"
+
 =item B<DSType> I<Type>
 
 Sets how the values are cumulated. I<Type> is one of:
@@ -3830,7 +4259,7 @@ selection is configured at all, B<all> devices are selected.
 
 =head2 Plugin C<tokyotyrant>
 
-The C<tokyotyrant plugin> connects to a TokyoTyrant server and collects a 
+The C<tokyotyrant plugin> connects to a TokyoTyrant server and collects a
 couple metrics: number of records, and database size on disk.
 
 =over 4
@@ -4000,6 +4429,12 @@ create output in the I<JavaScript Object Notation> (JSON).
 
 Defaults to B<Command>.
 
+=item B<StoreRates> B<true|false>
+
+If set to B<true>, convert counter values to rates. If set to B<false> (the
+default) counter values are stored as is, i.E<nbsp>e. as an increasing integer
+number.
+
 =back
 
 =head1 THRESHOLD CONFIGURATION
@@ -4021,10 +4456,12 @@ as a moving average or similar - at least not now.
 
 Also, all values that match a threshold are considered to be relevant or
 "interesting". As a consequence collectd will issue a notification if they are
-not received for twice the last timeout of the values. If, for example, some
-hosts sends it's CPU statistics to the server every 60 seconds, a notification
-will be dispatched after about 120 seconds. It may take a little longer because
-the timeout is checked only once each B<Interval> on the server.
+not received for B<Timeout> iterations. The B<Timeout> configuration option is
+explained in section L<"GLOBAL OPTIONS">. If, for example, B<Timeout> is set to
+"2" (the default) and some hosts sends it's CPU statistics to the server every
+60 seconds, a notification will be dispatched after about 120 seconds. It may
+take a little longer because the timeout is checked only once each B<Interval>
+on the server.
 
 When a value comes within range again or is received after it was missing, an
 "OKAY-notification" is dispatched.
@@ -4398,7 +4835,7 @@ This is the same as writing:
 
 =back
 
-=head2 Built-in targets 
+=head2 Built-in targets
 
 The following targets are built into the core daemon and therefore need no
 plugins to be loaded:
@@ -4507,6 +4944,12 @@ Match values where the given regular expressions match the various fields of
 the identifier of a value. If multiple regular expressions are given, B<all>
 regexen must match for a value to match.
 
+=item B<Invert> B<false>|B<true>
+
+When set to B<true>, the result of the match is inverted, i.e. all value lists
+where all regular expressions apply are not matched, all other value lists are
+matched. Defaults to B<false>.
+
 =back
 
 Example:
index 3d8998f..8849b30 100644 (file)
@@ -135,6 +135,12 @@ typedef bool _Bool;
 # ifndef isnan
 #  define isnan(f) ((f) != (f))
 # endif /* !defined(isnan) */
+# ifndef isfinite
+#  define isfinite(f) (((f) - (f)) == 0.0)
+# endif
+# ifndef isinf
+#  define isinf(f) (!isfinite(f) && !isnan(f))
+# endif
 #endif /* NAN_ZERO_ZERO */
 
 /* Try really, really hard to determine endianess. Under NexentaStor 1.0.2 this
@@ -294,5 +300,6 @@ typedef bool _Bool;
 
 extern char hostname_g[];
 extern int  interval_g;
+extern int  timeout_g;
 
 #endif /* COLLECTD_H */
index c6a651d..2598036 100644 (file)
@@ -668,6 +668,7 @@ long long get_kstat_value (kstat_t *ksp, char *name)
 }
 #endif /* HAVE_LIBKSTAT */
 
+#ifndef HAVE_HTONLL
 unsigned long long ntohll (unsigned long long n)
 {
 #if BYTE_ORDER == BIG_ENDIAN
@@ -685,6 +686,7 @@ unsigned long long htonll (unsigned long long n)
        return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
 #endif
 } /* unsigned long long htonll */
+#endif /* HAVE_HTONLL */
 
 #if FP_LAYOUT_NEED_NOTHING
 /* Well, we need nothing.. */
@@ -1008,7 +1010,7 @@ int notification_init (notification_t *n, int severity, const char *message,
 } /* int notification_init */
 
 int walk_directory (const char *dir, dirwalk_callback_f callback,
-               void *user_data)
+               void *user_data, int include_hidden)
 {
        struct dirent *ent;
        DIR *dh;
@@ -1029,9 +1031,18 @@ int walk_directory (const char *dir, dirwalk_callback_f callback,
        while ((ent = readdir (dh)) != NULL)
        {
                int status;
-
-               if (ent->d_name[0] == '.')
-                       continue;
+               
+               if (include_hidden)
+               {
+                       if ((strcmp (".", ent->d_name) == 0)
+                                       || (strcmp ("..", ent->d_name) == 0))
+                               continue;
+               }
+               else /* if (!include_hidden) */
+               {
+                       if (ent->d_name[0]=='.')
+                               continue;
+               }
 
                status = (*callback) (dir, ent->d_name, user_data);
                if (status != 0)
@@ -1133,3 +1144,21 @@ int service_name_to_port_number (const char *service_name)
                return (service_number);
        return (-1);
 } /* int service_name_to_port_number */
+
+int strtoderive (const char *string, derive_t *ret_value) /* {{{ */
+{
+       derive_t tmp;
+       char *endptr;
+
+       if ((string == NULL) || (ret_value == NULL))
+               return (EINVAL);
+
+       errno = 0;
+       endptr = NULL;
+       tmp = (derive_t) strtoll (string, &endptr, /* base = */ 0);
+       if ((endptr == string) || (errno != 0))
+               return (-1);
+
+       *ret_value = tmp;
+       return (0);
+} /* }}} int strtoderive */
index 019e8b6..c0bea36 100644 (file)
@@ -236,8 +236,10 @@ int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name);
 long long get_kstat_value (kstat_t *ksp, char *name);
 #endif
 
+#ifndef HAVE_HTONLL
 unsigned long long ntohll (unsigned long long n);
 unsigned long long htonll (unsigned long long n);
+#endif
 
 #if FP_LAYOUT_NEED_NOTHING
 # define ntohd(d) (d)
@@ -280,7 +282,7 @@ int notification_init (notification_t *n, int severity, const char *message,
 typedef int (*dirwalk_callback_f)(const char *dirname, const char *filename,
                void *user_data);
 int walk_directory (const char *dir, dirwalk_callback_f callback,
-               void *user_data);
+               void *user_data, int hidden);
 int read_file_contents (const char *filename, char *buf, int bufsize);
 
 counter_t counter_diff (counter_t old_value, counter_t new_value);
@@ -289,4 +291,6 @@ counter_t counter_diff (counter_t old_value, counter_t new_value);
  * (in the range [1-65535]). Returns less than zero on error. */
 int service_name_to_port_number (const char *service_name);
 
+int strtoderive (const char *string, derive_t *ret_value);
+
 #endif /* COMMON_H */
index 1a957f6..787ad0e 100644 (file)
@@ -75,7 +75,7 @@ typedef struct cf_global_option_s
  */
 static int dispatch_value_typesdb (const oconfig_item_t *ci);
 static int dispatch_value_plugindir (const oconfig_item_t *ci);
-static int dispatch_value_loadplugin (const oconfig_item_t *ci);
+static int dispatch_loadplugin (const oconfig_item_t *ci);
 
 /*
  * Private variables
@@ -87,7 +87,7 @@ static cf_value_map_t cf_value_map[] =
 {
        {"TypesDB",    dispatch_value_typesdb},
        {"PluginDir",  dispatch_value_plugindir},
-       {"LoadPlugin", dispatch_value_loadplugin}
+       {"LoadPlugin", dispatch_loadplugin}
 };
 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
 
@@ -99,6 +99,7 @@ static cf_global_option_t cf_global_options[] =
        {"FQDNLookup",  NULL, "false"},
        {"Interval",    NULL, "10"},
        {"ReadThreads", NULL, "5"},
+       {"Timeout",     NULL, "2"},
        {"PreCacheChain",  NULL, "PreCache"},
        {"PostCacheChain", NULL, "PostCache"}
 };
@@ -239,8 +240,10 @@ static int dispatch_value_plugindir (const oconfig_item_t *ci)
        return (0);
 }
 
-static int dispatch_value_loadplugin (const oconfig_item_t *ci)
+static int dispatch_loadplugin (const oconfig_item_t *ci)
 {
+       int i;
+       uint32_t flags = 0;
        assert (strcasecmp (ci->key, "LoadPlugin") == 0);
 
        if (ci->values_num != 1)
@@ -248,7 +251,19 @@ static int dispatch_value_loadplugin (const oconfig_item_t *ci)
        if (ci->values[0].type != OCONFIG_TYPE_STRING)
                return (-1);
 
-       return (plugin_load (ci->values[0].value.string));
+       for (i = 0; i < ci->children_num; ++i) {
+               if (ci->children[i].values_num != 1 ||
+                               ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) {
+                       WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
+                       continue;
+               }
+               if (strcasecmp(ci->children[i].key, "globals") == 0) {
+                       flags |= PLUGIN_FLAGS_GLOBAL;
+               } else {
+                       WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
+               }
+       }
+       return (plugin_load (ci->values[0].value.string, flags));
 } /* int dispatch_value_loadplugin */
 
 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
@@ -353,7 +368,9 @@ static int dispatch_block_plugin (oconfig_item_t *ci)
 
 static int dispatch_block (oconfig_item_t *ci)
 {
-       if (strcasecmp (ci->key, "Plugin") == 0)
+       if (strcasecmp (ci->key, "LoadPlugin") == 0)
+               return (dispatch_loadplugin (ci));
+       else if (strcasecmp (ci->key, "Plugin") == 0)
                return (dispatch_block_plugin (ci));
        else if (strcasecmp (ci->key, "Threshold") == 0)
                return (ut_config (ci));
@@ -559,14 +576,14 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth)
                ERROR ("configfile: malloc failed.");
                return (NULL);
        }
-       memset (root, '\0', sizeof (oconfig_item_t));
+       memset (root, 0, sizeof (oconfig_item_t));
 
        while ((de = readdir (dh)) != NULL)
        {
                char   name[1024];
                char **tmp;
 
-               if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
+               if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
                        continue;
 
                status = ssnprintf (name, sizeof (name), "%s/%s",
@@ -608,13 +625,11 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth)
                char *name = filenames[i];
 
                temp = cf_read_generic (name, depth);
-               if (temp == NULL) {
-                       int j;
-                       for (j = i; j < filenames_num; ++j)
-                               free (filenames[j]);
-                       free (filenames);
-                       oconfig_free (root);
-                       return (NULL);
+               if (temp == NULL)
+               {
+                       /* An error should already have been reported. */
+                       sfree (name);
+                       continue;
                }
 
                cf_ci_append_children (root, temp);
@@ -927,7 +942,7 @@ int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
 
        if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
        {
-               ERROR ("cf_util_get_string: The %s plugin requires "
+               ERROR ("cf_util_get_string: The %s option requires "
                                "exactly one string argument.", ci->key);
                return (-1);
        }
@@ -943,6 +958,62 @@ int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
        return (0);
 } /* }}} int cf_util_get_string */
 
+/* Assures the config option is a string and copies it to the provided buffer.
+ * Assures null-termination. */
+int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
+               size_t buffer_size)
+{
+       if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
+               return (EINVAL);
+
+       if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
+       {
+               ERROR ("cf_util_get_string_buffer: The %s option requires "
+                               "exactly one string argument.", ci->key);
+               return (-1);
+       }
+
+       strncpy (buffer, ci->values[0].value.string, buffer_size);
+       buffer[buffer_size - 1] = 0;
+
+       return (0);
+} /* }}} int cf_util_get_string_buffer */
+
+/* Assures the config option is a number and returns it as an int. */
+int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
+{
+       if ((ci == NULL) || (ret_value == NULL))
+               return (EINVAL);
+
+       if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
+       {
+               ERROR ("cf_util_get_int: The %s option requires "
+                               "exactly one numeric argument.", ci->key);
+               return (-1);
+       }
+
+       *ret_value = (int) ci->values[0].value.number;
+
+       return (0);
+} /* }}} int cf_util_get_int */
+
+int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
+{
+       if ((ci == NULL) || (ret_bool == NULL))
+               return (EINVAL);
+
+       if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
+       {
+               ERROR ("cf_util_get_boolean: The %s option requires "
+                               "exactly one boolean argument.", ci->key);
+               return (-1);
+       }
+
+       *ret_bool = ci->values[0].value.boolean ? true : false;
+
+       return (0);
+} /* }}} int cf_util_get_boolean */
+
 /* Assures that the config option is a string. The string is then converted to
  * a port number using `service_name_to_port_number' and returned. Returns the
  * port number in the range [1-65535] or less than zero upon failure. */
@@ -950,7 +1021,7 @@ int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
 {
        if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
        {
-               ERROR ("cf_util_get_port_number: The %s plugin requires "
+               ERROR ("cf_util_get_port_number: The %s option requires "
                                "exactly one string argument.", ci->key);
                return (-1);
        }
index 74d074e..432e09f 100644 (file)
@@ -91,6 +91,18 @@ const char *global_option_get (const char *option);
  * success. */
 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string);
 
+/* Assures the config option is a string and copies it to the provided buffer.
+ * Assures null-termination. */
+int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer,
+               size_t buffer_size);
+
+/* Assures the config option is a number and returns it as an int. */
+int cf_util_get_int (const oconfig_item_t *ci, int *ret_value);
+
+/* Assures the config option is a boolean and assignes it to `ret_bool'.
+ * Otherwise, `ret_bool' is not changed and non-zero is returned. */
+int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool);
+
 /* Assures that the config option is a string. The string is then converted to
  * a port number using `service_name_to_port_number' and returned. Returns the
  * port number in the range [1-65535] or less than zero upon failure. */
index 7787203..06055ca 100644 (file)
@@ -27,7 +27,7 @@
 # error "No applicable input method."
 #endif
 
-static void cs_submit (unsigned long context_switches)
+static void cs_submit (derive_t context_switches)
 {
        value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
@@ -49,7 +49,7 @@ static int cs_read (void)
        char buffer[64];
        int numfields;
        char *fields[3];
-       unsigned long result = 0;
+       derive_t result = 0;
        int status = -2;
 
        fh = fopen ("/proc/stat", "r");
@@ -72,7 +72,7 @@ static int cs_read (void)
 
                errno = 0;
                endptr = NULL;
-               result = strtoul(fields[1], &endptr, 10);
+               result = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
                if ((endptr == fields[1]) || (errno != 0)) {
                        ERROR ("contextswitch plugin: Cannot parse ctxt value: %s",
                                        fields[1]);
index b92b0e2..7aa6361 100644 (file)
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -3,6 +3,7 @@
  * Copyright (C) 2005-2009  Florian octo Forster
  * Copyright (C) 2008       Oleg King
  * Copyright (C) 2009       Simon Kuhnle
+ * Copyright (C) 2009       Manuel Sanmartin
  *
  * 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
@@ -21,6 +22,7 @@
  *   Florian octo Forster <octo at verplant.org>
  *   Oleg King <king2 at kaluga.ru>
  *   Simon Kuhnle <simon at blarzwurst.de>
+ *   Manuel Sanmartin
  **/
 
 #include "collectd.h"
 # include <statgrab.h>
 #endif
 
+# ifdef HAVE_PERFSTAT
+#  include <sys/protosw.h>
+#  include <libperfstat.h>
+# endif /* HAVE_PERFSTAT */
+
 #if !PROCESSOR_CPU_LOAD_INFO && !KERNEL_LINUX && !HAVE_LIBKSTAT \
-       && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB
+       && !CAN_USE_SYSCTL && !HAVE_SYSCTLBYNAME && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
 # error "No applicable input method."
 #endif
 
@@ -130,7 +137,13 @@ static int maxcpu;
 
 #elif defined(HAVE_LIBSTATGRAB)
 /* no variables needed */
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif  HAVE_LIBSTATGRAB */
+
+#elif defined(HAVE_PERFSTAT)
+static perfstat_cpu_t *perfcpu;
+static int numcpu;
+static int pnumcpu;
+#endif /* HAVE_PERFSTAT */
 
 static int init (void)
 {
@@ -219,7 +232,11 @@ static int init (void)
 
 #elif defined(HAVE_LIBSTATGRAB)
        /* nothing to initialize */
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif HAVE_LIBSTATGRAB */
+
+#elif defined(HAVE_PERFSTAT)
+       /* nothing to initialize */
+#endif /* HAVE_PERFSTAT */
 
        return (0);
 } /* int init */
@@ -540,7 +557,46 @@ static int cpu_read (void)
        submit (0, "system", (counter_t) cs->kernel);
        submit (0, "user",   (counter_t) cs->user);
        submit (0, "wait",   (counter_t) cs->iowait);
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif HAVE_LIBSTATGRAB */
+
+#elif defined(HAVE_PERFSTAT)
+       perfstat_id_t id;
+       int i, cpus;
+
+       numcpu =  perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
+       if(numcpu == -1)
+       {
+               char errbuf[1024];
+               WARNING ("cpu plugin: perfstat_cpu: %s",
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+       
+       if (pnumcpu != numcpu || perfcpu == NULL) 
+       {
+               if (perfcpu != NULL) 
+                       free(perfcpu);
+               perfcpu = malloc(numcpu * sizeof(perfstat_cpu_t));
+       }
+       pnumcpu = numcpu;
+
+       id.name[0] = '\0';
+       if ((cpus = perfstat_cpu(&id, perfcpu, sizeof(perfstat_cpu_t), numcpu)) < 0)
+       {
+               char errbuf[1024];
+               WARNING ("cpu plugin: perfstat_cpu: %s",
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       for (i = 0; i < cpus; i++) 
+       {
+               submit (i, "idle",   (counter_t) perfcpu[i].idle);
+               submit (i, "system", (counter_t) perfcpu[i].sys);
+               submit (i, "user",   (counter_t) perfcpu[i].user);
+               submit (i, "wait",   (counter_t) perfcpu[i].wait);
+       }
+#endif /* HAVE_PERFSTAT */
 
        return (0);
 }
diff --git a/src/cpython.h b/src/cpython.h
new file mode 100644 (file)
index 0000000..2a14ce0
--- /dev/null
@@ -0,0 +1,205 @@
+/**
+ * collectd - src/cpython.h
+ * Copyright (C) 2009  Sven Trenkel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Sven Trenkel <collectd at semidefinite.de>  
+ **/
+
+/* Some python versions don't include this by default. */
+
+#include <longintrepr.h>
+
+/* These two macros are basicly Py_BEGIN_ALLOW_THREADS and Py_BEGIN_ALLOW_THREADS
+ * from the other direction. If a Python thread calls a C function
+ * Py_BEGIN_ALLOW_THREADS is used to allow other python threads to run because
+ * we don't intend to call any Python functions.
+ *
+ * These two macros are used whenever a C thread intends to call some Python
+ * function, usually because some registered callback was triggered.
+ * Just like Py_BEGIN_ALLOW_THREADS it opens a block so these macros have to be
+ * used in pairs. They aquire the GIL, create a new Python thread state and swap
+ * the current thread state with the new one. This means this thread is now allowed
+ * to execute Python code. */
+
+#define CPY_LOCK_THREADS {\
+       PyGILState_STATE gil_state;\
+       gil_state = PyGILState_Ensure();
+
+#define CPY_RETURN_FROM_THREADS \
+       PyGILState_Release(gil_state);\
+       return
+
+#define CPY_RELEASE_THREADS \
+       PyGILState_Release(gil_state);\
+}
+
+/* Python 2.4 has this macro, older versions do not. */
+#ifndef Py_VISIT
+#define Py_VISIT(o) do {\
+       int _vret;\
+       if ((o) != NULL) {\
+               _vret = visit((o), arg);\
+               if (_vret != 0)\
+               return _vret;\
+       }\
+} while (0)
+#endif
+
+/* Python 2.4 has this macro, older versions do not. */
+#ifndef Py_CLEAR
+#define Py_CLEAR(o) do {\
+       PyObject *tmp = o;\
+       (o) = NULL;\
+       Py_XDECREF(tmp);\
+} while (0)
+#endif
+
+/* Python 2.4 has this macro, older versions do not. */
+#ifndef Py_RETURN_NONE
+# define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
+#endif
+
+/* This macro is a shortcut for calls like
+ * x = PyObject_Repr(x);
+ * This can't be done like this example because this would leak
+ * a reference the the original x and crash in case of x == NULL.
+ * This calling syntax is less than elegant but it works, saves
+ * a lot of lines and avoids potential refcount errors. */
+
+#define CPY_SUBSTITUTE(func, a, ...) do {\
+       if ((a) != NULL) {\
+               PyObject *__tmp = (a);\
+               (a) = func(__VA_ARGS__);\
+               Py_DECREF(__tmp);\
+       }\
+} while(0)
+
+/* Python3 compatibility layer. To keep the actual code as clean as possible
+ * do a lot of defines here. */
+
+#if PY_MAJOR_VERSION >= 3
+#define IS_PY3K
+#endif
+
+#ifdef IS_PY3K
+
+#define PyInt_FromLong PyLong_FromLong
+#define CPY_INIT_TYPE         PyVarObject_HEAD_INIT(NULL, 0)
+#define IS_BYTES_OR_UNICODE(o) (PyUnicode_Check(o) || PyBytes_Check(o))
+#define CPY_STRCAT_AND_DEL(a, b) do {\
+       CPY_STRCAT((a), (b));\
+       Py_XDECREF((b));\
+} while (0)
+static inline void CPY_STRCAT(PyObject **a, PyObject *b) {
+       PyObject *ret;
+       
+       if (!a || !*a)
+               return;
+       
+       ret = PyUnicode_Concat(*a, b);
+       Py_DECREF(*a);
+       *a = ret;
+}
+
+#else
+
+#define CPY_INIT_TYPE         PyObject_HEAD_INIT(NULL) 0,
+#define IS_BYTES_OR_UNICODE(o) (PyUnicode_Check(o) || PyString_Check(o))
+#define CPY_STRCAT_AND_DEL PyString_ConcatAndDel
+#define CPY_STRCAT PyString_Concat
+
+#endif
+
+static inline const char *cpy_unicode_or_bytes_to_string(PyObject **o) {
+       if (PyUnicode_Check(*o)) {
+               PyObject *tmp;
+               tmp = PyUnicode_AsEncodedString(*o, NULL, NULL); /* New reference. */
+               if (tmp == NULL)
+                       return NULL;
+               Py_DECREF(*o);
+               *o = tmp;
+       }
+#ifdef IS_PY3K
+       return PyBytes_AsString(*o);
+#else
+       return PyString_AsString(*o);
+#endif
+}
+
+static inline PyObject *cpy_string_to_unicode_or_bytes(const char *buf) {
+#ifdef IS_PY3K
+/* Python3 preferrs unicode */
+       PyObject *ret;
+       ret = PyUnicode_Decode(buf, strlen(buf), NULL, NULL);
+       if (ret != NULL)
+               return ret;
+       PyErr_Clear();
+       return PyBytes_FromString(buf);
+#else
+       return PyString_FromString(buf);
+#endif 
+}
+
+void cpy_log_exception(const char *context);
+
+/* Python object declarations. */
+
+typedef struct {
+       PyObject_HEAD        /* No semicolon! */
+       PyObject *parent;    /* Config */
+       PyObject *key;       /* String */
+       PyObject *values;    /* Sequence */
+       PyObject *children;  /* Sequence */
+} Config;
+PyTypeObject ConfigType;
+
+typedef struct {
+       PyObject_HEAD        /* No semicolon! */
+       double time;
+       char host[DATA_MAX_NAME_LEN];
+       char plugin[DATA_MAX_NAME_LEN];
+       char plugin_instance[DATA_MAX_NAME_LEN];
+       char type[DATA_MAX_NAME_LEN];
+       char type_instance[DATA_MAX_NAME_LEN];
+} PluginData;
+PyTypeObject PluginDataType;
+
+typedef struct {
+       PluginData data;
+       PyObject *values;    /* Sequence */
+       PyObject *meta;      /* dict */
+       int interval;
+} Values;
+PyTypeObject ValuesType;
+
+typedef struct {
+       PluginData data;
+       int severity;
+       char message[NOTIF_MAX_MSG_LEN];
+} Notification;
+PyTypeObject NotificationType;
+
+typedef PyLongObject Signed;
+PyTypeObject SignedType;
+
+typedef PyLongObject Unsigned;
+PyTypeObject UnsignedType;
index 0b34687..96c1e3e 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
@@ -299,7 +299,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 
                fprintf (use_stdio == 1 ? stdout : stderr,
                         "PUTVAL %s interval=%i %s\n",
-                        filename, interval_g, values);
+                        filename, vl->interval, values);
                return (0);
        }
 
index abf45c2..a533e14 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/curl.c
  * Copyright (C) 2006-2009  Florian octo Forster
+ * Copyright (C) 2009       Aman Gupta
  *
  * 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
@@ -17,6 +18,7 @@
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Aman Gupta <aman at tmm1.net>
  **/
 
 #include "collectd.h"
@@ -35,6 +37,7 @@ typedef struct web_match_s web_match_t;
 struct web_match_s /* {{{ */
 {
   char *regex;
+  char *exclude_regex;
   int dstype;
   char *type;
   char *instance;
@@ -289,6 +292,8 @@ static int cc_config_add_match (web_page_t *page, /* {{{ */
 
     if (strcasecmp ("Regex", child->key) == 0)
       status = cc_config_add_string ("Regex", &match->regex, child);
+    else if (strcasecmp ("ExcludeRegex", child->key) == 0)
+      status = cc_config_add_string ("ExcludeRegex", &match->exclude_regex, child);
     else if (strcasecmp ("DSType", child->key) == 0)
       status = cc_config_add_match_dstype (&match->dstype, child);
     else if (strcasecmp ("Type", child->key) == 0)
@@ -331,7 +336,8 @@ static int cc_config_add_match (web_page_t *page, /* {{{ */
   if (status != 0)
     return (status);
 
-  match->match = match_create_simple (match->regex, match->dstype);
+  match->match = match_create_simple (match->regex, match->exclude_regex,
+      match->dstype);
   if (match->match == NULL)
   {
     ERROR ("curl plugin: tail_match_add_match_simple failed.");
index 53e8abd..03ef6a3 100644 (file)
@@ -673,7 +673,7 @@ static int cj_config_add_url (oconfig_item_t *ci) /* {{{ */
     ssnprintf (cb_name, sizeof (cb_name), "curl_json-%s-%s",
                db->instance, db->url);
 
-    plugin_register_complex_read (cb_name, cj_read,
+    plugin_register_complex_read (/* group = */ NULL, cb_name, cj_read,
                                   /* interval = */ NULL, &ud);
   }
   else
diff --git a/src/curl_xml.c b/src/curl_xml.c
new file mode 100644 (file)
index 0000000..c10955c
--- /dev/null
@@ -0,0 +1,930 @@
+/**
+ * collectd - src/curl_xml.c
+ * Copyright (C) 2009,2010       Amit Gupta
+ *
+ * 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:
+ *   Amit Gupta <amit.gupta221 at gmail.com>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
+#include "utils_llist.h"
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+
+#include <curl/curl.h>
+
+#define CX_DEFAULT_HOST "localhost"
+
+/*
+ * Private data structures
+ */
+struct cx_values_s /* {{{ */
+{
+  char path[DATA_MAX_NAME_LEN];
+  size_t path_len;
+};
+typedef struct cx_values_s cx_values_t;
+/* }}} */
+
+struct cx_xpath_s /* {{{ */
+{
+  char *path;
+  char *type;
+  cx_values_t *values;
+  int values_len;
+  char *instance_prefix;
+  char *instance;
+  int is_table;
+  unsigned long magic;
+};
+typedef struct cx_xpath_s cx_xpath_t;
+/* }}} */
+
+struct cx_s /* {{{ */
+{
+  char *instance;
+  char *host;
+
+  char *url;
+  char *user;
+  char *pass;
+  char *credentials;
+  _Bool verify_peer;
+  _Bool verify_host;
+  char *cacert;
+
+  CURL *curl;
+  char curl_errbuf[CURL_ERROR_SIZE];
+  char *buffer;
+  size_t buffer_size;
+  size_t buffer_fill;
+
+  llist_t *list; /* list of xpath blocks */
+};
+typedef struct cx_s cx_t; /* }}} */
+
+/*
+ * Private functions
+ */
+static size_t cx_curl_callback (void *buf, /* {{{ */
+    size_t size, size_t nmemb, void *user_data)
+{
+  size_t len = size * nmemb;
+  cx_t *db;
+
+  db = user_data;
+  if (db == NULL)
+  {
+    ERROR ("curl_xml plugin: cx_curl_callback: "
+           "user_data pointer is NULL.");
+    return (0);
+  }
+
+   if (len <= 0)
+    return (len);
+
+  if ((db->buffer_fill + len) >= db->buffer_size)
+  {
+    char *temp;
+
+    temp = (char *) realloc (db->buffer,
+                    db->buffer_fill + len + 1);
+    if (temp == NULL)
+    {
+      ERROR ("curl_xml plugin: realloc failed.");
+      return (0);
+    }
+    db->buffer = temp;
+    db->buffer_size = db->buffer_fill + len + 1;
+  }
+
+  memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
+  db->buffer_fill += len;
+  db->buffer[db->buffer_fill] = 0;
+
+  return (len);
+} /* }}} size_t cx_curl_callback */
+
+static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
+{
+  if (xpath == NULL)
+    return;
+
+  sfree (xpath->path);
+  sfree (xpath->type);
+  sfree (xpath->instance_prefix);
+  sfree (xpath->instance);
+  sfree (xpath->values);
+  sfree (xpath);
+} /* }}} void cx_xpath_free */
+
+static void cx_list_free (llist_t *list) /* {{{ */
+{
+  llentry_t *le;
+
+  le = llist_head (list);
+  while (le != NULL)
+  {
+    llentry_t *le_next;
+
+    le_next = le->next;
+
+    sfree (le->key);
+    cx_xpath_free (le->value);
+
+    le = le_next;
+  }
+
+  llist_destroy (list);
+  list = NULL;
+} /* }}} void cx_list_free */
+
+static void cx_free (void *arg) /* {{{ */
+{
+  cx_t *db;
+
+  DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
+
+  db = (cx_t *) arg;
+
+  if (db == NULL)
+    return;
+
+  if (db->curl != NULL)
+    curl_easy_cleanup (db->curl);
+  db->curl = NULL;
+
+  if (db->list != NULL)
+    cx_list_free (db->list);
+
+  sfree (db->buffer);
+  sfree (db->instance);
+  sfree (db->host);
+
+  sfree (db->url);
+  sfree (db->user);
+  sfree (db->pass);
+  sfree (db->credentials);
+  sfree (db->cacert);
+
+  sfree (db);
+} /* }}} void cx_free */
+
+static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
+{
+  if (!ds)
+  {
+    WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
+    return (-1);
+  }
+
+  if (ds->ds_num != xpath->values_len)
+  {
+    WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
+        xpath->type, ds->ds_num, xpath->values_len);
+    return (-1);
+  }
+
+  return (0);
+} /* }}} cx_check_type */
+
+static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */ 
+           xmlChar *expr)
+{
+  xmlXPathObjectPtr xpath_obj;
+
+  /* XXX: When to free this? */
+  xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
+  if (xpath_obj == NULL)
+  {
+     WARNING ("curl_xml plugin: "
+               "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
+     return NULL;
+  }
+
+  return xpath_obj;
+} /* }}} cx_evaluate_xpath */
+
+static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
+{
+  if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
+    return (0);
+
+  WARNING ("curl_xml plugin: "
+           "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
+  return -1;
+} /* }}} cx_if_not_text_node */
+
+static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
+    cx_xpath_t *xpath,
+    const data_set_t *ds, value_list_t *vl, int index)
+{
+  xmlXPathObjectPtr values_node_obj;
+  xmlNodeSetPtr values_node;
+  int tmp_size;
+  char *node_value;
+
+  values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
+  if (values_node_obj == NULL)
+    return (-1); /* Error already logged. */
+
+  values_node = values_node_obj->nodesetval;
+  tmp_size = (values_node) ? values_node->nodeNr : 0;
+
+  if (tmp_size == 0)
+  {
+    WARNING ("curl_xml plugin: "
+        "relative xpath expression \"%s\" doesn't match any of the nodes. "
+        "Skipping...", xpath->values[index].path);
+    xmlXPathFreeObject (values_node_obj);
+    return (-1);
+  }
+
+  if (tmp_size > 1)
+  {
+    WARNING ("curl_xml plugin: "
+        "relative xpath expression \"%s\" is expected to return "
+        "only one node. Skipping...", xpath->values[index].path);
+    xmlXPathFreeObject (values_node_obj);
+    return (-1);
+  }
+
+  /* ignoring the element if other than textnode/attribute*/
+  if (cx_if_not_text_node(values_node->nodeTab[0]))
+  {
+    WARNING ("curl_xml plugin: "
+        "relative xpath expression \"%s\" is expected to return "
+        "only text/attribute node which is not the case. Skipping...", 
+        xpath->values[index].path);
+    xmlXPathFreeObject (values_node_obj);
+    return (-1);
+  }
+
+  node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
+  switch (ds->ds[index].type)
+  {
+    case DS_TYPE_COUNTER:
+      vl->values[index].counter = (counter_t) strtoull (node_value,
+          /* endptr = */ NULL, /* base = */ 0);
+      break;
+    case DS_TYPE_DERIVE:
+      vl->values[index].derive = (derive_t) strtoll (node_value,
+          /* endptr = */ NULL, /* base = */ 0);
+      break;
+    case DS_TYPE_ABSOLUTE:
+      vl->values[index].absolute = (absolute_t) strtoull (node_value,
+          /* endptr = */ NULL, /* base = */ 0);
+      break;
+    case DS_TYPE_GAUGE: 
+      vl->values[index].gauge = (gauge_t) strtod (node_value,
+          /* endptr = */ NULL);
+  }
+
+  /* free up object */
+  xmlXPathFreeObject (values_node_obj);
+
+  /* We have reached here which means that
+   * we have got something to work */
+  return (0);
+} /* }}} int cx_handle_single_value_xpath */
+
+static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
+    cx_xpath_t *xpath,
+    const data_set_t *ds, value_list_t *vl)
+{
+  value_t values[xpath->values_len];
+  int status;
+  int i;
+
+  assert (xpath->values_len > 0);
+  assert (xpath->values_len == vl->values_len);
+  assert (xpath->values_len == ds->ds_num);
+  vl->values = values;
+
+  for (i = 0; i < xpath->values_len; i++)
+  {
+    status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
+    if (status != 0)
+      return (-1); /* An error has been printed. */
+  } /* for (i = 0; i < xpath->values_len; i++) */
+
+  plugin_dispatch_values (vl);
+  vl->values = NULL;
+
+  return (0);
+} /* }}} int cx_handle_all_value_xpaths */
+
+static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
+    cx_xpath_t *xpath, value_list_t *vl,
+    _Bool is_table)
+{
+  xmlXPathObjectPtr instance_node_obj = NULL;
+  xmlNodeSetPtr instance_node = NULL;
+
+  memset (vl->type_instance, 0, sizeof (vl->type_instance));
+
+  /* If the base xpath returns more than one block, the result is assumed to be
+   * a table. The `Instnce' option is not optional in this case. Check for the
+   * condition and inform the user. */
+  if (is_table && (vl->type_instance == NULL))
+  {
+    WARNING ("curl_xml plugin: "
+        "Base-XPath %s is a table (more than one result was returned), "
+        "but no instance-XPath has been defined.",
+        xpath->path);
+    return (-1);
+  }
+
+  /* instance has to be an xpath expression */
+  if (xpath->instance != NULL)
+  {
+    int tmp_size;
+
+    instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
+    if (instance_node_obj == NULL)
+      return (-1); /* error is logged already */
+
+    instance_node = instance_node_obj->nodesetval;
+    tmp_size = (instance_node) ? instance_node->nodeNr : 0;
+
+    if ( (tmp_size == 0) && (is_table) )
+    {
+      WARNING ("curl_xml plugin: "
+          "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
+          "any of the nodes. Skipping the node.", xpath->instance);
+      xmlXPathFreeObject (instance_node_obj);
+      return (-1);
+    }
+
+    if (tmp_size > 1)
+    {
+      WARNING ("curl_xml plugin: "
+          "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
+          "to return only one text node. Skipping the node.", xpath->instance);
+      xmlXPathFreeObject (instance_node_obj);
+      return (-1);
+    }
+
+    /* ignoring the element if other than textnode/attribute */
+    if (cx_if_not_text_node(instance_node->nodeTab[0]))
+    {
+      WARNING ("curl_xml plugin: "
+          "relative xpath expression \"%s\" is expected to return only text node "
+          "which is not the case. Skipping the node.", xpath->instance);
+      xmlXPathFreeObject (instance_node_obj);
+      return (-1);
+    }
+  } /* if (xpath->instance != NULL) */
+
+  if (xpath->instance_prefix != NULL)
+  {
+    if (instance_node != NULL)
+      ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
+          xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
+    else
+      sstrncpy (vl->type_instance, xpath->instance_prefix,
+          sizeof (vl->type_instance));
+  }
+  else
+  {
+    /* If instance_prefix and instance_node are NULL, then
+     * don't set the type_instance */
+    if (instance_node != NULL)
+      sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
+          sizeof (vl->type_instance));
+  }
+
+  /* Free `instance_node_obj' this late, because `instance_node' points to
+   * somewhere inside this structure. */
+  xmlXPathFreeObject (instance_node_obj);
+
+  return (0);
+} /* }}} int cx_handle_instance_xpath */
+
+static int  cx_handle_base_xpath (char *plugin_instance, /* {{{ */
+    xmlXPathContextPtr xpath_ctx, const data_set_t *ds, 
+    char *base_xpath, cx_xpath_t *xpath)
+{
+  int total_nodes;
+  int i;
+
+  xmlXPathObjectPtr base_node_obj = NULL;
+  xmlNodeSetPtr base_nodes = NULL;
+
+  value_list_t vl = VALUE_LIST_INIT;
+
+  base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath); 
+  if (base_node_obj == NULL)
+    return -1; /* error is logged already */
+
+  base_nodes = base_node_obj->nodesetval;
+  total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
+
+  if (total_nodes == 0)
+  {
+     ERROR ("curl_xml plugin: "
+              "xpath expression \"%s\" doesn't match any of the nodes. "
+              "Skipping the xpath block...", base_xpath);
+     xmlXPathFreeObject (base_node_obj);
+     return -1;
+  }
+
+  /* If base_xpath returned multiple results, then */
+  /* Instance in the xpath block is required */ 
+  if (total_nodes > 1 && xpath->instance == NULL)
+  {
+    ERROR ("curl_xml plugin: "
+             "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
+             "returned multiple results. Skipping the xpath block...", base_xpath);
+    return -1;
+  }
+
+  /* set the values for the value_list */
+  vl.values_len = ds->ds_num;
+  sstrncpy (vl.type, xpath->type, sizeof (vl.type));
+  sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
+  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+  if (plugin_instance != NULL)
+    sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); 
+
+  for (i = 0; i < total_nodes; i++)
+  {
+    int status;
+
+    xpath_ctx->node = base_nodes->nodeTab[i];
+
+    status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
+        /* is_table = */ (total_nodes > 1));
+    if (status != 0)
+      continue; /* An error has already been reported. */
+
+    status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
+    if (status != 0)
+      continue; /* An error has been logged. */
+  } /* for (i = 0; i < total_nodes; i++) */
+
+  /* free up the allocated memory */
+  xmlXPathFreeObject (base_node_obj); 
+
+  return (0); 
+} /* }}} cx_handle_base_xpath */
+
+static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */ 
+                       xmlXPathContextPtr xpath_ctx, cx_t *db)
+{
+  llentry_t *le;
+  const data_set_t *ds;
+  cx_xpath_t *xpath;
+  int status=-1;
+  
+
+  le = llist_head (db->list);
+  while (le != NULL)
+  {
+    /* get the ds */
+    xpath = (cx_xpath_t *) le->value;
+    ds = plugin_get_ds (xpath->type);
+
+    if ( (cx_check_type(ds, xpath) == 0) &&
+         (cx_handle_base_xpath(db->instance, xpath_ctx, ds, le->key, xpath) == 0) )
+      status = 0; /* we got atleast one success */
+
+    le = le->next;
+  } /* while (le != NULL) */
+
+  return status;
+} /* }}} cx_handle_parsed_xml */
+
+static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
+{
+  int status;
+  xmlDocPtr doc;
+  xmlXPathContextPtr xpath_ctx;
+
+  /* Load the XML */
+  doc = xmlParseDoc(xml);
+  if (doc == NULL)
+  {
+    ERROR ("curl_xml plugin: Failed to parse the xml document  - %s", xml);
+    return (-1);
+  }
+
+  xpath_ctx = xmlXPathNewContext(doc);
+  if(xpath_ctx == NULL)
+  {
+    ERROR ("curl_xml plugin: Failed to create the xml context");
+    xmlFreeDoc(doc);
+    return (-1);
+  }
+
+  status = cx_handle_parsed_xml (doc, xpath_ctx, db);
+  /* Cleanup */
+  xmlXPathFreeContext(xpath_ctx);
+  xmlFreeDoc(doc);
+  return status;
+} /* }}} cx_parse_stats_xml */
+
+static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
+{
+  int status;
+  long rc;
+  char *ptr;
+  char *url;
+
+  db->buffer_fill = 0; 
+  status = curl_easy_perform (curl);
+
+  curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
+  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
+
+  if (rc != 200)
+  {
+    ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
+           rc, url);
+    return (-1);
+  }
+
+  if (status != 0)
+  {
+    ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
+           status, db->curl_errbuf, url);
+    return (-1);
+  }
+
+  ptr = db->buffer;
+
+  status = cx_parse_stats_xml(BAD_CAST ptr, db);
+  db->buffer_fill = 0;
+
+  return status;
+} /* }}} int cx_curl_perform */
+
+static int cx_read (user_data_t *ud) /* {{{ */
+{
+  cx_t *db;
+
+  if ((ud == NULL) || (ud->data == NULL))
+  {
+    ERROR ("curl_xml plugin: cx_read: Invalid user data.");
+    return (-1);
+  }
+
+  db = (cx_t *) ud->data;
+
+  return cx_curl_perform (db, db->curl);
+} /* }}} int cx_read */
+
+/* Configuration handling functions {{{ */
+
+static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
+                                      oconfig_item_t *ci)
+{
+  int i;
+
+  if (ci->values_num < 1)
+  {
+    WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
+    return (-1);
+  }
+
+  for (i = 0; i < ci->values_num; i++)
+    if (ci->values[i].type != OCONFIG_TYPE_STRING)
+    {
+      WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
+      return (-1);
+    }
+
+  sfree (xpath->values);
+
+  xpath->values_len = 0;
+  xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
+  if (xpath->values == NULL)
+    return (-1);
+  xpath->values_len = ci->values_num;
+
+  /* populate cx_values_t structure */
+  for (i = 0; i < ci->values_num; i++)
+  {
+    xpath->values[i].path_len = sizeof (ci->values[i].value.string);
+    sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
+  }
+
+  return (0); 
+} /* }}} cx_config_add_values */
+
+static int cx_config_add_xpath (cx_t *db, /* {{{ */
+                                   oconfig_item_t *ci)
+{
+  cx_xpath_t *xpath;
+  int status;
+  int i;
+
+  xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
+  if (xpath == NULL)
+  {
+    ERROR ("curl_xml plugin: malloc failed.");
+    return (-1);
+  }
+  memset (xpath, 0, sizeof (*xpath));
+
+  status = cf_util_get_string (ci, &xpath->path);
+  if (status != 0)
+  {
+    sfree (xpath);
+    return (status);
+  }
+
+  /* error out if xpath->path is an empty string */
+  if (*xpath->path == 0)
+  {
+    ERROR ("curl_xml plugin: invalid xpath. "
+           "xpath value can't be an empty string");
+    return (-1);
+  }
+
+  status = 0;
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Type", child->key) == 0)
+      status = cf_util_get_string (child, &xpath->type);
+    else if (strcasecmp ("InstancePrefix", child->key) == 0)
+      status = cf_util_get_string (child, &xpath->instance_prefix);
+    else if (strcasecmp ("InstanceFrom", child->key) == 0)
+      status = cf_util_get_string (child, &xpath->instance);
+    else if (strcasecmp ("ValuesFrom", child->key) == 0)
+      status = cx_config_add_values ("ValuesFrom", xpath, child);
+    else
+    {
+      WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (i = 0; i < ci->children_num; i++) */
+
+  if (status == 0 && xpath->type == NULL)
+  {
+    WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
+    status = -1;
+  }
+
+  if (status == 0)
+  {
+    char *name;
+    llentry_t *le;
+
+    if (db->list == NULL)
+    {
+      db->list = llist_create();
+      if (db->list == NULL)
+      {
+        ERROR ("curl_xml plugin: list creation failed.");
+        return (-1);
+      }
+    }
+
+    name = strdup(xpath->path);
+    if (name == NULL)
+    {
+        ERROR ("curl_xml plugin: strdup failed.");
+        return (-1);
+    }
+
+    le = llentry_create (name, xpath);
+    if (le == NULL)
+    {
+      ERROR ("curl_xml plugin: llentry_create failed.");
+      return (-1);
+    }
+
+    llist_append (db->list, le);
+  }
+
+  return (status);
+} /* }}} int cx_config_add_xpath */
+
+/* Initialize db->curl */
+static int cx_init_curl (cx_t *db) /* {{{ */
+{
+  db->curl = curl_easy_init ();
+  if (db->curl == NULL)
+  {
+    ERROR ("curl_xml plugin: curl_easy_init failed.");
+    return (-1);
+  }
+
+  curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
+  curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
+  curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
+                    PACKAGE_NAME"/"PACKAGE_VERSION);
+  curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
+  curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
+
+  if (db->user != NULL)
+  {
+    size_t credentials_size;
+
+    credentials_size = strlen (db->user) + 2;
+    if (db->pass != NULL)
+      credentials_size += strlen (db->pass);
+
+    db->credentials = (char *) malloc (credentials_size);
+    if (db->credentials == NULL)
+    {
+      ERROR ("curl_xml plugin: malloc failed.");
+      return (-1);
+    }
+
+    ssnprintf (db->credentials, credentials_size, "%s:%s",
+               db->user, (db->pass == NULL) ? "" : db->pass);
+    curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
+  }
+
+  curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
+  curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
+                    db->verify_host ? 2L : 0L);
+  if (db->cacert != NULL)
+    curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
+
+  return (0);
+} /* }}} int cx_init_curl */
+
+static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
+{
+  cx_t *db;
+  int status = 0;
+  int i;
+
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("curl_xml plugin: The `URL' block "
+             "needs exactly one string argument.");
+    return (-1);
+  }
+
+  db = (cx_t *) malloc (sizeof (*db));
+  if (db == NULL)
+  {
+    ERROR ("curl_xml plugin: malloc failed.");
+    return (-1);
+  }
+  memset (db, 0, sizeof (*db));
+
+  if (strcasecmp ("URL", ci->key) == 0)
+  {
+    status = cf_util_get_string (ci, &db->url);
+    if (status != 0)
+    {
+      sfree (db);
+      return (status);
+    }
+  }
+  else
+  {
+    ERROR ("curl_xml plugin: cx_config: "
+           "Invalid key: %s", ci->key);
+    return (-1);
+  }
+
+  /* Fill the `cx_t' structure.. */
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Instance", child->key) == 0)
+      status = cf_util_get_string (child, &db->instance);
+    else if (strcasecmp ("Host", child->key) == 0)
+      status = cf_util_get_string (child, &db->host);
+    else if (strcasecmp ("User", child->key) == 0)
+      status = cf_util_get_string (child, &db->user);
+    else if (strcasecmp ("Password", child->key) == 0)
+      status = cf_util_get_string (child, &db->pass);
+    else if (strcasecmp ("VerifyPeer", child->key) == 0)
+      status = cf_util_get_boolean (child, &db->verify_peer);
+    else if (strcasecmp ("VerifyHost", child->key) == 0)
+      status = cf_util_get_boolean (child, &db->verify_host);
+    else if (strcasecmp ("CACert", child->key) == 0)
+      status = cf_util_get_string (child, &db->cacert);
+    else if (strcasecmp ("xpath", child->key) == 0)
+      status = cx_config_add_xpath (db, child);
+    else
+    {
+      WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  }
+
+  if (status == 0)
+  {
+    if (db->list == NULL)
+    {
+      WARNING ("curl_xml plugin: No (valid) `Key' block "
+               "within `URL' block `%s'.", db->url);
+      status = -1;
+    }
+    if (status == 0)
+      status = cx_init_curl (db);
+  }
+
+  /* If all went well, register this database for reading */
+  if (status == 0)
+  {
+    user_data_t ud;
+    char cb_name[DATA_MAX_NAME_LEN];
+
+    if (db->instance == NULL)
+      db->instance = strdup("default");
+
+    DEBUG ("curl_xml plugin: Registering new read callback: %s",
+           db->instance);
+
+    memset (&ud, 0, sizeof (ud));
+    ud.data = (void *) db;
+    ud.free_func = cx_free;
+
+    ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
+               db->instance, db->url);
+
+    plugin_register_complex_read (/* group = */ NULL, cb_name, cx_read,
+                                  /* interval = */ NULL, &ud);
+  }
+  else
+  {
+    cx_free (db);
+    return (-1);
+  }
+
+  return (0);
+} /* }}} int cx_config_add_url */
+
+/* }}} End of configuration handling functions */
+
+static int cx_config (oconfig_item_t *ci) /* {{{ */
+{
+  int success;
+  int errors;
+  int status;
+  int i;
+
+  success = 0;
+  errors = 0;
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("URL", child->key) == 0)
+    {
+      status = cx_config_add_url (child);
+      if (status == 0)
+        success++;
+      else
+        errors++;
+    }
+    else
+    {
+      WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
+      errors++;
+    }
+  }
+
+  if ((success == 0) && (errors > 0))
+  {
+    ERROR ("curl_xml plugin: All statements failed.");
+    return (-1);
+  }
+
+  return (0);
+} /* }}} int cx_config */
+
+void module_register (void)
+{
+  plugin_register_complex_config ("curl_xml", cx_config);
+} /* void module_register */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
index ce4cd02..77f393f 100644 (file)
--- a/src/dbi.c
+++ b/src/dbi.c
@@ -46,6 +46,7 @@ struct cdbi_database_s /* {{{ */
   cdbi_driver_option_t *driver_options;
   size_t driver_options_num;
 
+  udb_query_preparation_area_t **q_prep_areas;
   udb_query_t **queries;
   size_t        queries_num;
 
@@ -162,6 +163,11 @@ static void cdbi_database_free (cdbi_database_t *db) /* {{{ */
   }
   sfree (db->driver_options);
 
+  if (db->q_prep_areas)
+    for (i = 0; i < db->queries_num; ++i)
+      udb_query_delete_preparation_area (db->q_prep_areas[i]);
+  free (db->q_prep_areas);
+
   sfree (db);
 } /* }}} void cdbi_database_free */
 
@@ -328,6 +334,34 @@ static int cdbi_config_add_database (oconfig_item_t *ci) /* {{{ */
     break;
   } /* while (status == 0) */
 
+  while ((status == 0) && (db->queries_num > 0))
+  {
+    db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
+        db->queries_num, sizeof (*db->q_prep_areas));
+
+    if (db->q_prep_areas == NULL)
+    {
+      WARNING ("dbi plugin: malloc failed");
+      status = -1;
+      break;
+    }
+
+    for (i = 0; i < db->queries_num; ++i)
+    {
+      db->q_prep_areas[i]
+        = udb_query_allocate_preparation_area (db->queries[i]);
+
+      if (db->q_prep_areas[i] == NULL)
+      {
+        WARNING ("dbi plugin: udb_query_allocate_preparation_area failed");
+        status = -1;
+        break;
+      }
+    }
+
+    break;
+  }
+
   /* If all went well, add this database to the global list of databases. */
   if (status == 0)
   {
@@ -422,7 +456,7 @@ static int cdbi_init (void) /* {{{ */
 } /* }}} int cdbi_init */
 
 static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
-    udb_query_t *q)
+    udb_query_t *q, udb_query_preparation_area_t *prep_area)
 {
   const char *statement;
   dbi_result res;
@@ -530,8 +564,9 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
     sstrncpy (column_names[i], column_name, DATA_MAX_NAME_LEN);
   } /* }}} for (i = 0; i < column_num; i++) */
 
-  udb_query_prepare_result (q, hostname_g, /* plugin = */ "dbi", db->name,
-      column_names, column_num);
+  udb_query_prepare_result (q, prep_area, hostname_g,
+      /* plugin = */ "dbi", db->name,
+      column_names, column_num, /* interval = */ -1);
 
   /* 0 = error; 1 = success; */
   status = dbi_result_first_row (res); /* {{{ */
@@ -543,7 +578,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
         "return any rows?",
         db->name, udb_query_get_name (q),
         cdbi_strerror (db->connection, errbuf, sizeof (errbuf)));
-    udb_query_finish_result (q);
+    udb_query_finish_result (q, prep_area);
     BAIL_OUT (-1);
   } /* }}} */
 
@@ -572,7 +607,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
      * to dispatch the row to the daemon. */
     if (status == 0) /* {{{ */
     {
-      status = udb_query_handle_result (q, column_values);
+      status = udb_query_handle_result (q, prep_area, column_values);
       if (status != 0)
       {
         ERROR ("dbi plugin: cdbi_read_database_query (%s, %s): "
@@ -598,7 +633,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */
   } /* }}} while (42) */
 
   /* Tell the db query interface that we're done with this query. */
-  udb_query_finish_result (q);
+  udb_query_finish_result (q, prep_area);
 
   /* Clean up and return `status = 0' (success) */
   BAIL_OUT (0);
@@ -741,7 +776,8 @@ static int cdbi_read_database (cdbi_database_t *db) /* {{{ */
         && (udb_query_check_version (db->queries[i], db_version) == 0))
       continue;
 
-    status = cdbi_read_database_query (db, db->queries[i]);
+    status = cdbi_read_database_query (db,
+        db->queries[i], db->q_prep_areas[i]);
     if (status == 0)
       success++;
   }
index 62775fd..9185ba4 100644 (file)
--- a/src/df.c
+++ b/src/df.c
@@ -1,6 +1,6 @@
 /**
  * collectd - src/df.c
- * Copyright (C) 2005-2007  Florian octo Forster
+ * Copyright (C) 2005-2009  Florian octo Forster
  * Copyright (C) 2009       Paul Sadauskas
  *
  * This program is free software; you can redistribute it and/or modify it
index 5df1c98..0a90899 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/disk.c
  * Copyright (C) 2005-2008  Florian octo Forster
+ * Copyright (C) 2009       Manuel Sanmartin
  *
  * 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
@@ -17,6 +18,7 @@
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Manuel Sanmartin
  **/
 
 #include "collectd.h"
 # include <statgrab.h>
 #endif
 
+#if HAVE_PERFSTAT
+# ifndef _AIXVERSION_610
+# include <sys/systemcfg.h>
+# endif
+# include <sys/protosw.h>
+# include <libperfstat.h>
+#endif
+
 #if HAVE_IOKIT_IOKITLIB_H
 static mach_port_t io_master_port = MACH_PORT_NULL;
 /* #endif HAVE_IOKIT_IOKITLIB_H */
@@ -105,6 +115,12 @@ static int numdisk = 0;
 #elif defined(HAVE_LIBSTATGRAB)
 /* #endif HAVE_LIBKSTATGRAB */
 
+#elif HAVE_PERFSTAT
+static perfstat_disk_t * stat_disk;
+static int numdisk;
+static int pnumdisk;
+/* #endif HAVE_PERFSTAT */
+
 #else
 # error "No applicable input method."
 #endif
@@ -148,7 +164,7 @@ static int disk_init (void)
 {
 #if HAVE_IOKIT_IOKITLIB_H
        kern_return_t status;
-       
+
        if (io_master_port != MACH_PORT_NULL)
        {
                mach_port_deallocate (mach_task_self (),
@@ -681,7 +697,60 @@ static int disk_read (void)
                disk_submit (name, "disk_octets", ds->read_bytes, ds->write_bytes);
                ds++;
        }
-#endif /* defined(HAVE_LIBSTATGRAB) */
+/* #endif defined(HAVE_LIBSTATGRAB) */
+
+#elif defined(HAVE_PERFSTAT)
+       counter_t read_sectors;
+       counter_t write_sectors;
+       counter_t read_time;
+       counter_t write_time;
+       counter_t read_ops;
+       counter_t write_ops;
+       perfstat_id_t firstpath;
+       int rnumdisk;
+       int i;
+
+       if ((numdisk = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0)) < 0) 
+       {
+               char errbuf[1024];
+               WARNING ("disk plugin: perfstat_disk: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       if (numdisk != pnumdisk || stat_disk==NULL) {
+               if (stat_disk!=NULL) 
+                       free(stat_disk);
+               stat_disk = (perfstat_disk_t *)calloc(numdisk, sizeof(perfstat_disk_t));
+       } 
+       pnumdisk = numdisk;
+
+       firstpath.name[0]='\0';
+       if ((rnumdisk = perfstat_disk(&firstpath, stat_disk, sizeof(perfstat_disk_t), numdisk)) < 0) 
+       {
+               char errbuf[1024];
+               WARNING ("disk plugin: perfstat_disk : %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       for (i = 0; i < rnumdisk; i++) 
+       {
+               read_sectors = stat_disk[i].rblks*stat_disk[i].bsize;
+               write_sectors = stat_disk[i].wblks*stat_disk[i].bsize;
+               disk_submit (stat_disk[i].name, "disk_octets", read_sectors, write_sectors);
+
+               read_ops = stat_disk[i].xrate;
+               write_ops = stat_disk[i].xfers - stat_disk[i].xrate;
+               disk_submit (stat_disk[i].name, "disk_ops", read_ops, write_ops);
+
+               read_time = stat_disk[i].rserv;
+               read_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
+               write_time = stat_disk[i].wserv;
+               write_time *= ((double)(_system_configuration.Xint)/(double)(_system_configuration.Xfrac)) / 1000000.0;
+               disk_submit (stat_disk[i].name, "disk_time", read_time, write_time);
+       }
+#endif /* defined(HAVE_PERFSTAT) */
 
        return (0);
 } /* int disk_read */
index acc6cf6..c64f949 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/exec.c
- * Copyright (C) 2007-2009  Florian octo Forster
+ * Copyright (C) 2007-2010  Florian octo Forster
  * Copyright (C) 2007-2009  Sebastian Harl
  * Copyright (C) 2008       Peter Holik
  *
@@ -269,13 +269,26 @@ static void set_environment (void) /* {{{ */
 {
   char buffer[1024];
 
+#ifdef HAVE_SETENV
   ssnprintf (buffer, sizeof (buffer), "%i", interval_g);
   setenv ("COLLECTD_INTERVAL", buffer, /* overwrite = */ 1);
 
   ssnprintf (buffer, sizeof (buffer), "%s", hostname_g);
   setenv ("COLLECTD_HOSTNAME", buffer, /* overwrite = */ 1);
+#else
+  ssnprintf (buffer, sizeof (buffer), "COLLECTD_INTERVAL=%i", interval_g);
+  putenv (buffer);
+
+  ssnprintf (buffer, sizeof (buffer), "COLLECTD_HOSTNAME=%s", hostname_g);
+  putenv (buffer);
+#endif
+
+#ifdef HAVE_SETENV
+#else
+#endif
 } /* }}} void set_environment */
 
+__attribute__((noreturn))
 static void exec_child (program_list_t *pl) /* {{{ */
 {
   int status;
@@ -292,8 +305,8 @@ static void exec_child (program_list_t *pl) /* {{{ */
   status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
   if (status != 0)
   {
-    ERROR ("exec plugin: getpwnam_r failed: %s",
-       sstrerror (errno, errbuf, sizeof (errbuf)));
+    ERROR ("exec plugin: Failed to get user information for user ``%s'': %s",
+       pl->user, sstrerror (errno, errbuf, sizeof (errbuf)));
     exit (-1);
   }
   if (sp_ptr == NULL)
@@ -322,7 +335,8 @@ static void exec_child (program_list_t *pl) /* {{{ */
       status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
       if (0 != status)
       {
-       ERROR ("exec plugin: getgrnam_r failed: %s",
+       ERROR ("exec plugin: Failed to get group information "
+           "for group ``%s'': %s", pl->group,
            sstrerror (errno, errbuf, sizeof (errbuf)));
        exit (-1);
       }
@@ -388,8 +402,8 @@ static void exec_child (program_list_t *pl) /* {{{ */
 
   status = execvp (pl->exec, pl->argv);
 
-  ERROR ("exec plugin: exec failed: %s",
-      sstrerror (errno, errbuf, sizeof (errbuf)));
+  ERROR ("exec plugin: Failed to execute ``%s'': %s",
+      pl->exec, sstrerror (errno, errbuf, sizeof (errbuf)));
   exit (-1);
 } /* void exec_child }}} */
 
@@ -549,7 +563,13 @@ static void *exec_read_one (void *arg) /* {{{ */
 
   status = fork_child (pl, NULL, &fd, &fd_err);
   if (status < 0)
+  {
+    /* Reset the "running" flag */
+    pthread_mutex_lock (&pl_lock);
+    pl->flags &= ~PL_RUNNING;
+    pthread_mutex_unlock (&pl_lock);
     pthread_exit ((void *) 1);
+  }
   pl->pid = status;
 
   assert (pl->pid != 0);
@@ -805,7 +825,7 @@ static int exec_read (void) /* {{{ */
   return (0);
 } /* int exec_read }}} */
 
-static int exec_notification (const notification_t *n,
+static int exec_notification (const notification_t *n, /* {{{ */
     user_data_t __attribute__((unused)) *user_data)
 {
   program_list_t *pl;
index 05bb4b3..47f99e9 100644 (file)
@@ -32,6 +32,7 @@
 #include <fnmatch.h>
 
 #define FC_RECURSIVE 1
+#define FC_HIDDEN 2
 
 struct fc_directory_conf_s
 {
@@ -310,8 +311,8 @@ static int fc_config_add_dir_size (fc_directory_conf_t *dir,
   return (0);
 } /* int fc_config_add_dir_size */
 
-static int fc_config_add_dir_recursive (fc_directory_conf_t *dir,
-    oconfig_item_t *ci)
+static int fc_config_add_dir_option (fc_directory_conf_t *dir,
+    oconfig_item_t *ci, int bit)
 {
   if ((ci->values_num != 1)
       || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
@@ -322,12 +323,12 @@ static int fc_config_add_dir_recursive (fc_directory_conf_t *dir,
   }
 
   if (ci->values[0].value.boolean)
-    dir->options |= FC_RECURSIVE;
+    dir->options |= bit;
   else
-    dir->options &= ~FC_RECURSIVE;
+    dir->options &= ~bit;
 
   return (0);
-} /* int fc_config_add_dir_recursive */
+} /* int fc_config_add_dir_option */
 
 static int fc_config_add_dir (oconfig_item_t *ci)
 {
@@ -380,7 +381,9 @@ static int fc_config_add_dir (oconfig_item_t *ci)
     else if (strcasecmp ("Size", option->key) == 0)
       status = fc_config_add_dir_size (dir, option);
     else if (strcasecmp ("Recursive", option->key) == 0)
-      status = fc_config_add_dir_recursive (dir, option);
+      status = fc_config_add_dir_option (dir, option, FC_RECURSIVE);
+    else if (strcasecmp ("IncludeHidden", option->key) == 0)
+      status = fc_config_add_dir_option (dir, option, FC_HIDDEN);
     else
     {
       WARNING ("filecount plugin: fc_config_add_dir: "
@@ -475,7 +478,8 @@ static int fc_read_dir_callback (const char *dirname, const char *filename,
 
   if (S_ISDIR (statbuf.st_mode) && (dir->options & FC_RECURSIVE))
   {
-    status = walk_directory (abs_path, fc_read_dir_callback, dir);
+    status = walk_directory (abs_path, fc_read_dir_callback, dir,
+        /* include hidden = */ (dir->options & FC_HIDDEN) ? 1 : 0);
     return (status);
   }
   else if (!S_ISREG (statbuf.st_mode))
@@ -537,8 +541,9 @@ static int fc_read_dir (fc_directory_conf_t *dir)
 
   if (dir->mtime != 0)
     dir->now = time (NULL);
-
-  status = walk_directory (dir->path, fc_read_dir_callback, dir);
+    
+  status = walk_directory (dir->path, fc_read_dir_callback, dir,
+      /* include hidden */ (dir->options & FC_HIDDEN) ? 1 : 0);
   if (status != 0)
   {
     WARNING ("filecount plugin: walk_directory (%s) failed.", dir->path);
index 7c23a35..4b164dc 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/filter_chain.h
- * Copyright (C) 2008,2009  Florian octo Forster
+ * Copyright (C) 2008-2010  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
@@ -693,7 +693,14 @@ static int fc_bit_write_invoke (const data_set_t *ds, /* {{{ */
   if ((plugin_list == NULL) || (plugin_list[0] == NULL))
   {
     status = plugin_write (/* plugin = */ NULL, ds, vl);
-    if (status != 0)
+    if (status == ENOENT)
+    {
+      INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
+          "all write plugins failed with status %i (ENOENT). "
+          "Most likely this means you didn't load any write plugins.",
+          status);
+    }
+    else if (status != 0)
     {
       INFO ("Filter subsystem: Built-in target `write': Dispatching value to "
           "all write plugins failed with status %i.", status);
index fead888..1ba6c8c 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/interface.c
  * Copyright (C) 2005-2008  Florian octo Forster
+ * Copyright (C) 2009       Manuel Sanmartin
  *
  * 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
@@ -18,6 +19,7 @@
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
  *   Sune Marcher <sm at flork.dk>
+ *   Manuel Sanmartin
  **/
 
 #include "collectd.h"
 # include <statgrab.h>
 #endif
 
+#if HAVE_PERFSTAT
+# include <sys/protosw.h>
+# include <libperfstat.h>
+#endif
+
 /*
  * Various people have reported problems with `getifaddrs' and varying versions
  * of `glibc'. That's why it's disabled by default. Since more statistics are
 # endif /* !COLLECT_GETIFADDRS */
 #endif /* KERNEL_LINUX */
 
-#if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB
+#if HAVE_PERFSTAT
+static perfstat_netinterface_t *ifstat;
+static int nif;
+static int pnif;
+#endif /* HAVE_PERFSTAT */
+
+#if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
 # error "No applicable input method."
 #endif
 
@@ -222,7 +235,7 @@ static int interface_read (void)
        char buffer[1024];
        unsigned long long incoming, outgoing;
        char *device;
-       
+
        char *dummy;
        char *fields[16];
        int numfields;
@@ -248,7 +261,7 @@ static int interface_read (void)
 
                if (device[0] == '\0')
                        continue;
-               
+
                numfields = strsplit (dummy, fields, 16);
 
                if (numfields < 11)
@@ -308,7 +321,44 @@ static int interface_read (void)
 
        for (i = 0; i < num; i++)
                if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif HAVE_LIBSTATGRAB */
+
+#elif defined(HAVE_PERFSTAT)
+       perfstat_id_t id;
+       int i, ifs;
+
+       if ((nif =  perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0)
+       {
+               char errbuf[1024];
+               WARNING ("interface plugin: perfstat_netinterface: %s",
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       if (pnif != nif || ifstat == NULL)
+       {
+               if (ifstat != NULL)
+                       free(ifstat);
+               ifstat = malloc(nif * sizeof(perfstat_netinterface_t));
+       }
+       pnif = nif;
+
+       id.name[0]='\0';
+       if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0)
+       {
+               char errbuf[1024];
+               WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s",
+                       nif, sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       for (i = 0; i < ifs; i++)
+       {
+               if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
+               if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
+               if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
+       }
+#endif /* HAVE_PERFSTAT */
 
        return (0);
 } /* int interface_read */
index 87b189f..528ec9c 100644 (file)
@@ -1419,7 +1419,7 @@ static jint JNICALL cjni_api_register_read (JNIEnv *jvm_env, /* {{{ */
   ud.data = (void *) cbi;
   ud.free_func = cjni_callback_info_destroy;
 
-  plugin_register_complex_read (cbi->name, cjni_read,
+  plugin_register_complex_read (/* group = */ NULL, cbi->name, cjni_read,
       /* interval = */ NULL, &ud);
 
   (*jvm_env)->DeleteLocalRef (jvm_env, o_read);
index b0d092d..11e7b13 100644 (file)
@@ -27,7 +27,9 @@
 /*
  * Includes (for data types)
  */
-#include <stdint.h>
+#if HAVE_STDINT_H
+# include <stdint.h>
+#endif
 #include <inttypes.h>
 #include <time.h>
 
index 49cd139..5b7aa94 100644 (file)
@@ -46,7 +46,7 @@ extern char           *c_file;
 }
 
 %token <number> NUMBER
-%token <boolean> TRUE FALSE
+%token <boolean> BTRUE BFALSE
 %token <string> QUOTED_STRING UNQUOTED_STRING
 %token SLASH OPENBRAC CLOSEBRAC EOL
 
@@ -76,8 +76,8 @@ string:
 
 argument:
        NUMBER                  {$$.value.number = $1; $$.type = OCONFIG_TYPE_NUMBER;}
-       | TRUE                  {$$.value.boolean = 1; $$.type = OCONFIG_TYPE_BOOLEAN;}
-       | FALSE                 {$$.value.boolean = 0; $$.type = OCONFIG_TYPE_BOOLEAN;}
+       | BTRUE                 {$$.value.boolean = 1; $$.type = OCONFIG_TYPE_BOOLEAN;}
+       | BFALSE                {$$.value.boolean = 0; $$.type = OCONFIG_TYPE_BOOLEAN;}
        | string                {$$.value.string = $1; $$.type = OCONFIG_TYPE_STRING;}
        ;
 
index b559e86..9f0cd8e 100644 (file)
@@ -69,8 +69,8 @@ IPV4_ADDR {IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}\.{IP_BYTE}(:{PORT})?
 "/"                    {return (SLASH);}
 "<"                    {return (OPENBRAC);}
 ">"                    {return (CLOSEBRAC);}
-{BOOL_TRUE}            {yylval.boolean = 1; return (TRUE);}
-{BOOL_FALSE}           {yylval.boolean = 0; return (FALSE);}
+{BOOL_TRUE}            {yylval.boolean = 1; return (BTRUE);}
+{BOOL_FALSE}           {yylval.boolean = 0; return (BFALSE);}
 
 {IPV4_ADDR}            {yylval.string = yytext; return (UNQUOTED_STRING);}
 
index cf5221b..0188da7 100644 (file)
@@ -1,6 +1,7 @@
 /**
  * collectd - src/load.c
  * Copyright (C) 2005-2008  Florian octo Forster
+ * Copyright (C) 2009       Manuel Sanmartin
  *
  * 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
@@ -17,6 +18,7 @@
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Manuel Sanmartin
  **/
 
 #define _BSD_SOURCE
 #endif
 #endif /* defined(HAVE_GETLOADAVG) */
 
+#ifdef HAVE_PERFSTAT
+# include <sys/proc.h> /* AIX 5 */
+# include <sys/protosw.h>
+# include <libperfstat.h>
+#endif /* HAVE_PERFSTAT */
+
 static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
 {
        value_t values[3];
@@ -81,7 +89,7 @@ static int load_read (void)
 
        char *fields[8];
        int numfields;
-       
+
        if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
        {
                char errbuf[1024];
@@ -132,6 +140,25 @@ static int load_read (void)
        load_submit (snum, mnum, lnum);
 /* #endif HAVE_LIBSTATGRAB */
 
+#elif HAVE_PERFSTAT
+       gauge_t snum, mnum, lnum;
+       perfstat_cpu_total_t cputotal;
+
+       if (perfstat_cpu_total(NULL,  &cputotal, sizeof(perfstat_cpu_total_t), 1) < 0)
+       {
+               char errbuf[1024];
+               WARNING ("load: perfstat_cpu : %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       snum = (float)cputotal.loadavg[0]/(float)(1<<SBITS);
+       mnum = (float)cputotal.loadavg[1]/(float)(1<<SBITS);
+       lnum = (float)cputotal.loadavg[2]/(float)(1<<SBITS);
+
+       load_submit (snum, mnum, lnum);
+/* #endif HAVE_PERFSTAT */
+
 #else
 # error "No applicable input method."
 #endif
index 7b96ac5..6d0f6e0 100644 (file)
@@ -39,12 +39,14 @@ static pthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;
 
 static char *log_file = NULL;
 static int print_timestamp = 1;
+static int print_severity = 0;
 
 static const char *config_keys[] =
 {
        "LogLevel",
        "File",
-       "Timestamp"
+       "Timestamp",
+       "PrintSeverity"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
@@ -78,6 +80,11 @@ static int logfile_config (const char *key, const char *value)
                        print_timestamp = 0;
                else
                        print_timestamp = 1;
+       } else if (0 == strcasecmp(key, "PrintSeverity")) {
+               if (IS_FALSE (value))
+                       print_severity = 0;
+               else
+                       print_severity = 1;
        }
        else {
                return -1;
@@ -85,12 +92,37 @@ static int logfile_config (const char *key, const char *value)
        return 0;
 } /* int logfile_config (const char *, const char *) */
 
-static void logfile_print (const char *msg, time_t timestamp_time)
+static void logfile_print (const char *msg, int severity, time_t timestamp_time)
 {
        FILE *fh;
        int do_close = 0;
        struct tm timestamp_tm;
        char timestamp_str[64];
+       char level_str[16] = "";
+
+       if (print_severity)
+       {
+               switch (severity)
+               {
+               case LOG_ERR:
+                       snprintf(level_str, sizeof (level_str), "[error] ");
+                       break;  
+               case LOG_WARNING:
+                       snprintf(level_str, sizeof (level_str), "[warning] ");
+                       break;
+               case LOG_NOTICE:
+                       snprintf(level_str, sizeof (level_str), "[notice] ");
+                       break;  
+               case LOG_INFO:
+                       snprintf(level_str, sizeof (level_str), "[info] ");
+                       break;  
+               case LOG_DEBUG:
+                       snprintf(level_str, sizeof (level_str), "[debug] ");
+                       break;  
+               default:
+                       break;
+               }
+       }
 
        if (print_timestamp)
        {
@@ -128,9 +160,9 @@ static void logfile_print (const char *msg, time_t timestamp_time)
        else
        {
                if (print_timestamp)
-                       fprintf (fh, "[%s] %s\n", timestamp_str, msg);
+                       fprintf (fh, "[%s] %s%s\n", timestamp_str, level_str, msg);
                else
-                       fprintf (fh, "%s\n", msg);
+                       fprintf (fh, "%s%s\n", level_str, msg);
 
                if (do_close != 0)
                        fclose (fh);
@@ -147,7 +179,7 @@ static void logfile_log (int severity, const char *msg,
        if (severity > log_level)
                return;
 
-       logfile_print (msg, time (NULL));
+       logfile_print (msg, severity, time (NULL));
 } /* void logfile_log (int, const char *) */
 
 static int logfile_notification (const notification_t *n,
@@ -185,7 +217,7 @@ static int logfile_notification (const notification_t *n,
 
        buf[sizeof (buf) - 1] = '\0';
 
-       logfile_print (buf,
+       logfile_print (buf, LOG_INFO,
                        (n->time > 0) ? n->time : time (NULL));
 
        return (0);
index 062a7a7..ee3101a 100644 (file)
@@ -158,8 +158,8 @@ static int mh_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
 
   for (host_ptr = vl->host; *host_ptr != 0; host_ptr++)
   {
-    /* 251 is the largest prime smaller than 256. */
-    hash_val = (hash_val * 251) + ((uint32_t) *host_ptr);
+    /* 2184401929 is some appropriately sized prime number. */
+    hash_val = (hash_val * UINT32_C (2184401929)) + ((uint32_t) *host_ptr);
   }
   DEBUG ("hashed match: host = %s; hash_val = %"PRIu32";", vl->host, hash_val);
 
index 1fca889..1defc18 100644 (file)
@@ -58,6 +58,7 @@ struct mr_match_s
        mr_regex_t *plugin_instance;
        mr_regex_t *type;
        mr_regex_t *type_instance;
+       _Bool invert;
 };
 
 /*
@@ -195,6 +196,8 @@ static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
                return (-ENOMEM);
        }
        memset (m, 0, sizeof (*m));
+       
+       m->invert = 0;
 
        status = 0;
        for (i = 0; i < ci->children_num; i++)
@@ -212,6 +215,8 @@ static int mr_create (const oconfig_item_t *ci, void **user_data) /* {{{ */
                        status = mr_config_add_regex (&m->type, child);
                else if (strcasecmp ("TypeInstance", child->key) == 0)
                        status = mr_config_add_regex (&m->type_instance, child);
+               else if (strcasecmp ("Invert", child->key) == 0)
+                       status = cf_util_get_boolean(child, &m->invert);
                else
                {
                        log_err ("The `%s' configuration option is not understood and "
@@ -263,26 +268,34 @@ static int mr_match (const data_set_t __attribute__((unused)) *ds, /* {{{ */
                void **user_data)
 {
        mr_match_t *m;
+       int match_value = FC_MATCH_MATCHES;
+       int nomatch_value = FC_MATCH_NO_MATCH;
 
        if ((user_data == NULL) || (*user_data == NULL))
                return (-1);
 
        m = *user_data;
 
+       if (m->invert)
+       {
+               match_value = FC_MATCH_NO_MATCH;
+               nomatch_value = FC_MATCH_MATCHES;
+       }
+
        if (mr_match_regexen (m->host, vl->host) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->plugin, vl->plugin) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->plugin_instance,
                                vl->plugin_instance) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->type, vl->type) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
        if (mr_match_regexen (m->type_instance,
                                vl->type_instance) == FC_MATCH_NO_MATCH)
-               return (FC_MATCH_NO_MATCH);
+               return (nomatch_value);
 
-       return (FC_MATCH_MATCHES);
+       return (match_value);
 } /* }}} int mr_match */
 
 void module_register (void)
index 451a853..d066501 100644 (file)
@@ -37,6 +37,7 @@ typedef struct web_match_s web_match_t;
 struct web_match_s /* {{{ */
 {
   char *regex;
+  char *exclude_regex;
   int dstype;
   char *type;
   char *instance;
@@ -220,6 +221,8 @@ static int cmc_config_add_match (web_page_t *page, /* {{{ */
 
     if (strcasecmp ("Regex", child->key) == 0)
       status = cmc_config_add_string ("Regex", &match->regex, child);
+    else if (strcasecmp ("ExcludeRegex", child->key) == 0)
+      status = cmc_config_add_string ("ExcludeRegex", &match->exclude_regex, child);
     else if (strcasecmp ("DSType", child->key) == 0)
       status = cmc_config_add_match_dstype (&match->dstype, child);
     else if (strcasecmp ("Type", child->key) == 0)
@@ -262,7 +265,8 @@ static int cmc_config_add_match (web_page_t *page, /* {{{ */
   if (status != 0)
     return (status);
 
-  match->match = match_create_simple (match->regex, match->dstype);
+  match->match = match_create_simple (match->regex, match->exclude_regex,
+      match->dstype);
   if (match->match == NULL)
   {
     ERROR ("memcachec plugin: tail_match_add_match_simple failed.");
index b332157..348591f 100644 (file)
 # include <netinet/in.h>
 # include <netinet/tcp.h>
 
+/* Hack to work around the missing define in AIX */
+#ifndef MSG_DONTWAIT
+# define MSG_DONTWAIT MSG_NONBLOCK
+#endif
+
 #define MEMCACHED_DEF_HOST "127.0.0.1"
 #define MEMCACHED_DEF_PORT "11211"
 
index 799a80c..b79a74b 100644 (file)
@@ -2,6 +2,7 @@
  * collectd - src/memory.c
  * Copyright (C) 2005-2008  Florian octo Forster
  * Copyright (C) 2009       Simon Kuhnle
+ * Copyright (C) 2009       Manuel Sanmartin
  *
  * 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
@@ -19,6 +20,7 @@
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
  *   Simon Kuhnle <simon at blarzwurst.de>
+ *   Manuel Sanmartin
  **/
 
 #include "collectd.h"
 # include <statgrab.h>
 #endif
 
+#if HAVE_PERFSTAT
+# include <sys/protosw.h>
+# include <libperfstat.h>
+#endif /* HAVE_PERFSTAT */
+
 /* vm_statistics_data_t */
 #if HAVE_HOST_STATISTICS
 static mach_port_t port_host;
@@ -75,7 +82,10 @@ static int pagesize;
 #elif HAVE_LIBSTATGRAB
 /* no global variables */
 /* endif HAVE_LIBSTATGRAB */
-
+#elif HAVE_PERFSTAT
+static int pagesize;
+static perfstat_memory_total_t pmemory;
+/* endif HAVE_PERFSTAT */
 #else
 # error "No applicable input method."
 #endif
@@ -116,8 +126,11 @@ static int memory_init (void)
 
 #elif HAVE_LIBSTATGRAB
 /* no init stuff */
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif HAVE_LIBSTATGRAB */
 
+#elif HAVE_PERFSTAT
+       pagesize = getpagesize ();
+#endif /* HAVE_PERFSTAT */
        return (0);
 } /* int memory_init */
 
@@ -145,10 +158,10 @@ static int memory_read (void)
        vm_statistics_data_t   vm_data;
        mach_msg_type_number_t vm_data_len;
 
-       long long wired;
-       long long active;
-       long long inactive;
-       long long free;
+       gauge_t wired;
+       gauge_t active;
+       gauge_t inactive;
+       gauge_t free;
 
        if (!port_host || !pagesize)
                return (-1);
@@ -182,10 +195,10 @@ static int memory_read (void)
         *   This memory is not being used.
         */
 
-       wired    = vm_data.wire_count     * pagesize;
-       active   = vm_data.active_count   * pagesize;
-       inactive = vm_data.inactive_count * pagesize;
-       free     = vm_data.free_count     * pagesize;
+       wired    = (gauge_t) (((uint64_t) vm_data.wire_count)     * ((uint64_t) pagesize));
+       active   = (gauge_t) (((uint64_t) vm_data.active_count)   * ((uint64_t) pagesize));
+       inactive = (gauge_t) (((uint64_t) vm_data.inactive_count) * ((uint64_t) pagesize));
+       free     = (gauge_t) (((uint64_t) vm_data.free_count)     * ((uint64_t) pagesize));
 
        memory_submit ("wired",    wired);
        memory_submit ("active",   active);
@@ -250,7 +263,7 @@ static int memory_read (void)
 #elif KERNEL_LINUX
        FILE *fh;
        char buffer[1024];
-       
+
        char *fields[8];
        int numfields;
 
@@ -364,7 +377,22 @@ static int memory_read (void)
                memory_submit ("cached", ios->cache);
                memory_submit ("free",   ios->free);
        }
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif HAVE_LIBSTATGRAB */
+
+#elif HAVE_PERFSTAT
+       if (perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
+       {
+               char errbuf[1024];
+               WARNING ("memory plugin: perfstat_memory_total failed: %s",
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+       memory_submit ("used",   pmemory.real_inuse * pagesize);
+       memory_submit ("free",   pmemory.real_free * pagesize);
+       memory_submit ("cached", pmemory.numperm * pagesize);
+       memory_submit ("system", pmemory.real_system * pagesize);
+       memory_submit ("user",   pmemory.real_process * pagesize);
+#endif /* HAVE_PERFSTAT */
 
        return (0);
 }
index 3a3f5e7..6a336c4 100644 (file)
 #include <pthread.h>
 
 /*
- * Defines
- */
-#define MD_TYPE_STRING       1
-#define MD_TYPE_SIGNED_INT   2
-#define MD_TYPE_UNSIGNED_INT 3
-#define MD_TYPE_DOUBLE       4
-#define MD_TYPE_BOOLEAN      5
-
-/*
  * Data types
  */
 union meta_value_u
@@ -249,6 +240,49 @@ int meta_data_exists (meta_data_t *md, const char *key) /* {{{ */
   return (0);
 } /* }}} int meta_data_exists */
 
+int meta_data_type (meta_data_t *md, const char *key) /* {{{ */
+{
+  meta_entry_t *e;
+
+  if ((md == NULL) || (key == NULL))
+    return -EINVAL;
+
+  pthread_mutex_lock (&md->lock);
+
+  for (e = md->head; e != NULL; e = e->next)
+  {
+    if (strcasecmp (key, e->key) == 0)
+    {
+      pthread_mutex_unlock (&md->lock);
+      return e->type;
+    }
+  }
+
+  pthread_mutex_unlock (&md->lock);
+  return 0;
+} /* }}} int meta_data_type */
+
+int meta_data_toc (meta_data_t *md, char ***toc) /* {{{ */
+{
+  int i = 0, count = 0;
+  meta_entry_t *e;
+
+  if ((md == NULL) || (toc == NULL))
+    return -EINVAL;
+
+  pthread_mutex_lock (&md->lock);
+
+  for (e = md->head; e != NULL; e = e->next)
+    ++count;    
+
+  *toc = malloc(count * sizeof(**toc));
+  for (e = md->head; e != NULL; e = e->next)
+    (*toc)[i++] = strdup(e->key);
+  
+  pthread_mutex_unlock (&md->lock);
+  return count;
+} /* }}} int meta_data_toc */
+
 int meta_data_delete (meta_data_t *md, const char *key) /* {{{ */
 {
   meta_entry_t *this;
index 8e5a785..9ef7b0a 100644 (file)
 
 #include "collectd.h"
 
+/*
+ * Defines
+ */
+#define MD_TYPE_STRING       1
+#define MD_TYPE_SIGNED_INT   2
+#define MD_TYPE_UNSIGNED_INT 3
+#define MD_TYPE_DOUBLE       4
+#define MD_TYPE_BOOLEAN      5
+
 struct meta_data_s;
 typedef struct meta_data_s meta_data_t;
 
@@ -31,6 +40,8 @@ meta_data_t *meta_data_create (void);
 void meta_data_destroy (meta_data_t *md);
 
 int meta_data_exists (meta_data_t *md, const char *key);
+int meta_data_type (meta_data_t *md, const char *key);
+int meta_data_toc (meta_data_t *md, char ***toc);
 int meta_data_delete (meta_data_t *md, const char *key);
 
 int meta_data_add_string (meta_data_t *md,
diff --git a/src/modbus.c b/src/modbus.c
new file mode 100644 (file)
index 0000000..adab0d0
--- /dev/null
@@ -0,0 +1,862 @@
+/**
+ * collectd - src/modbus.c
+ * Copyright (C) 2010  noris network AG
+ *
+ * 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 Forster <octo at noris.net>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
+
+#include <netdb.h>
+
+#include <modbus/modbus.h>
+
+#ifndef MODBUS_TCP_DEFAULT_PORT
+# ifdef MODBUS_TCP_PORT
+#  define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
+# else
+#  define MODBUS_TCP_DEFAULT_PORT 502
+# endif
+#endif
+
+/*
+ * <Data "data_name">
+ *   RegisterBase 1234
+ *   RegisterType float
+ *   Type gauge
+ *   Instance "..."
+ * </Data>
+ *
+ * <Host "name">
+ *   Address "addr"
+ *   Port "1234"
+ *   Interval 60
+ *
+ *   <Slave 1>
+ *     Instance "foobar" # optional
+ *     Collect "data_name"
+ *   </Slave>
+ * </Host>
+ */
+
+/*
+ * Data structures
+ */
+enum mb_register_type_e /* {{{ */
+{
+  REG_TYPE_UINT16,
+  REG_TYPE_UINT32,
+  REG_TYPE_FLOAT
+}; /* }}} */
+typedef enum mb_register_type_e mb_register_type_t;
+
+struct mb_data_s;
+typedef struct mb_data_s mb_data_t;
+struct mb_data_s /* {{{ */
+{
+  char *name;
+  int register_base;
+  mb_register_type_t register_type;
+  char type[DATA_MAX_NAME_LEN];
+  char instance[DATA_MAX_NAME_LEN];
+
+  mb_data_t *next;
+}; /* }}} */
+
+struct mb_slave_s /* {{{ */
+{
+  int id;
+  char instance[DATA_MAX_NAME_LEN];
+  mb_data_t *collect;
+}; /* }}} */
+typedef struct mb_slave_s mb_slave_t;
+
+struct mb_host_s /* {{{ */
+{
+  char host[DATA_MAX_NAME_LEN];
+  char node[NI_MAXHOST];
+  /* char service[NI_MAXSERV]; */
+  int port;
+  int interval;
+
+  mb_slave_t *slaves;
+  size_t slaves_num;
+
+  modbus_param_t connection;
+  _Bool is_connected;
+  _Bool have_reconnected;
+}; /* }}} */
+typedef struct mb_host_s mb_host_t;
+
+struct mb_data_group_s;
+typedef struct mb_data_group_s mb_data_group_t;
+struct mb_data_group_s /* {{{ */
+{
+  mb_data_t *registers;
+  size_t registers_num;
+
+  mb_data_group_t *next;
+}; /* }}} */
+
+/*
+ * Global variables
+ */
+static mb_data_t *data_definitions = NULL;
+
+/*
+ * Functions
+ */
+static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
+    const char *name)
+{
+  mb_data_t *ptr;
+
+  if (name == NULL)
+    return (NULL);
+
+  for (ptr = src; ptr != NULL; ptr = ptr->next)
+    if (strcasecmp (ptr->name, name) == 0)
+      return (ptr);
+
+  return (NULL);
+} /* }}} mb_data_t *data_get_by_name */
+
+static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
+{
+  mb_data_t *ptr;
+
+  if ((dst == NULL) || (src == NULL))
+    return (EINVAL);
+
+  ptr = *dst;
+
+  if (ptr == NULL)
+  {
+    *dst = src;
+    return (0);
+  }
+
+  while (ptr->next != NULL)
+    ptr = ptr->next;
+
+  ptr->next = src;
+
+  return (0);
+} /* }}} int data_append */
+
+/* Copy a single mb_data_t and append it to another list. */
+static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
+{
+  mb_data_t *tmp;
+  int status;
+
+  if ((dst == NULL) || (src == NULL))
+    return (EINVAL);
+
+  tmp = malloc (sizeof (*tmp));
+  if (tmp == NULL)
+    return (ENOMEM);
+  memcpy (tmp, src, sizeof (*tmp));
+  tmp->name = NULL;
+  tmp->next = NULL;
+
+  tmp->name = strdup (src->name);
+  if (tmp->name == NULL)
+  {
+    sfree (tmp);
+    return (ENOMEM);
+  }
+
+  status = data_append (dst, tmp);
+  if (status != 0)
+  {
+    sfree (tmp->name);
+    sfree (tmp);
+    return (status);
+  }
+
+  return (0);
+} /* }}} int data_copy */
+
+/* Lookup a single mb_data_t instance, copy it and append the copy to another
+ * list. */
+static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
+    const char *name)
+{
+  mb_data_t *ptr;
+
+  if ((dst == NULL) || (src == NULL) || (name == NULL))
+    return (EINVAL);
+
+  ptr = data_get_by_name (src, name);
+  if (ptr == NULL)
+    return (ENOENT);
+
+  return (data_copy (dst, ptr));
+} /* }}} int data_copy_by_name */
+
+/* Read functions */
+
+static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
+    mb_data_t *data, value_t value)
+{
+  value_list_t vl = VALUE_LIST_INIT;
+
+  if ((host == NULL) || (slave == NULL) || (data == NULL))
+    return (EINVAL);
+
+  if (host->interval <= 0)
+    host->interval = interval_g;
+
+  if (slave->instance[0] == 0)
+    ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
+        slave->id);
+
+  vl.values = &value;
+  vl.values_len = 1;
+  vl.interval = host->interval;
+  sstrncpy (vl.host, host->host, sizeof (vl.host));
+  sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
+  sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, data->type, sizeof (vl.type));
+  sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
+
+  return (plugin_dispatch_values (&vl));
+} /* }}} int mb_submit */
+
+static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
+{
+  union
+  {
+    uint8_t b[4];
+    float f;
+  } conv;
+
+#if BYTE_ORDER == LITTLE_ENDIAN
+  /* little endian */
+  conv.b[0] = lo & 0x00ff;
+  conv.b[1] = (lo >> 8) & 0x00ff;
+  conv.b[2] = hi & 0x00ff;
+  conv.b[3] = (hi >> 8) & 0x00ff;
+#else
+  conv.b[3] = lo & 0x00ff;
+  conv.b[2] = (lo >> 8) & 0x00ff;
+  conv.b[1] = hi & 0x00ff;
+  conv.b[0] = (hi >> 8) & 0x00ff;
+#endif
+
+  return (conv.f);
+} /* }}} float mb_register_to_float */
+
+static int mb_init_connection (mb_host_t *host) /* {{{ */
+{
+  int status;
+
+  if (host == NULL)
+    return (EINVAL);
+
+  if (host->is_connected)
+    return (0);
+
+  /* Only reconnect once per interval. */
+  if (host->have_reconnected)
+    return (-1);
+
+  modbus_set_debug (&host->connection, 1);
+
+#if 0
+  /* We'll do the error handling ourselves. */
+  modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
+#endif
+
+  if ((host->port < 1) || (host->port > 65535))
+    host->port = MODBUS_TCP_DEFAULT_PORT;
+
+  DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
+      host->node, host->port);
+
+  modbus_init_tcp (&host->connection,
+      /* host = */ host->node);
+#if 0
+      /* port = */ host->port);
+#endif
+
+  status = modbus_connect (&host->connection);
+  if (status != 0)
+  {
+    ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
+        host->node, host->port, status);
+    return (status);
+  }
+
+  host->is_connected = 1;
+  host->have_reconnected = 1;
+  return (0);
+} /* }}} int mb_init_connection */
+
+#define CAST_TO_VALUE_T(ds,vt,raw) do { \
+  if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
+    (vt).counter = (counter_t) (raw); \
+  else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
+    (vt).gauge = (gauge_t) (raw); \
+  else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
+    (vt).derive = (derive_t) (raw); \
+  else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
+    (vt).absolute = (absolute_t) (raw); \
+} while (0)
+
+static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
+    mb_data_t *data)
+{
+  int values[2];
+  int values_num;
+  const data_set_t *ds;
+  int status;
+  int i;
+
+  if ((host == NULL) || (slave == NULL) || (data == NULL))
+    return (EINVAL);
+
+  ds = plugin_get_ds (data->type);
+  if (ds == NULL)
+  {
+    ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
+    return (-1);
+  }
+
+  if (ds->ds_num != 1)
+  {
+    ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
+        "I can only handle data sets with only one data source.",
+        data->type, ds->ds_num);
+    return (-1);
+  }
+
+  if ((ds->ds[0].type != DS_TYPE_GAUGE)
+      && (data->register_type != REG_TYPE_UINT32))
+  {
+    NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
+        "This will most likely result in problems, because the register type "
+        "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
+  }
+
+  memset (values, 0, sizeof (values));
+  if ((data->register_type == REG_TYPE_UINT32)
+      || (data->register_type == REG_TYPE_FLOAT))
+    values_num = 2;
+  else
+    values_num = 1;
+
+  for (i = 0; i < 2; i++)
+  {
+    status = read_holding_registers (&host->connection,
+        /* slave = */ slave->id, /* start_addr = */ data->register_base,
+        /* num_registers = */ values_num, /* buffer = */ values);
+    if (status > 0)
+      break;
+
+    if (host->is_connected)
+      modbus_close (&host->connection);
+    host->is_connected = 0;
+
+    /* If we already tried reconnecting this round, give up. */
+    if (host->have_reconnected)
+    {
+      ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
+          "Reconnecting has already been tried. Giving up.", host->host);
+      return (-1);
+    }
+
+    /* Maybe the device closed the connection during the waiting interval.
+     * Try re-establishing the connection. */
+    status = mb_init_connection (host);
+    if (status != 0)
+    {
+      ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
+          "While trying to reconnect, connecting to \"%s\" failed. "
+          "Giving up.",
+          host->host, host->node);
+      return (-1);
+    }
+
+    DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
+
+    /* try again */
+    continue;
+  } /* for (i = 0, 1) */
+
+  DEBUG ("Modbus plugin: mb_read_data: Success! "
+      "read_holding_registers returned with status %i.", status);
+
+  if (data->register_type == REG_TYPE_FLOAT)
+  {
+    float float_value;
+    value_t vt;
+
+    float_value = mb_register_to_float (values[0], values[1]);
+    DEBUG ("Modbus plugin: mb_read_data: "
+        "Returned float value is %g", (double) float_value);
+
+    CAST_TO_VALUE_T (ds, vt, float_value);
+    mb_submit (host, slave, data, vt);
+  }
+  else if (data->register_type == REG_TYPE_UINT32)
+  {
+    uint32_t v32;
+    value_t vt;
+
+    v32 = (values[0] << 16) | values[1];
+    DEBUG ("Modbus plugin: mb_read_data: "
+        "Returned uint32 value is %"PRIu32, v32);
+
+    CAST_TO_VALUE_T (ds, vt, v32);
+    mb_submit (host, slave, data, vt);
+  }
+  else /* if (data->register_type == REG_TYPE_UINT16) */
+  {
+    value_t vt;
+
+    DEBUG ("Modbus plugin: mb_read_data: "
+        "Returned uint16 value is %"PRIu16, values[0]);
+
+    CAST_TO_VALUE_T (ds, vt, values[0]);
+    mb_submit (host, slave, data, vt);
+  }
+
+  return (0);
+} /* }}} int mb_read_data */
+
+static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
+{
+  mb_data_t *data;
+  int success;
+  int status;
+
+  if ((host == NULL) || (slave == NULL))
+    return (EINVAL);
+
+  success = 0;
+  for (data = slave->collect; data != NULL; data = data->next)
+  {
+    status = mb_read_data (host, slave, data);
+    if (status == 0)
+      success++;
+  }
+
+  if (success == 0)
+    return (-1);
+  else
+    return (0);
+} /* }}} int mb_read_slave */
+
+static int mb_read (user_data_t *user_data) /* {{{ */
+{
+  mb_host_t *host;
+  size_t i;
+  int success;
+  int status;
+
+  if ((user_data == NULL) || (user_data->data == NULL))
+    return (EINVAL);
+
+  host = user_data->data;
+
+  /* Clear the reconnect flag. */
+  host->have_reconnected = 0;
+
+  success = 0;
+  for (i = 0; i < host->slaves_num; i++)
+  {
+    status = mb_read_slave (host, host->slaves + i);
+    if (status == 0)
+      success++;
+  }
+
+  if (success == 0)
+    return (-1);
+  else
+    return (0);
+} /* }}} int mb_read */
+
+/* Free functions */
+
+static void data_free_one (mb_data_t *data) /* {{{ */
+{
+  if (data == NULL)
+    return;
+
+  sfree (data->name);
+  sfree (data);
+} /* }}} void data_free_one */
+
+static void data_free_all (mb_data_t *data) /* {{{ */
+{
+  mb_data_t *next;
+
+  if (data == NULL)
+    return;
+
+  next = data->next;
+  data_free_one (data);
+
+  data_free_all (next);
+} /* }}} void data_free_all */
+
+static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
+{
+  size_t i;
+
+  if (slaves == NULL)
+    return;
+
+  for (i = 0; i < slaves_num; i++)
+    data_free_all (slaves[i].collect);
+  sfree (slaves);
+} /* }}} void slaves_free_all */
+
+static void host_free (void *void_host) /* {{{ */
+{
+  mb_host_t *host = void_host;
+
+  if (host == NULL)
+    return;
+
+  slaves_free_all (host->slaves, host->slaves_num);
+  sfree (host);
+} /* }}} void host_free */
+
+/* Config functions */
+
+static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
+{
+  mb_data_t data;
+  int status;
+  int i;
+
+  memset (&data, 0, sizeof (data));
+  data.name = NULL;
+  data.register_type = REG_TYPE_UINT16;
+  data.next = NULL;
+
+  status = cf_util_get_string (ci, &data.name);
+  if (status != 0)
+    return (status);
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+    status = 0;
+
+    if (strcasecmp ("Type", child->key) == 0)
+      status = cf_util_get_string_buffer (child,
+          data.type, sizeof (data.type));
+    else if (strcasecmp ("Instance", child->key) == 0)
+      status = cf_util_get_string_buffer (child,
+          data.instance, sizeof (data.instance));
+    else if (strcasecmp ("RegisterBase", child->key) == 0)
+      status = cf_util_get_int (child, &data.register_base);
+    else if (strcasecmp ("RegisterType", child->key) == 0)
+    {
+      char tmp[16];
+      status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
+      if (status != 0)
+        /* do nothing */;
+      else if (strcasecmp ("Uint16", tmp) == 0)
+        data.register_type = REG_TYPE_UINT16;
+      else if (strcasecmp ("Uint32", tmp) == 0)
+        data.register_type = REG_TYPE_UINT32;
+      else if (strcasecmp ("Float", tmp) == 0)
+        data.register_type = REG_TYPE_FLOAT;
+      else
+      {
+        ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
+        status = -1;
+      }
+    }
+    else
+    {
+      ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (i = 0; i < ci->children_num; i++) */
+
+  assert (data.name != NULL);
+  if (data.type[0] == 0)
+  {
+    ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
+        data.name);
+    status = -1;
+  }
+
+  if (status == 0)
+    data_copy (&data_definitions, &data);
+
+  sfree (data.name);
+
+  return (status);
+} /* }}} int mb_config_add_data */
+
+static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
+    const char *address)
+{
+  struct addrinfo *ai_list;
+  struct addrinfo *ai_ptr;
+  struct addrinfo  ai_hints;
+  int status;
+
+  if ((host == NULL) || (address == NULL))
+    return (EINVAL);
+
+  memset (&ai_hints, 0, sizeof (ai_hints));
+#if AI_ADDRCONFIG
+  ai_hints.ai_flags |= AI_ADDRCONFIG;
+#endif
+  /* XXX: libmodbus can only handle IPv4 addresses. */
+  ai_hints.ai_family = AF_INET;
+  ai_hints.ai_addr = NULL;
+  ai_hints.ai_canonname = NULL;
+  ai_hints.ai_next = NULL;
+
+  ai_list = NULL;
+  status = getaddrinfo (address, /* service = */ NULL,
+      &ai_hints, &ai_list);
+  if (status != 0)
+  {
+    char errbuf[1024];
+    ERROR ("Modbus plugin: getaddrinfo failed: %s",
+        (status == EAI_SYSTEM)
+        ? sstrerror (errno, errbuf, sizeof (errbuf))
+        : gai_strerror (status));
+    return (status);
+  }
+
+  for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
+  {
+    status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
+        host->node, sizeof (host->node),
+        /* service = */ NULL, /* length = */ 0,
+        /* flags = */ NI_NUMERICHOST);
+    if (status == 0)
+      break;
+  } /* for (ai_ptr) */
+
+  freeaddrinfo (ai_list);
+
+  if (status != 0)
+    ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
+  else /* if (status == 0) */
+  {
+    DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
+        address, host->node);
+  }
+
+  return (status);
+} /* }}} int mb_config_set_host_address */
+
+static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
+{
+  mb_slave_t *slave;
+  int status;
+  int i;
+
+  if ((host == NULL) || (ci == NULL))
+    return (EINVAL);
+
+  slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
+  if (slave == NULL)
+    return (ENOMEM);
+  host->slaves = slave;
+  slave = host->slaves + host->slaves_num;
+  memset (slave, 0, sizeof (*slave));
+  slave->collect = NULL;
+
+  status = cf_util_get_int (ci, &slave->id);
+  if (status != 0)
+    return (status);
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+    status = 0;
+
+    if (strcasecmp ("Instance", child->key) == 0)
+      status = cf_util_get_string_buffer (child,
+          slave->instance, sizeof (slave->instance));
+    else if (strcasecmp ("Collect", child->key) == 0)
+    {
+      char buffer[1024];
+      status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
+      if (status == 0)
+        data_copy_by_name (&slave->collect, data_definitions, buffer);
+      status = 0; /* continue after failure. */
+    }
+    else
+    {
+      ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  }
+
+  if ((status == 0) && (slave->collect == NULL))
+    status = EINVAL;
+
+  if (slave->id < 0)
+    status = EINVAL;
+
+  if (status == 0)
+    host->slaves_num++;
+  else /* if (status != 0) */
+    data_free_all (slave->collect);
+
+  return (status);
+} /* }}} int mb_config_add_slave */
+
+static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
+{
+  mb_host_t *host;
+  int status;
+  int i;
+
+  host = malloc (sizeof (*host));
+  if (host == NULL)
+    return (ENOMEM);
+  memset (host, 0, sizeof (*host));
+  host->slaves = NULL;
+
+  status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
+  if (status != 0)
+    return (status);
+  if (host->host[0] == 0)
+    return (EINVAL);
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+    status = 0;
+
+    if (strcasecmp ("Address", child->key) == 0)
+    {
+      char buffer[NI_MAXHOST];
+      status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
+      if (status == 0)
+        status = mb_config_set_host_address (host, buffer);
+    }
+    else if (strcasecmp ("Port", child->key) == 0)
+    {
+      host->port = cf_util_get_port_number (child);
+      if (host->port <= 0)
+        status = -1;
+    }
+    else if (strcasecmp ("Interval", child->key) == 0)
+      status = cf_util_get_int (child, &host->interval);
+    else if (strcasecmp ("Slave", child->key) == 0)
+      /* Don't set status: Gracefully continue if a slave fails. */
+      mb_config_add_slave (host, child);
+    else
+    {
+      ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  } /* for (i = 0; i < ci->children_num; i++) */
+
+  assert (host->host[0] != 0);
+  if (host->host[0] == 0)
+  {
+    ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
+        host->host);
+    status = -1;
+  }
+
+  if (status == 0)
+  {
+    user_data_t ud;
+    char name[1024];
+    struct timespec interval;
+
+    ud.data = host;
+    ud.free_func = host_free;
+
+    ssnprintf (name, sizeof (name), "modbus-%s", host->host);
+
+    interval.tv_nsec = 0;
+    if (host->interval > 0)
+      interval.tv_sec = host->interval;
+    else
+      interval.tv_sec = 0;
+
+    plugin_register_complex_read (/* group = */ NULL, name,
+        mb_read, (interval.tv_sec > 0) ? &interval : NULL, &ud);
+  }
+  else
+  {
+    host_free (host);
+  }
+
+  return (status);
+} /* }}} int mb_config_add_host */
+
+static int mb_config (oconfig_item_t *ci) /* {{{ */
+{
+  int i;
+
+  if (ci == NULL)
+    return (EINVAL);
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Data", child->key) == 0)
+      mb_config_add_data (child);
+    else if (strcasecmp ("Host", child->key) == 0)
+      mb_config_add_host (child);
+    else
+      ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
+  }
+
+  return (0);
+} /* }}} int mb_config */
+
+/* ========= */
+
+static int mb_shutdown (void) /* {{{ */
+{
+  data_free_all (data_definitions);
+  data_definitions = NULL;
+
+  return (0);
+} /* }}} int mb_shutdown */
+
+void module_register (void)
+{
+  plugin_register_complex_config ("modbus", mb_config);
+  plugin_register_shutdown ("modbus", mb_shutdown);
+} /* void module_register */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
index 1048511..c7b796b 100644 (file)
@@ -342,7 +342,8 @@ static int mysql_config (oconfig_item_t *ci) /* {{{ */
                else
                        sstrncpy (cb_name, "mysql", sizeof (cb_name));
 
-               plugin_register_complex_read (cb_name, mysql_read,
+               plugin_register_complex_read (/* group = */ NULL, cb_name,
+                                             mysql_read,
                                              /* interval = */ NULL, &ud);
        }
        else
@@ -397,7 +398,7 @@ static MYSQL *getconnection (mysql_database_t *db)
        }
        else
        {
-               INFO ("mysql plugin: Sucessfully connected to database %s "
+               INFO ("mysql plugin: Successfully connected to database %s "
                                "at server %s (server version: %s, protocol version: %d)",
                                (db->database != NULL) ? db->database : "<none>",
                                mysql_get_host_info (db->con),
index ad19c6d..317b0fe 100644 (file)
@@ -837,7 +837,8 @@ static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /*
        if (instances == NULL)
        {
                ERROR ("netapp plugin: cna_handle_wafl_data: "
-                               "na_elem_child (\"instances\") failed.");
+                               "na_elem_child (\"instances\") failed "
+                               "for host %s.", hostname);
                return (-1);
        }
 
@@ -845,7 +846,8 @@ static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /*
        if (plugin_inst == NULL)
        {
                ERROR ("netapp plugin: cna_handle_wafl_data: "
-                               "na_child_get_string (\"name\") failed.");
+                               "na_child_get_string (\"name\") failed "
+                               "for host %s.", hostname);
                return (-1);
        }
 
@@ -892,7 +894,8 @@ static int cna_handle_wafl_data (const char *hostname, cfg_wafl_t *cfg_wafl, /*
                        perf_data.flags |= HAVE_WAFL_INODE_CACHE_MISS;
                } else {
                        DEBUG("netapp plugin: cna_handle_wafl_data: "
-                                       "Found unexpected child: %s", name);
+                                       "Found unexpected child: %s "
+                                       "for host %s.", name, hostname);
                }
        }
 
@@ -964,8 +967,8 @@ static int cna_query_wafl (host_config_t *host) /* {{{ */
        data = na_server_invoke_elem(host->srv, host->cfg_wafl->query);
        if (na_results_status (data) != NA_OK)
        {
-               ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed: %s",
-                               na_results_reason (data));
+               ERROR ("netapp plugin: cna_query_wafl: na_server_invoke_elem failed for host %s: %s",
+                               host->name, na_results_reason (data));
                na_elem_free (data);
                return (-1);
        }
@@ -998,7 +1001,8 @@ static int cna_handle_disk_data (const char *hostname, /* {{{ */
        if (instances == NULL)
        {
                ERROR ("netapp plugin: cna_handle_disk_data: "
-                               "na_elem_child (\"instances\") failed.");
+                               "na_elem_child (\"instances\") failed "
+                               "for host %s.", hostname);
                return (-1);
        }
 
@@ -1158,8 +1162,8 @@ static int cna_query_disk (host_config_t *host) /* {{{ */
        data = na_server_invoke_elem(host->srv, host->cfg_disk->query);
        if (na_results_status (data) != NA_OK)
        {
-               ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed: %s",
-                               na_results_reason (data));
+               ERROR ("netapp plugin: cna_query_disk: na_server_invoke_elem failed for host %s: %s",
+                               host->name, na_results_reason (data));
                na_elem_free (data);
                return (-1);
        }
@@ -1188,7 +1192,8 @@ static int cna_handle_volume_perf_data (const char *hostname, /* {{{ */
        if (elem_instances == NULL)
        {
                ERROR ("netapp plugin: handle_volume_perf_data: "
-                               "na_elem_child (\"instances\") failed.");
+                               "na_elem_child (\"instances\") failed "
+                               "for host %s.", hostname);
                return (-1);
        }
 
@@ -1328,8 +1333,8 @@ static int cna_query_volume_perf (host_config_t *host) /* {{{ */
        data = na_server_invoke_elem (host->srv, host->cfg_volume_perf->query);
        if (na_results_status (data) != NA_OK)
        {
-               ERROR ("netapp plugin: cna_query_volume_perf: na_server_invoke_elem failed: %s",
-                               na_results_reason (data));
+               ERROR ("netapp plugin: cna_query_volume_perf: na_server_invoke_elem failed for host %s: %s",
+                               host->name, na_results_reason (data));
                na_elem_free (data);
                return (-1);
        }
@@ -1384,8 +1389,8 @@ static int cna_submit_volume_usage_data (const char *hostname, /* {{{ */
                        else
                        {
                                ERROR ("netapp plugin: (norm_used = %"PRIu64") < (snap_norm_used = "
-                                               "%"PRIu64"). Invalidating both.",
-                                               norm_used, snap_norm_used);
+                                               "%"PRIu64") for host %s. Invalidating both.",
+                                               norm_used, snap_norm_used, hostname);
                                v->flags &= ~(HAVE_VOLUME_USAGE_NORM_USED | HAVE_VOLUME_USAGE_SNAP_USED);
                        }
                }
@@ -1470,8 +1475,8 @@ static void cna_handle_volume_snap_usage(const host_config_t *host, /* {{{ */
                                cna_change_volume_status (host->name, v);
                } else {
                        ERROR ("netapp plugin: cna_handle_volume_snap_usage: na_server_invoke_elem for "
-                                       "volume \"%s\" failed with error %d: %s", v->name,
-                                       na_results_errno(data), na_results_reason(data));
+                                       "volume \"%s\" on host %s failed with error %d: %s", v->name,
+                                       host->name, na_results_errno(data), na_results_reason(data));
                }
                na_elem_free(data);
                return;
@@ -1484,7 +1489,8 @@ static void cna_handle_volume_snap_usage(const host_config_t *host, /* {{{ */
        if (elem_snapshots == NULL)
        {
                ERROR ("netapp plugin: cna_handle_volume_snap_usage: "
-                               "na_elem_child (\"snapshots\") failed.");
+                               "na_elem_child (\"snapshots\") failed "
+                               "for host %s.", host->name);
                na_elem_free(data);
                return;
        }
@@ -1518,7 +1524,8 @@ static int cna_handle_volume_usage_data (const host_config_t *host, /* {{{ */
        if (elem_volumes == NULL)
        {
                ERROR ("netapp plugin: cna_handle_volume_usage_data: "
-                               "na_elem_child (\"volumes\") failed.");
+                               "na_elem_child (\"volumes\") failed "
+                               "for host %s.", host->name);
                return (-1);
        }
 
@@ -1690,8 +1697,8 @@ static int cna_query_volume_usage (host_config_t *host) /* {{{ */
        data = na_server_invoke_elem(host->srv, host->cfg_volume_usage->query);
        if (na_results_status (data) != NA_OK)
        {
-               ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed: %s",
-                               na_results_reason (data));
+               ERROR ("netapp plugin: cna_query_volume_usage: na_server_invoke_elem failed for host %s: %s",
+                               host->name, na_results_reason (data));
                na_elem_free (data);
                return (-1);
        }
@@ -1727,7 +1734,8 @@ static int cna_handle_system_data (const char *hostname, /* {{{ */
        if (instances == NULL)
        {
                ERROR ("netapp plugin: cna_handle_system_data: "
-                               "na_elem_child (\"instances\") failed.");
+                               "na_elem_child (\"instances\") failed "
+                               "for host %s.", hostname);
                return (-1);
        }
 
@@ -1735,7 +1743,8 @@ static int cna_handle_system_data (const char *hostname, /* {{{ */
        if (instance == NULL)
        {
                ERROR ("netapp plugin: cna_handle_system_data: "
-                               "na_child_get_string (\"name\") failed.");
+                               "na_child_get_string (\"name\") failed "
+                               "for host %s.", hostname);
                return (-1);
        }
 
@@ -1847,8 +1856,8 @@ static int cna_query_system (host_config_t *host) /* {{{ */
        data = na_server_invoke_elem(host->srv, host->cfg_system->query);
        if (na_results_status (data) != NA_OK)
        {
-               ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed: %s",
-                               na_results_reason (data));
+               ERROR ("netapp plugin: cna_query_system: na_server_invoke_elem failed for host %s: %s",
+                               host->name, na_results_reason (data));
                na_elem_free (data);
                return (-1);
        }
@@ -2550,7 +2559,7 @@ static int cna_config (oconfig_item_t *ci) { /* {{{ */
                        ud.data = host;
                        ud.free_func = (void (*) (void *)) free_host_config;
 
-                       plugin_register_complex_read (cb_name,
+                       plugin_register_complex_read (/* group = */ NULL, cb_name,
                                        /* callback  = */ cna_read, 
                                        /* interval  = */ (host->interval > 0) ? &interval : NULL,
                                        /* user data = */ &ud);
index 1b45375..457637b 100644 (file)
@@ -4,17 +4,18 @@
  * Copyright (C) 2009       Aman Gupta
  *
  * 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.
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; only version 2.1 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.
+ * Lesser 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
+ * You should have received a copy of the GNU Lesser 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>
@@ -51,6 +52,9 @@
 #if HAVE_POLL_H
 # include <poll.h>
 #endif
+#if HAVE_NET_IF_H
+# include <net/if.h>
+#endif
 
 #if HAVE_LIBGCRYPT
 # include <gcrypt.h>
@@ -117,6 +121,7 @@ typedef struct sockent
 
        char *node;
        char *service;
+       int interface;
 
        union
        {
@@ -345,7 +350,8 @@ static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
   return (!received);
 } /* }}} _Bool check_send_okay */
 
-static int network_dispatch_values (value_list_t *vl) /* {{{ */
+static int network_dispatch_values (value_list_t *vl, /* {{{ */
+    const char *username)
 {
   int status;
 
@@ -386,6 +392,18 @@ static int network_dispatch_values (value_list_t *vl) /* {{{ */
     return (status);
   }
 
+  if (username != NULL)
+  {
+    status = meta_data_add_string (vl->meta, "network:username", username);
+    if (status != 0)
+    {
+      ERROR ("network plugin: meta_data_add_string failed.");
+      meta_data_destroy (vl->meta);
+      vl->meta = NULL;
+      return (status);
+    }
+  }
+
   plugin_dispatch_values (vl);
   stats_values_dispatched++;
 
@@ -739,11 +757,11 @@ static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
                    break;
 
                  default:
-                   sfree (pkg_types);
-                   sfree (pkg_values);
                    NOTICE ("network plugin: parse_part_values: "
                        "Don't know how to handle data source type %"PRIu8,
                        pkg_types[i]);
+                   sfree (pkg_types);
+                   sfree (pkg_values);
                    return (-1);
                } /* switch (pkg_types[i]) */
        }
@@ -887,7 +905,8 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
 #define PP_SIGNED    0x01
 #define PP_ENCRYPTED 0x02
 static int parse_packet (sockent_t *se,
-               void *buffer, size_t buffer_size, int flags);
+               void *buffer, size_t buffer_size, int flags,
+               const char *username);
 
 #define BUFFER_READ(p,s) do { \
   memcpy ((p), buffer + buffer_offset, (s)); \
@@ -982,6 +1001,8 @@ static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
   {
     ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
     gcry_md_close (hd);
+    sfree (secret);
+    sfree (pss.username);
     return (-1);
   }
 
@@ -1003,9 +1024,6 @@ static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
   gcry_md_close (hd);
   hd = NULL;
 
-  sfree (secret);
-  sfree (pss.username);
-
   if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
   {
     WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
@@ -1014,9 +1032,12 @@ static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
   else
   {
     parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
-        flags | PP_SIGNED);
+        flags | PP_SIGNED, pss.username);
   }
 
+  sfree (secret);
+  sfree (pss.username);
+
   *ret_buffer = buffer + buffer_len;
   *ret_buffer_len = 0;
 
@@ -1060,7 +1081,8 @@ static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
     warning_has_been_printed = 1;
   }
 
-  parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
+  parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
+      /* username = */ NULL);
 
   *ret_buffer = buffer + buffer_size;
   *ret_buffer_size = 0;
@@ -1139,7 +1161,10 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
   cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
       pea.username);
   if (cypher == NULL)
+  {
+    sfree (pea.username);
     return (-1);
+  }
 
   payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
   assert (payload_len > 0);
@@ -1151,6 +1176,7 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
       /* in = */ NULL, /* in len = */ 0);
   if (err != 0)
   {
+    sfree (pea.username);
     ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
         gcry_strerror (err));
     return (-1);
@@ -1169,17 +1195,22 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
       buffer + buffer_offset, payload_len);
   if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
   {
+    sfree (pea.username);
     ERROR ("network plugin: Decryption failed: Checksum mismatch.");
     return (-1);
   }
 
   parse_packet (se, buffer + buffer_offset, payload_len,
-      flags | PP_ENCRYPTED);
+      flags | PP_ENCRYPTED, pea.username);
+
+  /* XXX: Free pea.username?!? */
 
   /* Update return values */
   *ret_buffer =     buffer     + part_size;
   *ret_buffer_len = buffer_len - part_size;
 
+  sfree (pea.username);
+
   return (0);
 } /* }}} int parse_part_encr_aes256 */
 /* #endif HAVE_LIBGCRYPT */
@@ -1234,7 +1265,8 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
 #undef BUFFER_READ
 
 static int parse_packet (sockent_t *se, /* {{{ */
-               void *buffer, size_t buffer_size, int flags)
+               void *buffer, size_t buffer_size, int flags,
+               const char *username)
 {
        int status;
 
@@ -1334,7 +1366,7 @@ static int parse_packet (sockent_t *se, /* {{{ */
                        if (status != 0)
                                break;
 
-                       network_dispatch_values (&vl);
+                       network_dispatch_values (&vl, username);
 
                        sfree (vl.values);
                }
@@ -1548,7 +1580,7 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
 
                if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
                                        &network_config_ttl,
-                                       sizeof (network_config_ttl)) == -1)
+                                       sizeof (network_config_ttl)) != 0)
                {
                        char errbuf[1024];
                        ERROR ("setsockopt: %s",
@@ -1569,7 +1601,7 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
 
                if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
                                        &network_config_ttl,
-                                       sizeof (network_config_ttl)) == -1)
+                                       sizeof (network_config_ttl)) != 0)
                {
                        char errbuf[1024];
                        ERROR ("setsockopt: %s",
@@ -1582,7 +1614,110 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
        return (0);
 } /* int network_set_ttl */
 
-static int network_bind_socket (int fd, const struct addrinfo *ai)
+static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
+{
+       DEBUG ("network plugin: network_set_interface: interface index = %i;",
+                       se->interface);
+
+        assert (se->type == SOCKENT_TYPE_CLIENT);
+
+       if (ai->ai_family == AF_INET)
+       {
+               struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
+
+               if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
+               {
+#if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
+                       /* If possible, use the "ip_mreqn" structure which has
+                        * an "interface index" member. Using the interface
+                        * index is preferred here, because of its similarity
+                        * to the way IPv6 handles this. Unfortunately, it
+                        * appears not to be portable. */
+                       struct ip_mreqn mreq;
+
+                       memset (&mreq, 0, sizeof (mreq));
+                       mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
+                       mreq.imr_address.s_addr = ntohl (INADDR_ANY);
+                       mreq.imr_ifindex = se->interface;
+#else
+                       struct ip_mreq mreq;
+
+                       memset (&mreq, 0, sizeof (mreq));
+                       mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
+                       mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
+#endif
+
+                       if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
+                                               &mreq, sizeof (mreq)) != 0)
+                       {
+                               char errbuf[1024];
+                               ERROR ("setsockopt: %s",
+                                               sstrerror (errno, errbuf, sizeof (errbuf)));
+                               return (-1);
+                       }
+
+                       return (0);
+               }
+       }
+       else if (ai->ai_family == AF_INET6)
+       {
+               struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
+
+               if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
+               {
+                       if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
+                                               &se->interface,
+                                               sizeof (se->interface)) != 0)
+                       {
+                               char errbuf[1024];
+                               ERROR ("setsockopt: %s",
+                                               sstrerror (errno, errbuf,
+                                                       sizeof (errbuf)));
+                               return (-1);
+                       }
+
+                       return (0);
+               }
+       }
+
+       /* else: Not a multicast interface. */
+#if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
+       if (se->interface != 0)
+       {
+               char interface_name[IFNAMSIZ];
+
+               if (if_indextoname (se->interface, interface_name) == NULL)
+                       return (-1);
+
+               DEBUG ("network plugin: Binding socket to interface %s", interface_name);
+
+               if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
+                                       interface_name,
+                                       sizeof(interface_name)) == -1 )
+               {
+                       char errbuf[1024];
+                       ERROR ("setsockopt: %s",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+                       return (-1);
+               }
+       }
+/* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
+
+#else
+       WARNING ("network plugin: Cannot set the interface on a unicast "
+                       "socket because "
+# if !defined(SO_BINDTODEVICE)
+                       "the the \"SO_BINDTODEVICE\" socket option "
+# else
+                       "the \"if_indextoname\" function "
+# endif
+                       "is not available on your system.");
+#endif
+
+       return (0);
+} /* }}} network_set_interface */
+
+static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
 {
        int loop = 0;
        int yes  = 1;
@@ -1611,12 +1746,24 @@ static int network_bind_socket (int fd, const struct addrinfo *ai)
                struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
                if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
                {
+#if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
+                       struct ip_mreqn mreq;
+#else
                        struct ip_mreq mreq;
+#endif
 
                        DEBUG ("fd = %i; IPv4 multicast address found", fd);
 
                        mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
-                       mreq.imr_interface.s_addr = htonl (INADDR_ANY);
+#if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
+                       /* Set the interface using the interface index if
+                        * possible (available). Unfortunately, the struct
+                        * ip_mreqn is not portable. */
+                       mreq.imr_address.s_addr = ntohl (INADDR_ANY);
+                       mreq.imr_ifindex = interface_idx;
+#else
+                       mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
+#endif
 
                        if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
                                                &loop, sizeof (loop)) == -1)
@@ -1637,6 +1784,8 @@ static int network_bind_socket (int fd, const struct addrinfo *ai)
                                                        sizeof (errbuf)));
                                return (-1);
                        }
+
+                       return (0);
                }
        }
        else if (ai->ai_family == AF_INET6)
@@ -1662,7 +1811,7 @@ static int network_bind_socket (int fd, const struct addrinfo *ai)
                         * single interface; programs running on
                         * multihomed hosts may need to join the same
                         * group on more than one interface.*/
-                       mreq.ipv6mr_interface = 0;
+                       mreq.ipv6mr_interface = interface_idx;
 
                        if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
                                                &loop, sizeof (loop)) == -1)
@@ -1683,9 +1832,36 @@ static int network_bind_socket (int fd, const struct addrinfo *ai)
                                                        sizeof (errbuf)));
                                return (-1);
                        }
+
+                       return (0);
                }
        }
 
+#if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
+       /* if a specific interface was set, bind the socket to it. But to avoid
+        * possible problems with multicast routing, only do that for non-multicast
+        * addresses */
+       if (interface_idx != 0)
+       {
+               char interface_name[IFNAMSIZ];
+
+               if (if_indextoname (interface_idx, interface_name) == NULL)
+                       return (-1);
+
+               DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
+
+               if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
+                                       interface_name,
+                                       sizeof(interface_name)) == -1 )
+               {
+                       char errbuf[1024];
+                       ERROR ("setsockopt: %s",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+                       return (-1);
+               }
+       }
+#endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
+
        return (0);
 } /* int network_bind_socket */
 
@@ -1701,6 +1877,7 @@ static int sockent_init (sockent_t *se, int type) /* {{{ */
        se->type = SOCKENT_TYPE_CLIENT;
        se->node = NULL;
        se->service = NULL;
+       se->interface = 0;
        se->next = NULL;
 
        if (type == SOCKENT_TYPE_SERVER)
@@ -1849,7 +2026,7 @@ static int sockent_open (sockent_t *se) /* {{{ */
                                continue;
                        }
 
-                       status = network_bind_socket (*tmp, ai_ptr);
+                       status = network_bind_socket (*tmp, ai_ptr, se->interface);
                        if (status != 0)
                        {
                                close (*tmp);
@@ -1889,6 +2066,7 @@ static int sockent_open (sockent_t *se) /* {{{ */
                        se->data.client.addrlen = ai_ptr->ai_addrlen;
 
                        network_set_ttl (se, ai_ptr);
+                       network_set_interface (se, ai_ptr);
 
                        /* We don't open more than one write-socket per
                         * node/service pair.. */
@@ -2022,7 +2200,8 @@ static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
       continue;
     }
 
-    parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
+    parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
+       /* username = */ NULL);
     sfree (ent->data);
     sfree (ent);
   } /* while (42) */
@@ -2600,6 +2779,25 @@ static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
   return (0);
 } /* }}} int network_config_set_ttl */
 
+static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
+    int *interface)
+{
+  if ((ci->values_num != 1)
+      || (ci->values[0].type != OCONFIG_TYPE_STRING))
+  {
+    WARNING ("network plugin: The `Interface' config option needs exactly "
+        "one string argument.");
+    return (-1);
+  }
+
+  if (interface == NULL)
+    return (-1);
+
+  *interface = if_nametoindex (ci->values[0].value.string);
+
+  return (0);
+} /* }}} int network_config_set_interface */
+
 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
 {
   int tmp;
@@ -2711,6 +2909,10 @@ static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
           &se->data.server.security_level);
     else
 #endif /* HAVE_LIBGCRYPT */
+    if (strcasecmp ("Interface", child->key) == 0)
+      network_config_set_interface (child,
+          &se->interface);
+    else
     {
       WARNING ("network plugin: Option `%s' is not allowed here.",
           child->key);
@@ -2789,6 +2991,10 @@ static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
           &se->data.client.security_level);
     else
 #endif /* HAVE_LIBGCRYPT */
+    if (strcasecmp ("Interface", child->key) == 0)
+      network_config_set_interface (child,
+          &se->interface);
+    else
     {
       WARNING ("network plugin: Option `%s' is not allowed here.",
           child->key);
index cae0d63..462458c 100644 (file)
@@ -310,7 +310,7 @@ static int cow_init (void)
   if (ow_interval > 0)
     cb_interval.tv_sec = (time_t) ow_interval;
 
-  plugin_register_complex_read ("onewire", cow_read,
+  plugin_register_complex_read (/* group = */ NULL, "onewire", cow_read,
       &cb_interval, /* user data = */ NULL);
   plugin_register_shutdown ("onewire", cow_shutdown);
 
index 9f386f0..2aca414 100644 (file)
@@ -2,6 +2,8 @@
  * collectd - src/openvpn.c
  * Copyright (C) 2008  Doug MacEachern
  * Copyright (C) 2009  Florian octo Forster
+ * Copyright (C) 2009  Marco Chiappero
+ * Copyright (C) 2009  Fabian Schuh
  *
  * 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
  * Authors:
  *   Doug MacEachern <dougm at hyperic.com>
  *   Florian octo Forster <octo at verplant.org>
+ *   Marco Chiappero <marco at absence.it>
+ *   Fabian Schuh <mail at xeroc.org>
  **/
 
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 
-#define DEFAULT_STATUS_FILE "/etc/openvpn/openvpn-status.log"
-#define CLIENT_LIST_PREFIX  "CLIENT_LIST,"
+#define V1STRING "Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since\n"
+#define V2STRING "HEADER,CLIENT_LIST,Common Name,Real Address,Virtual Address,Bytes Received,Bytes Sent,Connected Since,Connected Since (time_t)\n"
+#define V3STRING "HEADER CLIENT_LIST Common Name Real Address Virtual Address Bytes Received Bytes Sent Connected Since Connected Since (time_t)\n"
+#define VSSTRING "OpenVPN STATISTICS\n"
 
-static char *status_file = NULL;
 
-/* For compression stats we need to convert these counters to a rate. */
-static counter_t pre_compress_old    = 0;
-static counter_t post_compress_old   = 0;
-static counter_t pre_decompress_old  = 0;
-static counter_t post_decompress_old = 0;
-static int compression_counter_valid = 0;
+struct vpn_status_s
+{
+       char *file;
+       enum
+       {
+               MULTI1 = 1, /* status-version 1 */
+               MULTI2,     /* status-version 2 */
+               MULTI3,     /* status-version 3 */
+               SINGLE = 10 /* currently no versions for single mode, maybe in the future */
+       } version;
+       char *name;
+};
+typedef struct vpn_status_s vpn_status_t;
+
+static vpn_status_t **vpn_list = NULL;
+static int vpn_num = 0;
+
+static _Bool new_naming_schema = 0;
+static _Bool collect_compression = 1;
+static _Bool collect_user_count  = 0;
+static _Bool collect_individual_users  = 1;
 
 static const char *config_keys[] =
 {
-       "StatusFile"
+       "StatusFile",
+       "Compression", /* old, deprecated name */
+       "ImprovedNamingSchema",
+       "CollectCompression",
+       "CollectUserCount",
+       "CollectIndividualUsers"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
-/* copy-n-pasted from common.c - changed delim to "," */
+
+/* Helper function
+ * copy-n-pasted from common.c - changed delim to ","  */
 static int openvpn_strsplit (char *string, char **fields, size_t size)
 {
-        size_t i;
-        char *ptr;
-        char *saveptr;
-
-        i = 0;
-        ptr = string;
-        saveptr = NULL;
-        while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
-        {
-                ptr = NULL;
-                i++;
+       size_t i;
+       char *ptr;
+       char *saveptr;
+
+       i = 0;
+       ptr = string;
+       saveptr = NULL;
+       while ((fields[i] = strtok_r (ptr, ",", &saveptr)) != NULL)
+       {
+               ptr = NULL;
+               i++;
 
-                if (i >= size)
-                        break;
-        }
+               if (i >= size)
+                       break;
+       }
 
-        return (i);
+       return (i);
 } /* int openvpn_strsplit */
 
-static void openvpn_submit (char *name, counter_t rx, counter_t tx)
+/* dispatches number of users */
+static void numusers_submit (char *pinst, char *tinst, gauge_t value)
+{
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].gauge = value;
+
+       vl.values = values;
+       vl.values_len = STATIC_ARRAY_SIZE (values);
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
+       sstrncpy (vl.type, "users", sizeof (vl.type));
+       if (pinst != NULL)
+               sstrncpy (vl.plugin_instance, pinst, sizeof (vl.plugin_instance));
+       if (tinst != NULL)
+               sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
+
+       plugin_dispatch_values (&vl);
+} /* void numusers_submit */
+
+/* dispatches stats about traffic (TCP or UDP) generated by the tunnel per single endpoint */
+static void iostats_submit (char *pinst, char *tinst, counter_t rx, counter_t tx)
 {
        value_t values[2];
        value_list_t vl = VALUE_LIST_INIT;
@@ -73,163 +122,608 @@ static void openvpn_submit (char *name, counter_t rx, counter_t tx)
        values[0].counter = rx;
        values[1].counter = tx;
 
+       /* NOTE ON THE NEW NAMING SCHEMA:
+        *       using plugin_instance to identify each vpn config (and
+        *       status) file; using type_instance to identify the endpoint
+        *       host when in multimode, traffic or overhead when in single.
+        */
+
        vl.values = values;
        vl.values_len = STATIC_ARRAY_SIZE (values);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
-       sstrncpy (vl.plugin_instance, name, sizeof (vl.plugin_instance));
+       if (pinst != NULL)
+               sstrncpy (vl.plugin_instance, pinst,
+                               sizeof (vl.plugin_instance));
        sstrncpy (vl.type, "if_octets", sizeof (vl.type));
+       if (tinst != NULL)
+               sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
 
        plugin_dispatch_values (&vl);
-} /* void openvpn_submit */
+} /* void traffic_submit */
 
-static void compression_submit (char *type_instance, gauge_t ratio)
+/* dispatches stats about data compression shown when in single mode */
+static void compression_submit (char *pinst, char *tinst,
+               counter_t uncompressed, counter_t compressed)
 {
-       value_t values[1];
+       value_t values[2];
        value_list_t vl = VALUE_LIST_INIT;
 
-       values[0].gauge = ratio;
+       values[0].counter = uncompressed;
+       values[1].counter = compressed;
 
        vl.values = values;
        vl.values_len = STATIC_ARRAY_SIZE (values);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "openvpn", sizeof (vl.plugin));
-       sstrncpy (vl.type, "compression_ratio", sizeof (vl.type));
-       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type));
+       if (pinst != NULL)
+               sstrncpy (vl.plugin_instance, pinst,
+                               sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, "compression", sizeof (vl.type));
+       if (tinst != NULL)
+               sstrncpy (vl.type_instance, tinst, sizeof (vl.type_instance));
 
        plugin_dispatch_values (&vl);
 } /* void compression_submit */
 
-static int openvpn_read (void)
+static int single_read (char *name, FILE *fh)
 {
-       FILE *fh;
        char buffer[1024];
-       char *fields[10];
+       char *fields[4];
        const int max_fields = STATIC_ARRAY_SIZE (fields);
-       int   fields_num;
+       int  fields_num, read = 0;
+
+       counter_t link_rx, link_tx;
+       counter_t tun_rx, tun_tx;
+       counter_t pre_compress, post_compress;
+       counter_t pre_decompress, post_decompress;
+       counter_t overhead_rx, overhead_tx;
+
+       link_rx = 0;
+       link_tx = 0;
+       tun_rx = 0;
+       tun_tx = 0;
+       pre_compress = 0;
+       post_compress = 0;
+       pre_decompress = 0;
+       post_decompress = 0;
+       overhead_rx = 0;
+       overhead_tx = 0;
 
-       counter_t pre_compress_new    = 0;
-       counter_t post_compress_new   = 0;
-       counter_t pre_decompress_new  = 0;
-       counter_t post_decompress_new = 0;
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
+       {
+               fields_num = openvpn_strsplit (buffer, fields, max_fields);
 
-       /* Clear the least significant four bits, just to make sure all four
-        * counters above are considered to be invalid. */
-       compression_counter_valid &= ~0x0f;
+               /* status file is generated by openvpn/sig.c:print_status()
+                * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/sig.c
+                *
+                * The line we're expecting has 2 fields. We ignore all lines
+                *  with more or less fields.
+                */
+               if (fields_num != 2)
+               {
+                       continue;
+               }
 
-       fh = fopen ((status_file != NULL)
-                       ? status_file
-                       : DEFAULT_STATUS_FILE, "r");
-       if (fh == NULL)
-               return (-1);
+               if (strcmp (fields[0], "TUN/TAP read bytes") == 0)
+               {
+                       /* read from the system and sent over the tunnel */
+                       tun_tx = atoll (fields[1]);
+               }
+               else if (strcmp (fields[0], "TUN/TAP write bytes") == 0)
+               {
+                       /* read from the tunnel and written in the system */
+                       tun_rx = atoll (fields[1]);
+               }
+               else if (strcmp (fields[0], "TCP/UDP read bytes") == 0)
+               {
+                       link_rx = atoll (fields[1]);
+               }
+               else if (strcmp (fields[0], "TCP/UDP write bytes") == 0)
+               {
+                       link_tx = atoll (fields[1]);
+               }
+               else if (strcmp (fields[0], "pre-compress bytes") == 0)
+               {
+                       pre_compress = atoll (fields[1]);
+               }
+               else if (strcmp (fields[0], "post-compress bytes") == 0)
+               {
+                       post_compress = atoll (fields[1]);
+               }
+               else if (strcmp (fields[0], "pre-decompress bytes") == 0)
+               {
+                       pre_decompress = atoll (fields[1]);
+               }
+               else if (strcmp (fields[0], "post-decompress bytes") == 0)
+               {
+                       post_decompress = atoll (fields[1]);
+               }
+       }
 
-        /* status file is generated by openvpn/multi.c:multi_print_status()
-        * this plugin requires server.conf: "status-version 2"
-         * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
-         */
+       iostats_submit (name, "traffic", link_rx, link_tx);
+
+       /* we need to force this order to avoid negative values with these unsigned */
+       overhead_rx = (((link_rx - pre_decompress) + post_decompress) - tun_rx);
+       overhead_tx = (((link_tx - post_compress) + pre_compress) - tun_tx);
+
+       iostats_submit (name, "overhead", overhead_rx, overhead_tx);
+
+       if (collect_compression)
+       {
+               compression_submit (name, "data_in", post_decompress, pre_decompress);
+               compression_submit (name, "data_out", pre_compress, post_compress);
+       }
+
+       read = 1;
+
+       return (read);
+} /* int single_read */
+
+/* for reading status version 1 */
+static int multi1_read (char *name, FILE *fh)
+{
+       char buffer[1024];
+       char *fields[10];
+       int  fields_num, read = 0, found_header = 0;
+       long long sum_users = 0;
+
+       /* read the file until the "ROUTING TABLE" line is found (no more info after) */
        while (fgets (buffer, sizeof (buffer), fh) != NULL)
        {
-               fields_num = openvpn_strsplit (buffer, fields, max_fields);
+               if (strcmp (buffer, "ROUTING TABLE\n") == 0)
+                       break;
 
-               /* Expect at least ``key,value''. */
-               if (fields_num < 2)
+               if (strcmp (buffer, V1STRING) == 0)
+               {
+                       found_header = 1;
                        continue;
+               }
 
-               if (strcmp (fields[0], "CLIENT_LIST") == 0)
-               {
-                       char *name;
-                       counter_t rx;
-                       counter_t tx;
+               /* skip the first lines until the client list section is found */
+               if (found_header == 0)
+                       /* we can't start reading data until this string is found */
+                       continue;
 
-                       /* The line we're expecting has 8 fields. We ignore all lines
-                        * with more or less fields. */
-                       if (fields_num != 8)
-                               continue;
+               fields_num = openvpn_strsplit (buffer,
+                               fields, STATIC_ARRAY_SIZE (fields));
+               if (fields_num < 4)
+                       continue;
 
-                       name =      fields[1];  /* "Common Name" */
-                       rx = atoll (fields[4]); /* "Bytes Received */
-                       tx = atoll (fields[5]); /* "Bytes Sent" */
-                       openvpn_submit (name, rx, tx);
+               if (collect_user_count)
+                       /* If so, sum all users, ignore the individuals*/
+               {
+                       sum_users += 1;
                }
-               else if (strcmp (fields[0], "pre-compress") == 0)
+               if (collect_individual_users)
                {
-                       pre_compress_new = atoll (fields[1]);
-                       compression_counter_valid |= 0x01;
+                       if (new_naming_schema)
+                       {
+                               iostats_submit (name,               /* vpn instance */
+                                               fields[0],          /* "Common Name" */
+                                               atoll (fields[2]),  /* "Bytes Received" */
+                                               atoll (fields[3])); /* "Bytes Sent" */
+                       }
+                       else
+                       {
+                               iostats_submit (fields[0],          /* "Common Name" */
+                                               NULL,               /* unused when in multimode */
+                                               atoll (fields[2]),  /* "Bytes Received" */
+                                               atoll (fields[3])); /* "Bytes Sent" */
+                       }
                }
-               else if (strcmp (fields[0], "post-compress") == 0)
+
+               read = 1;
+       }
+
+       if (collect_user_count)
+       {
+               numusers_submit(name, name, sum_users);
+               read = 1;
+       }
+
+       return (read);
+} /* int multi1_read */
+
+/* for reading status version 2 */
+static int multi2_read (char *name, FILE *fh)
+{
+       char buffer[1024];
+       char *fields[10];
+       const int max_fields = STATIC_ARRAY_SIZE (fields);
+       int  fields_num, read = 0;
+       long long sum_users    = 0;
+
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
+       {
+               fields_num = openvpn_strsplit (buffer, fields, max_fields);
+
+               /* status file is generated by openvpn/multi.c:multi_print_status()
+                * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
+                *
+                * The line we're expecting has 8 fields. We ignore all lines
+                *  with more or less fields.
+                */
+               if (fields_num != 8)
+                       continue;
+
+               if (strcmp (fields[0], "CLIENT_LIST") != 0)
+                       continue;
+
+               if (collect_user_count)
+                       /* If so, sum all users, ignore the individuals*/
                {
-                       post_compress_new = atoll (fields[1]);
-                       compression_counter_valid |= 0x02;
+                       sum_users += 1;
                }
-               else if (strcmp (fields[0], "pre-decompress") == 0)
+               if (collect_individual_users)
                {
-                       pre_decompress_new = atoll (fields[1]);
-                       compression_counter_valid |= 0x04;
+                       if (new_naming_schema)
+                       {
+                               /* plugin inst = file name, type inst = fields[1] */
+                               iostats_submit (name,               /* vpn instance */
+                                               fields[1],          /* "Common Name" */
+                                               atoll (fields[4]),  /* "Bytes Received" */
+                                               atoll (fields[5])); /* "Bytes Sent" */
+                       }
+                       else
+                       {
+                               /* plugin inst = fields[1], type inst = "" */
+                               iostats_submit (fields[1],          /* "Common Name" */
+                                               NULL,               /* unused when in multimode */
+                                               atoll (fields[4]),  /* "Bytes Received" */
+                                               atoll (fields[5])); /* "Bytes Sent" */
+                       }
                }
-               else if (strcmp (fields[0], "post-decompress") == 0)
+
+               read = 1;
+       }
+
+       if (collect_user_count)
+       {
+               numusers_submit(name, name, sum_users);
+               read = 1;
+       }
+
+       return (read);
+} /* int multi2_read */
+
+/* for reading status version 3 */
+static int multi3_read (char *name, FILE *fh)
+{
+       char buffer[1024];
+       char *fields[15];
+       const int max_fields = STATIC_ARRAY_SIZE (fields);
+       int  fields_num, read = 0;
+       long long sum_users    = 0;
+
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
+       {
+               fields_num = strsplit (buffer, fields, max_fields);
+
+               /* status file is generated by openvpn/multi.c:multi_print_status()
+                * http://svn.openvpn.net/projects/openvpn/trunk/openvpn/multi.c
+                *
+                * The line we're expecting has 12 fields. We ignore all lines
+                *  with more or less fields.
+                */
+               if (fields_num != 12)
                {
-                       post_decompress_new = atoll (fields[1]);
-                       compression_counter_valid |= 0x08;
+                       continue;
+               }
+               else
+               {
+                       if (strcmp (fields[0], "CLIENT_LIST") != 0)
+                               continue;
+
+                       if (collect_user_count)
+                               /* If so, sum all users, ignore the individuals*/
+                       {
+                               sum_users += 1;
+                       }
+
+                       if (collect_individual_users)
+                       {
+                               if (new_naming_schema)
+                               {
+                                       iostats_submit (name,               /* vpn instance */
+                                                       fields[1],          /* "Common Name" */
+                                                       atoll (fields[4]),  /* "Bytes Received" */
+                                                       atoll (fields[5])); /* "Bytes Sent" */
+                               }
+                               else
+                               {
+                                       iostats_submit (fields[1],          /* "Common Name" */
+                                                       NULL,               /* unused when in multimode */
+                                                       atoll (fields[4]),  /* "Bytes Received" */
+                                                       atoll (fields[5])); /* "Bytes Sent" */
+                               }
+                       }
+
+                       read = 1;
                }
        }
-       fclose (fh);
 
-       /* Check that all four counters are valid, {pre,post}_*_{old,new}. */
-       if ((compression_counter_valid & 0x33) == 0x33)
+       if (collect_user_count)
+       {
+               numusers_submit(name, name, sum_users);
+               read = 1;
+       }
+
+       return (read);
+} /* int multi3_read */
+
+/* read callback */
+static int openvpn_read (void)
+{
+       FILE *fh;
+       int  i, read;
+
+       read = 0;
+
+       /* call the right read function for every status entry in the list */
+       for (i = 0; i < vpn_num; i++)
        {
-               counter_t pre_diff;
-               counter_t post_diff;
+               fh = fopen (vpn_list[i]->file, "r");
+               if (fh == NULL)
+               {
+                       char errbuf[1024];
+                       WARNING ("openvpn plugin: fopen(%s) failed: %s", vpn_list[i]->file,
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
 
-               pre_diff = counter_diff (pre_compress_old, pre_compress_new);
-               post_diff = counter_diff (post_compress_old, post_compress_new);
+                       continue;
+               }
+
+               switch (vpn_list[i]->version)
+               {
+                       case SINGLE:
+                               read = single_read(vpn_list[i]->name, fh);
+                               break;
+
+                       case MULTI1:
+                               read = multi1_read(vpn_list[i]->name, fh);
+                               break;
+
+                       case MULTI2:
+                               read = multi2_read(vpn_list[i]->name, fh);
+                               break;
+
+                       case MULTI3:
+                               read = multi3_read(vpn_list[i]->name, fh);
+                               break;
+               }
 
-               /* If we compress, we're sending. */
-               compression_submit ("tx",
-                               ((gauge_t) post_diff) / ((gauge_t) pre_diff));
+               fclose (fh);
        }
 
-       /* Now check the other found counters. */
-       if ((compression_counter_valid & 0xcc) == 0xcc)
+       return (read ? 0 : -1);
+} /* int openvpn_read */
+
+static int version_detect (const char *filename)
+{
+       FILE *fh;
+       char buffer[1024];
+       int version = 0;
+
+       /* Sanity checking. We're called from the config handling routine, so
+        * better play it save. */
+       if ((filename == NULL) || (*filename == 0))
+               return (0);
+
+       fh = fopen (filename, "r");
+       if (fh == NULL)
        {
-               counter_t pre_diff;
-               counter_t post_diff;
+               char errbuf[1024];
+               WARNING ("openvpn plugin: Unable to read \"%s\": %s", filename,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (0);
+       }
 
-               pre_diff = counter_diff (pre_decompress_old, pre_decompress_new);
-               post_diff = counter_diff (post_decompress_old, post_decompress_new);
+       /* now search for the specific multimode data format */
+       while ((fgets (buffer, sizeof (buffer), fh)) != NULL)
+       {
+               /* we look at the first line searching for SINGLE mode configuration */
+               if (strcmp (buffer, VSSTRING) == 0)
+               {
+                       DEBUG ("openvpn plugin: found status file version SINGLE");
+                       version = SINGLE;
+                       break;
+               }
+               /* searching for multi version 1 */
+               else if (strcmp (buffer, V1STRING) == 0)
+               {
+                       DEBUG ("openvpn plugin: found status file version MULTI1");
+                       version = MULTI1;
+                       break;
+               }
+               /* searching for multi version 2 */
+               else if (strcmp (buffer, V2STRING) == 0)
+               {
+                       DEBUG ("openvpn plugin: found status file version MULTI2");
+                       version = MULTI2;
+                       break;
+               }
+               /* searching for multi version 3 */
+               else if (strcmp (buffer, V3STRING) == 0)
+               {
+                       DEBUG ("openvpn plugin: found status file version MULTI3");
+                       version = MULTI3;
+                       break;
+               }
+       }
 
-               /* If we decompress, we're receiving. */
-               compression_submit ("rx",
-                               ((gauge_t) pre_diff) / ((gauge_t) post_diff));
+       if (version == 0)
+       {
+               /* This is only reached during configuration, so complaining to
+                * the user is in order. */
+               NOTICE ("openvpn plugin: %s: Unknown file format, please "
+                               "report this as bug. Make sure to include "
+                               "your status file, so the plugin can "
+                               "be adapted.", filename);
        }
 
-       /* Now copy all the new counters to the old counters and move the flags
-        * up. */
-       pre_compress_old = pre_compress_new;
-       post_compress_old = post_compress_new;
-       pre_decompress_old = pre_decompress_new;
-       post_decompress_old = post_decompress_new;
-       compression_counter_valid = (compression_counter_valid & 0x0f) << 4;
+       fclose (fh);
 
-       return (0);
-} /* int openvpn_read */
+       return version;
+} /* int version_detect */
 
 static int openvpn_config (const char *key, const char *value)
 {
        if (strcasecmp ("StatusFile", key) == 0)
        {
-               sfree (status_file);
+               char    *status_file, *status_name, *filename;
+               int     status_version, i;
+               vpn_status_t *temp;
+
+               /* try to detect the status file format */
+               status_version = version_detect (value);
+
+               if (status_version == 0)
+               {
+                       WARNING ("openvpn plugin: unable to detect status version, \
+                                       discarding status file \"%s\".", value);
+                       return (1);
+               }
+
                status_file = sstrdup (value);
-       }
+               if (status_file == NULL)
+               {
+                       char errbuf[1024];
+                       WARNING ("openvpn plugin: sstrdup failed: %s",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+                       return (1);
+               }
+
+               /* it determines the file name as string starting at location filename + 1 */
+               filename = strrchr (status_file, (int) '/');
+               if (filename == NULL)
+               {
+                       /* status_file is already the file name only */
+                       status_name = status_file;
+               }
+               else
+               {
+                       /* doesn't waste memory, uses status_file starting at filename + 1 */
+                       status_name = filename + 1;
+               }
+
+               /* scan the list looking for a clone */
+               for (i = 0; i < vpn_num; i++)
+               {
+                       if (strcasecmp (vpn_list[i]->name, status_name) == 0)
+                       {
+                               WARNING ("openvpn plugin: status filename \"%s\" "
+                                               "already used, please choose a "
+                                               "different one.", status_name);
+                               sfree (status_file);
+                               return (1);
+                       }
+               }
+
+               /* create a new vpn element since file, version and name are ok */
+               temp = (vpn_status_t *) malloc (sizeof (vpn_status_t));
+               temp->file = status_file;
+               temp->version = status_version;
+               temp->name = status_name;
+
+               vpn_list = (vpn_status_t **) realloc (vpn_list, (vpn_num + 1) * sizeof (vpn_status_t *));
+               if (vpn_list == NULL)
+               {
+                       char errbuf[1024];
+                       ERROR ("openvpn plugin: malloc failed: %s",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+
+                       sfree (temp->file);
+                       sfree (temp);
+                       return (1);
+               }
+
+               vpn_list[vpn_num] = temp;
+               vpn_num++;
+
+               DEBUG ("openvpn plugin: status file \"%s\" added", temp->file);
+
+       } /* if (strcasecmp ("StatusFile", key) == 0) */
+       else if ((strcasecmp ("CollectCompression", key) == 0)
+               || (strcasecmp ("Compression", key) == 0)) /* old, deprecated name */
+       {
+               if (IS_FALSE (value))
+                       collect_compression = 0;
+               else
+                       collect_compression = 1;
+       } /* if (strcasecmp ("CollectCompression", key) == 0) */
+       else if (strcasecmp ("ImprovedNamingSchema", key) == 0)
+       {
+               if (IS_TRUE (value))
+               {
+                       DEBUG ("openvpn plugin: using the new naming schema");
+                       new_naming_schema = 1;
+               }
+               else
+               {
+                       new_naming_schema = 0;
+               }
+       } /* if (strcasecmp ("ImprovedNamingSchema", key) == 0) */
+       else if (strcasecmp("CollectUserCount", key) == 0)
+       {
+               if (IS_TRUE(value))
+                       collect_user_count = 1;
+               else
+                       collect_user_count = 0;
+       } /* if (strcasecmp("CollectUserCount", key) == 0) */
+       else if (strcasecmp("CollectIndividualUsers", key) == 0)
+       {
+               if (IS_FALSE (value))
+                       collect_individual_users = 0;
+               else
+                       collect_individual_users = 1;
+       } /* if (strcasecmp("CollectIndividualUsers", key) == 0) */
        else
        {
                return (-1);
        }
-        return (0);
+
+       return (0);
 } /* int openvpn_config */
 
+/* shutdown callback */
+static int openvpn_shutdown (void)
+{
+       int i;
+
+       for (i = 0; i < vpn_num; i++)
+       {
+               sfree (vpn_list[i]->file);
+               sfree (vpn_list[i]);
+       }
+
+       sfree (vpn_list);
+
+       return (0);
+} /* int openvpn_shutdown */
+
+static int openvpn_init (void)
+{
+       if (!collect_individual_users
+                       && !collect_compression
+                       && !collect_user_count)
+       {
+               WARNING ("OpenVPN plugin: Neither `CollectIndividualUsers', "
+                               "`CollectCompression', nor `CollectUserCount' is true. There's no "
+                               "data left to collect.");
+               return (-1);
+       }
+
+       plugin_register_read ("openvpn", openvpn_read);
+       plugin_register_shutdown ("openvpn", openvpn_shutdown);
+
+       return (0);
+} /* int openvpn_init */
+
 void module_register (void)
 {
        plugin_register_config ("openvpn", openvpn_config,
-                               config_keys, config_keys_num);
-       plugin_register_read ("openvpn", openvpn_read);
+                       config_keys, config_keys_num);
+       plugin_register_init ("openvpn", openvpn_init);
 } /* void module_register */
+
+/* vim: set sw=2 ts=2 : */
index 78a09ff..3fe2125 100644 (file)
@@ -62,6 +62,7 @@ struct o_database_s
   char *username;
   char *password;
 
+  udb_query_preparation_area_t **q_prep_areas;
   udb_query_t **queries;
   size_t        queries_num;
 
@@ -121,6 +122,8 @@ static void o_report_error (const char *where, /* {{{ */
 
 static void o_database_free (o_database_t *db) /* {{{ */
 {
+  size_t i;
+
   if (db == NULL)
     return;
 
@@ -130,6 +133,11 @@ static void o_database_free (o_database_t *db) /* {{{ */
   sfree (db->password);
   sfree (db->queries);
 
+  if (db->q_prep_areas != NULL)
+    for (i = 0; i < db->queries_num; ++i)
+      udb_query_delete_preparation_area (db->q_prep_areas[i]);
+  free (db->q_prep_areas);
+
   sfree (db);
 } /* }}} void o_database_free */
 
@@ -256,6 +264,34 @@ static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
     break;
   } /* while (status == 0) */
 
+  while ((status == 0) && (db->queries_num > 0))
+  {
+    db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
+        db->queries_num, sizeof (*db->q_prep_areas));
+
+    if (db->q_prep_areas == NULL)
+    {
+      WARNING ("oracle plugin: malloc failed");
+      status = -1;
+      break;
+    }
+
+    for (i = 0; i < db->queries_num; ++i)
+    {
+      db->q_prep_areas[i]
+        = udb_query_allocate_preparation_area (db->queries[i]);
+
+      if (db->q_prep_areas[i] == NULL)
+      {
+        WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
+        status = -1;
+        break;
+      }
+    }
+
+    break;
+  }
+
   /* If all went well, add this query to the list of queries within the
    * database structure. */
   if (status == 0)
@@ -349,7 +385,7 @@ static int o_init (void) /* {{{ */
 } /* }}} int o_init */
 
 static int o_read_database_query (o_database_t *db, /* {{{ */
-    udb_query_t *q)
+    udb_query_t *q, udb_query_preparation_area_t *prep_area)
 {
   char **column_names;
   char **column_values;
@@ -548,8 +584,9 @@ static int o_read_database_query (o_database_t *db, /* {{{ */
   } /* for (j = 1; j <= param_counter; j++) */
   /* }}} End of the ``define'' stuff. */
 
-  status = udb_query_prepare_result (q, hostname_g, /* plugin = */ "oracle",
-      db->name, column_names, column_num);
+  status = udb_query_prepare_result (q, prep_area, hostname_g,
+      /* plugin = */ "oracle", db->name, column_names, column_num,
+      /* interval = */ -1);
   if (status != 0)
   {
     ERROR ("oracle plugin: o_read_database_query (%s, %s): "
@@ -576,7 +613,7 @@ static int o_read_database_query (o_database_t *db, /* {{{ */
       break;
     }
 
-    status = udb_query_handle_result (q, column_values);
+    status = udb_query_handle_result (q, prep_area, column_values);
     if (status != 0)
     {
       WARNING ("oracle plugin: o_read_database_query (%s, %s): "
@@ -598,6 +635,47 @@ static int o_read_database (o_database_t *db) /* {{{ */
   size_t i;
   int status;
 
+  if (db->oci_service_context != NULL)
+  {
+    OCIServer *server_handle;
+    ub4 connection_status;
+
+    server_handle = NULL;
+    status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX, 
+        (void *) &server_handle, /* size pointer = */ NULL,
+        OCI_ATTR_SERVER, oci_error);
+    if (status != OCI_SUCCESS)
+    {
+      o_report_error ("o_read_database", "OCIAttrGet", oci_error);
+      return (-1);
+    }
+
+    if (server_handle == NULL)
+    {
+      connection_status = OCI_SERVER_NOT_CONNECTED;
+    }
+    else /* if (server_handle != NULL) */
+    {
+      connection_status = 0;
+      status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
+          (void *) &connection_status, /* size pointer = */ NULL,
+          OCI_ATTR_SERVER_STATUS, oci_error);
+      if (status != OCI_SUCCESS)
+      {
+        o_report_error ("o_read_database", "OCIAttrGet", oci_error);
+        return (-1);
+      }
+    }
+
+    if (connection_status != OCI_SERVER_NORMAL)
+    {
+      INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
+          db->name);
+      OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
+      db->oci_service_context = NULL;
+    }
+  } /* if (db->oci_service_context != NULL) */
+
   if (db->oci_service_context == NULL)
   {
     status = OCILogon (oci_env, oci_error,
@@ -620,7 +698,7 @@ static int o_read_database (o_database_t *db) /* {{{ */
       db->connect_id, db->oci_service_context);
 
   for (i = 0; i < db->queries_num; i++)
-    o_read_database_query (db, db->queries[i]);
+    o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
 
   return (0);
 } /* }}} int o_read_database */
index 66abb44..bf7327c 100644 (file)
@@ -173,7 +173,8 @@ dump_entry(STRUCT_ENTRY *e, const TC_HANDLE_T handle)
        t = GET_TARGET(e);
        printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
        if (strcmp(t->u.user.name, STANDARD_TARGET) == 0) {
-               int pos = *(int *)t->data;
+               const unsigned char *data = t->data;
+               int pos = *(const int *)data;
                if (pos < 0)
                        printf("verdict=%s\n",
                               pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
index 276b7af..672dae1 100644 (file)
@@ -204,7 +204,8 @@ dump_entry(struct ip6t_entry *e, const ip6tc_handle_t handle)
        t = ip6t_get_target(e);
        printf("Target name: `%s' [%u]\n", t->u.user.name, t->u.target_size);
        if (strcmp(t->u.user.name, IP6T_STANDARD_TARGET) == 0) {
-               int pos = *(int *)t->data;
+               const unsigned char *data = t->data;
+               int pos = *(const int *)data;
                if (pos < 0)
                        printf("verdict=%s\n",
                               pos == -NF_ACCEPT-1 ? "NF_ACCEPT"
index 5e5fde0..8f0b0f0 100644 (file)
@@ -744,14 +744,16 @@ static void iptcc_delete_rule(struct rule_head *r)
  * to be called from specific places within the parser */
 static int __iptcc_p_del_policy(TC_HANDLE_T h, unsigned int num)
 {
+       const unsigned char *data;
+
        if (h->chain_iterator_cur) {
                /* policy rule is last rule */
                struct rule_head *pr = (struct rule_head *)
                        h->chain_iterator_cur->rules.prev;
 
                /* save verdict */
-               h->chain_iterator_cur->verdict = 
-                       *(int *)GET_TARGET(pr->entry)->data;
+               data = GET_TARGET(pr->entry)->data;
+               h->chain_iterator_cur->verdict = *(const int *)data;
 
                /* save counter and counter_map information */
                h->chain_iterator_cur->counter_map.maptype = 
@@ -1563,6 +1565,7 @@ const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
 {
        STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
        struct rule_head *r = container_of(e, struct rule_head, entry[0]);
+       const unsigned char *data;
 
        iptc_fn = TC_GET_TARGET;
 
@@ -1576,7 +1579,8 @@ const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
                        return r->jump->name;
                        break;
                case IPTCC_R_STANDARD:
-                       spos = *(int *)GET_TARGET(e)->data;
+                       data = GET_TARGET(e)->data;
+                       spos = *(const int *)data;
                        DEBUGP("r=%p, spos=%d'\n", r, spos);
                        return standard_target_map(spos);
                        break;
diff --git a/src/pinba.c b/src/pinba.c
new file mode 100644 (file)
index 0000000..a6fd06f
--- /dev/null
@@ -0,0 +1,750 @@
+/**
+ * collectd - src/pinba.c (based on code from pinba_engine 0.0.5)
+ * Copyright (c) 2007-2009  Antony Dovgal
+ * Copyright (C) 2010       Phoenix Kayo
+ * Copyright (C) 2010       Florian 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:
+ *   Antony Dovgal <tony at daylessday.org>
+ *   Phoenix Kayo <kayo.k11.4 at gmail.com>
+ *   Florian Forster <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
+
+#include <pthread.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <poll.h>
+
+#include "pinba.pb-c.h"
+
+/*
+ * Defines
+ */
+#ifndef PINBA_UDP_BUFFER_SIZE
+# define PINBA_UDP_BUFFER_SIZE 65536
+#endif
+
+#ifndef PINBA_DEFAULT_NODE
+# define PINBA_DEFAULT_NODE "::0"
+#endif
+
+#ifndef PINBA_DEFAULT_SERVICE
+# define PINBA_DEFAULT_SERVICE "30002"
+#endif
+
+#ifndef PINBA_MAX_SOCKETS
+# define PINBA_MAX_SOCKETS 16
+#endif
+
+/*
+ * Private data structures
+ */
+/* {{{ */
+struct pinba_socket_s
+{
+  struct pollfd fd[PINBA_MAX_SOCKETS];
+  nfds_t fd_num;
+};
+typedef struct pinba_socket_s pinba_socket_t;
+
+/* Fixed point counter value. n is the decimal part multiplied by 10^9. */
+struct float_counter_s
+{
+  uint64_t i;
+  uint64_t n; /* nanos */
+};
+typedef struct float_counter_s float_counter_t;
+
+struct pinba_statnode_s
+{
+  /* collector name, used as plugin instance */
+  char *name;
+
+  /* query data */
+  char *host;
+  char *server;
+  char *script;
+
+  derive_t req_count;
+
+  float_counter_t req_time;
+  float_counter_t ru_utime;
+  float_counter_t ru_stime;
+
+  derive_t doc_size;
+  gauge_t mem_peak;
+};
+typedef struct pinba_statnode_s pinba_statnode_t;
+/* }}} */
+
+/*
+ * Module global variables
+ */
+/* {{{ */
+static pinba_statnode_t *stat_nodes = NULL;
+static unsigned int stat_nodes_num = 0;
+static pthread_mutex_t stat_nodes_lock;
+
+static char *conf_node = NULL;
+static char *conf_service = NULL;
+
+static _Bool collector_thread_running = 0;
+static _Bool collector_thread_do_shutdown = 0;
+static pthread_t collector_thread_id;
+/* }}} */
+
+/*
+ * Functions
+ */
+static void float_counter_add (float_counter_t *fc, float val) /* {{{ */
+{
+  uint64_t tmp;
+
+  if (val < 0.0)
+    return;
+
+  tmp = (uint64_t) val;
+  val -= (double) tmp;
+
+  fc->i += tmp;
+  fc->n += (uint64_t) ((val * 1000000000.0) + .5);
+
+  if (fc->n >= 1000000000)
+  {
+    fc->i += 1;
+    fc->n -= 1000000000;
+    assert (fc->n < 1000000000);
+  }
+} /* }}} void float_counter_add */
+
+static derive_t float_counter_get (const float_counter_t *fc, /* {{{ */
+    uint64_t factor)
+{
+  derive_t ret;
+
+  ret = (derive_t) (fc->i * factor);
+  ret += (derive_t) (fc->n / (1000000000 / factor));
+
+  return (ret);
+} /* }}} derive_t float_counter_get */
+
+static void strset (char **str, const char *new) /* {{{ */
+{
+  char *tmp;
+
+  if (!str || !new)
+    return;
+
+  tmp = strdup (new);
+  if (tmp == NULL)
+    return;
+
+  sfree (*str);
+  *str = tmp;
+} /* }}} void strset */
+
+static void service_statnode_add(const char *name, /* {{{ */
+    const char *host,
+    const char *server,
+    const char *script)
+{
+  pinba_statnode_t *node;
+  
+  node = realloc (stat_nodes,
+      sizeof (*stat_nodes) * (stat_nodes_num + 1));
+  if (node == NULL)
+  {
+    ERROR ("pinba plugin: realloc failed");
+    return;
+  }
+  stat_nodes = node;
+
+  node = stat_nodes + stat_nodes_num;
+  memset (node, 0, sizeof (*node));
+  
+  /* reset strings */
+  node->name   = NULL;
+  node->host   = NULL;
+  node->server = NULL;
+  node->script = NULL;
+
+  node->mem_peak = NAN;
+  
+  /* fill query data */
+  strset (&node->name, name);
+  strset (&node->host, host);
+  strset (&node->server, server);
+  strset (&node->script, script);
+  
+  /* increment counter */
+  stat_nodes_num++;
+} /* }}} void service_statnode_add */
+
+/* Copy the data from the global "stat_nodes" list into the buffer pointed to
+ * by "res", doing the derivation in the process. Returns the next index or
+ * zero if the end of the list has been reached. */
+static unsigned int service_statnode_collect (pinba_statnode_t *res, /* {{{ */
+    unsigned int index)
+{
+  pinba_statnode_t *node;
+  
+  if (stat_nodes_num == 0)
+    return 0;
+  
+  /* begin collecting */
+  if (index == 0)
+    pthread_mutex_lock (&stat_nodes_lock);
+  
+  /* end collecting */
+  if (index >= stat_nodes_num)
+  {
+    pthread_mutex_unlock (&stat_nodes_lock);
+    return 0;
+  }
+
+  node = stat_nodes + index;
+  memcpy (res, node, sizeof (*res));
+
+  /* reset node */
+  node->mem_peak = NAN;
+  
+  return (index + 1);
+} /* }}} unsigned int service_statnode_collect */
+
+static void service_statnode_process (pinba_statnode_t *node, /* {{{ */
+    Pinba__Request* request)
+{
+  node->req_count++;
+
+  float_counter_add (&node->req_time, request->request_time);
+  float_counter_add (&node->ru_utime, request->ru_utime);
+  float_counter_add (&node->ru_stime, request->ru_stime);
+
+  node->doc_size += request->document_size;
+
+  if (isnan (node->mem_peak)
+      || (node->mem_peak < ((gauge_t) request->memory_peak)))
+    node->mem_peak = (gauge_t) request->memory_peak;
+
+} /* }}} void service_statnode_process */
+
+static void service_process_request (Pinba__Request *request) /* {{{ */
+{
+  unsigned int i;
+
+  pthread_mutex_lock (&stat_nodes_lock);
+  
+  for (i = 0; i < stat_nodes_num; i++)
+  {
+    if ((stat_nodes[i].host != NULL)
+        && (strcmp (request->hostname, stat_nodes[i].host) != 0))
+      continue;
+
+    if ((stat_nodes[i].server != NULL)
+      && (strcmp (request->server_name, stat_nodes[i].server) != 0))
+      continue;
+
+    if ((stat_nodes[i].script != NULL)
+      && (strcmp (request->script_name, stat_nodes[i].script) != 0))
+      continue;
+
+    service_statnode_process(&stat_nodes[i], request);
+  }
+  
+  pthread_mutex_unlock(&stat_nodes_lock);
+} /* }}} void service_process_request */
+
+static int pb_del_socket (pinba_socket_t *s, /* {{{ */
+    nfds_t index)
+{
+  if (index >= s->fd_num)
+    return (EINVAL);
+
+  close (s->fd[index].fd);
+  s->fd[index].fd = -1;
+
+  /* When deleting the last element in the list, no memmove is necessary. */
+  if (index < (s->fd_num - 1))
+  {
+    memmove (&s->fd[index], &s->fd[index + 1],
+        sizeof (s->fd[0]) * (s->fd_num - (index + 1)));
+  }
+
+  s->fd_num--;
+  return (0);
+} /* }}} int pb_del_socket */
+
+static int pb_add_socket (pinba_socket_t *s, /* {{{ */
+    const struct addrinfo *ai)
+{
+  int fd;
+  int tmp;
+  int status;
+
+  if (s->fd_num == PINBA_MAX_SOCKETS)
+  {
+    WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
+        "%i sockets. Please complain to the collectd developers so we can "
+        "raise the limit.", PINBA_MAX_SOCKETS);
+    return (-1);
+  }
+
+  fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+  if (fd < 0)
+  {
+    char errbuf[1024];
+    ERROR ("pinba plugin: socket(2) failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (0);
+  }
+
+  tmp = 1;
+  status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
+  if (status != 0)
+  {
+    char errbuf[1024];
+    WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
+  }
+
+  status = bind (fd, ai->ai_addr, ai->ai_addrlen);
+  if (status != 0)
+  {
+    char errbuf[1024];
+    ERROR ("pinba plugin: bind(2) failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (0);
+  }
+
+  s->fd[s->fd_num].fd = fd;
+  s->fd[s->fd_num].events = POLLIN | POLLPRI;
+  s->fd[s->fd_num].revents = 0;
+  s->fd_num++;
+
+  return (0);
+} /* }}} int pb_add_socket */
+
+static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
+    const char *service)
+{
+  pinba_socket_t *s;
+  struct addrinfo *ai_list;
+  struct addrinfo *ai_ptr;
+  struct addrinfo  ai_hints;
+  int status;
+
+  memset (&ai_hints, 0, sizeof (ai_hints));
+  ai_hints.ai_flags = AI_PASSIVE;
+  ai_hints.ai_family = AF_UNSPEC;
+  ai_hints.ai_socktype = SOCK_DGRAM;
+  ai_hints.ai_addr = NULL;
+  ai_hints.ai_canonname = NULL;
+  ai_hints.ai_next = NULL;
+
+  if (node == NULL)
+    node = PINBA_DEFAULT_NODE;
+
+  if (service == NULL)
+    service = PINBA_DEFAULT_SERVICE;
+
+  ai_list = NULL;
+  status = getaddrinfo (node, service,
+      &ai_hints, &ai_list);
+  if (status != 0)
+  {
+    ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
+        gai_strerror (status));
+    return (NULL);
+  }
+  assert (ai_list != NULL);
+
+  s = malloc (sizeof (*s));
+  if (s == NULL)
+  {
+    freeaddrinfo (ai_list);
+    ERROR ("pinba plugin: malloc failed.");
+    return (NULL);
+  }
+  memset (s, 0, sizeof (*s));
+
+  for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
+  {
+    status = pb_add_socket (s, ai_ptr);
+    if (status != 0)
+      break;
+  } /* for (ai_list) */
+  
+  freeaddrinfo (ai_list);
+
+  if (s->fd_num < 1)
+  {
+    WARNING ("pinba plugin: Unable to open socket for address %s.", node);
+    sfree (s);
+    s = NULL;
+  }
+
+  return (s);
+} /* }}} pinba_socket_open */
+
+static void pinba_socket_free (pinba_socket_t *socket) /* {{{ */
+{
+  nfds_t i;
+
+  if (!socket)
+    return;
+  
+  for (i = 0; i < socket->fd_num; i++)
+  {
+    if (socket->fd[i].fd < 0)
+      continue;
+    close (socket->fd[i].fd);
+    socket->fd[i].fd = -1;
+  }
+  
+  sfree(socket);
+} /* }}} void pinba_socket_free */
+
+static int pinba_process_stats_packet (const uint8_t *buffer, /* {{{ */
+    size_t buffer_size)
+{
+  Pinba__Request *request;  
+  
+  request = pinba__request__unpack (NULL, buffer_size, buffer);
+  
+  if (!request)
+    return (-1);
+
+  service_process_request(request);
+  pinba__request__free_unpacked (request, NULL);
+    
+  return (0);
+} /* }}} int pinba_process_stats_packet */
+
+static int pinba_udp_read_callback_fn (int sock) /* {{{ */
+{
+  uint8_t buffer[PINBA_UDP_BUFFER_SIZE];
+  size_t buffer_size;
+  int status;
+
+  while (42)
+  {
+    buffer_size = sizeof (buffer);
+    status = recvfrom (sock, buffer, buffer_size - 1, MSG_DONTWAIT, /* from = */ NULL, /* from len = */ 0);
+    if (status < 0)
+    {
+      char errbuf[1024];
+
+      if ((errno == EINTR)
+#ifdef EWOULDBLOCK
+          || (errno == EWOULDBLOCK)
+#endif
+          || (errno == EAGAIN))
+      {
+        continue;
+      }
+
+      WARNING("pinba plugin: recvfrom(2) failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
+      return (-1);
+    }
+    else if (status == 0)
+    {
+      DEBUG ("pinba plugin: recvfrom(2) returned unexpected status zero.");
+      return (-1);
+    }
+    else /* if (status > 0) */
+    {
+      assert (((size_t) status) < buffer_size);
+      buffer_size = (size_t) status;
+      buffer[buffer_size] = 0;
+
+      status = pinba_process_stats_packet (buffer, buffer_size);
+      if (status != 0)
+        DEBUG("pinba plugin: Parsing packet failed.");
+      return (status);
+    }
+  } /* while (42) */
+
+  /* not reached */
+  assert (23 == 42);
+  return (-1);
+} /* }}} void pinba_udp_read_callback_fn */
+
+static int receive_loop (void) /* {{{ */
+{
+  pinba_socket_t *s;
+
+  s = pinba_socket_open (conf_node, conf_service);
+  if (s == NULL)
+  {
+    ERROR ("pinba plugin: Collector thread is exiting prematurely.");
+    return (-1);
+  }
+
+  while (!collector_thread_do_shutdown)
+  {
+    int status;
+    nfds_t i;
+
+    if (s->fd_num < 1)
+      break;
+
+    status = poll (s->fd, s->fd_num, /* timeout = */ 1000);
+    if (status == 0) /* timeout */
+    {
+      continue;
+    }
+    else if (status < 0)
+    {
+      char errbuf[1024];
+
+      if ((errno == EINTR) || (errno == EAGAIN))
+        continue;
+
+      ERROR ("pinba plugin: poll(2) failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
+      pinba_socket_free (s);
+      return (-1);
+    }
+
+    for (i = 0; i < s->fd_num; i++)
+    {
+      if (s->fd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
+      {
+        pb_del_socket (s, i);
+        i--;
+      }
+      else if (s->fd[i].revents & (POLLIN | POLLPRI))
+      {
+        pinba_udp_read_callback_fn (s->fd[i].fd);
+      }
+    } /* for (s->fd) */
+  } /* while (!collector_thread_do_shutdown) */
+
+  pinba_socket_free (s);
+  s = NULL;
+
+  return (0);
+} /* }}} int receive_loop */
+
+static void *collector_thread (void *arg) /* {{{ */
+{
+  receive_loop ();
+
+  memset (&collector_thread_id, 0, sizeof (collector_thread_id));
+  collector_thread_running = 0;
+  pthread_exit (NULL);
+  return (NULL);
+} /* }}} void *collector_thread */
+
+/*
+ * Plugin declaration section
+ */
+static int pinba_config_view (const oconfig_item_t *ci) /* {{{ */
+{
+  char *name   = NULL;
+  char *host   = NULL;
+  char *server = NULL;
+  char *script = NULL;
+  int status;
+  int i;
+
+  status = cf_util_get_string (ci, &name);
+  if (status != 0)
+    return (status);
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Host", child->key) == 0)
+      status = cf_util_get_string (child, &host);
+    else if (strcasecmp ("Server", child->key) == 0)
+      status = cf_util_get_string (child, &server);
+    else if (strcasecmp ("Script", child->key) == 0)
+      status = cf_util_get_string (child, &script);
+    else
+    {
+      WARNING ("pinba plugin: Unknown config option: %s", child->key);
+      status = -1;
+    }
+
+    if (status != 0)
+      break;
+  }
+
+  if (status == 0)
+    service_statnode_add (name, host, server, script);
+
+  sfree (name);
+  sfree (host);
+  sfree (server);
+  sfree (script);
+
+  return (status);
+} /* }}} int pinba_config_view */
+
+static int plugin_config (oconfig_item_t *ci) /* {{{ */
+{
+  int i;
+  
+  /* The lock should not be necessary in the config callback, but let's be
+   * sure.. */
+  pthread_mutex_lock (&stat_nodes_lock);
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Address", child->key) == 0)
+      cf_util_get_string (child, &conf_node);
+    else if (strcasecmp ("Port", child->key) == 0)
+      cf_util_get_string (child, &conf_service);
+    else if (strcasecmp ("View", child->key) == 0)
+      pinba_config_view (child);
+    else
+      WARNING ("pinba plugin: Unknown config option: %s", child->key);
+  }
+
+  pthread_mutex_unlock(&stat_nodes_lock);
+  
+  return (0);
+} /* }}} int pinba_config */
+
+static int plugin_init (void) /* {{{ */
+{
+  int status;
+
+  if (stat_nodes == NULL)
+  {
+    /* Collect the "total" data by default. */
+    service_statnode_add ("total",
+        /* host   = */ NULL,
+        /* server = */ NULL,
+        /* script = */ NULL);
+  }
+
+  if (collector_thread_running)
+    return (0);
+
+  status = pthread_create (&collector_thread_id,
+      /* attrs = */ NULL,
+      collector_thread,
+      /* args = */ NULL);
+  if (status != 0)
+  {
+    char errbuf[1024];
+    ERROR ("pinba plugin: pthread_create(3) failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (-1);
+  }
+  collector_thread_running = 1;
+
+  return (0);
+} /* }}} */
+
+static int plugin_shutdown (void) /* {{{ */
+{
+  if (collector_thread_running)
+  {
+    int status;
+
+    DEBUG ("pinba plugin: Shutting down collector thread.");
+    collector_thread_do_shutdown = 1;
+
+    status = pthread_join (collector_thread_id, /* retval = */ NULL);
+    if (status != 0)
+    {
+      char errbuf[1024];
+      ERROR ("pinba plugin: pthread_join(3) failed: %s",
+          sstrerror (status, errbuf, sizeof (errbuf)));
+    }
+
+    collector_thread_running = 0;
+    collector_thread_do_shutdown = 0;
+  } /* if (collector_thread_running) */
+
+  return (0);
+} /* }}} int plugin_shutdown */
+
+static int plugin_submit (const pinba_statnode_t *res) /* {{{ */
+{
+  value_t value;
+  value_list_t vl = VALUE_LIST_INIT;
+  
+  vl.values = &value;
+  vl.values_len = 1;
+  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+  sstrncpy (vl.plugin, "pinba", sizeof (vl.plugin));
+  sstrncpy (vl.plugin_instance, res->name, sizeof (vl.plugin_instance));
+
+  value.derive = res->req_count;
+  sstrncpy (vl.type, "total_requests", sizeof (vl.type)); 
+  plugin_dispatch_values (&vl);
+
+  value.derive = float_counter_get (&res->req_time, /* factor = */ 1000);
+  sstrncpy (vl.type, "total_time_in_ms", sizeof (vl.type)); 
+  plugin_dispatch_values (&vl);
+
+  value.derive = res->doc_size;
+  sstrncpy (vl.type, "total_bytes", sizeof (vl.type)); 
+  plugin_dispatch_values (&vl);
+
+  value.derive = float_counter_get (&res->ru_utime, /* factor = */ 100);
+  sstrncpy (vl.type, "cpu", sizeof (vl.type));
+  sstrncpy (vl.type_instance, "user", sizeof (vl.type_instance));
+  plugin_dispatch_values (&vl);
+
+  value.derive = float_counter_get (&res->ru_stime, /* factor = */ 100);
+  sstrncpy (vl.type, "cpu", sizeof (vl.type));
+  sstrncpy (vl.type_instance, "system", sizeof (vl.type_instance));
+  plugin_dispatch_values (&vl);
+
+  value.gauge = res->mem_peak;
+  sstrncpy (vl.type, "memory", sizeof (vl.type));
+  sstrncpy (vl.type_instance, "peak", sizeof (vl.type_instance));
+  plugin_dispatch_values (&vl);
+
+  return (0);
+} /* }}} int plugin_submit */
+
+static int plugin_read (void) /* {{{ */
+{
+  unsigned int i=0;
+  pinba_statnode_t data;
+  
+  while ((i = service_statnode_collect (&data, i)) != 0)
+  {
+    plugin_submit (&data);
+  }
+  
+  return 0;
+} /* }}} int plugin_read */
+
+void module_register (void) /* {{{ */
+{
+  plugin_register_complex_config ("pinba", plugin_config);
+  plugin_register_init ("pinba", plugin_init);
+  plugin_register_read ("pinba", plugin_read);
+  plugin_register_shutdown ("pinba", plugin_shutdown);
+} /* }}} void module_register */
+
+/* vim: set sw=2 sts=2 et fdm=marker : */
diff --git a/src/pinba.proto b/src/pinba.proto
new file mode 100644 (file)
index 0000000..5fd5439
--- /dev/null
@@ -0,0 +1,22 @@
+package Pinba;
+option optimize_for = SPEED;
+
+message Request {
+       required string hostname                = 1;
+       required string server_name             = 2;
+       required string script_name             = 3;
+       required uint32 request_count   = 4;
+       required uint32 document_size   = 5;
+       required uint32 memory_peak             = 6;
+       required float request_time             = 7;
+       required float ru_utime                 = 8;
+       required float ru_stime                 = 9;
+
+       repeated uint32 timer_hit_count = 10;
+       repeated float timer_value      = 11;
+       repeated uint32 timer_tag_count = 12;
+       repeated uint32 timer_tag_name  = 13;
+       repeated uint32 timer_tag_value = 14;
+       repeated string dictionary      = 15;
+       optional uint32 status          = 16;
+}
index de9c45b..b536f42 100644 (file)
 # define NI_MAXHOST 1025
 #endif
 
+#if defined(OPING_VERSION) && (OPING_VERSION >= 1003000)
+# define HAVE_OPING_1_3
+#endif
+
 /*
  * Private data types
  */
@@ -45,6 +49,7 @@ struct hostlist_s
 
   uint32_t pkg_sent;
   uint32_t pkg_recv;
+  uint32_t pkg_missed;
 
   double latency_total;
   double latency_squared;
@@ -58,9 +63,14 @@ typedef struct hostlist_s hostlist_t;
  */
 static hostlist_t *hostlist_head = NULL;
 
+static char  *ping_source = NULL;
+#ifdef HAVE_OPING_1_3
+static char  *ping_device = NULL;
+#endif
 static int    ping_ttl = PING_DEF_TTL;
 static double ping_interval = 1.0;
 static double ping_timeout = 0.9;
+static int    ping_max_missed = -1;
 
 static int             ping_thread_loop = 0;
 static int             ping_thread_error = 0;
@@ -71,9 +81,14 @@ static pthread_cond_t  ping_cond = PTHREAD_COND_INITIALIZER;
 static const char *config_keys[] =
 {
   "Host",
+  "SourceAddress",
+#ifdef HAVE_OPING_1_3
+  "Device",
+#endif
   "TTL",
   "Interval",
-  "Timeout"
+  "Timeout",
+  "MaxMissed"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
@@ -137,7 +152,7 @@ static void *ping_thread (void *arg) /* {{{ */
   struct timespec ts_int;
 
   hostlist_t *hl;
-  int status;
+  int count;
 
   pthread_mutex_lock (&ping_lock);
 
@@ -150,22 +165,35 @@ static void *ping_thread (void *arg) /* {{{ */
     return ((void *) -1);
   }
 
+  if (ping_source != NULL)
+    if (ping_setopt (pingobj, PING_OPT_SOURCE, (void *) ping_source) != 0)
+      ERROR ("ping plugin: Failed to set source address: %s",
+          ping_get_error (pingobj));
+
+#ifdef HAVE_OPING_1_3
+  if (ping_device != NULL)
+    if (ping_setopt (pingobj, PING_OPT_DEVICE, (void *) ping_device) != 0)
+      ERROR ("ping plugin: Failed to set device: %s",
+          ping_get_error (pingobj));
+#endif
+
   ping_setopt (pingobj, PING_OPT_TIMEOUT, (void *) &ping_timeout);
   ping_setopt (pingobj, PING_OPT_TTL, (void *) &ping_ttl);
 
   /* Add all the hosts to the ping object. */
-  status = 0;
+  count = 0;
   for (hl = hostlist_head; hl != NULL; hl = hl->next)
   {
     int tmp_status;
     tmp_status = ping_host_add (pingobj, hl->host);
     if (tmp_status != 0)
-      WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host);
+      WARNING ("ping plugin: ping_host_add (%s) failed: %s",
+          hl->host, ping_get_error (pingobj));
     else
-      status++;
+      count++;
   }
 
-  if (status == 0)
+  if (count == 0)
   {
     ERROR ("ping plugin: No host could be added to ping object. Giving up.");
     ping_thread_error = 1;
@@ -190,7 +218,9 @@ static void *ping_thread (void *arg) /* {{{ */
 
     if (gettimeofday (&tv_begin, NULL) < 0)
     {
-      ERROR ("ping plugin: gettimeofday failed");
+      char errbuf[1024];
+      ERROR ("ping plugin: gettimeofday failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
       ping_thread_error = 1;
       break;
     }
@@ -260,12 +290,44 @@ static void *ping_thread (void *arg) /* {{{ */
         hl->pkg_recv++;
         hl->latency_total += latency;
         hl->latency_squared += (latency * latency);
-      }
+
+        /* reset missed packages counter */
+        hl->pkg_missed = 0;
+      } else
+        hl->pkg_missed++;
+
+      /* if the host did not answer our last N packages, trigger a resolv. */
+      if (ping_max_missed >= 0 && hl->pkg_missed >= ping_max_missed)
+      { /* {{{ */
+        /* we reset the missed package counter here, since we only want to
+         * trigger a resolv every N packages and not every package _AFTER_ N
+         * missed packages */
+        hl->pkg_missed = 0;
+
+        WARNING ("ping plugin: host %s has not answered %d PING requests,"
+          " triggering resolve", hl->host, ping_max_missed);
+
+        /* we trigger the resolv simply be removeing and adding the host to our
+         * ping object */
+        status = ping_host_remove (pingobj, hl->host);
+        if (status != 0)
+        {
+          WARNING ("ping plugin: ping_host_remove (%s) failed.", hl->host);
+        }
+        else
+        {
+          status = ping_host_add (pingobj, hl->host);
+          if (status != 0)
+            WARNING ("ping plugin: ping_host_add (%s) failed.", hl->host);
+        }
+      } /* }}} ping_max_missed */
     } /* }}} for (iter) */
 
     if (gettimeofday (&tv_end, NULL) < 0)
     {
-      ERROR ("ping plugin: gettimeofday failed");
+      char errbuf[1024];
+      ERROR ("ping plugin: gettimeofday failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
       ping_thread_error = 1;
       break;
     }
@@ -363,6 +425,26 @@ static int ping_init (void) /* {{{ */
   return (0);
 } /* }}} int ping_init */
 
+static int config_set_string (const char *name, /* {{{ */
+    char **var, const char *value)
+{
+  char *tmp;
+
+  tmp = strdup (value);
+  if (tmp == NULL)
+  {
+    char errbuf[1024];
+    ERROR ("ping plugin: Setting `%s' to `%s' failed: strdup failed: %s",
+        name, value, sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (1);
+  }
+
+  if (*var != NULL)
+    free (*var);
+  *var = tmp;
+  return (0);
+} /* }}} int config_set_string */
+
 static int ping_config (const char *key, const char *value) /* {{{ */
 {
   if (strcasecmp (key, "Host") == 0)
@@ -392,11 +474,26 @@ static int ping_config (const char *key, const char *value) /* {{{ */
     hl->host = host;
     hl->pkg_sent = 0;
     hl->pkg_recv = 0;
+    hl->pkg_missed = 0;
     hl->latency_total = 0.0;
     hl->latency_squared = 0.0;
     hl->next = hostlist_head;
     hostlist_head = hl;
   }
+  else if (strcasecmp (key, "SourceAddress") == 0)
+  {
+    int status = config_set_string (key, &ping_source, value);
+    if (status != 0)
+      return (status);
+  }
+#ifdef HAVE_OPING_1_3
+  else if (strcasecmp (key, "Device") == 0)
+  {
+    int status = config_set_string (key, &ping_device, value);
+    if (status != 0)
+      return (status);
+  }
+#endif
   else if (strcasecmp (key, "TTL") == 0)
   {
     int ttl = atoi (value);
@@ -427,6 +524,12 @@ static int ping_config (const char *key, const char *value) /* {{{ */
       WARNING ("ping plugin: Ignoring invalid timeout %g (%s)",
           tmp, value);
   }
+  else if (strcasecmp (key, "MaxMissed") == 0)
+  {
+    ping_max_missed = atoi (value);
+    if (ping_max_missed < 0)
+      INFO ("ping plugin: MaxMissed < 0, disabled re-resolving of hosts");
+  }
   else
   {
     return (-1);
index 11a0ef6..3682fa5 100644 (file)
@@ -59,6 +59,7 @@ struct read_func_s
 #define rf_callback rf_super.cf_callback
 #define rf_udata rf_super.cf_udata
        callback_func_t rf_super;
+       char rf_group[DATA_MAX_NAME_LEN];
        char rf_name[DATA_MAX_NAME_LEN];
        int rf_type;
        struct timespec rf_interval;
@@ -151,7 +152,7 @@ static void destroy_read_heap (void) /* {{{ */
        {
                callback_func_t *cf;
 
-               cf = c_head_get_root (read_heap);
+               cf = c_heap_get_root (read_heap);
                if (cf == NULL)
                        break;
 
@@ -173,7 +174,7 @@ static int register_callback (llist_t **list, /* {{{ */
                *list = llist_create ();
                if (*list == NULL)
                {
-                       ERROR ("plugin: create_register_callback: "
+                       ERROR ("plugin: register_callback: "
                                        "llist_create failed.");
                        destroy_callback (cf);
                        return (-1);
@@ -183,7 +184,7 @@ static int register_callback (llist_t **list, /* {{{ */
        key = strdup (name);
        if (key == NULL)
        {
-               ERROR ("plugin: create_register_callback: strdup failed.");
+               ERROR ("plugin: register_callback: strdup failed.");
                destroy_callback (cf);
                return (-1);
        }
@@ -194,7 +195,7 @@ static int register_callback (llist_t **list, /* {{{ */
                le = llentry_create (key, cf);
                if (le == NULL)
                {
-                       ERROR ("plugin: create_register_callback: "
+                       ERROR ("plugin: register_callback: "
                                        "llentry_create failed.");
                        free (key);
                        destroy_callback (cf);
@@ -210,6 +211,10 @@ static int register_callback (llist_t **list, /* {{{ */
                old_cf = le->value;
                le->value = cf;
 
+               WARNING ("plugin: register_callback: "
+                               "a callback named `%s' already exists - "
+                               "overwriting the old entry!", name);
+
                destroy_callback (old_cf);
                sfree (key);
        }
@@ -270,7 +275,7 @@ static int plugin_unregister (llist_t *list, const char *name) /* {{{ */
  * object, but it will bitch about a shared object not having a
  * ``module_register'' symbol..
  */
-static int plugin_load_file (char *file)
+static int plugin_load_file (char *file, uint32_t flags)
 {
        lt_dlhandle dlh;
        void (*reg_handle) (void);
@@ -280,7 +285,24 @@ static int plugin_load_file (char *file)
        lt_dlinit ();
        lt_dlerror (); /* clear errors */
 
-       if ((dlh = lt_dlopen (file)) == NULL)
+#if LIBTOOL_VERSION == 2
+       if (flags & PLUGIN_FLAGS_GLOBAL) {
+               lt_dladvise advise;
+               lt_dladvise_init(&advise);
+               lt_dladvise_global(&advise);
+               dlh = lt_dlopenadvise(file, advise);
+               lt_dladvise_destroy(&advise);
+       } else {
+               dlh = lt_dlopen (file);
+       }
+#else /* if LIBTOOL_VERSION == 1 */
+       if (flags & PLUGIN_FLAGS_GLOBAL)
+               ERROR ("plugin_load_file: The global flag is not supported, "
+                               "libtool 2 is required for this.");
+       dlh = lt_dlopen (file);
+#endif
+
+       if (dlh == NULL)
        {
                const char *error = lt_dlerror ();
 
@@ -302,6 +324,13 @@ static int plugin_load_file (char *file)
        return (0);
 }
 
+static _Bool timeout_reached(struct timespec timeout)
+{
+       struct timeval now;
+       gettimeofday(&now, NULL);
+       return (now.tv_sec >= timeout.tv_sec && now.tv_usec >= (timeout.tv_nsec / 1000));
+}
+
 static void *plugin_read_thread (void __attribute__((unused)) *args)
 {
        while (read_loop != 0)
@@ -310,9 +339,10 @@ static void *plugin_read_thread (void __attribute__((unused)) *args)
                struct timeval now;
                int status;
                int rf_type;
+               int rc;
 
                /* Get the read function that needs to be read next. */
-               rf = c_head_get_root (read_heap);
+               rf = c_heap_get_root (read_heap);
                if (rf == NULL)
                {
                        struct timespec abstime;
@@ -345,8 +375,16 @@ static void *plugin_read_thread (void __attribute__((unused)) *args)
                /* sleep until this entry is due,
                 * using pthread_cond_timedwait */
                pthread_mutex_lock (&read_lock);
-               pthread_cond_timedwait (&read_cond, &read_lock,
+               /* In pthread_cond_timedwait, spurious wakeups are possible
+                * (and really happen, at least on NetBSD with > 1 CPU), thus
+                * we need to re-evaluate the condition every time
+                * pthread_cond_timedwait returns. */
+               rc = 0;
+               while (!timeout_reached(rf->rf_next_read) && rc == 0) {
+                       rc = pthread_cond_timedwait (&read_cond, &read_lock,
                                &rf->rf_next_read);
+               }
+
                /* Must hold `real_lock' when accessing `rf->rf_type'. */
                rf_type = rf->rf_type;
                pthread_mutex_unlock (&read_lock);
@@ -535,7 +573,7 @@ void plugin_set_dir (const char *dir)
 }
 
 #define BUFSIZE 512
-int plugin_load (const char *type)
+int plugin_load (const char *type, uint32_t flags)
 {
        DIR  *dh;
        const char *dir;
@@ -597,7 +635,7 @@ int plugin_load (const char *type)
                        continue;
                }
 
-               if (plugin_load_file (filename) == 0)
+               if (plugin_load_file (filename, flags) == 0)
                {
                        /* success */
                        ret = 0;
@@ -735,6 +773,7 @@ int plugin_register_read (const char *name,
        rf->rf_callback = (void *) callback;
        rf->rf_udata.data = NULL;
        rf->rf_udata.free_func = NULL;
+       rf->rf_group[0] = '\0';
        sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
        rf->rf_type = RF_SIMPLE;
        rf->rf_interval.tv_sec = 0;
@@ -744,7 +783,7 @@ int plugin_register_read (const char *name,
        return (plugin_insert_read (rf));
 } /* int plugin_register_read */
 
-int plugin_register_complex_read (const char *name,
+int plugin_register_complex_read (const char *group, const char *name,
                plugin_read_cb callback,
                const struct timespec *interval,
                user_data_t *user_data)
@@ -760,6 +799,10 @@ int plugin_register_complex_read (const char *name,
 
        memset (rf, 0, sizeof (read_func_t));
        rf->rf_callback = (void *) callback;
+       if (group != NULL)
+               sstrncpy (rf->rf_group, group, sizeof (rf->rf_group));
+       else
+               rf->rf_group[0] = '\0';
        sstrncpy (rf->rf_name, name, sizeof (rf->rf_name));
        rf->rf_type = RF_COMPLEX;
        if (interval != NULL)
@@ -911,6 +954,67 @@ int plugin_unregister_read (const char *name) /* {{{ */
        return (0);
 } /* }}} int plugin_unregister_read */
 
+static int compare_read_func_group (llentry_t *e, void *ud) /* {{{ */
+{
+       read_func_t *rf    = e->value;
+       char        *group = ud;
+
+       return strcmp (rf->rf_group, (const char *)group);
+} /* }}} int compare_read_func_group */
+
+int plugin_unregister_read_group (const char *group) /* {{{ */
+{
+       llentry_t *le;
+       read_func_t *rf;
+
+       int found = 0;
+
+       if (group == NULL)
+               return (-ENOENT);
+
+       pthread_mutex_lock (&read_lock);
+
+       if (read_list == NULL)
+       {
+               pthread_mutex_unlock (&read_lock);
+               return (-ENOENT);
+       }
+
+       while (42)
+       {
+               le = llist_search_custom (read_list,
+                               compare_read_func_group, (void *)group);
+
+               if (le == NULL)
+                       break;
+
+               ++found;
+
+               llist_remove (read_list, le);
+
+               rf = le->value;
+               assert (rf != NULL);
+               rf->rf_type = RF_REMOVE;
+
+               llentry_destroy (le);
+
+               DEBUG ("plugin_unregister_read_group: "
+                               "Marked `%s' (group `%s') for removal.",
+                               rf->rf_name, group);
+       }
+
+       pthread_mutex_unlock (&read_lock);
+
+       if (found == 0)
+       {
+               WARNING ("plugin_unregister_read_group: No such "
+                               "group of read function: %s", group);
+               return (-ENOENT);
+       }
+
+       return (0);
+} /* }}} int plugin_unregister_read_group */
+
 int plugin_unregister_write (const char *name)
 {
        return (plugin_unregister (list_write, name));
@@ -1036,7 +1140,7 @@ int plugin_read_all_once (void)
        {
                read_func_t *rf;
 
-               rf = c_head_get_root (read_heap);
+               rf = c_heap_get_root (read_heap);
                if (rf == NULL)
                        break;
 
@@ -1443,14 +1547,6 @@ void plugin_log (int level, const char *format, ...)
        va_list ap;
        llentry_t *le;
 
-       if (list_log == NULL)
-       {
-               va_start (ap, format);
-               vfprintf (stderr, format, ap);
-               va_end (ap);
-               return;
-       }
-
 #if !COLLECT_DEBUG
        if (level >= LOG_DEBUG)
                return;
@@ -1461,6 +1557,12 @@ void plugin_log (int level, const char *format, ...)
        msg[sizeof (msg) - 1] = '\0';
        va_end (ap);
 
+       if (list_log == NULL)
+       {
+               fprintf (stderr, "%s\n", msg);
+               return;
+       }
+
        le = llist_head (list_log);
        while (le != NULL)
        {
index b35fcf1..8b9449e 100644 (file)
@@ -26,6 +26,8 @@
 #include "configfile.h"
 #include "meta_data.h"
 
+#define PLUGIN_FLAGS_GLOBAL 0x0001
+
 #define DATA_MAX_NAME_LEN 64
 
 #define DS_TYPE_COUNTER  0
@@ -200,7 +202,7 @@ void plugin_set_dir (const char *dir);
  *
  * ARGUMENTS
  *  `name'      Name of the plugin to load.
- *  `mr'        Types of functions to request from the plugin.
+ *  `flags'     Hints on how to handle this plugin.
  *
  * RETURN VALUE
  *  Returns zero upon success, a value greater than zero if no plugin was found
@@ -209,7 +211,7 @@ void plugin_set_dir (const char *dir);
  * NOTES
  *  No attempt is made to re-load an already loaded module.
  */
-int plugin_load (const char *name);
+int plugin_load (const char *name, uint32_t flags);
 
 void plugin_init_all (void);
 void plugin_read_all (void);
@@ -262,7 +264,7 @@ int plugin_register_init (const char *name,
                plugin_init_cb callback);
 int plugin_register_read (const char *name,
                int (*callback) (void));
-int plugin_register_complex_read (const char *name,
+int plugin_register_complex_read (const char *group, const char *name,
                plugin_read_cb callback,
                const struct timespec *interval,
                user_data_t *user_data);
@@ -282,7 +284,7 @@ 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_complex_read (const char *name, void **user_data);
+int plugin_unregister_read_group (const char *group);
 int plugin_unregister_write (const char *name);
 int plugin_unregister_flush (const char *name);
 int plugin_unregister_shutdown (const char *name);
index d6dd0ba..dd53cb4 100644 (file)
@@ -2,19 +2,30 @@
  * collectd - src/postgresql.c
  * Copyright (C) 2008, 2009  Sebastian Harl
  * Copyright (C) 2009        Florian Forster
+ * All rights reserved.
  *
- * 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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
  *
- * 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.
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
  *
- * 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
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
  *
  * Authors:
  *   Sebastian Harl <sh at tokkee.org>
@@ -102,9 +113,12 @@ typedef struct {
        int max_params_num;
 
        /* user configuration */
+       udb_query_preparation_area_t **q_prep_areas;
        udb_query_t    **queries;
        size_t           queries_num;
 
+       int interval;
+
        char *host;
        char *port;
        char *database;
@@ -132,22 +146,16 @@ static int def_queries_num = STATIC_ARRAY_SIZE (def_queries);
 static udb_query_t      **queries       = NULL;
 static size_t             queries_num   = 0;
 
-static c_psql_database_t *databases     = NULL;
-static int                databases_num = 0;
-
 static c_psql_database_t *c_psql_database_new (const char *name)
 {
        c_psql_database_t *db;
 
-       ++databases_num;
-       if (NULL == (databases = (c_psql_database_t *)realloc (databases,
-                               databases_num * sizeof (*databases)))) {
+       db = (c_psql_database_t *)malloc (sizeof (*db));
+       if (NULL == db) {
                log_err ("Out of memory.");
-               exit (5);
+               return NULL;
        }
 
-       db = databases + (databases_num - 1);
-
        db->conn = NULL;
 
        C_COMPLAIN_INIT (&db->conn_complaint);
@@ -157,9 +165,12 @@ static c_psql_database_t *c_psql_database_new (const char *name)
 
        db->max_params_num = 0;
 
+       db->q_prep_areas   = NULL;
        db->queries        = NULL;
        db->queries_num    = 0;
 
+       db->interval   = 0;
+
        db->database   = sstrdup (name);
        db->host       = NULL;
        db->port       = NULL;
@@ -174,11 +185,20 @@ static c_psql_database_t *c_psql_database_new (const char *name)
        return db;
 } /* c_psql_database_new */
 
-static void c_psql_database_delete (c_psql_database_t *db)
+static void c_psql_database_delete (void *data)
 {
+       size_t i;
+
+       c_psql_database_t *db = data;
+
        PQfinish (db->conn);
        db->conn = NULL;
 
+       if (db->q_prep_areas)
+               for (i = 0; i < db->queries_num; ++i)
+                       udb_query_delete_preparation_area (db->q_prep_areas[i]);
+       free (db->q_prep_areas);
+
        sfree (db->queries);
        db->queries_num = 0;
 
@@ -196,8 +216,49 @@ static void c_psql_database_delete (c_psql_database_t *db)
        return;
 } /* c_psql_database_delete */
 
+static int c_psql_connect (c_psql_database_t *db)
+{
+       char  conninfo[4096];
+       char *buf     = conninfo;
+       int   buf_len = sizeof (conninfo);
+       int   status;
+
+       if (! db)
+               return -1;
+
+       status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
+       if (0 < status) {
+               buf     += status;
+               buf_len -= status;
+       }
+
+       C_PSQL_PAR_APPEND (buf, buf_len, "host",       db->host);
+       C_PSQL_PAR_APPEND (buf, buf_len, "port",       db->port);
+       C_PSQL_PAR_APPEND (buf, buf_len, "user",       db->user);
+       C_PSQL_PAR_APPEND (buf, buf_len, "password",   db->password);
+       C_PSQL_PAR_APPEND (buf, buf_len, "sslmode",    db->sslmode);
+       C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
+       C_PSQL_PAR_APPEND (buf, buf_len, "service",    db->service);
+
+       db->conn = PQconnectdb (conninfo);
+       db->proto_version = PQprotocolVersion (db->conn);
+       return 0;
+} /* c_psql_connect */
+
 static int c_psql_check_connection (c_psql_database_t *db)
 {
+       _Bool init = 0;
+
+       if (! db->conn) {
+               init = 1;
+
+               /* trigger c_release() */
+               if (0 == db->conn_complaint.interval)
+                       db->conn_complaint.interval = 1;
+
+               c_psql_connect (db);
+       }
+
        /* "ping" */
        PQclear (PQexec (db->conn, "SELECT 42;"));
 
@@ -216,15 +277,30 @@ static int c_psql_check_connection (c_psql_database_t *db)
                }
 
                db->proto_version = PQprotocolVersion (db->conn);
-               if (3 > db->proto_version)
-                       log_warn ("Protocol version %d does not support parameters.",
-                                       db->proto_version);
        }
 
        db->server_version = PQserverVersion (db->conn);
 
-       c_release (LOG_INFO, &db->conn_complaint,
-                       "Successfully reconnected to database %s", PQdb (db->conn));
+       if (c_would_release (&db->conn_complaint)) {
+               char *server_host;
+               int   server_version;
+
+               server_host    = PQhost (db->conn);
+               server_version = PQserverVersion (db->conn);
+
+               c_do_release (LOG_INFO, &db->conn_complaint,
+                               "Successfully %sconnected to database %s (user %s) "
+                               "at server %s%s%s (server version: %d.%d.%d, "
+                               "protocol version: %d, pid: %d)", init ? "" : "re",
+                               PQdb (db->conn), PQuser (db->conn),
+                               C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
+                               C_PSQL_SERVER_VERSION3 (server_version),
+                               db->proto_version, PQbackendPID (db->conn));
+
+               if (3 > db->proto_version)
+                       log_warn ("Protocol version %d does not support parameters.",
+                                       db->proto_version);
+       }
        return 0;
 } /* c_psql_check_connection */
 
@@ -259,7 +335,8 @@ static PGresult *c_psql_exec_query_params (c_psql_database_t *db,
                                params[i] = db->user;
                                break;
                        case C_PSQL_PARAM_INTERVAL:
-                               ssnprintf (interval, sizeof (interval), "%i", interval_g);
+                               ssnprintf (interval, sizeof (interval), "%i",
+                                               db->interval > 0 ? db->interval : interval_g);
                                params[i] = interval;
                                break;
                        default:
@@ -273,7 +350,8 @@ static PGresult *c_psql_exec_query_params (c_psql_database_t *db,
                        NULL, NULL, /* return text data */ 0);
 } /* c_psql_exec_query_params */
 
-static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q)
+static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q,
+               udb_query_preparation_area_t *prep_area)
 {
        PGresult *res;
 
@@ -345,7 +423,7 @@ static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q)
                 * `BAIL_OUT'. */
                column_names[col] = PQfname (res, col);
                if (NULL == column_names[col]) {
-                       log_err ("Failed to resolv name of column %i.", col);
+                       log_err ("Failed to resolve name of column %i.", col);
                        BAIL_OUT (-1);
                }
        }
@@ -356,8 +434,8 @@ static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q)
        else
                host = db->host;
 
-       status = udb_query_prepare_result (q, host, "postgresql",
-                       db->database, column_names, (size_t) column_num);
+       status = udb_query_prepare_result (q, prep_area, host, "postgresql",
+                       db->database, column_names, (size_t) column_num, db->interval);
        if (0 != status) {
                log_err ("udb_query_prepare_result failed with status %i.",
                                status);
@@ -380,46 +458,52 @@ static int c_psql_exec_query (c_psql_database_t *db, udb_query_t *q)
                if (col < column_num)
                        continue;
 
-               status = udb_query_handle_result (q, column_values);
+               status = udb_query_handle_result (q, prep_area, column_values);
                if (status != 0) {
                        log_err ("udb_query_handle_result failed with status %i.",
                                        status);
                }
        } /* for (row = 0; row < rows_num; ++row) */
 
+       udb_query_finish_result (q, prep_area);
+
        BAIL_OUT (0);
 #undef BAIL_OUT
 } /* c_psql_exec_query */
 
-static int c_psql_read (void)
+static int c_psql_read (user_data_t *ud)
 {
+       c_psql_database_t *db;
+
        int success = 0;
        int i;
 
-       for (i = 0; i < databases_num; ++i) {
-               c_psql_database_t *db = databases + i;
+       if ((ud == NULL) || (ud->data == NULL)) {
+               log_err ("c_psql_read: Invalid user data.");
+               return -1;
+       }
 
-               int j;
+       db = ud->data;
 
-               assert (NULL != db->database);
+       assert (NULL != db->database);
 
-               if (0 != c_psql_check_connection (db))
-                       continue;
+       if (0 != c_psql_check_connection (db))
+               return -1;
 
-               for (j = 0; j < db->queries_num; ++j)
-               {
-                       udb_query_t *q;
+       for (i = 0; i < db->queries_num; ++i)
+       {
+               udb_query_preparation_area_t *prep_area;
+               udb_query_t *q;
 
-                       q = db->queries[j];
+               prep_area = db->q_prep_areas[i];
+               q = db->queries[i];
 
-                       if ((0 != db->server_version)
+               if ((0 != db->server_version)
                                && (udb_query_check_version (q, db->server_version) <= 0))
-                               continue;
-
-                       c_psql_exec_query (db, q);
-               }
+                       continue;
 
-               ++success;
+               if (0 == c_psql_exec_query (db, q, prep_area))
+                       success = 1;
        }
 
        if (! success)
@@ -429,19 +513,7 @@ static int c_psql_read (void)
 
 static int c_psql_shutdown (void)
 {
-       int i;
-
-       if ((NULL == databases) || (0 == databases_num))
-               return 0;
-
-       plugin_unregister_read ("postgresql");
-       plugin_unregister_shutdown ("postgresql");
-
-       for (i = 0; i < databases_num; ++i)
-               c_psql_database_delete (databases + i);
-
-       sfree (databases);
-       databases_num = 0;
+       plugin_unregister_read_group ("postgresql");
 
        udb_query_free (queries, queries_num);
        queries = NULL;
@@ -450,70 +522,6 @@ static int c_psql_shutdown (void)
        return 0;
 } /* c_psql_shutdown */
 
-static int c_psql_init (void)
-{
-       int i;
-
-       if ((NULL == databases) || (0 == databases_num))
-               return 0;
-
-       for (i = 0; i < databases_num; ++i) {
-               c_psql_database_t *db = databases + i;
-
-               char  conninfo[4096];
-               char *buf     = conninfo;
-               int   buf_len = sizeof (conninfo);
-               int   status;
-
-               char *server_host;
-               int   server_version;
-
-               /* this will happen during reinitialization */
-               if (NULL != db->conn) {
-                       c_psql_check_connection (db);
-                       continue;
-               }
-
-               status = ssnprintf (buf, buf_len, "dbname = '%s'", db->database);
-               if (0 < status) {
-                       buf     += status;
-                       buf_len -= status;
-               }
-
-               C_PSQL_PAR_APPEND (buf, buf_len, "host",       db->host);
-               C_PSQL_PAR_APPEND (buf, buf_len, "port",       db->port);
-               C_PSQL_PAR_APPEND (buf, buf_len, "user",       db->user);
-               C_PSQL_PAR_APPEND (buf, buf_len, "password",   db->password);
-               C_PSQL_PAR_APPEND (buf, buf_len, "sslmode",    db->sslmode);
-               C_PSQL_PAR_APPEND (buf, buf_len, "krbsrvname", db->krbsrvname);
-               C_PSQL_PAR_APPEND (buf, buf_len, "service",    db->service);
-
-               db->conn = PQconnectdb (conninfo);
-               if (0 != c_psql_check_connection (db))
-                       continue;
-
-               db->proto_version = PQprotocolVersion (db->conn);
-
-               server_host    = PQhost (db->conn);
-               server_version = PQserverVersion (db->conn);
-               log_info ("Sucessfully connected to database %s (user %s) "
-                               "at server %s%s%s (server version: %d.%d.%d, "
-                               "protocol version: %d, pid: %d)",
-                               PQdb (db->conn), PQuser (db->conn),
-                               C_PSQL_SOCKET3 (server_host, PQport (db->conn)),
-                               C_PSQL_SERVER_VERSION3 (server_version),
-                               db->proto_version, PQbackendPID (db->conn));
-
-               if (3 > db->proto_version)
-                       log_warn ("Protocol version %d does not support parameters.",
-                                       db->proto_version);
-       }
-
-       plugin_register_read ("postgresql", c_psql_read);
-       plugin_register_shutdown ("postgresql", c_psql_shutdown);
-       return 0;
-} /* c_psql_init */
-
 static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
 {
        if ((0 != ci->children_num) || (1 != ci->values_num)
@@ -527,6 +535,28 @@ static int config_set_s (char *name, char **var, const oconfig_item_t *ci)
        return 0;
 } /* config_set_s */
 
+static int config_set_i (char *name, int *var,
+               const oconfig_item_t *ci, int min)
+{
+       int value;
+
+       if ((0 != ci->children_num) || (1 != ci->values_num)
+                       || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
+               log_err ("%s expects a single number argument.", name);
+               return 1;
+       }
+
+       value = (int)ci->values[0].value.number;
+
+       if (value < min) {
+               log_err ("%s expects a number greater or equal to %i.", name, min);
+               return 1;
+       }
+
+       *var = value;
+       return 0;
+} /* config_set_s */
+
 static int config_query_param_add (udb_query_t *q, oconfig_item_t *ci)
 {
        c_psql_user_data_t *data;
@@ -587,6 +617,10 @@ static int c_psql_config_database (oconfig_item_t *ci)
 {
        c_psql_database_t *db;
 
+       char cb_name[DATA_MAX_NAME_LEN];
+       struct timespec cb_interval;
+       user_data_t ud;
+
        int i;
 
        if ((1 != ci->values_num)
@@ -595,7 +629,11 @@ static int c_psql_config_database (oconfig_item_t *ci)
                return 1;
        }
 
+       memset (&ud, 0, sizeof (ud));
+
        db = c_psql_database_new (ci->values[0].value.string);
+       if (db == NULL)
+               return -1;
 
        for (i = 0; i < ci->children_num; ++i) {
                oconfig_item_t *c = ci->children + i;
@@ -617,25 +655,58 @@ static int c_psql_config_database (oconfig_item_t *ci)
                else if (0 == strcasecmp (c->key, "Query"))
                        udb_query_pick_from_list (c, queries, queries_num,
                                        &db->queries, &db->queries_num);
+               else if (0 == strcasecmp (c->key, "Interval"))
+                       config_set_i ("Interval", &db->interval, c, /* min = */ 1);
                else
                        log_warn ("Ignoring unknown config key \"%s\".", c->key);
        }
 
        /* If no `Query' options were given, add the default queries.. */
-       if (db->queries_num == 0)
-       {
+       if (db->queries_num == 0) {
                for (i = 0; i < def_queries_num; i++)
                        udb_query_pick_from_list_by_name (def_queries[i],
                                        queries, queries_num,
                                        &db->queries, &db->queries_num);
        }
 
+       if (db->queries_num > 0) {
+               db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
+                               db->queries_num, sizeof (*db->q_prep_areas));
+
+               if (db->q_prep_areas == NULL) {
+                       log_err ("Out of memory.");
+                       c_psql_database_delete (db);
+                       return -1;
+               }
+       }
+
        for (i = 0; (size_t)i < db->queries_num; ++i) {
                c_psql_user_data_t *data;
                data = udb_query_get_user_data (db->queries[i]);
                if ((data != NULL) && (data->params_num > db->max_params_num))
                        db->max_params_num = data->params_num;
+
+               db->q_prep_areas[i]
+                       = udb_query_allocate_preparation_area (db->queries[i]);
+
+               if (db->q_prep_areas[i] == NULL) {
+                       log_err ("Out of memory.");
+                       c_psql_database_delete (db);
+                       return -1;
+               }
        }
+
+       ud.data = db;
+       ud.free_func = c_psql_database_delete;
+
+       ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->database);
+
+       memset (&cb_interval, 0, sizeof (cb_interval));
+       if (db->interval > 0)
+               cb_interval.tv_sec = (time_t)db->interval;
+
+       plugin_register_complex_read ("postgresql", cb_name, c_psql_read,
+                       /* interval = */ &cb_interval, &ud);
        return 0;
 } /* c_psql_config_database */
 
@@ -679,7 +750,7 @@ static int c_psql_config (oconfig_item_t *ci)
 void module_register (void)
 {
        plugin_register_complex_config ("postgresql", c_psql_config);
-       plugin_register_init ("postgresql", c_psql_init);
+       plugin_register_shutdown ("postgresql", c_psql_shutdown);
 } /* module_register */
 
 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
index 5ef4acc..c642e58 100644 (file)
@@ -4,6 +4,9 @@
  * Copyright (C) 2006-2008  Florian octo Forster
  * Copyright (C) 2008       Oleg King
  * Copyright (C) 2009       Sebastian Harl
+ * Copyright (C) 2009       Andrés J. Díaz
+ * Copyright (C) 2009       Manuel Sanmartin
+ * Copyright (C) 2010       Clément Stenac
  *
  * 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
@@ -24,6 +27,9 @@
  *   Florian octo Forster <octo at verplant.org>
  *   Oleg King <king2 at kaluga.ru>
  *   Sebastian Harl <sh at tokkee.org>
+ *   Andrés J. Díaz <ajdiaz at connectical.com>
+ *   Manuel Sanmartin
+ *   Clément Stenac <clement.stenac at diwi.org>
  **/
 
 #include "collectd.h"
 #  include <sys/proc.h>
 /* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
 
+#elif HAVE_PROCINFO_H
+#  include <procinfo.h>
+#  include <sys/types.h>
+
+#define MAXPROCENTRY 32
+#define MAXTHRDENTRY 16
+#define MAXARGLN 1024
+/* #endif HAVE_PROCINFO_H */
+
 #else
 # error "No applicable input method."
 #endif
 #  define ARG_MAX 4096
 #endif
 
-#define BUFSIZE 256
-
 static const char *config_keys[] =
 {
        "Process",
@@ -124,6 +137,8 @@ typedef struct procstat_entry_s
        unsigned long num_lwp;
        unsigned long vmem_size;
        unsigned long vmem_rss;
+       unsigned long vmem_data;
+       unsigned long vmem_code;
        unsigned long stack_size;
 
        unsigned long vmem_minflt;
@@ -137,10 +152,10 @@ typedef struct procstat_entry_s
        unsigned long cpu_system_counter;
 
        /* io data */
-       long io_rchar;
-       long io_wchar;
-       long io_syscr;
-       long io_syscw;
+       derive_t io_rchar;
+       derive_t io_wchar;
+       derive_t io_syscr;
+       derive_t io_syscw;
 
        struct procstat_entry_s *next;
 } procstat_entry_t;
@@ -157,6 +172,8 @@ typedef struct procstat
        unsigned long num_lwp;
        unsigned long vmem_size;
        unsigned long vmem_rss;
+       unsigned long vmem_data;
+       unsigned long vmem_code;
        unsigned long stack_size;
 
        unsigned long vmem_minflt_counter;
@@ -166,10 +183,10 @@ typedef struct procstat
        unsigned long cpu_system_counter;
 
        /* io data */
-       long io_rchar;
-       long io_wchar;
-       long io_syscr;
-       long io_syscw;
+       derive_t io_rchar;
+       derive_t io_wchar;
+       derive_t io_syscr;
+       derive_t io_syscw;
 
        struct procstat   *next;
        struct procstat_entry_s *instances;
@@ -191,7 +208,19 @@ static long pagesize_g;
 
 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
 /* no global variables */
-#endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
+/* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
+
+#elif HAVE_PROCINFO_H
+static  struct procentry64 procentry[MAXPROCENTRY];
+static  struct thrdentry64 thrdentry[MAXTHRDENTRY];
+static int pagesize;
+
+#ifndef _AIXVERSION_610
+int     getprocs64 (void *procsinfo, int sizproc, void *fdsinfo, int sizfd, pid_t *index, int count);
+int     getthrds64( pid_t, void *, int, tid64_t *, int );
+#endif
+int getargs (struct procentry64 *processBuffer, int bufferLen, char *argsBuffer, int argsLen);
+#endif /* HAVE_PROCINFO_H */
 
 /* put name of process from config to list_head_g tree
    list_head_g is a list of 'procstat_t' structs with
@@ -237,13 +266,13 @@ static void ps_list_register (const char *name, const char *regexp)
                ERROR ("processes plugin: ps_list_register: "
                                "Regular expression \"%s\" found in config "
                                "file, but support for regular expressions "
-                               "has been dispabled at compile time.",
+                               "has been disabled at compile time.",
                                regexp);
                sfree (new);
                return;
        }
 #endif
-       
+
        for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
        {
                if (strcmp (ptr->name, name) == 0)
@@ -319,13 +348,13 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t
                if ((pse == NULL) || (pse->id != entry->id))
                {
                        procstat_entry_t *new;
-                       
+
                        new = (procstat_entry_t *) malloc (sizeof (procstat_entry_t));
                        if (new == NULL)
                                return;
                        memset (new, 0, sizeof (procstat_entry_t));
                        new->id = entry->id;
-                       
+
                        if (pse == NULL)
                                ps->instances = new;
                        else
@@ -339,6 +368,8 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t
                pse->num_lwp    = entry->num_lwp;
                pse->vmem_size  = entry->vmem_size;
                pse->vmem_rss   = entry->vmem_rss;
+               pse->vmem_data  = entry->vmem_data;
+               pse->vmem_code  = entry->vmem_code;
                pse->stack_size = entry->stack_size;
                pse->io_rchar   = entry->io_rchar;
                pse->io_wchar   = entry->io_wchar;
@@ -349,6 +380,8 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t
                ps->num_lwp    += pse->num_lwp;
                ps->vmem_size  += pse->vmem_size;
                ps->vmem_rss   += pse->vmem_rss;
+               ps->vmem_data  += pse->vmem_data;
+               ps->vmem_code  += pse->vmem_code;
                ps->stack_size += pse->stack_size;
 
                ps->io_rchar   += ((pse->io_rchar == -1)?0:pse->io_rchar);
@@ -377,7 +410,7 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t
                                pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
                        }
                        pse->vmem_minflt_counter = entry->vmem_minflt_counter;
-                       
+
                        if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
                        {
                                pse->vmem_majflt = entry->vmem_majflt_counter
@@ -414,7 +447,7 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t
                                pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
                        }
                        pse->cpu_user_counter = entry->cpu_user_counter;
-                       
+
                        if (entry->cpu_system_counter < pse->cpu_system_counter)
                        {
                                pse->cpu_system = entry->cpu_system_counter
@@ -445,6 +478,8 @@ static void ps_list_reset (void)
                ps->num_lwp     = 0;
                ps->vmem_size   = 0;
                ps->vmem_rss    = 0;
+               ps->vmem_data   = 0;
+               ps->vmem_code   = 0;
                ps->stack_size  = 0;
                ps->io_rchar = -1;
                ps->io_wchar = -1;
@@ -563,7 +598,11 @@ static int ps_init (void)
 
 #elif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD
 /* no initialization */
-#endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
+/* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
+
+#elif HAVE_PROCINFO_H
+       pagesize = getpagesize();
+#endif /* HAVE_PROCINFO_H */
 
        return (0);
 } /* int ps_init */
@@ -609,6 +648,16 @@ static void ps_submit_proc_list (procstat_t *ps)
        vl.values_len = 1;
        plugin_dispatch_values (&vl);
 
+       sstrncpy (vl.type, "ps_data", sizeof (vl.type));
+       vl.values[0].gauge = ps->vmem_data;
+       vl.values_len = 1;
+       plugin_dispatch_values (&vl);
+
+       sstrncpy (vl.type, "ps_code", sizeof (vl.type));
+       vl.values[0].gauge = ps->vmem_code;
+       vl.values_len = 1;
+       plugin_dispatch_values (&vl);
+
        sstrncpy (vl.type, "ps_stacksize", sizeof (vl.type));
        vl.values[0].gauge = ps->stack_size;
        vl.values_len = 1;
@@ -635,8 +684,8 @@ static void ps_submit_proc_list (procstat_t *ps)
        if ( (ps->io_rchar != -1) && (ps->io_wchar != -1) )
        {
                sstrncpy (vl.type, "ps_disk_octets", sizeof (vl.type));
-               vl.values[0].counter = ps->io_rchar;
-               vl.values[1].counter = ps->io_wchar;
+               vl.values[0].derive = ps->io_rchar;
+               vl.values[1].derive = ps->io_wchar;
                vl.values_len = 2;
                plugin_dispatch_values (&vl);
        }
@@ -644,18 +693,22 @@ static void ps_submit_proc_list (procstat_t *ps)
        if ( (ps->io_syscr != -1) && (ps->io_syscw != -1) )
        {
                sstrncpy (vl.type, "ps_disk_ops", sizeof (vl.type));
-               vl.values[0].counter = ps->io_syscr;
-               vl.values[1].counter = ps->io_syscw;
+               vl.values[0].derive = ps->io_syscr;
+               vl.values[1].derive = ps->io_syscw;
                vl.values_len = 2;
                plugin_dispatch_values (&vl);
        }
 
-       DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
+       DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; "
+                        "vmem_size = %lu; vmem_rss = %lu; vmem_data = %lu; "
+                       "vmem_code = %lu; "
                        "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
                        "cpu_user_counter = %lu; cpu_system_counter = %lu; "
-                       "io_rchar = %ld; io_wchar = %ld; "
-                       "io_syscr = %ld; io_syscw = %ld;",
-                       ps->name, ps->num_proc, ps->num_lwp, ps->vmem_rss,
+                       "io_rchar = %"PRIi64"; io_wchar = %"PRIi64"; "
+                       "io_syscr = %"PRIi64"; io_syscw = %"PRIi64";",
+                       ps->name, ps->num_proc, ps->num_lwp,
+                       ps->vmem_size, ps->vmem_rss,
+                       ps->vmem_data, ps->vmem_code,
                        ps->vmem_minflt_counter, ps->vmem_majflt_counter,
                        ps->cpu_user_counter, ps->cpu_system_counter,
                        ps->io_rchar, ps->io_wchar, ps->io_syscr, ps->io_syscw);
@@ -690,6 +743,69 @@ static int ps_read_tasks (int pid)
        return ((count >= 1) ? count : 1);
 } /* int *ps_read_tasks */
 
+/* Read advanced virtual memory data from /proc/pid/status */
+static procstat_t *ps_read_vmem (int pid, procstat_t *ps)
+{
+       FILE *fh;
+       char buffer[1024];
+       char filename[64];
+       unsigned long long lib = 0;
+       unsigned long long exe = 0;
+       unsigned long long data = 0;
+       char *fields[8];
+       int numfields;
+
+       ssnprintf (filename, sizeof (filename), "/proc/%i/status", pid);
+       if ((fh = fopen (filename, "r")) == NULL)
+               return (NULL);
+
+       while (fgets (buffer, sizeof(buffer), fh) != NULL)
+       {
+               long long tmp;
+               char *endptr;
+
+               if (strncmp (buffer, "Vm", 2) != 0)
+                       continue;
+
+               numfields = strsplit (buffer, fields,
+                                      STATIC_ARRAY_SIZE (fields));
+
+               if (numfields < 2)
+                       continue;
+
+               errno = 0;
+               endptr = NULL;
+               tmp = strtoll (fields[1], &endptr, /* base = */ 10);
+               if ((errno == 0) && (endptr != fields[1]))
+               {
+                       if (strncmp (buffer, "VmData", 6) == 0) 
+                       {
+                               data = tmp;
+                       }
+                       else if (strncmp (buffer, "VmLib", 5) == 0)
+                       {
+                               lib = tmp;
+                       }
+                       else if  (strncmp(buffer, "VmExe", 5) == 0)
+                       {
+                               exe = tmp;
+                       }
+               }
+       } /* while (fgets) */
+
+       if (fclose (fh))
+       {
+               char errbuf[1024];
+               WARNING ("processes: fclose: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+       }
+
+       ps->vmem_data = data * 1024;
+       ps->vmem_code = (exe + lib) * 1024;
+
+       return (ps);
+} /* procstat_t *ps_read_vmem */
+
 static procstat_t *ps_read_io (int pid, procstat_t *ps)
 {
        FILE *fh;
@@ -703,9 +819,11 @@ static procstat_t *ps_read_io (int pid, procstat_t *ps)
        if ((fh = fopen (filename, "r")) == NULL)
                return (NULL);
 
-       while (fgets (buffer, 1024, fh) != NULL)
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
        {
-               long *val = NULL;
+               derive_t *val = NULL;
+               long long tmp;
+               char *endptr;
 
                if (strncasecmp (buffer, "rchar:", 6) == 0)
                        val = &(ps->io_rchar);
@@ -718,13 +836,20 @@ static procstat_t *ps_read_io (int pid, procstat_t *ps)
                else
                        continue;
 
-               numfields = strsplit (buffer, fields, 8);
+               numfields = strsplit (buffer, fields,
+                               STATIC_ARRAY_SIZE (fields));
 
                if (numfields < 2)
                        continue;
 
-               *val = atol (fields[1]);
-       }
+               errno = 0;
+               endptr = NULL;
+               tmp = strtoll (fields[1], &endptr, /* base = */ 10);
+               if ((errno != 0) || (endptr == fields[1]))
+                       *val = -1;
+               else
+                       *val = (derive_t) tmp;
+       } /* while (fgets) */
 
        if (fclose (fh))
        {
@@ -764,7 +889,7 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
                return (-1);
        buffer[i] = 0;
 
-       fields_len = strsplit (buffer, fields, 64);
+       fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
        if (fields_len < 24)
        {
                DEBUG ("processes plugin: ps_read_process (pid = %i):"
@@ -832,6 +957,14 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
        cpu_system_counter = cpu_system_counter * 1000000 / CONFIG_HZ;
        vmem_rss = vmem_rss * pagesize_g;
 
+       if ( (ps_read_vmem(pid, ps)) == NULL)
+       {
+               /* No VMem data */
+               ps->vmem_data = -1;
+               ps->vmem_code = -1;
+               DEBUG("ps_read_process: did not get vmem data for pid %i",pid);
+       }
+
        ps->cpu_user_counter = (unsigned long) cpu_user_counter;
        ps->cpu_system_counter = (unsigned long) cpu_system_counter;
        ps->vmem_size = (unsigned long) vmem_size;
@@ -972,7 +1105,7 @@ static unsigned long read_fork_rate ()
 
                errno = 0;
                endptr = NULL;
-               result = strtoul(fields[1], &endptr, 10);
+               result = strtoul(fields[1], &endptr, /* base = */ 10);
                if ((endptr == fields[1]) || (errno != 0)) {
                        ERROR ("processes plugin: Cannot parse fork rate: %s",
                                        fields[1]);
@@ -1171,7 +1304,11 @@ static int ps_read (void)
                                }
 
                                pse.num_proc++;
+                               pse.vmem_size = task_basic_info.virtual_size;
                                pse.vmem_rss = task_basic_info.resident_size;
+                               /* Does not seem to be easily exposed */
+                               pse.vmem_data = 0;
+                               pse.vmem_code = 0;
 
                                pse.vmem_minflt_counter = task_events_info.cow_faults;
                                pse.vmem_majflt_counter = task_events_info.faults;
@@ -1365,6 +1502,8 @@ static int ps_read (void)
                pse.num_lwp    = ps.num_lwp;
                pse.vmem_size  = ps.vmem_size;
                pse.vmem_rss   = ps.vmem_rss;
+               pse.vmem_data  = ps.vmem_data;
+               pse.vmem_code  = ps.vmem_code;
                pse.stack_size = ps.stack_size;
 
                pse.vmem_minflt = 0;
@@ -1495,6 +1634,8 @@ static int ps_read (void)
 
                pse.vmem_size = procs[i].ki_size;
                pse.vmem_rss = procs[i].ki_rssize * getpagesize();
+               pse.vmem_data = procs[i].ki_dsize * getpagesize();
+               pse.vmem_code = procs[i].ki_tsize * getpagesize();
                pse.stack_size = procs[i].ki_ssize * getpagesize();
                pse.vmem_minflt = 0;
                pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
@@ -1542,7 +1683,136 @@ static int ps_read (void)
 
        for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
                ps_submit_proc_list (ps_ptr);
-#endif /* HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
+/* #endif HAVE_LIBKVM_GETPROCS && HAVE_STRUCT_KINFO_PROC_FREEBSD */
+
+#elif HAVE_PROCINFO_H
+       /* AIX */
+       int running  = 0;
+       int sleeping = 0;
+       int zombies  = 0;
+       int stopped  = 0;
+       int paging   = 0;
+       int blocked  = 0;
+
+       pid_t pindex = 0;
+       int nprocs;
+
+       procstat_t *ps;
+       procstat_entry_t pse;
+
+       ps_list_reset ();
+       while ((nprocs = getprocs64 (procentry, sizeof(struct procentry64),
+                                       /* fdsinfo = */ NULL, sizeof(struct fdsinfo64),
+                                       &pindex, MAXPROCENTRY)) > 0)
+       {
+               int i;
+
+               for (i = 0; i < nprocs; i++)
+               {
+                       tid64_t thindex;
+                       int nthreads;
+                       char arglist[MAXARGLN+1];
+                       char *cargs;
+                       char *cmdline;
+
+                       if (procentry[i].pi_state == SNONE) continue;
+                       /* if (procentry[i].pi_state == SZOMB)  FIXME */
+
+                       cmdline = procentry[i].pi_comm;
+                       cargs = procentry[i].pi_comm;
+                       if ( procentry[i].pi_flags & SKPROC )
+                       {
+                               if (procentry[i].pi_pid == 0)
+                                       cmdline = "swapper";
+                               cargs = cmdline;
+                       }
+                       else
+                       {
+                               if (getargs(&procentry[i], sizeof(struct procentry64), arglist, MAXARGLN) >= 0)
+                               {
+                                       int n;
+
+                                       n = -1;
+                                       while (++n < MAXARGLN)
+                                       {
+                                               if (arglist[n] == '\0')
+                                               {
+                                                       if (arglist[n+1] == '\0')
+                                                               break;
+                                                       arglist[n] = ' ';
+                                               }
+                                       }
+                                       cargs = arglist;
+                               }
+                       }
+
+                       pse.id       = procentry[i].pi_pid;
+                       pse.age      = 0;
+                       pse.num_lwp  = procentry[i].pi_thcount;
+                       pse.num_proc = 1;
+
+                       thindex=0;
+                       while ((nthreads = getthrds64(procentry[i].pi_pid,
+                                                       thrdentry, sizeof(struct thrdentry64),
+                                                       &thindex, MAXTHRDENTRY)) > 0)
+                       {
+                               int j;
+
+                               for (j=0; j< nthreads; j++)
+                               {
+                                       switch (thrdentry[j].ti_state)
+                                       {
+                                               /* case TSNONE: break; */
+                                               case TSIDL:     blocked++;      break; /* FIXME is really blocked */
+                                               case TSRUN:     running++;      break;
+                                               case TSSLEEP:   sleeping++;     break;
+                                               case TSSWAP:    paging++;       break;
+                                               case TSSTOP:    stopped++;      break;
+                                               case TSZOMB:    zombies++;      break;
+                                       }
+                               }
+                               if (nthreads < MAXTHRDENTRY)
+                                       break;
+                       }
+
+                       pse.cpu_user = 0;
+                       /* tv_usec is nanosec ??? */
+                       pse.cpu_user_counter = procentry[i].pi_ru.ru_utime.tv_sec * 1000000 +
+                               procentry[i].pi_ru.ru_utime.tv_usec / 1000;
+
+                       pse.cpu_system = 0;
+                       /* tv_usec is nanosec ??? */
+                       pse.cpu_system_counter = procentry[i].pi_ru.ru_stime.tv_sec * 1000000 +
+                               procentry[i].pi_ru.ru_stime.tv_usec / 1000;
+
+                       pse.vmem_minflt = 0;
+                       pse.vmem_minflt_counter = procentry[i].pi_minflt;
+                       pse.vmem_majflt = 0;
+                       pse.vmem_majflt_counter = procentry[i].pi_majflt;
+
+                       pse.vmem_size = procentry[i].pi_tsize + procentry[i].pi_dvm * pagesize;
+                       pse.vmem_rss = (procentry[i].pi_drss + procentry[i].pi_trss) * pagesize;
+                       /* Not supported */
+                       pse.vmem_data = 0;
+                       pse.vmem_code = 0;
+                       pse.stack_size =  0;
+
+                       ps_list_add (cmdline, cargs, &pse);
+               } /* for (i = 0 .. nprocs) */
+
+               if (nprocs < MAXPROCENTRY)
+                       break;
+       } /* while (getprocs64() > 0) */
+       ps_submit_state ("running",  running);
+       ps_submit_state ("sleeping", sleeping);
+       ps_submit_state ("zombies",  zombies);
+       ps_submit_state ("stopped",  stopped);
+       ps_submit_state ("paging",   paging);
+       ps_submit_state ("blocked",  blocked);
+
+       for (ps = list_head_g; ps != NULL; ps = ps->next)
+               ps_submit_proc_list (ps);
+#endif /* HAVE_PROCINFO_H */
 
        return (0);
 } /* int ps_read */
index 73fe154..e90c1a4 100644 (file)
@@ -166,7 +166,7 @@ static int read_file (const char *path)
     if (key_fields_num != value_fields_num)
     {
       ERROR ("protocols plugin: Number of fields in keys and values lines "
-          "dont match: %i vs %i.",
+          "don't match: %i vs %i.",
           key_fields_num, value_fields_num);
       break;
     }
diff --git a/src/pyconfig.c b/src/pyconfig.c
new file mode 100644 (file)
index 0000000..b5c01aa
--- /dev/null
@@ -0,0 +1,217 @@
+/**
+ * collectd - src/pyconfig.c
+ * Copyright (C) 2009  Sven Trenkel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Sven Trenkel <collectd at semidefinite.de>  
+ **/
+
+#include <Python.h>
+#include <structmember.h>
+
+#include "collectd.h"
+#include "common.h"
+
+#include "cpython.h"
+
+static char config_doc[] = "This represents a piece of collectd's config file.\n"
+               "It is passed to scripts with config callbacks (see \"register_config\")\n"
+               "and is of little use if created somewhere else.\n"
+               "\n"
+               "It has no methods beyond the bare minimum and only exists for its\n"
+               "data members";
+
+static char parent_doc[] = "This represents the parent of this node. On the root node\n"
+               "of the config tree it will be None.\n";
+
+static char key_doc[] = "This is the keyword of this item, ie the first word of any\n"
+               "given line in the config file. It will always be a string.\n";
+
+static char values_doc[] = "This is a tuple (which might be empty) of all value, ie words\n"
+               "following the keyword in any given line in the config file.\n"
+               "\n"
+               "Every item in this tuple will be either a string or a float or a bool,\n"
+               "depending on the contents of the configuration file.\n";
+
+static char children_doc[] = "This is a tuple of child nodes. For most nodes this will be\n"
+               "empty. If this node represents a block instead of a single line of the config\n"
+               "file it will contain all nodes in this block.\n";
+
+static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+       Config *self;
+       
+       self = (Config *) type->tp_alloc(type, 0);
+       if (self == NULL)
+               return NULL;
+       
+       self->parent = NULL;
+       self->key = NULL;
+       self->values = NULL;
+       self->children = NULL;
+       return (PyObject *) self;
+}
+
+static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) {
+       PyObject *key = NULL, *parent = NULL, *values = NULL, *children = NULL, *tmp;
+       Config *self = (Config *) s;
+       static char *kwlist[] = {"key", "parent", "values", "children", NULL};
+       
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOO", kwlist,
+                       &key, &parent, &values, &children))
+               return -1;
+       
+       if (!IS_BYTES_OR_UNICODE(key)) {
+               PyErr_SetString(PyExc_TypeError, "argument 1 must be str");
+               Py_XDECREF(parent);
+               Py_XDECREF(values);
+               Py_XDECREF(children);
+               return -1;
+       }
+       if (values == NULL) {
+               values = PyTuple_New(0);
+               PyErr_Clear();
+       }
+       if (children == NULL) {
+               children = PyTuple_New(0);
+               PyErr_Clear();
+       }
+       tmp = self->key;
+       Py_INCREF(key);
+       self->key = key;
+       Py_XDECREF(tmp);
+       if (parent != NULL) {
+               tmp = self->parent;
+               Py_INCREF(parent);
+               self->parent = parent;
+               Py_XDECREF(tmp);
+       }
+       if (values != NULL) {
+               tmp = self->values;
+               Py_INCREF(values);
+               self->values = values;
+               Py_XDECREF(tmp);
+       }
+       if (children != NULL) {
+               tmp = self->children;
+               Py_INCREF(children);
+               self->children = children;
+               Py_XDECREF(tmp);
+       }
+       return 0;
+}
+
+static PyObject *Config_repr(PyObject *s) {
+       Config *self = (Config *) s;
+       PyObject *ret = NULL;
+       static PyObject *node_prefix = NULL, *root_prefix = NULL, *ending = NULL;
+       
+       /* This is ok because we have the GIL, so this is thread-save by default. */
+       if (node_prefix == NULL)
+               node_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config node ");
+       if (root_prefix == NULL)
+               root_prefix = cpy_string_to_unicode_or_bytes("<collectd.Config root node ");
+       if (ending == NULL)
+               ending = cpy_string_to_unicode_or_bytes(">");
+       if (node_prefix == NULL || root_prefix == NULL || ending == NULL)
+               return NULL;
+       
+       ret = PyObject_Str(self->key);
+       CPY_SUBSTITUTE(PyObject_Repr, ret, ret);
+       if (self->parent == NULL || self->parent == Py_None)
+               CPY_STRCAT(&ret, root_prefix);
+       else
+               CPY_STRCAT(&ret, node_prefix);
+       CPY_STRCAT(&ret, ending);
+       
+       return ret;
+}
+
+static int Config_traverse(PyObject *self, visitproc visit, void *arg) {
+       Config *c = (Config *) self;
+       Py_VISIT(c->parent);
+       Py_VISIT(c->key);
+       Py_VISIT(c->values);
+       Py_VISIT(c->children);
+       return 0;}
+
+static int Config_clear(PyObject *self) {
+       Config *c = (Config *) self;
+       Py_CLEAR(c->parent);
+       Py_CLEAR(c->key);
+       Py_CLEAR(c->values);
+       Py_CLEAR(c->children);
+       return 0;
+}
+
+static void Config_dealloc(PyObject *self) {
+       Config_clear(self);
+       self->ob_type->tp_free(self);
+}
+
+static PyMemberDef Config_members[] = {
+       {"parent", T_OBJECT, offsetof(Config, parent), 0, parent_doc},
+       {"key", T_OBJECT_EX, offsetof(Config, key), 0, key_doc},
+       {"values", T_OBJECT_EX, offsetof(Config, values), 0, values_doc},
+       {"children", T_OBJECT_EX, offsetof(Config, children), 0, children_doc},
+       {NULL}
+};
+
+PyTypeObject ConfigType = {
+       CPY_INIT_TYPE
+       "collectd.Config",         /* tp_name */
+       sizeof(Config),            /* tp_basicsize */
+       0,                         /* Will be filled in later */
+       Config_dealloc,            /* tp_dealloc */
+       0,                         /* tp_print */
+       0,                         /* tp_getattr */
+       0,                         /* tp_setattr */
+       0,                         /* tp_compare */
+       Config_repr,               /* tp_repr */
+       0,                         /* tp_as_number */
+       0,                         /* tp_as_sequence */
+       0,                         /* tp_as_mapping */
+       0,                         /* tp_hash */
+       0,                         /* tp_call */
+       0,                         /* tp_str */
+       0,                         /* tp_getattro */
+       0,                         /* tp_setattro */
+       0,                         /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+       config_doc,                /* tp_doc */
+       Config_traverse,           /* tp_traverse */
+       Config_clear,              /* tp_clear */
+       0,                         /* tp_richcompare */
+       0,                         /* tp_weaklistoffset */
+       0,                         /* tp_iter */
+       0,                         /* tp_iternext */
+       0,                         /* tp_methods */
+       Config_members,            /* tp_members */
+       0,                         /* tp_getset */
+       0,                         /* tp_base */
+       0,                         /* tp_dict */
+       0,                         /* tp_descr_get */
+       0,                         /* tp_descr_set */
+       0,                         /* tp_dictoffset */
+       Config_init,               /* tp_init */
+       0,                         /* tp_alloc */
+       Config_new                 /* tp_new */
+};
+
diff --git a/src/python.c b/src/python.c
new file mode 100644 (file)
index 0000000..00516fb
--- /dev/null
@@ -0,0 +1,1133 @@
+/**
+ * collectd - src/python.c
+ * Copyright (C) 2009  Sven Trenkel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Sven Trenkel <collectd at semidefinite.de>  
+ **/
+
+#include <Python.h>
+#include <structmember.h>
+
+#include <signal.h>
+#if HAVE_PTHREAD_H
+# include <pthread.h>
+#endif
+
+#include "collectd.h"
+#include "common.h"
+
+#include "cpython.h"
+
+typedef struct cpy_callback_s {
+       char *name;
+       PyObject *callback;
+       PyObject *data;
+       struct cpy_callback_s *next;
+} cpy_callback_t;
+
+static char log_doc[] = "This function sends a string to all logging plugins.";
+
+static char flush_doc[] = "flush([plugin][, timeout][, identifier]) -> None\n"
+               "\n"
+               "Flushes the cache of another plugin.";
+
+static char unregister_doc[] = "Unregisters a callback. This function needs exactly one parameter either\n"
+               "the function to unregister or the callback identifier to unregister.";
+
+static char reg_log_doc[] = "register_log(callback[, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function for log messages.\n"
+               "\n"
+               "'callback' is a callable object that will be called every time something\n"
+               "    is logged.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function every time it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called with two or three parameters:\n"
+               "severity: An integer that should be compared to the LOG_ constants.\n"
+               "message: The text to be logged.\n"
+               "data: The optional data parameter passed to the register function.\n"
+               "    If the parameter was omitted it will be omitted here, too.";
+
+static char reg_init_doc[] = "register_init(callback[, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function that will be executed once after the config.\n"
+               "file has been read, all plugins heve been loaded and the collectd has\n"
+               "forked into the background.\n"
+               "\n"
+               "'callback' is a callable object that will be executed.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function when it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called without parameters, except for\n"
+               "data if it was supplied.";
+
+static char reg_config_doc[] = "register_config(callback[, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function for config file entries.\n"
+               "'callback' is a callable object that will be called for every config block.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function every time it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called with one or two parameters:\n"
+               "config: A Config object.\n"
+               "data: The optional data parameter passed to the register function.\n"
+               "    If the parameter was omitted it will be omitted here, too.";
+
+static char reg_read_doc[] = "register_read(callback[, interval][, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function for reading data. It will just be called\n"
+               "in a fixed interval to signal that it's time to dispatch new values.\n"
+               "'callback' is a callable object that will be called every time something\n"
+               "    is logged.\n"
+               "'interval' is the number of seconds between between calls to the callback\n"
+               "    function. Full float precision is supported here.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function every time it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called without parameters, except for\n"
+               "data if it was supplied.";
+
+static char reg_write_doc[] = "register_write(callback[, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function to receive values dispatched by other plugins.\n"
+               "'callback' is a callable object that will be called every time a value\n"
+               "    is dispatched.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function every time it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called with one or two parameters:\n"
+               "values: A Values object which is a copy of the dispatched values.\n"
+               "data: The optional data parameter passed to the register function.\n"
+               "    If the parameter was omitted it will be omitted here, too.";
+
+static char reg_notification_doc[] = "register_notification(callback[, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function for notifications.\n"
+               "'callback' is a callable object that will be called every time a notification\n"
+               "    is dispatched.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function every time it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called with one or two parameters:\n"
+               "notification: A copy of the notification that was dispatched.\n"
+               "data: The optional data parameter passed to the register function.\n"
+               "    If the parameter was omitted it will be omitted here, too.";
+
+static char reg_flush_doc[] = "register_flush(callback[, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function for flush messages.\n"
+               "'callback' is a callable object that will be called every time a plugin\n"
+               "    requests a flush for either this or all plugins.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function every time it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called with two or three parameters:\n"
+               "timeout: Indicates that only data older than 'timeout' seconds is to\n"
+               "    be flushed.\n"
+               "id: Specifies which values are to be flushed.\n"
+               "data: The optional data parameter passed to the register function.\n"
+               "    If the parameter was omitted it will be omitted here, too.";
+
+static char reg_shutdown_doc[] = "register_shutdown(callback[, data][, name]) -> identifier\n"
+               "\n"
+               "Register a callback function for collectd shutdown.\n"
+               "'callback' is a callable object that will be called once collectd is\n"
+               "    shutting down.\n"
+               "'data' is an optional object that will be passed back to the callback\n"
+               "    function if it is called.\n"
+               "'name' is an optional identifier for this callback. The default name\n"
+               "    is 'python.<module>'.\n"
+               "    Every callback needs a unique identifier, so if you want to\n"
+               "    register this callback multiple time from the same module you need\n"
+               "    to specify a name here.\n"
+               "'identifier' is the full identifier assigned to this callback.\n"
+               "\n"
+               "The callback function will be called with no parameters except for\n"
+               "    data if it was supplied.";
+
+
+static int do_interactive = 0;
+
+/* This is our global thread state. Python saves some stuff in thread-local
+ * storage. So if we allow the interpreter to run in the background
+ * (the scriptwriters might have created some threads from python), we have
+ * to save the state so we can resume it later after shutdown. */
+
+static PyThreadState *state;
+
+static PyObject *cpy_format_exception;
+
+static cpy_callback_t *cpy_config_callbacks;
+static cpy_callback_t *cpy_init_callbacks;
+static cpy_callback_t *cpy_shutdown_callbacks;
+
+static void cpy_destroy_user_data(void *data) {
+       cpy_callback_t *c = data;
+       free(c->name);
+       Py_DECREF(c->callback);
+       Py_XDECREF(c->data);
+       free(c);
+}
+
+/* You must hold the GIL to call this function!
+ * But if you managed to extract the callback parameter then you probably already do. */
+
+static void cpy_build_name(char *buf, size_t size, PyObject *callback, const char *name) {
+       const char *module = NULL;
+       PyObject *mod = NULL;
+       
+       if (name != NULL) {
+               snprintf(buf, size, "python.%s", name);
+               return;
+       }
+       
+       mod = PyObject_GetAttrString(callback, "__module__"); /* New reference. */
+       if (mod != NULL)
+               module = cpy_unicode_or_bytes_to_string(&mod);
+       
+       if (module != NULL) {
+               snprintf(buf, size, "python.%s", module);
+               Py_XDECREF(mod);
+               PyErr_Clear();
+               return;
+       }
+       Py_XDECREF(mod);
+       
+       snprintf(buf, size, "python.%p", callback);
+       PyErr_Clear();
+}
+
+void cpy_log_exception(const char *context) {
+       int l = 0, i;
+       const char *typename = NULL, *message = NULL;
+       PyObject *type, *value, *traceback, *tn, *m, *list;
+       
+       PyErr_Fetch(&type, &value, &traceback);
+       PyErr_NormalizeException(&type, &value, &traceback);
+       if (type == NULL) return;
+       tn = PyObject_GetAttrString(type, "__name__"); /* New reference. */
+       m = PyObject_Str(value); /* New reference. */
+       if (tn != NULL)
+               typename = cpy_unicode_or_bytes_to_string(&tn);
+       if (m != NULL)
+               message = cpy_unicode_or_bytes_to_string(&m);
+       if (typename == NULL)
+               typename = "NamelessException";
+       if (message == NULL)
+               message = "N/A";
+       Py_BEGIN_ALLOW_THREADS
+       ERROR("Unhandled python exception in %s: %s: %s", context, typename, message);
+       Py_END_ALLOW_THREADS
+       Py_XDECREF(tn);
+       Py_XDECREF(m);
+       if (!cpy_format_exception) {
+               PyErr_Clear();
+               Py_XDECREF(type);
+               Py_XDECREF(value);
+               Py_XDECREF(traceback);
+               return;
+       }
+       if (!traceback) {
+               PyErr_Clear();
+               return;
+       }
+       list = PyObject_CallFunction(cpy_format_exception, "NNN", type, value, traceback); /* New reference. */
+       if (list)
+               l = PyObject_Length(list);
+       for (i = 0; i < l; ++i) {
+               char *s;
+               PyObject *line;
+               
+               line = PyList_GET_ITEM(list, i); /* Borrowed reference. */
+               Py_INCREF(line);
+               s = strdup(cpy_unicode_or_bytes_to_string(&line));
+               Py_DECREF(line);
+               if (s[strlen(s) - 1] == '\n')
+                       s[strlen(s) - 1] = 0;
+               Py_BEGIN_ALLOW_THREADS
+               ERROR("%s", s);
+               Py_END_ALLOW_THREADS
+               free(s);
+       }
+       Py_XDECREF(list);
+       PyErr_Clear();
+}
+
+static int cpy_read_callback(user_data_t *data) {
+       cpy_callback_t *c = data->data;
+       PyObject *ret;
+
+       CPY_LOCK_THREADS
+               ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
+               if (ret == NULL) {
+                       cpy_log_exception("read callback");
+               } else {
+                       Py_DECREF(ret);
+               }
+       CPY_RELEASE_THREADS
+       if (ret == NULL)
+               return 1;
+       return 0;
+}
+
+static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_list, user_data_t *data) {
+       int i;
+       cpy_callback_t *c = data->data;
+       PyObject *ret, *list, *temp, *dict = NULL;
+       Values *v;
+
+       CPY_LOCK_THREADS
+               list = PyList_New(value_list->values_len); /* New reference. */
+               if (list == NULL) {
+                       cpy_log_exception("write callback");
+                       CPY_RETURN_FROM_THREADS 0;
+               }
+               for (i = 0; i < value_list->values_len; ++i) {
+                       if (ds->ds->type == DS_TYPE_COUNTER) {
+                               if ((long) value_list->values[i].counter == value_list->values[i].counter)
+                                       PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].counter));
+                               else
+                                       PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].counter));
+                       } else if (ds->ds->type == DS_TYPE_GAUGE) {
+                               PyList_SetItem(list, i, PyFloat_FromDouble(value_list->values[i].gauge));
+                       } else if (ds->ds->type == DS_TYPE_DERIVE) {
+                               if ((long) value_list->values[i].derive == value_list->values[i].derive)
+                                       PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].derive));
+                               else
+                                       PyList_SetItem(list, i, PyLong_FromLongLong(value_list->values[i].derive));
+                       } else if (ds->ds->type == DS_TYPE_ABSOLUTE) {
+                               if ((long) value_list->values[i].absolute == value_list->values[i].absolute)
+                                       PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].absolute));
+                               else
+                                       PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].absolute));
+                       } else {
+                               Py_BEGIN_ALLOW_THREADS
+                               ERROR("cpy_write_callback: Unknown value type %d.", ds->ds->type);
+                               Py_END_ALLOW_THREADS
+                               Py_DECREF(list);
+                               CPY_RETURN_FROM_THREADS 0;
+                       }
+                       if (PyErr_Occurred() != NULL) {
+                               cpy_log_exception("value building for write callback");
+                               CPY_RETURN_FROM_THREADS 0;
+                       }
+               }
+               dict = PyDict_New();
+               if (value_list->meta) {
+                       int i, num;
+                       char **table;
+                       meta_data_t *meta = value_list->meta;
+
+                       num = meta_data_toc(meta, &table);
+                       for (i = 0; i < num; ++i) {
+                               int type;
+                               char *string;
+                               int64_t si;
+                               uint64_t ui;
+                               double d;
+                               _Bool b;
+                               
+                               type = meta_data_type(meta, table[i]);
+                               if (type == MD_TYPE_STRING) {
+                                       if (meta_data_get_string(meta, table[i], &string))
+                                               continue;
+                                       temp = cpy_string_to_unicode_or_bytes(string);
+                                       free(string);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_SIGNED_INT) {
+                                       if (meta_data_get_signed_int(meta, table[i], &si))
+                                               continue;
+                                       temp = PyObject_CallFunctionObjArgs((void *) &SignedType, PyLong_FromLongLong(si), (void *) 0);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_UNSIGNED_INT) {
+                                       if (meta_data_get_unsigned_int(meta, table[i], &ui))
+                                               continue;
+                                       temp = PyObject_CallFunctionObjArgs((void *) &UnsignedType, PyLong_FromUnsignedLongLong(ui), (void *) 0);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_DOUBLE) {
+                                       if (meta_data_get_double(meta, table[i], &d))
+                                               continue;
+                                       temp = PyFloat_FromDouble(d);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_BOOLEAN) {
+                                       if (meta_data_get_boolean(meta, table[i], &b))
+                                               continue;
+                                       if (b)
+                                               PyDict_SetItemString(dict, table[i], Py_True);
+                                       else
+                                               PyDict_SetItemString(dict, table[i], Py_False);
+                               }
+                               free(table[i]);
+                       }
+                       free(table);
+               }
+               v = PyObject_New(Values, (void *) &ValuesType);
+               sstrncpy(v->data.host, value_list->host, sizeof(v->data.host));
+               sstrncpy(v->data.type, value_list->type, sizeof(v->data.type));
+               sstrncpy(v->data.type_instance, value_list->type_instance, sizeof(v->data.type_instance));
+               sstrncpy(v->data.plugin, value_list->plugin, sizeof(v->data.plugin));
+               sstrncpy(v->data.plugin_instance, value_list->plugin_instance, sizeof(v->data.plugin_instance));
+               v->data.time = value_list->time;
+               v->interval = value_list->interval;
+               v->values = list;
+               v->meta = dict;
+               ret = PyObject_CallFunctionObjArgs(c->callback, v, c->data, (void *) 0); /* New reference. */
+               if (ret == NULL) {
+                       cpy_log_exception("write callback");
+               } else {
+                       Py_DECREF(ret);
+               }
+       CPY_RELEASE_THREADS
+       return 0;
+}
+
+static int cpy_notification_callback(const notification_t *notification, user_data_t *data) {
+       cpy_callback_t *c = data->data;
+       PyObject *ret;
+       Notification *n;
+
+       CPY_LOCK_THREADS
+               n = PyObject_New(Notification, (void *) &NotificationType);
+               sstrncpy(n->data.host, notification->host, sizeof(n->data.host));
+               sstrncpy(n->data.type, notification->type, sizeof(n->data.type));
+               sstrncpy(n->data.type_instance, notification->type_instance, sizeof(n->data.type_instance));
+               sstrncpy(n->data.plugin, notification->plugin, sizeof(n->data.plugin));
+               sstrncpy(n->data.plugin_instance, notification->plugin_instance, sizeof(n->data.plugin_instance));
+               n->data.time = notification->time;
+               sstrncpy(n->message, notification->message, sizeof(n->message));
+               n->severity = notification->severity;
+               ret = PyObject_CallFunctionObjArgs(c->callback, n, c->data, (void *) 0); /* New reference. */
+               if (ret == NULL) {
+                       cpy_log_exception("notification callback");
+               } else {
+                       Py_DECREF(ret);
+               }
+       CPY_RELEASE_THREADS
+       return 0;
+}
+
+static void cpy_log_callback(int severity, const char *message, user_data_t *data) {
+       cpy_callback_t * c = data->data;
+       PyObject *ret, *text;
+
+       CPY_LOCK_THREADS
+       text = cpy_string_to_unicode_or_bytes(message);
+       if (c->data == NULL)
+               ret = PyObject_CallFunction(c->callback, "iN", severity, text); /* New reference. */
+       else
+               ret = PyObject_CallFunction(c->callback, "iNO", severity, text, c->data); /* New reference. */
+
+       if (ret == NULL) {
+               /* FIXME */
+               /* Do we really want to trigger a log callback because a log callback failed?
+                * Probably not. */
+               PyErr_Print();
+               /* In case someone wanted to be clever, replaced stderr and failed at that. */
+               PyErr_Clear();
+       } else {
+               Py_DECREF(ret);
+       }
+       CPY_RELEASE_THREADS
+}
+
+static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) {
+       cpy_callback_t * c = data->data;
+       PyObject *ret, *text;
+
+       CPY_LOCK_THREADS
+       text = cpy_string_to_unicode_or_bytes(id);
+       if (c->data == NULL)
+               ret = PyObject_CallFunction(c->callback, "iN", timeout, text); /* New reference. */
+       else
+               ret = PyObject_CallFunction(c->callback, "iNO", timeout, text, c->data); /* New reference. */
+
+       if (ret == NULL) {
+               cpy_log_exception("flush callback");
+       } else {
+               Py_DECREF(ret);
+       }
+       CPY_RELEASE_THREADS
+}
+
+static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
+       char buf[512];
+       cpy_callback_t *c;
+       const char *name = NULL;
+       PyObject *callback = NULL, *data = NULL, *mod = NULL;
+       static char *kwlist[] = {"callback", "data", "name", NULL};
+       
+       if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL;
+       if (PyCallable_Check(callback) == 0) {
+               PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
+               return NULL;
+       }
+       cpy_build_name(buf, sizeof(buf), callback, name);
+
+       Py_INCREF(callback);
+       Py_XINCREF(data);
+       c = malloc(sizeof(*c));
+       c->name = strdup(buf);
+       c->callback = callback;
+       c->data = data;
+       c->next = *list_head;
+       *list_head = c;
+       Py_XDECREF(mod);
+       return cpy_string_to_unicode_or_bytes(buf);
+}
+
+static PyObject *cpy_flush(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
+       int timeout = -1;
+       const char *plugin = NULL, *identifier = NULL;
+       static char *kwlist[] = {"plugin", "timeout", "identifier", NULL};
+       
+       if (PyArg_ParseTupleAndKeywords(args, kwds, "|etiet", kwlist, NULL, &plugin, &timeout, NULL, &identifier) == 0) return NULL;
+       Py_BEGIN_ALLOW_THREADS
+       plugin_flush(plugin, timeout, identifier);
+       Py_END_ALLOW_THREADS
+       Py_RETURN_NONE;
+}
+
+static PyObject *cpy_register_config(PyObject *self, PyObject *args, PyObject *kwds) {
+       return cpy_register_generic(&cpy_config_callbacks, args, kwds);
+}
+
+static PyObject *cpy_register_init(PyObject *self, PyObject *args, PyObject *kwds) {
+       return cpy_register_generic(&cpy_init_callbacks, args, kwds);
+}
+
+typedef int reg_function_t(const char *name, void *callback, void *data);
+
+static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObject *args, PyObject *kwds) {
+       char buf[512];
+       reg_function_t *register_function = (reg_function_t *) reg;
+       cpy_callback_t *c = NULL;
+       user_data_t *user_data = NULL;
+       const char *name = NULL;
+       PyObject *callback = NULL, *data = NULL;
+       static char *kwlist[] = {"callback", "data", "name", NULL};
+       
+       if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL;
+       if (PyCallable_Check(callback) == 0) {
+               PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
+               return NULL;
+       }
+       cpy_build_name(buf, sizeof(buf), callback, name);
+       
+       Py_INCREF(callback);
+       Py_XINCREF(data);
+       c = malloc(sizeof(*c));
+       c->name = strdup(buf);
+       c->callback = callback;
+       c->data = data;
+       c->next = NULL;
+       user_data = malloc(sizeof(*user_data));
+       user_data->free_func = cpy_destroy_user_data;
+       user_data->data = c;
+       register_function(buf, handler, user_data);
+       return cpy_string_to_unicode_or_bytes(buf);
+}
+
+static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) {
+       char buf[512];
+       cpy_callback_t *c = NULL;
+       user_data_t *user_data = NULL;
+       double interval = 0;
+       const char *name = NULL;
+       PyObject *callback = NULL, *data = NULL;
+       struct timespec ts;
+       static char *kwlist[] = {"callback", "interval", "data", "name", NULL};
+       
+       if (PyArg_ParseTupleAndKeywords(args, kwds, "O|dOet", kwlist, &callback, &interval, &data, NULL, &name) == 0) return NULL;
+       if (PyCallable_Check(callback) == 0) {
+               PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
+               return NULL;
+       }
+       cpy_build_name(buf, sizeof(buf), callback, name);
+       
+       Py_INCREF(callback);
+       Py_XINCREF(data);
+       c = malloc(sizeof(*c));
+       c->name = strdup(buf);
+       c->callback = callback;
+       c->data = data;
+       c->next = NULL;
+       user_data = malloc(sizeof(*user_data));
+       user_data->free_func = cpy_destroy_user_data;
+       user_data->data = c;
+       ts.tv_sec = interval;
+       ts.tv_nsec = (interval - ts.tv_sec) * 1000000000;
+       plugin_register_complex_read(/* group = */ NULL, buf,
+                       cpy_read_callback, &ts, user_data);
+       return cpy_string_to_unicode_or_bytes(buf);
+}
+
+static PyObject *cpy_register_log(PyObject *self, PyObject *args, PyObject *kwds) {
+       return cpy_register_generic_userdata((void *) plugin_register_log,
+                       (void *) cpy_log_callback, args, kwds);
+}
+
+static PyObject *cpy_register_write(PyObject *self, PyObject *args, PyObject *kwds) {
+       return cpy_register_generic_userdata((void *) plugin_register_write,
+                       (void *) cpy_write_callback, args, kwds);
+}
+
+static PyObject *cpy_register_notification(PyObject *self, PyObject *args, PyObject *kwds) {
+       return cpy_register_generic_userdata((void *) plugin_register_notification,
+                       (void *) cpy_notification_callback, args, kwds);
+}
+
+static PyObject *cpy_register_flush(PyObject *self, PyObject *args, PyObject *kwds) {
+       return cpy_register_generic_userdata((void *) plugin_register_flush,
+                       (void *) cpy_flush_callback, args, kwds);
+}
+
+static PyObject *cpy_register_shutdown(PyObject *self, PyObject *args, PyObject *kwds) {
+       return cpy_register_generic(&cpy_shutdown_callbacks, args, kwds);
+}
+
+static PyObject *cpy_error(PyObject *self, PyObject *args) {
+       const char *text;
+       if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
+       Py_BEGIN_ALLOW_THREADS
+       plugin_log(LOG_ERR, "%s", text);
+       Py_END_ALLOW_THREADS
+       Py_RETURN_NONE;
+}
+
+static PyObject *cpy_warning(PyObject *self, PyObject *args) {
+       const char *text;
+       if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
+       Py_BEGIN_ALLOW_THREADS
+       plugin_log(LOG_WARNING, "%s", text);
+       Py_END_ALLOW_THREADS
+       Py_RETURN_NONE;
+}
+
+static PyObject *cpy_notice(PyObject *self, PyObject *args) {
+       const char *text;
+       if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
+       Py_BEGIN_ALLOW_THREADS
+       plugin_log(LOG_NOTICE, "%s", text);
+       Py_END_ALLOW_THREADS
+       Py_RETURN_NONE;
+}
+
+static PyObject *cpy_info(PyObject *self, PyObject *args) {
+       const char *text;
+       if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
+       Py_BEGIN_ALLOW_THREADS
+       plugin_log(LOG_INFO, "%s", text);
+       Py_END_ALLOW_THREADS
+       Py_RETURN_NONE;
+}
+
+static PyObject *cpy_debug(PyObject *self, PyObject *args) {
+#ifdef COLLECT_DEBUG
+       const char *text;
+       if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
+       Py_BEGIN_ALLOW_THREADS
+       plugin_log(LOG_DEBUG, "%s", text);
+       Py_END_ALLOW_THREADS
+#endif
+       Py_RETURN_NONE;
+}
+
+static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *arg, const char *desc) {
+       char buf[512];
+       const char *name;
+       cpy_callback_t *prev = NULL, *tmp;
+
+       Py_INCREF(arg);
+       name = cpy_unicode_or_bytes_to_string(&arg);
+       if (name == NULL) {
+               PyErr_Clear();
+               if (!PyCallable_Check(arg)) {
+                       PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
+                       Py_DECREF(arg);
+                       return NULL;
+               }
+               cpy_build_name(buf, sizeof(buf), arg, NULL);
+               name = buf;
+       }
+       for (tmp = *list_head; tmp; prev = tmp, tmp = tmp->next)
+               if (strcmp(name, tmp->name) == 0)
+                       break;
+       
+       Py_DECREF(arg);
+       if (tmp == NULL) {
+               PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
+               return NULL;
+       }
+       /* Yes, this is actually save. To call this function the caller has to
+        * hold the GIL. Well, save as long as there is only one GIL anyway ... */
+       if (prev == NULL)
+               *list_head = tmp->next;
+       else
+               prev->next = tmp->next;
+       cpy_destroy_user_data(tmp);
+       Py_RETURN_NONE;
+}
+
+typedef int cpy_unregister_function_t(const char *name);
+
+static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc) {
+       char buf[512];
+       const char *name;
+
+       Py_INCREF(arg);
+       name = cpy_unicode_or_bytes_to_string(&arg);
+       if (name == NULL) {
+               PyErr_Clear();
+               if (!PyCallable_Check(arg)) {
+                       PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
+                       Py_DECREF(arg);
+                       return NULL;
+               }
+               cpy_build_name(buf, sizeof(buf), arg, NULL);
+               name = buf;
+       }
+       if (unreg(name) == 0) {
+               Py_DECREF(arg);
+               Py_RETURN_NONE;
+       }
+       PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
+       Py_DECREF(arg);
+       return NULL;
+}
+
+static PyObject *cpy_unregister_log(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic_userdata(plugin_unregister_log, arg, "log");
+}
+
+static PyObject *cpy_unregister_init(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic(&cpy_init_callbacks, arg, "init");
+}
+
+static PyObject *cpy_unregister_config(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic(&cpy_config_callbacks, arg, "config");
+}
+
+static PyObject *cpy_unregister_read(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic_userdata(plugin_unregister_read, arg, "read");
+}
+
+static PyObject *cpy_unregister_write(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic_userdata(plugin_unregister_write, arg, "write");
+}
+
+static PyObject *cpy_unregister_notification(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic_userdata(plugin_unregister_notification, arg, "notification");
+}
+
+static PyObject *cpy_unregister_flush(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic_userdata(plugin_unregister_flush, arg, "flush");
+}
+
+static PyObject *cpy_unregister_shutdown(PyObject *self, PyObject *arg) {
+       return cpy_unregister_generic(&cpy_shutdown_callbacks, arg, "shutdown");
+}
+
+static PyMethodDef cpy_methods[] = {
+       {"debug", cpy_debug, METH_VARARGS, log_doc},
+       {"info", cpy_info, METH_VARARGS, log_doc},
+       {"notice", cpy_notice, METH_VARARGS, log_doc},
+       {"warning", cpy_warning, METH_VARARGS, log_doc},
+       {"error", cpy_error, METH_VARARGS, log_doc},
+       {"flush", (PyCFunction) cpy_flush, METH_VARARGS | METH_KEYWORDS, flush_doc},
+       {"register_log", (PyCFunction) cpy_register_log, METH_VARARGS | METH_KEYWORDS, reg_log_doc},
+       {"register_init", (PyCFunction) cpy_register_init, METH_VARARGS | METH_KEYWORDS, reg_init_doc},
+       {"register_config", (PyCFunction) cpy_register_config, METH_VARARGS | METH_KEYWORDS, reg_config_doc},
+       {"register_read", (PyCFunction) cpy_register_read, METH_VARARGS | METH_KEYWORDS, reg_read_doc},
+       {"register_write", (PyCFunction) cpy_register_write, METH_VARARGS | METH_KEYWORDS, reg_write_doc},
+       {"register_notification", (PyCFunction) cpy_register_notification, METH_VARARGS | METH_KEYWORDS, reg_notification_doc},
+       {"register_flush", (PyCFunction) cpy_register_flush, METH_VARARGS | METH_KEYWORDS, reg_flush_doc},
+       {"register_shutdown", (PyCFunction) cpy_register_shutdown, METH_VARARGS | METH_KEYWORDS, reg_shutdown_doc},
+       {"unregister_log", cpy_unregister_log, METH_O, unregister_doc},
+       {"unregister_init", cpy_unregister_init, METH_O, unregister_doc},
+       {"unregister_config", cpy_unregister_config, METH_O, unregister_doc},
+       {"unregister_read", cpy_unregister_read, METH_O, unregister_doc},
+       {"unregister_write", cpy_unregister_write, METH_O, unregister_doc},
+       {"unregister_notification", cpy_unregister_notification, METH_O, unregister_doc},
+       {"unregister_flush", cpy_unregister_flush, METH_O, unregister_doc},
+       {"unregister_shutdown", cpy_unregister_shutdown, METH_O, unregister_doc},
+       {0, 0, 0, 0}
+};
+
+static int cpy_shutdown(void) {
+       cpy_callback_t *c;
+       PyObject *ret;
+       
+       /* This can happen if the module was loaded but not configured. */
+       if (state != NULL)
+               PyEval_RestoreThread(state);
+
+       for (c = cpy_shutdown_callbacks; c; c = c->next) {
+               ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
+               if (ret == NULL)
+                       cpy_log_exception("shutdown callback");
+               else
+                       Py_DECREF(ret);
+       }
+       PyErr_Print();
+       Py_Finalize();
+       return 0;
+}
+
+static void cpy_int_handler(int sig) {
+       return;
+}
+
+static void *cpy_interactive(void *data) {
+       sigset_t sigset;
+       struct sigaction sig_int_action, old;
+       
+       /* Signal handler in a plugin? Bad stuff, but the best way to
+        * handle it I guess. In an interactive session people will
+        * press Ctrl+C at some time, which will generate a SIGINT.
+        * This will cause collectd to shutdown, thus killing the
+        * interactive interpreter, and leaving the terminal in a
+        * mess. Chances are, this isn't what the user wanted to do.
+        * 
+        * So this is the plan:
+        * 1. Block SIGINT in the main thread.
+        * 2. Install our own signal handler that does nothing.
+        * 3. Unblock SIGINT in the interactive thread.
+        *
+        * This will make sure that SIGINT won't kill collectd but
+        * still interrupt syscalls like sleep and pause.
+        * It does not raise a KeyboardInterrupt exception because so
+        * far nobody managed to figure out how to do that. */
+       memset (&sig_int_action, '\0', sizeof (sig_int_action));
+       sig_int_action.sa_handler = cpy_int_handler;
+       sigaction (SIGINT, &sig_int_action, &old);
+       
+       sigemptyset(&sigset);
+       sigaddset(&sigset, SIGINT);
+       pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
+       PyEval_AcquireThread(state);
+       if (PyImport_ImportModule("readline") == NULL) {
+               /* This interactive session will suck. */
+               cpy_log_exception("interactive session init");
+       }
+       PyRun_InteractiveLoop(stdin, "<stdin>");
+       PyErr_Print();
+       PyEval_ReleaseThread(state);
+       NOTICE("python: Interactive interpreter exited, stopping collectd ...");
+       /* Restore the original collectd SIGINT handler and raise SIGINT.
+        * The main thread still has SIGINT blocked and there's nothing we
+        * can do about that so this thread will handle it. But that's not
+        * important, except that it won't interrupt the main loop and so
+        * it might take a few seconds before collectd really shuts down. */
+       sigaction (SIGINT, &old, NULL);
+       raise(SIGINT);
+       pause();
+       return NULL;
+}
+
+static int cpy_init(void) {
+       cpy_callback_t *c;
+       PyObject *ret;
+       static pthread_t thread;
+       sigset_t sigset;
+       
+       PyEval_InitThreads();
+       /* Now it's finally OK to use python threads. */
+       for (c = cpy_init_callbacks; c; c = c->next) {
+               ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
+               if (ret == NULL)
+                       cpy_log_exception("init callback");
+               else
+                       Py_DECREF(ret);
+       }
+       sigemptyset(&sigset);
+       sigaddset(&sigset, SIGINT);
+       pthread_sigmask(SIG_BLOCK, &sigset, NULL);
+       state = PyEval_SaveThread();
+       if (do_interactive) {
+               if (pthread_create(&thread, NULL, cpy_interactive, NULL)) {
+                       ERROR("python: Error creating thread for interactive interpreter.");
+               }
+       }
+
+       return 0;
+}
+
+static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) {
+       int i;
+       PyObject *item, *values, *children, *tmp;
+       
+       if (parent == NULL)
+               parent = Py_None;
+       
+       values = PyTuple_New(ci->values_num); /* New reference. */
+       for (i = 0; i < ci->values_num; ++i) {
+               if (ci->values[i].type == OCONFIG_TYPE_STRING) {
+                       PyTuple_SET_ITEM(values, i, cpy_string_to_unicode_or_bytes(ci->values[i].value.string));
+               } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) {
+                       PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number));
+               } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) {
+                       PyTuple_SET_ITEM(values, i, PyBool_FromLong(ci->values[i].value.boolean));
+               }
+       }
+       
+       tmp = cpy_string_to_unicode_or_bytes(ci->key);
+       item = PyObject_CallFunction((void *) &ConfigType, "NONO", tmp, parent, values, Py_None);
+       if (item == NULL)
+               return NULL;
+       children = PyTuple_New(ci->children_num); /* New reference. */
+       for (i = 0; i < ci->children_num; ++i) {
+               PyTuple_SET_ITEM(children, i, cpy_oconfig_to_pyconfig(ci->children + i, item));
+       }
+       tmp = ((Config *) item)->children;
+       ((Config *) item)->children = children;
+       Py_XDECREF(tmp);
+       return item;
+}
+
+#ifdef IS_PY3K
+static struct PyModuleDef collectdmodule = {
+       PyModuleDef_HEAD_INIT,
+       "collectd",   /* name of module */
+       "The python interface to collectd", /* module documentation, may be NULL */
+       -1,
+       cpy_methods
+};
+
+PyMODINIT_FUNC PyInit_collectd(void) {
+       return PyModule_Create(&collectdmodule);
+}
+#endif
+
+static int cpy_config(oconfig_item_t *ci) {
+       int i;
+       PyObject *sys, *tb;
+       PyObject *sys_path;
+       PyObject *module;
+       
+       /* Ok in theory we shouldn't do initialization at this point
+        * but we have to. In order to give python scripts a chance
+        * to register a config callback we need to be able to execute
+        * python code during the config callback so we have to start
+        * the interpreter here. */
+       /* Do *not* use the python "thread" module at this point! */
+
+#ifdef IS_PY3K
+       /* Add a builtin module, before Py_Initialize */
+       PyImport_AppendInittab("collectd", PyInit_collectd);
+#endif
+       
+       Py_Initialize();
+       
+       PyType_Ready(&ConfigType);
+       PyType_Ready(&PluginDataType);
+       ValuesType.tp_base = &PluginDataType;
+       PyType_Ready(&ValuesType);
+       NotificationType.tp_base = &PluginDataType;
+       PyType_Ready(&NotificationType);
+       SignedType.tp_base = &PyLong_Type;
+       PyType_Ready(&SignedType);
+       UnsignedType.tp_base = &PyLong_Type;
+       PyType_Ready(&UnsignedType);
+       sys = PyImport_ImportModule("sys"); /* New reference. */
+       if (sys == NULL) {
+               cpy_log_exception("python initialization");
+               return 1;
+       }
+       sys_path = PyObject_GetAttrString(sys, "path"); /* New reference. */
+       Py_DECREF(sys);
+       if (sys_path == NULL) {
+               cpy_log_exception("python initialization");
+               return 1;
+       }
+#ifdef IS_PY3K
+       module = PyImport_ImportModule("collectd");
+#else
+       module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */
+#endif
+       PyModule_AddObject(module, "Config", (void *) &ConfigType); /* Steals a reference. */
+       PyModule_AddObject(module, "Values", (void *) &ValuesType); /* Steals a reference. */
+       PyModule_AddObject(module, "Notification", (void *) &NotificationType); /* Steals a reference. */
+       PyModule_AddObject(module, "Signed", (void *) &SignedType); /* Steals a reference. */
+       PyModule_AddObject(module, "Unsigned", (void *) &UnsignedType); /* Steals a reference. */
+       PyModule_AddIntConstant(module, "LOG_DEBUG", LOG_DEBUG);
+       PyModule_AddIntConstant(module, "LOG_INFO", LOG_INFO);
+       PyModule_AddIntConstant(module, "LOG_NOTICE", LOG_NOTICE);
+       PyModule_AddIntConstant(module, "LOG_WARNING", LOG_WARNING);
+       PyModule_AddIntConstant(module, "LOG_ERROR", LOG_ERR);
+       PyModule_AddIntConstant(module, "NOTIF_FAILURE", NOTIF_FAILURE);
+       PyModule_AddIntConstant(module, "NOTIF_WARNING", NOTIF_WARNING);
+       PyModule_AddIntConstant(module, "NOTIF_OKAY", NOTIF_OKAY);
+       for (i = 0; i < ci->children_num; ++i) {
+               oconfig_item_t *item = ci->children + i;
+               
+               if (strcasecmp(item->key, "Interactive") == 0) {
+                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
+                               continue;
+                       do_interactive = item->values[0].value.boolean;
+               } else if (strcasecmp(item->key, "Encoding") == 0) {
+                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_STRING)
+                               continue;
+                       /* Why is this even necessary? And undocumented? */
+                       if (PyUnicode_SetDefaultEncoding(item->values[0].value.string))
+                               cpy_log_exception("setting default encoding");
+               } else if (strcasecmp(item->key, "LogTraces") == 0) {
+                       if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
+                               continue;
+                       if (!item->values[0].value.boolean) {
+                               Py_XDECREF(cpy_format_exception);
+                               cpy_format_exception = NULL;
+                               continue;
+                       }
+                       if (cpy_format_exception)
+                               continue;
+                       tb = PyImport_ImportModule("traceback"); /* New reference. */
+                       if (tb == NULL) {
+                               cpy_log_exception("python initialization");
+                               continue;
+                       }
+                       cpy_format_exception = PyObject_GetAttrString(tb, "format_exception"); /* New reference. */
+                       Py_DECREF(tb);
+                       if (cpy_format_exception == NULL)
+                               cpy_log_exception("python initialization");
+               } else if (strcasecmp(item->key, "ModulePath") == 0) {
+                       char *dir = NULL;
+                       PyObject *dir_object;
+                       
+                       if (cf_util_get_string(item, &dir) != 0) 
+                               continue;
+                       dir_object = cpy_string_to_unicode_or_bytes(dir); /* New reference. */
+                       if (dir_object == NULL) {
+                               ERROR("python plugin: Unable to convert \"%s\" to "
+                                     "a python object.", dir);
+                               free(dir);
+                               cpy_log_exception("python initialization");
+                               continue;
+                       }
+                       if (PyList_Append(sys_path, dir_object) != 0) {
+                               ERROR("python plugin: Unable to append \"%s\" to "
+                                     "python module path.", dir);
+                               cpy_log_exception("python initialization");
+                       }
+                       Py_DECREF(dir_object);
+                       free(dir);
+               } else if (strcasecmp(item->key, "Import") == 0) {
+                       char *module_name = NULL;
+                       PyObject *module;
+                       
+                       if (cf_util_get_string(item, &module_name) != 0) 
+                               continue;
+                       module = PyImport_ImportModule(module_name); /* New reference. */
+                       if (module == NULL) {
+                               ERROR("python plugin: Error importing module \"%s\".", module_name);
+                               cpy_log_exception("importing module");
+                       }
+                       free(module_name);
+                       Py_XDECREF(module);
+               } else if (strcasecmp(item->key, "Module") == 0) {
+                       char *name = NULL;
+                       cpy_callback_t *c;
+                       PyObject *ret;
+                       
+                       if (cf_util_get_string(item, &name) != 0)
+                               continue;
+                       for (c = cpy_config_callbacks; c; c = c->next) {
+                               if (strcasecmp(c->name + 7, name) == 0)
+                                       break;
+                       }
+                       if (c == NULL) {
+                               WARNING("python plugin: Found a configuration for the \"%s\" plugin, "
+                                       "but the plugin isn't loaded or didn't register "
+                                       "a configuration callback.", name);
+                               free(name);
+                               continue;
+                       }
+                       free(name);
+                       if (c->data == NULL)
+                               ret = PyObject_CallFunction(c->callback, "N",
+                                       cpy_oconfig_to_pyconfig(item, NULL)); /* New reference. */
+                       else
+                               ret = PyObject_CallFunction(c->callback, "NO",
+                                       cpy_oconfig_to_pyconfig(item, NULL), c->data); /* New reference. */
+                       if (ret == NULL)
+                               cpy_log_exception("loading module");
+                       else
+                               Py_DECREF(ret);
+               } else {
+                       WARNING("python plugin: Ignoring unknown config key \"%s\".", item->key);
+               }
+       }
+       Py_DECREF(sys_path);
+       return 0;
+}
+
+void module_register(void) {
+       plugin_register_complex_config("python", cpy_config);
+       plugin_register_init("python", cpy_init);
+       plugin_register_shutdown("python", cpy_shutdown);
+}
diff --git a/src/pyvalues.c b/src/pyvalues.c
new file mode 100644 (file)
index 0000000..60462ad
--- /dev/null
@@ -0,0 +1,1064 @@
+/**
+ * collectd - src/pyvalues.c
+ * Copyright (C) 2009  Sven Trenkel
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ *   Sven Trenkel <collectd at semidefinite.de>  
+ **/
+
+#include <Python.h>
+#include <structmember.h>
+
+#include "collectd.h"
+#include "common.h"
+
+#include "cpython.h"
+
+static PyObject *cpy_common_repr(PyObject *s) {
+       PyObject *ret, *tmp;
+       static PyObject *l_type = NULL, *l_type_instance = NULL, *l_plugin = NULL, *l_plugin_instance = NULL;
+       static PyObject *l_host = NULL, *l_time = NULL;
+       PluginData *self = (PluginData *) s;
+       
+       if (l_type == NULL)
+               l_type = cpy_string_to_unicode_or_bytes("(type=");
+       if (l_type_instance == NULL)
+               l_type_instance = cpy_string_to_unicode_or_bytes(",type_instance=");
+       if (l_plugin == NULL)
+               l_plugin = cpy_string_to_unicode_or_bytes(",plugin=");
+       if (l_plugin_instance == NULL)
+               l_plugin_instance = cpy_string_to_unicode_or_bytes(",plugin_instance=");
+       if (l_host == NULL)
+               l_host = cpy_string_to_unicode_or_bytes(",host=");
+       if (l_time == NULL)
+               l_time = cpy_string_to_unicode_or_bytes(",time=");
+       
+       if (!l_type || !l_type_instance || !l_plugin || !l_plugin_instance || !l_host || !l_time)
+               return NULL;
+       
+       ret = cpy_string_to_unicode_or_bytes(s->ob_type->tp_name);
+
+       CPY_STRCAT(&ret, l_type);
+       tmp = cpy_string_to_unicode_or_bytes(self->type);
+       CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+       CPY_STRCAT_AND_DEL(&ret, tmp);
+
+       if (self->type_instance[0] != 0) {
+               CPY_STRCAT(&ret, l_type_instance);
+               tmp = cpy_string_to_unicode_or_bytes(self->type_instance);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+
+       if (self->plugin[0] != 0) {
+               CPY_STRCAT(&ret, l_plugin);
+               tmp = cpy_string_to_unicode_or_bytes(self->plugin);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+
+       if (self->plugin_instance[0] != 0) {
+               CPY_STRCAT(&ret, l_plugin_instance);
+               tmp = cpy_string_to_unicode_or_bytes(self->plugin_instance);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+
+       if (self->host[0] != 0) {
+               CPY_STRCAT(&ret, l_host);
+               tmp = cpy_string_to_unicode_or_bytes(self->host);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+
+       if (self->time != 0) {
+               CPY_STRCAT(&ret, l_time);
+               tmp = PyInt_FromLong(self->time);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+       return ret;
+}
+
+static char time_doc[] = "This is the Unix timestap of the time this value was read.\n"
+               "For dispatching values this can be set to 0 which means \"now\".\n"
+               "This means the time the value is actually dispatched, not the time\n"
+               "it was set to 0.";
+
+static char host_doc[] = "The hostname of the host this value was read from.\n"
+               "For dispatching this can be set to an empty string which means\n"
+               "the local hostname as defined in the collectd.conf.";
+
+static char type_doc[] = "The type of this value. This type has to be defined\n"
+               "in your types.db. Attempting to set it to any other value will\n"
+               "raise a TypeError exception.\n"
+               "Assigning a type is mandetory, calling dispatch without doing\n"
+               "so will raise a RuntimeError exception.";
+
+static char type_instance_doc[] = "";
+
+static char plugin_doc[] = "The name of the plugin that read the data. Setting this\n"
+               "member to an empty string will insert \"python\" upon dispatching.";
+
+static char plugin_instance_doc[] = "";
+
+static char PluginData_doc[] = "This is an internal class that is the base for Values\n"
+               "and Notification. It is pretty useless by itself and was therefore not\n"
+               "exported to the collectd module.";
+
+static PyObject *PluginData_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+       PluginData *self;
+       
+       self = (PluginData *) type->tp_alloc(type, 0);
+       if (self == NULL)
+               return NULL;
+       
+       self->time = 0;
+       self->host[0] = 0;
+       self->plugin[0] = 0;
+       self->plugin_instance[0] = 0;
+       self->type[0] = 0;
+       self->type_instance[0] = 0;
+       return (PyObject *) self;
+}
+
+static int PluginData_init(PyObject *s, PyObject *args, PyObject *kwds) {
+       PluginData *self = (PluginData *) s;
+       double time = 0;
+       const char *type = "", *plugin_instance = "", *type_instance = "", *plugin = "", *host = "";
+       static char *kwlist[] = {"type", "plugin_instance", "type_instance",
+                       "plugin", "host", "time", NULL};
+       
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetd", kwlist, NULL, &type,
+                       NULL, &plugin_instance, NULL, &type_instance, NULL, &plugin, NULL, &host, &time))
+               return -1;
+       
+       if (type[0] != 0 && plugin_get_ds(type) == NULL) {
+               PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
+               return -1;
+       }
+
+       sstrncpy(self->host, host, sizeof(self->host));
+       sstrncpy(self->plugin, plugin, sizeof(self->plugin));
+       sstrncpy(self->plugin_instance, plugin_instance, sizeof(self->plugin_instance));
+       sstrncpy(self->type, type, sizeof(self->type));
+       sstrncpy(self->type_instance, type_instance, sizeof(self->type_instance));
+       
+       self->time = time;
+       return 0;
+}
+
+static PyObject *PluginData_repr(PyObject *s) {
+       PyObject *ret;
+       static PyObject *l_closing = NULL;
+       
+       if (l_closing == NULL)
+               l_closing = cpy_string_to_unicode_or_bytes(")");
+       
+       if (l_closing == NULL)
+               return NULL;
+       
+       ret = cpy_common_repr(s);
+       CPY_STRCAT(&ret, l_closing);
+       return ret;
+}
+
+static PyMemberDef PluginData_members[] = {
+       {"time", T_DOUBLE, offsetof(PluginData, time), 0, time_doc},
+       {NULL}
+};
+
+static PyObject *PluginData_getstring(PyObject *self, void *data) {
+       const char *value = ((char *) self) + (intptr_t) data;
+       
+       return cpy_string_to_unicode_or_bytes(value);
+}
+
+static int PluginData_setstring(PyObject *self, PyObject *value, void *data) {
+       char *old;
+       const char *new;
+       
+       if (value == NULL) {
+               PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute");
+               return -1;
+       }
+       Py_INCREF(value);
+       new = cpy_unicode_or_bytes_to_string(&value);
+       if (new == NULL) {
+               Py_DECREF(value);
+               return -1;
+       }
+       old = ((char *) self) + (intptr_t) data;
+       sstrncpy(old, new, DATA_MAX_NAME_LEN);
+       Py_DECREF(value);
+       return 0;
+}
+
+static int PluginData_settype(PyObject *self, PyObject *value, void *data) {
+       char *old;
+       const char *new;
+       
+       if (value == NULL) {
+               PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute");
+               return -1;
+       }
+       Py_INCREF(value);
+       new = cpy_unicode_or_bytes_to_string(&value);
+       if (new == NULL) {
+               Py_DECREF(value);
+               return -1;
+       }
+
+       if (plugin_get_ds(new) == NULL) {
+               PyErr_Format(PyExc_TypeError, "Dataset %s not found", new);
+               Py_DECREF(value);
+               return -1;
+       }
+
+       old = ((char *) self) + (intptr_t) data;
+       sstrncpy(old, new, DATA_MAX_NAME_LEN);
+       Py_DECREF(value);
+       return 0;
+}
+
+static PyGetSetDef PluginData_getseters[] = {
+       {"host", PluginData_getstring, PluginData_setstring, host_doc, (void *) offsetof(PluginData, host)},
+       {"plugin", PluginData_getstring, PluginData_setstring, plugin_doc, (void *) offsetof(PluginData, plugin)},
+       {"plugin_instance", PluginData_getstring, PluginData_setstring, plugin_instance_doc, (void *) offsetof(PluginData, plugin_instance)},
+       {"type_instance", PluginData_getstring, PluginData_setstring, type_instance_doc, (void *) offsetof(PluginData, type_instance)},
+       {"type", PluginData_getstring, PluginData_settype, type_doc, (void *) offsetof(PluginData, type)},
+       {NULL}
+};
+
+PyTypeObject PluginDataType = {
+       CPY_INIT_TYPE
+       "collectd.PluginData",     /* tp_name */
+       sizeof(PluginData),        /* tp_basicsize */
+       0,                         /* Will be filled in later */
+       0,                         /* tp_dealloc */
+       0,                         /* tp_print */
+       0,                         /* tp_getattr */
+       0,                         /* tp_setattr */
+       0,                         /* tp_compare */
+       PluginData_repr,           /* tp_repr */
+       0,                         /* tp_as_number */
+       0,                         /* tp_as_sequence */
+       0,                         /* tp_as_mapping */
+       0,                         /* tp_hash */
+       0,                         /* tp_call */
+       0,                         /* tp_str */
+       0,                         /* tp_getattro */
+       0,                         /* tp_setattro */
+       0,                         /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE /*| Py_TPFLAGS_HAVE_GC*/, /*tp_flags*/
+       PluginData_doc,            /* tp_doc */
+       0,                         /* tp_traverse */
+       0,                         /* tp_clear */
+       0,                         /* tp_richcompare */
+       0,                         /* tp_weaklistoffset */
+       0,                         /* tp_iter */
+       0,                         /* tp_iternext */
+       0,                         /* tp_methods */
+       PluginData_members,        /* tp_members */
+       PluginData_getseters,      /* tp_getset */
+       0,                         /* tp_base */
+       0,                         /* tp_dict */
+       0,                         /* tp_descr_get */
+       0,                         /* tp_descr_set */
+       0,                         /* tp_dictoffset */
+       PluginData_init,           /* tp_init */
+       0,                         /* tp_alloc */
+       PluginData_new             /* tp_new */
+};
+
+static char interval_doc[] = "The interval is the timespan in seconds between two submits for\n"
+               "the same data source. This value has to be a positive integer, so you can't\n"
+               "submit more than one value per second. If this member is set to a\n"
+               "non-positive value, the default value as specified in the config file will\n"
+               "be used (default: 10).\n"
+               "\n"
+               "If you submit values more often than the specified interval, the average\n"
+               "will be used. If you submit less values, your graphs will have gaps.";
+
+static char values_doc[] = "These are the actual values that get dispatched to collectd.\n"
+               "It has to be a sequence (a tuple or list) of numbers.\n"
+               "The size of the sequence and the type of its content depend on the type\n"
+               "member your types.db file. For more information on this read the types.db\n"
+               "man page.\n"
+               "\n"
+               "If the sequence does not have the correct size upon dispatch a RuntimeError\n"
+               "exception will be raised. If the content of the sequence is not a number,\n"
+               "a TypeError exception will be raised.";
+
+static char meta_doc[] = "These are the meta data for this Value object.\n"
+               "It has to be a dictionary of numbers, strings or bools. All keys must be\n"
+               "strings. int and long objects will be dispatched as signed integers unless\n"
+               "they are between 2**63 and 2**64-1, which will result in a unsigned integer.\n"
+               "You can force one of these storage classes by using the classes\n"
+               "collectd.Signed and collectd.Unsigned. A meta object received by a write\n"
+               "callback will always contain Signed or Unsigned objects.";
+
+static char dispatch_doc[] = "dispatch([type][, values][, plugin_instance][, type_instance]"
+               "[, plugin][, host][, time][, interval]) -> None.  Dispatch a value list.\n"
+               "\n"
+               "Dispatch this instance to the collectd process. The object has members\n"
+               "for each of the possible arguments for this method. For a detailed explanation\n"
+               "of these parameters see the member of the same same.\n"
+               "\n"
+               "If you do not submit a parameter the value saved in its member will be submitted.\n"
+               "If you do provide a parameter it will be used instead, without altering the member.";
+
+static char write_doc[] = "write([destination][, type][, values][, plugin_instance][, type_instance]"
+               "[, plugin][, host][, time][, interval]) -> None.  Dispatch a value list.\n"
+               "\n"
+               "Write this instance to a single plugin or all plugins if 'destination' is obmitted.\n"
+               "This will bypass the main collectd process and all filtering and caching.\n"
+               "Other than that it works similar to 'dispatch'. In most cases 'dispatch' should be\n"
+               "used instead of 'write'.\n";
+
+static char Values_doc[] = "A Values object used for dispatching values to collectd and receiving values from write callbacks.";
+
+static PyObject *Values_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+       Values *self;
+       
+       self = (Values *) PluginData_new(type, args, kwds);
+       if (self == NULL)
+               return NULL;
+       
+       self->values = PyList_New(0);
+       self->meta = PyDict_New();
+       self->interval = 0;
+       return (PyObject *) self;
+}
+
+static int Values_init(PyObject *s, PyObject *args, PyObject *kwds) {
+       Values *self = (Values *) s;
+       int interval = 0;
+       double time = 0;
+       PyObject *values = NULL, *meta = NULL, *tmp;
+       const char *type = "", *plugin_instance = "", *type_instance = "", *plugin = "", *host = "";
+       static char *kwlist[] = {"type", "values", "plugin_instance", "type_instance",
+                       "plugin", "host", "time", "interval", "meta", NULL};
+       
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etOetetetetdiO", kwlist,
+                       NULL, &type, &values, NULL, &plugin_instance, NULL, &type_instance,
+                       NULL, &plugin, NULL, &host, &time, &interval, &meta))
+               return -1;
+       
+       if (type[0] != 0 && plugin_get_ds(type) == NULL) {
+               PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
+               return -1;
+       }
+
+       sstrncpy(self->data.host, host, sizeof(self->data.host));
+       sstrncpy(self->data.plugin, plugin, sizeof(self->data.plugin));
+       sstrncpy(self->data.plugin_instance, plugin_instance, sizeof(self->data.plugin_instance));
+       sstrncpy(self->data.type, type, sizeof(self->data.type));
+       sstrncpy(self->data.type_instance, type_instance, sizeof(self->data.type_instance));
+       self->data.time = time;
+
+       if (values == NULL) {
+               values = PyList_New(0);
+               PyErr_Clear();
+       } else {
+               Py_INCREF(values);
+       }
+       
+       if (meta == NULL) {
+               meta = PyDict_New();
+               PyErr_Clear();
+       } else {
+               Py_INCREF(meta);
+       }
+       
+       tmp = self->values;
+       self->values = values;
+       Py_XDECREF(tmp);
+       
+       tmp = self->meta;
+       self->meta = meta;
+       Py_XDECREF(tmp);
+
+       self->interval = interval;
+       return 0;
+}
+
+static meta_data_t *cpy_build_meta(PyObject *meta) {
+       int i, s;
+       meta_data_t *m = NULL;
+       PyObject *l;
+       
+       if (!meta)
+               return NULL;
+
+       m = meta_data_create();
+       l = PyDict_Items(meta);
+       s = PyList_Size(l);
+       for (i = 0; i < s; ++i) {
+               const char *string, *keystring;
+               PyObject *key, *value, *item, *tmp;
+               
+               item = PyList_GET_ITEM(l, i);
+               key = PyTuple_GET_ITEM(item, 0);
+               Py_INCREF(key);
+               keystring = cpy_unicode_or_bytes_to_string(&key);
+               if (!keystring) {
+                       PyErr_Clear();
+                       Py_XDECREF(key);
+                       continue;
+               }
+               value = PyTuple_GET_ITEM(item, 1);
+               Py_INCREF(value);
+               if (value == Py_True) {
+                       meta_data_add_boolean(m, keystring, 1);
+               } else if (value == Py_False) {
+                       meta_data_add_boolean(m, keystring, 0);
+               } else if (PyFloat_Check(value)) {
+                       meta_data_add_double(m, keystring, PyFloat_AsDouble(value));
+               } else if (PyObject_TypeCheck(value, &SignedType)) {
+                       long long int lli;
+                       lli = PyLong_AsLongLong(value);
+                       if (!PyErr_Occurred() && (lli == (int64_t) lli))
+                               meta_data_add_signed_int(m, keystring, lli);
+               } else if (PyObject_TypeCheck(value, &UnsignedType)) {
+                       long long unsigned llu;
+                       llu = PyLong_AsUnsignedLongLong(value);
+                       if (!PyErr_Occurred() && (llu == (uint64_t) llu))
+                               meta_data_add_unsigned_int(m, keystring, llu);
+               } else if (PyNumber_Check(value)) {
+                       long long int lli;
+                       long long unsigned llu;
+                       tmp = PyNumber_Long(value);
+                       lli = PyLong_AsLongLong(tmp);
+                       if (!PyErr_Occurred() && (lli == (int64_t) lli)) {
+                               meta_data_add_signed_int(m, keystring, lli);
+                       } else {
+                               PyErr_Clear();
+                               llu = PyLong_AsUnsignedLongLong(tmp);
+                               if (!PyErr_Occurred() && (llu == (uint64_t) llu))
+                                       meta_data_add_unsigned_int(m, keystring, llu);
+                       }
+                       Py_XDECREF(tmp);
+               } else {
+                       string = cpy_unicode_or_bytes_to_string(&value);
+                       if (string) {
+                               meta_data_add_string(m, keystring, string);
+                       } else {
+                               PyErr_Clear();
+                               tmp = PyObject_Str(value);
+                               string = cpy_unicode_or_bytes_to_string(&tmp);
+                               if (string)
+                                       meta_data_add_string(m, keystring, string);
+                               Py_XDECREF(tmp);
+                       }
+               }
+               if (PyErr_Occurred())
+                       cpy_log_exception("building meta data");
+               Py_XDECREF(value);
+               Py_DECREF(key);
+       }
+       return m;
+}
+
+static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) {
+       int i, ret;
+       const data_set_t *ds;
+       int size;
+       value_t *value;
+       value_list_t value_list = VALUE_LIST_INIT;
+       PyObject *values = self->values, *meta = self->meta;
+       double time = self->data.time;
+       int interval = self->interval;
+       const char *host = self->data.host;
+       const char *plugin = self->data.plugin;
+       const char *plugin_instance = self->data.plugin_instance;
+       const char *type = self->data.type;
+       const char *type_instance = self->data.type_instance;
+       
+       static char *kwlist[] = {"type", "values", "plugin_instance", "type_instance",
+                       "plugin", "host", "time", "interval", "meta", NULL};
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etOetetetetdiO", kwlist,
+                       NULL, &type, &values, NULL, &plugin_instance, NULL, &type_instance,
+                       NULL, &plugin, NULL, &host, &time, &interval, &meta))
+               return NULL;
+
+       if (type[0] == 0) {
+               PyErr_SetString(PyExc_RuntimeError, "type not set");
+               return NULL;
+       }
+       ds = plugin_get_ds(type);
+       if (ds == NULL) {
+               PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
+               return NULL;
+       }
+       if (values == NULL || (PyTuple_Check(values) == 0 && PyList_Check(values) == 0)) {
+               PyErr_Format(PyExc_TypeError, "values must be list or tuple");
+               return NULL;
+       }
+       if (meta != NULL && meta != Py_None && !PyDict_Check(meta)) {
+               PyErr_Format(PyExc_TypeError, "meta must be a dict");
+               return NULL;
+       }
+       size = (int) PySequence_Length(values);
+       if (size != ds->ds_num) {
+               PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", type, ds->ds_num, size);
+               return NULL;
+       }
+       value = malloc(size * sizeof(*value));
+       for (i = 0; i < size; ++i) {
+               PyObject *item, *num;
+               item = PySequence_GetItem(values, i);
+               if (ds->ds->type == DS_TYPE_COUNTER) {
+                       num = PyNumber_Long(item);
+                       if (num != NULL)
+                               value[i].counter = PyLong_AsUnsignedLongLong(num);
+               } else if (ds->ds->type == DS_TYPE_GAUGE) {
+                       num = PyNumber_Float(item);
+                       if (num != NULL)
+                               value[i].gauge = PyFloat_AsDouble(num);
+               } else if (ds->ds->type == DS_TYPE_DERIVE) {
+                       /* This might overflow without raising an exception.
+                        * Not much we can do about it */
+                       num = PyNumber_Long(item);
+                       if (num != NULL)
+                               value[i].derive = PyLong_AsLongLong(num);
+               } else if (ds->ds->type == DS_TYPE_ABSOLUTE) {
+                       /* This might overflow without raising an exception.
+                        * Not much we can do about it */
+                       num = PyNumber_Long(item);
+                       if (num != NULL)
+                               value[i].absolute = PyLong_AsUnsignedLongLong(num);
+               } else {
+                       free(value);
+                       PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, type);
+                       return NULL;
+               }
+               if (PyErr_Occurred() != NULL) {
+                       free(value);
+                       return NULL;
+               }
+       }
+       value_list.values = value;
+       value_list.meta = cpy_build_meta(meta);
+       value_list.values_len = size;
+       value_list.time = time;
+       value_list.interval = interval;
+       sstrncpy(value_list.host, host, sizeof(value_list.host));
+       sstrncpy(value_list.plugin, plugin, sizeof(value_list.plugin));
+       sstrncpy(value_list.plugin_instance, plugin_instance, sizeof(value_list.plugin_instance));
+       sstrncpy(value_list.type, type, sizeof(value_list.type));
+       sstrncpy(value_list.type_instance, type_instance, sizeof(value_list.type_instance));
+       if (value_list.host[0] == 0)
+               sstrncpy(value_list.host, hostname_g, sizeof(value_list.host));
+       if (value_list.plugin[0] == 0)
+               sstrncpy(value_list.plugin, "python", sizeof(value_list.plugin));
+       Py_BEGIN_ALLOW_THREADS;
+       ret = plugin_dispatch_values(&value_list);
+       Py_END_ALLOW_THREADS;
+       if (ret != 0) {
+               PyErr_SetString(PyExc_RuntimeError, "error dispatching values, read the logs");
+               return NULL;
+       }
+       free(value);
+       Py_RETURN_NONE;
+}
+
+static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) {
+       int i, ret;
+       const data_set_t *ds;
+       int size;
+       value_t *value;
+       value_list_t value_list = VALUE_LIST_INIT;
+       PyObject *values = self->values, *meta = self->meta;
+       double time = self->data.time;
+       int interval = self->interval;
+       const char *host = self->data.host;
+       const char *plugin = self->data.plugin;
+       const char *plugin_instance = self->data.plugin_instance;
+       const char *type = self->data.type;
+       const char *type_instance = self->data.type_instance;
+       const char *dest = NULL;
+       
+       static char *kwlist[] = {"destination", "type", "values", "plugin_instance", "type_instance",
+                       "plugin", "host", "time", "interval", "meta", NULL};
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etOetetetetdiO", kwlist,
+                       NULL, &type, &values, NULL, &plugin_instance, NULL, &type_instance,
+                       NULL, &plugin, NULL, &host, &time, &interval, &meta))
+               return NULL;
+
+       if (type[0] == 0) {
+               PyErr_SetString(PyExc_RuntimeError, "type not set");
+               return NULL;
+       }
+       ds = plugin_get_ds(type);
+       if (ds == NULL) {
+               PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
+               return NULL;
+       }
+       if (values == NULL || (PyTuple_Check(values) == 0 && PyList_Check(values) == 0)) {
+               PyErr_Format(PyExc_TypeError, "values must be list or tuple");
+               return NULL;
+       }
+       size = (int) PySequence_Length(values);
+       if (size != ds->ds_num) {
+               PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", type, ds->ds_num, size);
+               return NULL;
+       }
+       value = malloc(size * sizeof(*value));
+       for (i = 0; i < size; ++i) {
+               PyObject *item, *num;
+               item = PySequence_GetItem(values, i);
+               if (ds->ds->type == DS_TYPE_COUNTER) {
+                       num = PyNumber_Long(item);
+                       if (num != NULL)
+                               value[i].counter = PyLong_AsUnsignedLongLong(num);
+               } else if (ds->ds->type == DS_TYPE_GAUGE) {
+                       num = PyNumber_Float(item);
+                       if (num != NULL)
+                               value[i].gauge = PyFloat_AsDouble(num);
+               } else if (ds->ds->type == DS_TYPE_DERIVE) {
+                       /* This might overflow without raising an exception.
+                        * Not much we can do about it */
+                       num = PyNumber_Long(item);
+                       if (num != NULL)
+                               value[i].derive = PyLong_AsLongLong(num);
+               } else if (ds->ds->type == DS_TYPE_ABSOLUTE) {
+                       /* This might overflow without raising an exception.
+                        * Not much we can do about it */
+                       num = PyNumber_Long(item);
+                       if (num != NULL)
+                               value[i].absolute = PyLong_AsUnsignedLongLong(num);
+               } else {
+                       free(value);
+                       PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, type);
+                       return NULL;
+               }
+               if (PyErr_Occurred() != NULL) {
+                       free(value);
+                       return NULL;
+               }
+       }
+       value_list.values = value;
+       value_list.values_len = size;
+       value_list.time = time;
+       value_list.interval = interval;
+       sstrncpy(value_list.host, host, sizeof(value_list.host));
+       sstrncpy(value_list.plugin, plugin, sizeof(value_list.plugin));
+       sstrncpy(value_list.plugin_instance, plugin_instance, sizeof(value_list.plugin_instance));
+       sstrncpy(value_list.type, type, sizeof(value_list.type));
+       sstrncpy(value_list.type_instance, type_instance, sizeof(value_list.type_instance));
+       value_list.meta = cpy_build_meta(meta);;
+       if (value_list.host[0] == 0)
+               sstrncpy(value_list.host, hostname_g, sizeof(value_list.host));
+       if (value_list.plugin[0] == 0)
+               sstrncpy(value_list.plugin, "python", sizeof(value_list.plugin));
+       Py_BEGIN_ALLOW_THREADS;
+       ret = plugin_write(dest, NULL, &value_list);
+       Py_END_ALLOW_THREADS;
+       if (ret != 0) {
+               PyErr_SetString(PyExc_RuntimeError, "error dispatching values, read the logs");
+               return NULL;
+       }
+       free(value);
+       Py_RETURN_NONE;
+}
+
+static PyObject *Values_repr(PyObject *s) {
+       PyObject *ret, *tmp;
+       static PyObject *l_interval = NULL, *l_values = NULL, *l_meta = NULL, *l_closing = NULL;
+       Values *self = (Values *) s;
+       
+       if (l_interval == NULL)
+               l_interval = cpy_string_to_unicode_or_bytes(",interval=");
+       if (l_values == NULL)
+               l_values = cpy_string_to_unicode_or_bytes(",values=");
+       if (l_meta == NULL)
+               l_meta = cpy_string_to_unicode_or_bytes(",meta=");
+       if (l_closing == NULL)
+               l_closing = cpy_string_to_unicode_or_bytes(")");
+       
+       if (l_interval == NULL || l_values == NULL || l_meta == NULL || l_closing == NULL)
+               return NULL;
+       
+       ret = cpy_common_repr(s);
+       if (self->interval != 0) {
+               CPY_STRCAT(&ret, l_interval);
+               tmp = PyInt_FromLong(self->interval);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+       if (self->values && (!PyList_Check(self->values) || PySequence_Length(self->values) > 0)) {
+               CPY_STRCAT(&ret, l_values);
+               tmp = PyObject_Repr(self->values);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+       if (self->meta && (!PyDict_Check(self->meta) || PyDict_Size(self->meta) > 0)) {
+               CPY_STRCAT(&ret, l_meta);
+               tmp = PyObject_Repr(self->meta);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+       CPY_STRCAT(&ret, l_closing);
+       return ret;
+}
+
+static int Values_traverse(PyObject *self, visitproc visit, void *arg) {
+       Values *v = (Values *) self;
+       Py_VISIT(v->values);
+       Py_VISIT(v->meta);
+       return 0;
+}
+
+static int Values_clear(PyObject *self) {
+       Values *v = (Values *) self;
+       Py_CLEAR(v->values);
+       Py_CLEAR(v->meta);
+       return 0;
+}
+
+static void Values_dealloc(PyObject *self) {
+       Values_clear(self);
+       self->ob_type->tp_free(self);
+}
+
+static PyMemberDef Values_members[] = {
+       {"interval", T_INT, offsetof(Values, interval), 0, interval_doc},
+       {"values", T_OBJECT_EX, offsetof(Values, values), 0, values_doc},
+       {"meta", T_OBJECT_EX, offsetof(Values, meta), 0, meta_doc},
+       {NULL}
+};
+
+static PyMethodDef Values_methods[] = {
+       {"dispatch", (PyCFunction) Values_dispatch, METH_VARARGS | METH_KEYWORDS, dispatch_doc},
+       {"write", (PyCFunction) Values_write, METH_VARARGS | METH_KEYWORDS, write_doc},
+       {NULL}
+};
+
+PyTypeObject ValuesType = {
+       CPY_INIT_TYPE
+       "collectd.Values",         /* tp_name */
+       sizeof(Values),            /* tp_basicsize */
+       0,                         /* Will be filled in later */
+       Values_dealloc,            /* tp_dealloc */
+       0,                         /* tp_print */
+       0,                         /* tp_getattr */
+       0,                         /* tp_setattr */
+       0,                         /* tp_compare */
+       Values_repr,               /* tp_repr */
+       0,                         /* tp_as_number */
+       0,                         /* tp_as_sequence */
+       0,                         /* tp_as_mapping */
+       0,                         /* tp_hash */
+       0,                         /* tp_call */
+       0,                         /* tp_str */
+       0,                         /* tp_getattro */
+       0,                         /* tp_setattro */
+       0,                         /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
+       Values_doc,                /* tp_doc */
+       Values_traverse,           /* tp_traverse */
+       Values_clear,              /* tp_clear */
+       0,                         /* tp_richcompare */
+       0,                         /* tp_weaklistoffset */
+       0,                         /* tp_iter */
+       0,                         /* tp_iternext */
+       Values_methods,            /* tp_methods */
+       Values_members,            /* tp_members */
+       0,                         /* tp_getset */
+       0,                         /* tp_base */
+       0,                         /* tp_dict */
+       0,                         /* tp_descr_get */
+       0,                         /* tp_descr_set */
+       0,                         /* tp_dictoffset */
+       Values_init,               /* tp_init */
+       0,                         /* tp_alloc */
+       Values_new                 /* tp_new */
+};
+
+static char severity_doc[] = "The severity of this notification. Assign or compare to\n"
+               "NOTIF_FAILURE, NOTIF_WARNING or NOTIF_OKAY.";
+
+static char message_doc[] = "Some kind of description what's going on and why this Notification was generated.";
+
+static char Notification_doc[] = "The Notification class is a wrapper around the collectd notification.\n"
+               "It can be used to notify other plugins about bad stuff happening. It works\n"
+               "similar to Values but has a severity and a message instead of interval\n"
+               "and time.\n"
+               "Notifications can be dispatched at any time and can be received with register_notification.";
+
+static int Notification_init(PyObject *s, PyObject *args, PyObject *kwds) {
+       Notification *self = (Notification *) s;
+       int severity = 0;
+       double time = 0;
+       const char *message = "";
+       const char *type = "", *plugin_instance = "", *type_instance = "", *plugin = "", *host = "";
+       static char *kwlist[] = {"type", "message", "plugin_instance", "type_instance",
+                       "plugin", "host", "time", "severity", NULL};
+       
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetetdi", kwlist,
+                       NULL, &type, NULL, &message, NULL, &plugin_instance, NULL, &type_instance,
+                       NULL, &plugin, NULL, &host, &time, &severity))
+               return -1;
+       
+       if (type[0] != 0 && plugin_get_ds(type) == NULL) {
+               PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
+               return -1;
+       }
+
+       sstrncpy(self->data.host, host, sizeof(self->data.host));
+       sstrncpy(self->data.plugin, plugin, sizeof(self->data.plugin));
+       sstrncpy(self->data.plugin_instance, plugin_instance, sizeof(self->data.plugin_instance));
+       sstrncpy(self->data.type, type, sizeof(self->data.type));
+       sstrncpy(self->data.type_instance, type_instance, sizeof(self->data.type_instance));
+       self->data.time = time;
+
+       sstrncpy(self->message, message, sizeof(self->message));
+       self->severity = severity;
+       return 0;
+}
+
+static PyObject *Notification_dispatch(Notification *self, PyObject *args, PyObject *kwds) {
+       int ret;
+       const data_set_t *ds;
+       notification_t notification;
+       double t = self->data.time;
+       int severity = self->severity;
+       const char *host = self->data.host;
+       const char *plugin = self->data.plugin;
+       const char *plugin_instance = self->data.plugin_instance;
+       const char *type = self->data.type;
+       const char *type_instance = self->data.type_instance;
+       const char *message = self->message;
+       
+       static char *kwlist[] = {"type", "message", "plugin_instance", "type_instance",
+                       "plugin", "host", "time", "severity", NULL};
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetetdi", kwlist,
+                       NULL, &type, NULL, &message, NULL, &plugin_instance, NULL, &type_instance,
+                       NULL, &plugin, NULL, &host, &t, &severity))
+               return NULL;
+
+       if (type[0] == 0) {
+               PyErr_SetString(PyExc_RuntimeError, "type not set");
+               return NULL;
+       }
+       ds = plugin_get_ds(type);
+       if (ds == NULL) {
+               PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
+               return NULL;
+       }
+
+       notification.time = t;
+       notification.severity = severity;
+       sstrncpy(notification.message, message, sizeof(notification.message));
+       sstrncpy(notification.host, host, sizeof(notification.host));
+       sstrncpy(notification.plugin, plugin, sizeof(notification.plugin));
+       sstrncpy(notification.plugin_instance, plugin_instance, sizeof(notification.plugin_instance));
+       sstrncpy(notification.type, type, sizeof(notification.type));
+       sstrncpy(notification.type_instance, type_instance, sizeof(notification.type_instance));
+       notification.meta = NULL;
+       if (notification.time < 1)
+               notification.time = time(0);
+       if (notification.host[0] == 0)
+               sstrncpy(notification.host, hostname_g, sizeof(notification.host));
+       if (notification.plugin[0] == 0)
+               sstrncpy(notification.plugin, "python", sizeof(notification.plugin));
+       Py_BEGIN_ALLOW_THREADS;
+       ret = plugin_dispatch_notification(&notification);
+       Py_END_ALLOW_THREADS;
+       if (ret != 0) {
+               PyErr_SetString(PyExc_RuntimeError, "error dispatching notification, read the logs");
+               return NULL;
+       }
+       Py_RETURN_NONE;
+}
+
+static PyObject *Notification_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+       Notification *self;
+       
+       self = (Notification *) PluginData_new(type, args, kwds);
+       if (self == NULL)
+               return NULL;
+       
+       self->message[0] = 0;
+       self->severity = 0;
+       return (PyObject *) self;
+}
+
+static int Notification_setstring(PyObject *self, PyObject *value, void *data) {
+       char *old;
+       const char *new;
+       
+       if (value == NULL) {
+               PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute");
+               return -1;
+       }
+       Py_INCREF(value);
+       new = cpy_unicode_or_bytes_to_string(&value);
+       if (new == NULL) {
+               Py_DECREF(value);
+               return -1;
+       }
+       old = ((char *) self) + (intptr_t) data;
+       sstrncpy(old, new, NOTIF_MAX_MSG_LEN);
+       Py_DECREF(value);
+       return 0;
+}
+
+static PyObject *Notification_repr(PyObject *s) {
+       PyObject *ret, *tmp;
+       static PyObject *l_severity = NULL, *l_message = NULL, *l_closing = NULL;
+       Notification *self = (Notification *) s;
+       
+       if (l_severity == NULL)
+               l_severity = cpy_string_to_unicode_or_bytes(",severity=");
+       if (l_message == NULL)
+               l_message = cpy_string_to_unicode_or_bytes(",message=");
+       if (l_closing == NULL)
+               l_closing = cpy_string_to_unicode_or_bytes(")");
+       
+       if (l_severity == NULL || l_message == NULL || l_closing == NULL)
+               return NULL;
+       
+       ret = cpy_common_repr(s);
+       if (self->severity != 0) {
+               CPY_STRCAT(&ret, l_severity);
+               tmp = PyInt_FromLong(self->severity);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+       if (self->message[0] != 0) {
+               CPY_STRCAT(&ret, l_message);
+               tmp = cpy_string_to_unicode_or_bytes(self->message);
+               CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
+               CPY_STRCAT_AND_DEL(&ret, tmp);
+       }
+       CPY_STRCAT(&ret, l_closing);
+       return ret;
+}
+
+static PyMethodDef Notification_methods[] = {
+       {"dispatch", (PyCFunction) Notification_dispatch, METH_VARARGS | METH_KEYWORDS, dispatch_doc},
+       {NULL}
+};
+
+static PyMemberDef Notification_members[] = {
+       {"severity", T_INT, offsetof(Notification, severity), 0, severity_doc},
+       {NULL}
+};
+
+static PyGetSetDef Notification_getseters[] = {
+       {"message", PluginData_getstring, Notification_setstring, message_doc, (void *) offsetof(Notification, message)},
+       {NULL}
+};
+
+PyTypeObject NotificationType = {
+       CPY_INIT_TYPE
+       "collectd.Notification",   /* tp_name */
+       sizeof(Notification),      /* tp_basicsize */
+       0,                         /* Will be filled in later */
+       0,                         /* tp_dealloc */
+       0,                         /* tp_print */
+       0,                         /* tp_getattr */
+       0,                         /* tp_setattr */
+       0,                         /* tp_compare */
+       Notification_repr,         /* tp_repr */
+       0,                         /* tp_as_number */
+       0,                         /* tp_as_sequence */
+       0,                         /* tp_as_mapping */
+       0,                         /* tp_hash */
+       0,                         /* tp_call */
+       0,                         /* tp_str */
+       0,                         /* tp_getattro */
+       0,                         /* tp_setattro */
+       0,                         /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+       Notification_doc,          /* tp_doc */
+       0,                         /* tp_traverse */
+       0,                         /* tp_clear */
+       0,                         /* tp_richcompare */
+       0,                         /* tp_weaklistoffset */
+       0,                         /* tp_iter */
+       0,                         /* tp_iternext */
+       Notification_methods,      /* tp_methods */
+       Notification_members,      /* tp_members */
+       Notification_getseters,    /* tp_getset */
+       0,                         /* tp_base */
+       0,                         /* tp_dict */
+       0,                         /* tp_descr_get */
+       0,                         /* tp_descr_set */
+       0,                         /* tp_dictoffset */
+       Notification_init,         /* tp_init */
+       0,                         /* tp_alloc */
+       Notification_new           /* tp_new */
+};
+
+static const char Signed_doc[] = "This is a long by another name. Use it in meta data dicts\n"
+               "to choose the way it is stored in the meta data.";
+
+PyTypeObject SignedType = {
+       CPY_INIT_TYPE
+       "collectd.Signed",         /* tp_name */
+       sizeof(Signed),            /* tp_basicsize */
+       0,                         /* Will be filled in later */
+       0,                         /* tp_dealloc */
+       0,                         /* tp_print */
+       0,                         /* tp_getattr */
+       0,                         /* tp_setattr */
+       0,                         /* tp_compare */
+       0,                         /* tp_repr */
+       0,                         /* tp_as_number */
+       0,                         /* tp_as_sequence */
+       0,                         /* tp_as_mapping */
+       0,                         /* tp_hash */
+       0,                         /* tp_call */
+       0,                         /* tp_str */
+       0,                         /* tp_getattro */
+       0,                         /* tp_setattro */
+       0,                         /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+       Signed_doc                 /* tp_doc */
+};
+
+static const char Unsigned_doc[] = "This is a long by another name. Use it in meta data dicts\n"
+               "to choose the way it is stored in the meta data.";
+
+PyTypeObject UnsignedType = {
+       CPY_INIT_TYPE
+       "collectd.Unsigned",       /* tp_name */
+       sizeof(Unsigned),          /* tp_basicsize */
+       0,                         /* Will be filled in later */
+       0,                         /* tp_dealloc */
+       0,                         /* tp_print */
+       0,                         /* tp_getattr */
+       0,                         /* tp_setattr */
+       0,                         /* tp_compare */
+       0,                         /* tp_repr */
+       0,                         /* tp_as_number */
+       0,                         /* tp_as_sequence */
+       0,                         /* tp_as_mapping */
+       0,                         /* tp_hash */
+       0,                         /* tp_call */
+       0,                         /* tp_str */
+       0,                         /* tp_getattro */
+       0,                         /* tp_setattro */
+       0,                         /* tp_as_buffer */
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+       Unsigned_doc               /* tp_doc */
+};
diff --git a/src/routeros.c b/src/routeros.c
new file mode 100644 (file)
index 0000000..ff8789e
--- /dev/null
@@ -0,0 +1,444 @@
+/**
+ * collectd - src/routeros.c
+ * Copyright (C) 2009  Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#include <routeros_api.h>
+
+struct cr_data_s
+{
+  ros_connection_t *connection;
+
+  char *node;
+  char *service;
+  char *username;
+  char *password;
+
+  _Bool collect_interface;
+  _Bool collect_regtable;
+  _Bool collect_cpu_load;
+  _Bool collect_memory;
+  _Bool collect_df;
+  _Bool collect_disk;
+};
+typedef struct cr_data_s cr_data_t;
+
+static void cr_submit_io (cr_data_t *rd, const char *type, /* {{{ */
+    const char *type_instance, counter_t rx, counter_t tx)
+{
+       value_t values[2];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].counter = rx;
+       values[1].counter = tx;
+
+       vl.values = values;
+       vl.values_len = STATIC_ARRAY_SIZE (values);
+       sstrncpy (vl.host, rd->node, sizeof (vl.host));
+       sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+
+       plugin_dispatch_values (&vl);
+} /* }}} void cr_submit_io */
+
+static void submit_interface (cr_data_t *rd, /* {{{ */
+    const ros_interface_t *i)
+{
+  if (i == NULL)
+    return;
+
+  if (!i->running)
+  {
+    submit_interface (rd, i->next);
+    return;
+  }
+
+  cr_submit_io (rd, "if_packets", i->name,
+      (counter_t) i->rx_packets, (counter_t) i->tx_packets);
+  cr_submit_io (rd, "if_octets", i->name,
+      (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
+  cr_submit_io (rd, "if_errors", i->name,
+      (counter_t) i->rx_errors, (counter_t) i->tx_errors);
+  cr_submit_io (rd, "if_dropped", i->name,
+      (counter_t) i->rx_drops, (counter_t) i->tx_drops);
+
+  submit_interface (rd, i->next);
+} /* }}} void submit_interface */
+
+static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
+    const ros_interface_t *i, void *user_data)
+{
+  if ((i == NULL) || (user_data == NULL))
+    return (EINVAL);
+
+  submit_interface (user_data, i);
+  return (0);
+} /* }}} int handle_interface */
+
+static void cr_submit_gauge (cr_data_t *rd, const char *type, /* {{{ */
+    const char *type_instance, gauge_t value)
+{
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].gauge = value;
+
+       vl.values = values;
+       vl.values_len = STATIC_ARRAY_SIZE (values);
+       sstrncpy (vl.host, rd->node, sizeof (vl.host));
+       sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+
+       plugin_dispatch_values (&vl);
+} /* }}} void cr_submit_gauge */
+
+#if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
+static void cr_submit_counter (cr_data_t *rd, const char *type, /* {{{ */
+    const char *type_instance, counter_t value)
+{
+       value_t values[1];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].counter = value;
+
+       vl.values = values;
+       vl.values_len = STATIC_ARRAY_SIZE (values);
+       sstrncpy (vl.host, rd->node, sizeof (vl.host));
+       sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+
+       plugin_dispatch_values (&vl);
+} /* }}} void cr_submit_gauge */
+#endif
+
+static void submit_regtable (cr_data_t *rd, /* {{{ */
+    const ros_registration_table_t *r)
+{
+  char type_instance[DATA_MAX_NAME_LEN];
+
+  if (r == NULL)
+    return;
+
+  /*** RX ***/
+  ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
+      r->interface, r->radio_name);
+  cr_submit_gauge (rd, "bitrate", type_instance,
+      (gauge_t) (1000000.0 * r->rx_rate));
+  cr_submit_gauge (rd, "signal_power", type_instance,
+      (gauge_t) r->rx_signal_strength);
+  cr_submit_gauge (rd, "signal_quality", type_instance,
+      (gauge_t) r->rx_ccq);
+
+  /*** TX ***/
+  ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
+      r->interface, r->radio_name);
+  cr_submit_gauge (rd, "bitrate", type_instance,
+      (gauge_t) (1000000.0 * r->tx_rate));
+  cr_submit_gauge (rd, "signal_power", type_instance,
+      (gauge_t) r->tx_signal_strength);
+  cr_submit_gauge (rd, "signal_quality", type_instance,
+      (gauge_t) r->tx_ccq);
+
+  /*** RX / TX ***/
+  ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
+      r->interface, r->radio_name);
+  cr_submit_io (rd, "if_octets", type_instance,
+      (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
+  cr_submit_gauge (rd, "snr", type_instance, (gauge_t) r->signal_to_noise);
+
+  submit_regtable (rd, r->next);
+} /* }}} void submit_regtable */
+
+static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
+    const ros_registration_table_t *r, void *user_data)
+{
+  if ((r == NULL) || (user_data == NULL))
+    return (EINVAL);
+
+  submit_regtable (user_data, r);
+  return (0);
+} /* }}} int handle_regtable */
+
+#if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
+static int handle_system_resource (__attribute__((unused)) ros_connection_t *c, /* {{{ */
+        const ros_system_resource_t *r,
+       __attribute__((unused)) void *user_data)
+{
+  cr_data_t *rd;
+
+  if ((r == NULL) || (user_data == NULL))
+    return (EINVAL);
+  rd = user_data;
+
+  if (rd->collect_cpu_load)
+    cr_submit_gauge (rd, "gauge", "cpu_load", (gauge_t) r->cpu_load);
+
+  if (rd->collect_memory)
+  {
+    cr_submit_gauge (rd, "memory", "used",
+       (gauge_t) (r->total_memory - r->free_memory));
+    cr_submit_gauge (rd, "memory", "free", (gauge_t) r->free_memory);
+  }
+
+  if (rd->collect_df)
+  {
+    cr_submit_gauge (rd, "df_complex", "used",
+       (gauge_t) (r->total_memory - r->free_memory));
+    cr_submit_gauge (rd, "df_complex", "free", (gauge_t) r->free_memory);
+  }
+
+  if (rd->collect_disk)
+  {
+    cr_submit_counter (rd, "counter", "secors_written", (counter_t) r->write_sect_total);
+    cr_submit_gauge (rd, "gauge", "bad_blocks", (gauge_t) r->bad_blocks);
+  }
+
+  return (0);
+} /* }}} int handle_system_resource */
+#endif
+
+static int cr_read (user_data_t *user_data) /* {{{ */
+{
+  int status;
+  cr_data_t *rd;
+
+  if (user_data == NULL)
+    return (EINVAL);
+
+  rd = user_data->data;
+  if (rd == NULL)
+    return (EINVAL);
+
+  if (rd->connection == NULL)
+  {
+    rd->connection = ros_connect (rd->node, rd->service,
+       rd->username, rd->password);
+    if (rd->connection == NULL)
+    {
+      char errbuf[128];
+      ERROR ("routeros plugin: ros_connect failed: %s",
+         sstrerror (errno, errbuf, sizeof (errbuf)));
+      return (-1);
+    }
+  }
+  assert (rd->connection != NULL);
+
+  if (rd->collect_interface)
+  {
+    status = ros_interface (rd->connection, handle_interface,
+       /* user data = */ rd);
+    if (status != 0)
+    {
+      char errbuf[128];
+      ERROR ("routeros plugin: ros_interface failed: %s",
+         sstrerror (status, errbuf, sizeof (errbuf)));
+      ros_disconnect (rd->connection);
+      rd->connection = NULL;
+      return (-1);
+    }
+  }
+
+  if (rd->collect_regtable)
+  {
+    status = ros_registration_table (rd->connection, handle_regtable,
+       /* user data = */ rd);
+    if (status != 0)
+    {
+      char errbuf[128];
+      ERROR ("routeros plugin: ros_registration_table failed: %s",
+         sstrerror (status, errbuf, sizeof (errbuf)));
+      ros_disconnect (rd->connection);
+      rd->connection = NULL;
+      return (-1);
+    }
+  }
+
+#if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
+  if (rd->collect_cpu_load
+      || rd->collect_memory
+      || rd->collect_df
+      || rd->collect_disk)
+  {
+    status = ros_system_resource (rd->connection, handle_system_resource,
+       /* user data = */ rd);
+    if (status != 0)
+    {
+      char errbuf[128];
+      ERROR ("routeros plugin: ros_system_resource failed: %s",
+         sstrerror (status, errbuf, sizeof (errbuf)));
+      ros_disconnect (rd->connection);
+      rd->connection = NULL;
+      return (-1);
+    }
+  }
+#endif
+
+  return (0);
+} /* }}} int cr_read */
+
+static void cr_free_data (cr_data_t *ptr) /* {{{ */
+{
+  if (ptr == NULL)
+    return;
+
+  ros_disconnect (ptr->connection);
+  ptr->connection = NULL;
+
+  sfree (ptr->node);
+  sfree (ptr->service);
+  sfree (ptr->username);
+  sfree (ptr->password);
+
+  sfree (ptr);
+} /* }}} void cr_free_data */
+
+static int cr_config_router (oconfig_item_t *ci) /* {{{ */
+{
+  cr_data_t *router_data;
+  char read_name[128];
+  user_data_t user_data;
+  int status;
+  int i;
+
+  router_data = malloc (sizeof (*router_data));
+  if (router_data == NULL)
+    return (-1);
+  memset (router_data, 0, sizeof (router_data));
+  router_data->connection = NULL;
+  router_data->node = NULL;
+  router_data->service = NULL;
+  router_data->username = NULL;
+  router_data->password = NULL;
+
+  status = 0;
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Host", child->key) == 0)
+      status = cf_util_get_string (child, &router_data->node);
+    else if (strcasecmp ("Port", child->key) == 0)
+      status = cf_util_get_string (child, &router_data->service);
+    else if (strcasecmp ("User", child->key) == 0)
+      status = cf_util_get_string (child, &router_data->username);
+    else if (strcasecmp ("Password", child->key) == 0)
+      status = cf_util_get_string (child, &router_data->password);
+    else if (strcasecmp ("CollectInterface", child->key) == 0)
+      cf_util_get_boolean (child, &router_data->collect_interface);
+    else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
+      cf_util_get_boolean (child, &router_data->collect_regtable);
+#if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
+    else if (strcasecmp ("CollectCPULoad", child->key) == 0)
+      cf_util_get_boolean (child, &router_data->collect_cpu_load);
+    else if (strcasecmp ("CollectMemory", child->key) == 0)
+      cf_util_get_boolean (child, &router_data->collect_memory);
+    else if (strcasecmp ("CollectDF", child->key) == 0)
+      cf_util_get_boolean (child, &router_data->collect_df);
+    else if (strcasecmp ("CollectDisk", child->key) == 0)
+      cf_util_get_boolean (child, &router_data->collect_disk);
+#endif
+    else
+    {
+      WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
+    }
+
+    if (status != 0)
+      break;
+  }
+
+  if (status == 0)
+  {
+    if (router_data->node == NULL)
+    {
+      ERROR ("routeros plugin: No `Host' option within a `Router' block. "
+         "Where should I connect to?");
+      status = -1;
+    }
+
+    if (router_data->password == NULL)
+    {
+      ERROR ("routeros plugin: No `Password' option within a `Router' block. "
+         "How should I authenticate?");
+      status = -1;
+    }
+
+    if (!router_data->collect_interface
+       && !router_data->collect_regtable)
+    {
+      ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
+         "What statistics should I collect?");
+      status = -1;
+    }
+  }
+
+  if ((status == 0) && (router_data->username == NULL))
+  {
+    router_data->username = sstrdup ("admin");
+    if (router_data->username == NULL)
+    {
+      ERROR ("routeros plugin: sstrdup failed.");
+      status = -1;
+    }
+  }
+
+  ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
+  user_data.data = router_data;
+  user_data.free_func = (void *) cr_free_data;
+  if (status == 0)
+    status = plugin_register_complex_read (/* group = */ NULL, read_name,
+       cr_read, /* interval = */ NULL, &user_data);
+
+  if (status != 0)
+    cr_free_data (router_data);
+
+  return (status);
+} /* }}} int cr_config_router */
+
+static int cr_config (oconfig_item_t *ci)
+{
+  int i;
+
+  for (i = 0; i < ci->children_num; i++)
+  {
+    oconfig_item_t *child = ci->children + i;
+
+    if (strcasecmp ("Router", child->key) == 0)
+      cr_config_router (child);
+    else
+    {
+      WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
+    }
+  }
+
+  return (0);
+} /* }}} int cr_config */
+
+void module_register (void)
+{
+  plugin_register_complex_config ("routeros", cr_config);
+} /* void module_register */
+
+/* vim: set sw=2 noet fdm=marker : */
index bca10be..34e860b 100644 (file)
@@ -24,6 +24,8 @@
 #include "common.h"
 #include "utils_rrdcreate.h"
 
+#undef HAVE_CONFIG_H
+#include <rrd.h>
 #include <rrd_client.h>
 
 /*
index ee5d70c..01fca30 100644 (file)
@@ -432,11 +432,6 @@ static void *rrd_queue_thread (void __attribute__((unused)) *data)
                sfree (queue_entry);
        } /* while (42) */
 
-       pthread_mutex_lock (&cache_lock);
-       c_avl_destroy (cache);
-       cache = NULL;
-       pthread_mutex_unlock (&cache_lock);
-
        pthread_exit ((void *) 0);
        return ((void *) 0);
 } /* void *rrd_queue_thread */
@@ -676,6 +671,7 @@ static int rrd_cache_insert (const char *filename,
                rc->values = NULL;
                rc->first_value = 0;
                rc->last_value = 0;
+               rc->random_variation = 0;
                rc->flags = FLAG_NONE;
                new_rc = 1;
        }
@@ -785,6 +781,59 @@ static int rrd_cache_insert (const char *filename,
        return (0);
 } /* int rrd_cache_insert */
 
+static int rrd_cache_destroy (void) /* {{{ */
+{
+  void *key = NULL;
+  void *value = NULL;
+
+  int non_empty = 0;
+
+  pthread_mutex_lock (&cache_lock);
+
+  if (cache == NULL)
+  {
+    pthread_mutex_unlock (&cache_lock);
+    return (0);
+  }
+
+  while (c_avl_pick (cache, &key, &value) == 0)
+  {
+    rrd_cache_t *rc;
+    int i;
+
+    sfree (key);
+    key = NULL;
+
+    rc = value;
+    value = NULL;
+
+    if (rc->values_num > 0)
+      non_empty++;
+
+    for (i = 0; i < rc->values_num; i++)
+      sfree (rc->values[i]);
+    sfree (rc->values);
+    sfree (rc);
+  }
+
+  c_avl_destroy (cache);
+  cache = NULL;
+
+  if (non_empty > 0)
+  {
+    INFO ("rrdtool plugin: %i cache %s had values when destroying the cache.",
+        non_empty, (non_empty == 1) ? "entry" : "entries");
+  }
+  else
+  {
+    DEBUG ("rrdtool plugin: No values have been lost "
+        "when destroying the cache.");
+  }
+
+  pthread_mutex_unlock (&cache_lock);
+  return (0);
+} /* }}} int rrd_cache_destroy */
+
 static int rrd_compare_numeric (const void *a_ptr, const void *b_ptr)
 {
        int a = *((int *) a_ptr);
@@ -806,6 +855,9 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl,
        char         values[512];
        int          status;
 
+       if (do_shutdown)
+               return (0);
+
        if (0 != strcmp (ds->type, vl->type)) {
                ERROR ("rrdtool plugin: DS type does not match value list type");
                return -1;
@@ -1062,7 +1114,7 @@ static int rrd_shutdown (void)
                DEBUG ("rrdtool plugin: queue_thread exited.");
        }
 
-       /* TODO: Maybe it'd be a good idea to free the cache here.. */
+       rrd_cache_destroy ();
 
        return (0);
 } /* int rrd_shutdown */
index 7568cf6..fdb7c65 100644 (file)
@@ -655,8 +655,9 @@ static int csnmp_config_add_host (oconfig_item_t *ci)
   if (hd->interval != 0)
     cb_interval.tv_sec = (time_t) hd->interval;
 
-  status = plugin_register_complex_read (cb_name, csnmp_read_host,
-      /* interval = */ &cb_interval, /* user_data = */ &cb_data);
+  status = plugin_register_complex_read (/* group = */ NULL, cb_name,
+      csnmp_read_host, /* interval = */ &cb_interval,
+      /* user_data = */ &cb_data);
   if (status != 0)
   {
     ERROR ("snmp plugin: Registering complex read function failed.");
@@ -724,7 +725,8 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
     double scale, double shift)
 {
   value_t ret;
-  uint64_t temp = 0;
+  uint64_t tmp_unsigned = 0;
+  int64_t tmp_signed = 0;
   int defined = 1;
 
   if ((vl->type == ASN_INTEGER)
@@ -735,15 +737,17 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
 #endif
       || (vl->type == ASN_GAUGE))
   {
-    temp = (uint32_t) *vl->val.integer;
-    DEBUG ("snmp plugin: Parsed int32 value is %"PRIu64".", temp);
+    tmp_unsigned = (uint32_t) *vl->val.integer;
+    tmp_signed = (int32_t) *vl->val.integer;
+    DEBUG ("snmp plugin: Parsed int32 value is %"PRIi64".", tmp_signed);
   }
   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 %"PRIu64".", temp);
+    tmp_unsigned = (uint32_t) vl->val.counter64->high;
+    tmp_unsigned = tmp_unsigned << 32;
+    tmp_unsigned += (uint32_t) vl->val.counter64->low;
+    tmp_signed = (int64_t) tmp_unsigned;
+    DEBUG ("snmp plugin: Parsed int64 value is %"PRIu64".", tmp_unsigned);
   }
   else if (vl->type == ASN_OCTET_STR)
   {
@@ -805,17 +809,19 @@ static value_t csnmp_value_list_to_value (struct variable_list *vl, int type,
     }
   } /* if (vl->type == ASN_OCTET_STR) */
   else if (type == DS_TYPE_COUNTER)
-    ret.counter = temp;
+  {
+    ret.counter = tmp_unsigned;
+  }
   else if (type == DS_TYPE_GAUGE)
   {
     ret.gauge = NAN;
     if (defined != 0)
-      ret.gauge = (scale * temp) + shift;
+      ret.gauge = (scale * tmp_signed) + shift;
   }
   else if (type == DS_TYPE_DERIVE)
-    ret.derive = (derive_t) temp;
+    ret.derive = (derive_t) tmp_signed;
   else if (type == DS_TYPE_ABSOLUTE)
-    ret.absolute = (absolute_t) temp;
+    ret.absolute = (absolute_t) tmp_unsigned;
   else
   {
     ERROR ("snmp plugin: csnmp_value_list_to_value: Unknown data source "
@@ -889,6 +895,72 @@ static int csnmp_check_res_left_subtree (const host_definition_t *host,
   return (0);
 } /* int csnmp_check_res_left_subtree */
 
+static int csnmp_strvbcopy_hexstring (char *dst, /* {{{ */
+    const struct variable_list *vb, size_t dst_size)
+{
+  char *buffer_ptr;
+  size_t buffer_free;
+  size_t i;
+
+  buffer_ptr = dst;
+  buffer_free = dst_size;
+
+  for (i = 0; i < vb->val_len; i++)
+  {
+    int status;
+
+    status = snprintf (buffer_ptr, buffer_free,
+       (i == 0) ? "%02x" : ":%02x", (unsigned int) vb->val.bitstring[i]);
+
+    if (status >= buffer_free)
+    {
+      buffer_ptr += (buffer_free - 1);
+      *buffer_ptr = 0;
+      return (dst_size + (buffer_free - status));
+    }
+    else /* if (status < buffer_free) */
+    {
+      buffer_ptr += status;
+      buffer_free -= status;
+    }
+  }
+
+  return ((int) (dst_size - buffer_free));
+} /* }}} int csnmp_strvbcopy_hexstring */
+
+static int csnmp_strvbcopy (char *dst, /* {{{ */
+    const struct variable_list *vb, size_t dst_size)
+{
+  char *src;
+  size_t num_chars;
+  size_t i;
+
+  if (vb->type == ASN_OCTET_STR)
+    src = (char *) vb->val.string;
+  else if (vb->type == ASN_BIT_STR)
+    src = (char *) vb->val.bitstring;
+  else
+  {
+    dst[0] = 0;
+    return (EINVAL);
+  }
+
+  num_chars = dst_size - 1;
+  if (num_chars > vb->val_len)
+    num_chars = vb->val_len;
+
+  for (i = 0; i < num_chars; i++)
+  {
+    /* Check for control characters. */
+    if ((src[i] >= 0) && (src[i] < 32))
+      return (csnmp_strvbcopy_hexstring (dst, vb, dst_size));
+    dst[i] = src[i];
+  }
+  dst[num_chars] = 0;
+
+  return ((int) vb->val_len);
+} /* }}} int csnmp_strvbcopy */
+
 static int csnmp_instance_list_add (csnmp_list_instances_t **head,
     csnmp_list_instances_t **tail,
     const struct snmp_pdu *res)
@@ -917,17 +989,8 @@ static int csnmp_instance_list_add (csnmp_list_instances_t **head,
   if ((vb->type == ASN_OCTET_STR) || (vb->type == ASN_BIT_STR))
   {
     char *ptr;
-    size_t instance_len;
-
-    memset (il->instance, 0, sizeof (il->instance));
-    instance_len = sizeof (il->instance) - 1;
-    if (instance_len > vb->val_len)
-      instance_len = vb->val_len;
 
-    sstrncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
-         ? vb->val.string
-         : vb->val.bitstring),
-       instance_len + 1);
+    csnmp_strvbcopy (il->instance, vb, sizeof (il->instance));
 
     for (ptr = il->instance; *ptr != '\0'; ptr++)
     {
@@ -1065,7 +1128,7 @@ static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *dat
       char temp[DATA_MAX_NAME_LEN];
 
       if (instance_list_ptr == NULL)
-       ssnprintf (temp, sizeof (temp), "%u", (uint32_t) subid);
+       ssnprintf (temp, sizeof (temp), "%"PRIu32, (uint32_t) subid);
       else
        sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
 
index 22eda1f..e467818 100644 (file)
@@ -2,6 +2,7 @@
  * collectd - src/swap.c
  * Copyright (C) 2005-2009  Florian octo Forster
  * Copyright (C) 2009       Stefan Völkel
+ * Copyright (C) 2009       Manuel Sanmartin
  *
  * 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
@@ -18,6 +19,7 @@
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Manuel Sanmartin
  **/
 
 #if HAVE_CONFIG_H
 # include <statgrab.h>
 #endif
 
+#if HAVE_PERFSTAT
+# include <sys/protosw.h>
+# include <libperfstat.h>
+#endif
+
 #undef  MAX
 #define MAX(x,y) ((x) > (y) ? (x) : (y))
 
@@ -86,6 +93,11 @@ int kvm_pagesize;
 /* No global variables */
 /* #endif HAVE_LIBSTATGRAB */
 
+#elif HAVE_PERFSTAT
+static int pagesize;
+static perfstat_memory_total_t pmemory;
+/*# endif HAVE_PERFSTAT */
+
 #else
 # error "No applicable input method."
 #endif /* HAVE_LIBSTATGRAB */
@@ -134,7 +146,11 @@ static int swap_init (void)
 
 #elif HAVE_LIBSTATGRAB
        /* No init stuff */
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif HAVE_LIBSTATGRAB */
+
+#elif HAVE_PERFSTAT
+       pagesize = getpagesize();
+#endif /* HAVE_PERFSTAT */
 
        return (0);
 }
@@ -173,10 +189,12 @@ static int swap_read (void)
 #if KERNEL_LINUX
        FILE *fh;
        char buffer[1024];
-       
+
        char *fields[8];
        int numfields;
 
+       _Bool old_kernel=0;
+
        derive_t swap_used   = 0;
        derive_t swap_cached = 0;
        derive_t swap_free   = 0;
@@ -227,30 +245,44 @@ static int swap_read (void)
 
        if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
        {
-               char errbuf[1024];
-               WARNING ("swap: fopen: %s",
-                               sstrerror (errno, errbuf, sizeof (errbuf)));
-               return (-1);
+               // /proc/vmstat does not exist in kernels <2.6
+               if ((fh = fopen ("/proc/stat", "r")) == NULL )
+               {
+                       char errbuf[1024];
+                       WARNING ("swap: fopen: %s",
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
+                       return (-1);
+               }
+               else
+                       old_kernel = 1;
        }
 
        while (fgets (buffer, 1024, fh) != NULL)
        {
-               derive_t *val = NULL;
-
-               if (strncasecmp (buffer, "pswpin", 6) == 0)
-                       val = &swap_in;
-               else if (strncasecmp (buffer, "pswpout", 7) == 0)
-                       val = &swap_out;
-               else
-                       continue;
-
-               numfields = strsplit (buffer, fields, 8);
-
-               if (numfields < 2)
-                       continue;
-
-               *val = (derive_t) atoll (fields[1]);
-       }
+               numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
+
+               if (!old_kernel)
+               {
+                       if (numfields != 2)
+                               continue;
+
+                       if (strcasecmp ("pswpin", fields[0]) != 0)
+                               strtoderive (fields[1], &swap_in);
+                       else if (strcasecmp ("pswpout", fields[0]) == 0)
+                               strtoderive (fields[1], &swap_out);
+               }
+               else /* if (old_kernel) */
+               {
+                       if (numfields != 3)
+                               continue;
+
+                       if (strcasecmp ("page", fields[0]) == 0)
+                       {
+                               strtoderive (fields[1], &swap_in);
+                               strtoderive (fields[2], &swap_out);
+                       }
+               }
+       } /* while (fgets) */
 
        if (fclose (fh))
        {
@@ -264,7 +296,6 @@ static int swap_read (void)
        swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
        swap_submit ("in", swap_in, DS_TYPE_DERIVE);
        swap_submit ("out", swap_out, DS_TYPE_DERIVE);
-
 /* #endif KERNEL_LINUX */
 
 #elif HAVE_LIBKSTAT
@@ -294,7 +325,7 @@ static int swap_read (void)
         * However, Solaris does not allow to allocated/reserved more than the
         * available swap (physical memory + disk swap), so the pedant may
         * prefer: allocated + unallocated = reserved, available
-        * 
+        *
         * We map the above to: used + resv = n/a, free
         *
         * Does your brain hurt yet?  - Christophe Kalt
@@ -449,7 +480,19 @@ static int swap_read (void)
 
        swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
        swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
-#endif /* HAVE_LIBSTATGRAB */
+/* #endif  HAVE_LIBSTATGRAB */
+
+#elif HAVE_PERFSTAT
+        if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
+       {
+                char errbuf[1024];
+                WARNING ("memory plugin: perfstat_memory_total failed: %s",
+                        sstrerror (errno, errbuf, sizeof (errbuf)));
+                return (-1);
+        }
+       swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
+       swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
+#endif /* HAVE_PERFSTAT */
 
        return (0);
 } /* int swap_read */
index 8becc05..25cbcd4 100644 (file)
@@ -30,6 +30,7 @@
  *     Instance "exim"
  *     <Match>
  *       Regex "S=([1-9][0-9]*)"
+ *       ExcludeRegex "U=root.*S="
  *       DSType "CounterAdd"
  *       Type "ipt_bytes"
  *       Instance "total"
@@ -41,6 +42,7 @@
 struct ctail_config_match_s
 {
   char *regex;
+  char *excluderegex;
   int flags;
   char *type;
   char *type_instance;
@@ -157,6 +159,9 @@ static int ctail_config_add_match (cu_tail_match_t *tm,
 
     if (strcasecmp ("Regex", option->key) == 0)
       status = ctail_config_add_string ("Regex", &cm.regex, option);
+    else if (strcasecmp ("ExcludeRegex", option->key) == 0)
+      status = ctail_config_add_string ("ExcludeRegex", &cm.excluderegex,
+                                       option);
     else if (strcasecmp ("DSType", option->key) == 0)
       status = ctail_config_add_match_dstype (&cm, option);
     else if (strcasecmp ("Type", option->key) == 0)
@@ -201,8 +206,8 @@ static int ctail_config_add_match (cu_tail_match_t *tm,
 
   if (status == 0)
   {
-    status = tail_match_add_match_simple (tm, cm.regex, cm.flags,
-       "tail", plugin_instance, cm.type, cm.type_instance);
+    status = tail_match_add_match_simple (tm, cm.regex, cm.excluderegex,
+       cm.flags, "tail", plugin_instance, cm.type, cm.type_instance);
 
     if (status != 0)
     {
@@ -211,6 +216,7 @@ static int ctail_config_add_match (cu_tail_match_t *tm,
   }
 
   sfree (cm.regex);
+  sfree (cm.excluderegex);
   sfree (cm.type);
   sfree (cm.type_instance);
 
index 6b261c7..29fecdf 100644 (file)
@@ -302,11 +302,15 @@ static int ts_config_set_double (double *ret, oconfig_item_t *ci) /* {{{ */
 
 static int ts_destroy (void **user_data) /* {{{ */
 {
+       ts_data_t **data;
+
        if (user_data == NULL)
                return (-EINVAL);
 
-       free (*user_data);
-       *user_data = NULL;
+       data = (ts_data_t **) user_data;
+
+       free (*data);
+       *data = NULL;
 
        return (0);
 } /* }}} int ts_destroy */
index 2b70805..b9d07bf 100644 (file)
@@ -218,13 +218,13 @@ static int thermal_config (const char *key, const char *value)
 static int thermal_sysfs_read (void)
 {
        return walk_directory (dirname_sysfs, thermal_sysfs_device_read,
-                       /* user_data = */ NULL);
+                       /* user_data = */ NULL, /* include hidden */ 0);
 }
 
 static int thermal_procfs_read (void)
 {
        return walk_directory (dirname_procfs, thermal_procfs_device_read,
-                       /* user_data = */ NULL);
+                       /* user_data = */ NULL, /* include hidden */ 0);
 }
 
 static int thermal_init (void)
index c8c6b98..69301b2 100644 (file)
@@ -1,6 +1,7 @@
 absolute               count:ABSOLUTE:0:U
 apache_bytes           count:COUNTER:0:134217728
 apache_connections     count:GAUGE:0:65535
+apache_idle_workers    count:GAUGE:0:65535
 apache_requests                count:COUNTER:0:134217728
 apache_scoreboard      count:GAUGE:0:65535
 arc_counts             demand_data:COUNTER:0:U, demand_metadata:COUNTER:0:U, prefetch_data:COUNTER:0:U, prefetch_metadata:COUNTER:0:U
@@ -16,6 +17,7 @@ cache_ratio           value:GAUGE:0:100
 cache_result           value:COUNTER:0:4294967295
 cache_size             value:GAUGE:0:4294967295
 charge                 value:GAUGE:0:U
+compression            uncompressed:COUNTER:0:U, compressed:COUNTER:0:U
 compression_ratio      value:GAUGE:0:2
 connections            value:COUNTER:0:U
 conntrack              entropy:GAUGE:0:4294967295
@@ -126,6 +128,8 @@ ps_pagefaults               minflt:COUNTER:0:9223372036854775807, majflt:COUNTER:0:9223372036
 ps_disk_octets         read:DERIVE:0:U, write:DERIVE:0:U
 ps_disk_ops            read:DERIVE:0:U, write:DERIVE:0:U
 ps_rss                 value:GAUGE:0:9223372036854775807
+ps_code                        value:GAUGE:0:9223372036854775807
+ps_data                        value:GAUGE:0:9223372036854775807
 ps_stacksize           value:GAUGE:0:9223372036854775807
 ps_state               value:GAUGE:0:65535
 ps_vm                  value:GAUGE:0:9223372036854775807
@@ -139,6 +143,7 @@ serial_octets               rx:COUNTER:0:4294967295, tx:COUNTER:0:4294967295
 signal_noise           value:GAUGE:U:0
 signal_power           value:GAUGE:U:0
 signal_quality         value:GAUGE:0:U
+snr                    value:GAUGE:0:U
 spam_check             value:GAUGE:0:U
 spam_score             value:GAUGE:U:U
 swap_io                        value:DERIVE:0:1099511627776
@@ -149,6 +154,7 @@ threads                     value:GAUGE:0:U
 time_dispersion                seconds:GAUGE:-1000000:1000000
 timeleft               timeleft:GAUGE:0:3600
 time_offset            seconds:GAUGE:-1000000:1000000
+total_bytes            value:DERIVE:0:U
 total_requests         value:DERIVE:0:U
 total_time_in_ms       value:DERIVE:0:U
 total_values           value:DERIVE:0:U
@@ -165,3 +171,4 @@ voltage                     value:GAUGE:U:U
 vs_memory              value:GAUGE:0:9223372036854775807
 vs_processes           value:GAUGE:0:65535
 vs_threads             value:GAUGE:0:65535
+pinba_view              req_per_sec:GAUGE:0:U, req_time:GAUGE:0:U, ru_utime:GAUGE:0:U, ru_stime:GAUGE:0:U, doc_size:GAUGE:0:U, mem_peak:GAUGE:0:U
index a46eb41..67f9881 100644 (file)
@@ -21,7 +21,8 @@ The format of the data-source specification has been inspired by RRDtool's
 data-source specification. Each data-source is defined by a quadruple made up
 of the data-source name, type, minimal and maximal values, delimited by colons
 (":"): I<ds-name>:I<ds-type>:I<min>:I<max>. I<ds-type> may be either
-B<COUNTER> or B<GAUGE>. I<min> and I<max> define the range of valid values for
+B<ABSOLUTE>, B<COUNTER>, B<DERIVE>, or B<GAUGE>. I<min> and I<max> define the
+range of valid values for
 data stored for this data-source. If B<U> is specified for either the min or
 max value, it will be set to unknown, meaning that no range checks will
 happen. See L<rrdcreate(1)> for more details.
index 648c54d..69ea864 100644 (file)
@@ -319,7 +319,7 @@ int uc_check_timeout (void)
   while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0)
   {
     /* If entry has not been updated, add to `keys' array */
-    if ((now - ce->last_update) >= (2 * ce->interval))
+    if ((now - ce->last_update) >= (timeout_g * ce->interval))
     {
       char **tmp;
 
index bca83a9..4ca9646 100644 (file)
 #include "utils_cache.h"
 #include "utils_parse_option.h"
 
+#define free_everything_and_return(status) do { \
+    size_t j; \
+    for (j = 0; j < number; j++) { \
+      sfree(names[j]); \
+      names[j] = NULL; \
+    } \
+    sfree(names); \
+    sfree(times); \
+    return (status); \
+  } while (0)
+
 #define print_to_socket(fh, ...) \
   if (fprintf (fh, __VA_ARGS__) < 0) { \
     char errbuf[1024]; \
     WARNING ("handle_listval: failed to write to socket #%i: %s", \
        fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
-    return -1; \
+    free_everything_and_return (-1); \
   }
 
 int handle_listval (FILE *fh, char *buffer)
@@ -52,20 +63,20 @@ int handle_listval (FILE *fh, char *buffer)
   if (status != 0)
   {
     print_to_socket (fh, "-1 Cannot parse command.\n");
-    return (-1);
+    free_everything_and_return (-1);
   }
   assert (command != NULL);
 
   if (strcasecmp ("LISTVAL", command) != 0)
   {
     print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
-    return (-1);
+    free_everything_and_return (-1);
   }
 
   if (*buffer != 0)
   {
     print_to_socket (fh, "-1 Garbage after end of command: %s\n", buffer);
-    return (-1);
+    free_everything_and_return (-1);
   }
 
   status = uc_get_names (&names, &times, &number);
@@ -73,7 +84,7 @@ int handle_listval (FILE *fh, char *buffer)
   {
     DEBUG ("command listval: uc_get_names failed with status %i", status);
     print_to_socket (fh, "-1 uc_get_names failed.\n");
-    return (-1);
+    free_everything_and_return (-1);
   }
 
   print_to_socket (fh, "%i Value%s found\n",
@@ -81,7 +92,7 @@ int handle_listval (FILE *fh, char *buffer)
   for (i = 0; i < number; i++)
     print_to_socket (fh, "%u %s\n", (unsigned int) times[i], names[i]);
 
-  return (0);
+  free_everything_and_return (0);
 } /* int handle_listval */
 
 /* vim: set sw=2 sts=2 ts=8 : */
index 5f892a4..7d594d8 100644 (file)
@@ -39,13 +39,6 @@ struct udb_result_s
   char   **values;
   size_t   values_num;
 
-  /* Preparation area */
-  const   data_set_t *ds;
-  size_t *instances_pos;
-  size_t *values_pos;
-  char  **instances_buffer;
-  char  **values_buffer;
-
   /* Legacy data */
   int legacy_mode;
   size_t legacy_position;
@@ -69,13 +62,31 @@ struct udb_query_s /* {{{ */
   unsigned int min_version;
   unsigned int max_version;
 
-  /* Preparation area */
+  udb_result_t *results;
+}; /* }}} */
+
+struct udb_result_preparation_area_s /* {{{ */
+{
+  const   data_set_t *ds;
+  size_t *instances_pos;
+  size_t *values_pos;
+  char  **instances_buffer;
+  char  **values_buffer;
+
+  struct udb_result_preparation_area_s *next;
+}; /* }}} */
+typedef struct udb_result_preparation_area_s udb_result_preparation_area_t;
+
+struct udb_query_preparation_area_s /* {{{ */
+{
   size_t column_num;
   char *host;
   char *plugin;
   char *db_name;
 
-  udb_result_t *results;
+  int interval;
+
+  udb_result_preparation_area_t *result_prep_areas;
 }; /* }}} */
 
 /*
@@ -182,43 +193,49 @@ static int udb_config_set_uint (unsigned int *ret_value, /* {{{ */
 /*
  * Legacy result private functions
  */
-static void udb_legacy_result_finish_result (udb_result_t *r) /* {{{ */
+static void udb_legacy_result_finish_result (const udb_result_t const *r, /* {{{ */
+    udb_result_preparation_area_t *prep_area)
 {
-  if (r == NULL)
+  if ((r == NULL) || (prep_area))
     return;
 
   assert (r->legacy_mode == 1);
 
-  r->ds = NULL;
+  prep_area->ds = NULL;
 } /* }}} void udb_legacy_result_finish_result */
 
 static int udb_legacy_result_handle_result (udb_result_t *r, /* {{{ */
-    udb_query_t *q, char **column_values)
+    udb_query_preparation_area_t *q_area,
+    udb_result_preparation_area_t *r_area,
+    const udb_query_t const *q, char **column_values)
 {
   value_list_t vl = VALUE_LIST_INIT;
   value_t value;
   char *value_str;
 
   assert (r->legacy_mode == 1);
-  assert (r->ds != NULL);
-  assert (r->ds->ds_num == 1);
+  assert (r_area->ds != NULL);
+  assert (r_area->ds->ds_num == 1);
 
   vl.values = &value;
   vl.values_len = 1;
 
   value_str = column_values[r->legacy_position];
-  if (0 != parse_value (value_str, &vl.values[0], r->ds->ds[0].type))
+  if (0 != parse_value (value_str, &vl.values[0], r_area->ds->ds[0].type))
   {
     ERROR ("db query utils: udb_legacy_result_handle_result: "
         "Parsing `%s' as %s failed.", value_str,
-        DS_TYPE_TO_STRING (r->ds->ds[0].type));
+        DS_TYPE_TO_STRING (r_area->ds->ds[0].type));
     errno = EINVAL;
     return (-1);
   }
 
-  sstrncpy (vl.host, q->host, sizeof (vl.host));
-  sstrncpy (vl.plugin, q->plugin, sizeof (vl.plugin));
-  sstrncpy (vl.plugin_instance, q->db_name, sizeof (vl.type_instance));
+  if (q_area->interval > 0)
+    vl.interval = q_area->interval;
+
+  sstrncpy (vl.host, q_area->host, sizeof (vl.host));
+  sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
+  sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.type_instance));
   sstrncpy (vl.type, r->type, sizeof (vl.type));
 
   if (r->instance_prefix != NULL)
@@ -230,7 +247,8 @@ static int udb_legacy_result_handle_result (udb_result_t *r, /* {{{ */
   return (0);
 } /* }}} int udb_legacy_result_handle_result */
 
-static int udb_legacy_result_prepare_result (udb_result_t *r, /* {{{ */
+static int udb_legacy_result_prepare_result (const udb_result_t const *r, /* {{{ */
+    udb_result_preparation_area_t *prep_area,
     char **column_names, size_t column_num)
 {
   if (r == NULL)
@@ -239,7 +257,7 @@ static int udb_legacy_result_prepare_result (udb_result_t *r, /* {{{ */
   assert (r->legacy_mode == 1);
 
   /* Make sure previous preparations are cleaned up. */
-  udb_legacy_result_finish_result (r);
+  udb_legacy_result_finish_result (r, prep_area);
 
   if (r->legacy_position >= column_num)
   {
@@ -250,8 +268,8 @@ static int udb_legacy_result_prepare_result (udb_result_t *r, /* {{{ */
   }
 
   /* Read `ds' and check number of values {{{ */
-  r->ds = plugin_get_ds (r->type);
-  if (r->ds == NULL)
+  prep_area->ds = plugin_get_ds (r->type);
+  if (prep_area->ds == NULL)
   {
     ERROR ("db query utils: udb_result_prepare_result: Type `%s' is not "
         "known by the daemon. See types.db(5) for details.",
@@ -259,13 +277,13 @@ static int udb_legacy_result_prepare_result (udb_result_t *r, /* {{{ */
     return (-1);
   }
 
-  if (r->ds->ds_num != 1)
+  if (prep_area->ds->ds_num != 1)
   {
     ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
         "requires exactly %i values, but the legacy configuration "
         "requires exactly one!",
         r->type,
-        r->ds->ds_num);
+        prep_area->ds->ds_num);
     return (-1);
   }
   /* }}} */
@@ -342,40 +360,45 @@ static int udb_legacy_result_create (const char *query_name, /* {{{ */
 /*
  * Result private functions
  */
-static int udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */
+static int udb_result_submit (udb_result_t *r, /* {{{ */
+    udb_result_preparation_area_t *r_area,
+    const udb_query_t const *q, udb_query_preparation_area_t *q_area)
 {
   value_list_t vl = VALUE_LIST_INIT;
   size_t i;
 
   assert (r != NULL);
   assert (r->legacy_mode == 0);
-  assert (r->ds != NULL);
-  assert (((size_t) r->ds->ds_num) == r->values_num);
+  assert (r_area->ds != NULL);
+  assert (((size_t) r_area->ds->ds_num) == r->values_num);
 
-  vl.values = (value_t *) calloc (r->ds->ds_num, sizeof (value_t));
+  vl.values = (value_t *) calloc (r_area->ds->ds_num, sizeof (value_t));
   if (vl.values == NULL)
   {
     ERROR ("db query utils: malloc failed.");
     return (-1);
   }
-  vl.values_len = r->ds->ds_num;
+  vl.values_len = r_area->ds->ds_num;
 
   for (i = 0; i < r->values_num; i++)
   {
-    char *value_str = r->values_buffer[i];
+    char *value_str = r_area->values_buffer[i];
 
-    if (0 != parse_value (value_str, &vl.values[i], r->ds->ds[i].type))
+    if (0 != parse_value (value_str, &vl.values[i], r_area->ds->ds[i].type))
     {
       ERROR ("db query utils: udb_result_submit: Parsing `%s' as %s failed.",
-          value_str, DS_TYPE_TO_STRING (r->ds->ds[i].type));
+          value_str, DS_TYPE_TO_STRING (r_area->ds->ds[i].type));
       errno = EINVAL;
       return (-1);
     }
   }
 
-  sstrncpy (vl.host, q->host, sizeof (vl.host));
-  sstrncpy (vl.plugin, q->plugin, sizeof (vl.plugin));
-  sstrncpy (vl.plugin_instance, q->db_name, sizeof (vl.type_instance));
+  if (q_area->interval > 0)
+    vl.interval = q_area->interval;
+
+  sstrncpy (vl.host, q_area->host, sizeof (vl.host));
+  sstrncpy (vl.plugin, q_area->plugin, sizeof (vl.plugin));
+  sstrncpy (vl.plugin_instance, q_area->db_name, sizeof (vl.type_instance));
   sstrncpy (vl.type, r->type, sizeof (vl.type));
 
   /* Set vl.type_instance {{{ */
@@ -392,13 +415,14 @@ static int udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */
     if (r->instance_prefix == NULL)
     {
       strjoin (vl.type_instance, sizeof (vl.type_instance),
-          r->instances_buffer, r->instances_num, "-");
+          r_area->instances_buffer, r->instances_num, "-");
     }
     else
     {
       char tmp[DATA_MAX_NAME_LEN];
 
-      strjoin (tmp, sizeof (tmp), r->instances_buffer, r->instances_num, "-");
+      strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
+          r->instances_num, "-");
       tmp[sizeof (tmp) - 1] = 0;
 
       snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
@@ -414,74 +438,82 @@ static int udb_result_submit (udb_result_t *r, udb_query_t *q) /* {{{ */
   return (0);
 } /* }}} void udb_result_submit */
 
-static void udb_result_finish_result (udb_result_t *r) /* {{{ */
+static void udb_result_finish_result (const udb_result_t const *r, /* {{{ */
+    udb_result_preparation_area_t *prep_area)
 {
-  if (r == NULL)
+  if ((r == NULL) || (prep_area == NULL))
     return;
 
   if (r->legacy_mode == 1)
   {
-    udb_legacy_result_finish_result (r);
+    udb_legacy_result_finish_result (r, prep_area);
     return;
   }
 
   assert (r->legacy_mode == 0);
 
-  r->ds = NULL;
-  sfree (r->instances_pos);
-  sfree (r->values_pos);
-  sfree (r->instances_buffer);
-  sfree (r->values_buffer);
+  prep_area->ds = NULL;
+  sfree (prep_area->instances_pos);
+  sfree (prep_area->values_pos);
+  sfree (prep_area->instances_buffer);
+  sfree (prep_area->values_buffer);
 } /* }}} void udb_result_finish_result */
 
 static int udb_result_handle_result (udb_result_t *r, /* {{{ */
-    udb_query_t *q, char **column_values)
+    udb_query_preparation_area_t *q_area,
+    udb_result_preparation_area_t *r_area,
+    const udb_query_t const *q, char **column_values)
 {
   size_t i;
 
+  assert (r && q_area && r_area);
+
   if (r->legacy_mode == 1)
-    return (udb_legacy_result_handle_result (r, q, column_values));
+    return (udb_legacy_result_handle_result (r, q_area, r_area,
+          q, column_values));
 
   assert (r->legacy_mode == 0);
 
   for (i = 0; i < r->instances_num; i++)
-    r->instances_buffer[i] = column_values[r->instances_pos[i]];
+    r_area->instances_buffer[i] = column_values[r_area->instances_pos[i]];
 
   for (i = 0; i < r->values_num; i++)
-    r->values_buffer[i] = column_values[r->values_pos[i]];
+    r_area->values_buffer[i] = column_values[r_area->values_pos[i]];
 
-  return udb_result_submit (r, q);
+  return udb_result_submit (r, r_area, q, q_area);
 } /* }}} int udb_result_handle_result */
 
-static int udb_result_prepare_result (udb_result_t *r, /* {{{ */
+static int udb_result_prepare_result (const udb_result_t const *r, /* {{{ */
+    udb_result_preparation_area_t *prep_area,
     char **column_names, size_t column_num)
 {
   size_t i;
 
-  if (r == NULL)
+  if ((r == NULL) || (prep_area == NULL))
     return (-EINVAL);
 
   if (r->legacy_mode == 1)
-    return (udb_legacy_result_prepare_result (r, column_names, column_num));
+    return (udb_legacy_result_prepare_result (r, prep_area,
+          column_names, column_num));
 
   assert (r->legacy_mode == 0);
 
 #define BAIL_OUT(status) \
-  r->ds = NULL; \
-  sfree (r->instances_pos); \
-  sfree (r->values_pos); \
-  sfree (r->instances_buffer); \
-  sfree (r->values_buffer); \
+  prep_area->ds = NULL; \
+  sfree (prep_area->instances_pos); \
+  sfree (prep_area->values_pos); \
+  sfree (prep_area->instances_buffer); \
+  sfree (prep_area->values_buffer); \
   return (status)
 
   /* Make sure previous preparations are cleaned up. */
-  udb_result_finish_result (r);
-  r->instances_pos = NULL;
-  r->values_pos = NULL;
+  udb_result_finish_result (r, prep_area);
+  prep_area->instances_pos = NULL;
+  prep_area->values_pos = NULL;
 
   /* Read `ds' and check number of values {{{ */
-  r->ds = plugin_get_ds (r->type);
-  if (r->ds == NULL)
+  prep_area->ds = plugin_get_ds (r->type);
+  if (prep_area->ds == NULL)
   {
     ERROR ("db query utils: udb_result_prepare_result: Type `%s' is not "
         "known by the daemon. See types.db(5) for details.",
@@ -489,12 +521,12 @@ static int udb_result_prepare_result (udb_result_t *r, /* {{{ */
     BAIL_OUT (-1);
   }
 
-  if (((size_t) r->ds->ds_num) != r->values_num)
+  if (((size_t) prep_area->ds->ds_num) != r->values_num)
   {
     ERROR ("db query utils: udb_result_prepare_result: The type `%s' "
         "requires exactly %i value%s, but the configuration specifies %zu.",
         r->type,
-        r->ds->ds_num, (r->ds->ds_num == 1) ? "" : "s",
+        prep_area->ds->ds_num, (prep_area->ds->ds_num == 1) ? "" : "s",
         r->values_num);
     BAIL_OUT (-1);
   }
@@ -504,30 +536,34 @@ static int udb_result_prepare_result (udb_result_t *r, /* {{{ */
    * r->values_buffer {{{ */
   if (r->instances_num > 0)
   {
-    r->instances_pos = (size_t *) calloc (r->instances_num, sizeof (size_t));
-    if (r->instances_pos == NULL)
+    prep_area->instances_pos
+      = (size_t *) calloc (r->instances_num, sizeof (size_t));
+    if (prep_area->instances_pos == NULL)
     {
       ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
       BAIL_OUT (-ENOMEM);
     }
 
-    r->instances_buffer = (char **) calloc (r->instances_num, sizeof (char *));
-    if (r->instances_buffer == NULL)
+    prep_area->instances_buffer
+      = (char **) calloc (r->instances_num, sizeof (char *));
+    if (prep_area->instances_buffer == NULL)
     {
       ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
       BAIL_OUT (-ENOMEM);
     }
   } /* if (r->instances_num > 0) */
 
-  r->values_pos = (size_t *) calloc (r->values_num, sizeof (size_t));
-  if (r->values_pos == NULL)
+  prep_area->values_pos
+    = (size_t *) calloc (r->values_num, sizeof (size_t));
+  if (prep_area->values_pos == NULL)
   {
     ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
     BAIL_OUT (-ENOMEM);
   }
 
-  r->values_buffer = (char **) calloc (r->values_num, sizeof (char *));
-  if (r->values_buffer == NULL)
+  prep_area->values_buffer
+    = (char **) calloc (r->values_num, sizeof (char *));
+  if (prep_area->values_buffer == NULL)
   {
     ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
     BAIL_OUT (-ENOMEM);
@@ -543,7 +579,7 @@ static int udb_result_prepare_result (udb_result_t *r, /* {{{ */
     {
       if (strcasecmp (r->instances[i], column_names[j]) == 0)
       {
-        r->instances_pos[i] = j;
+        prep_area->instances_pos[i] = j;
         break;
       }
     }
@@ -566,7 +602,7 @@ static int udb_result_prepare_result (udb_result_t *r, /* {{{ */
     {
       if (strcasecmp (r->values[i], column_names[j]) == 0)
       {
-        r->values_pos[i] = j;
+        prep_area->values_pos[i] = j;
         break;
       }
     }
@@ -1029,33 +1065,45 @@ int udb_query_check_version (udb_query_t *q, unsigned int version) /* {{{ */
   return (1);
 } /* }}} int udb_query_check_version */
 
-void udb_query_finish_result (udb_query_t *q) /* {{{ */
+void udb_query_finish_result (const udb_query_t const *q, /* {{{ */
+    udb_query_preparation_area_t *prep_area)
 {
+  udb_result_preparation_area_t *r_area;
   udb_result_t *r;
 
-  if (q == NULL)
+  if ((q == NULL) || (prep_area == NULL))
     return;
 
-  q->column_num = 0;
-  sfree (q->host);
-  sfree (q->plugin);
-  sfree (q->db_name);
+  prep_area->column_num = 0;
+  sfree (prep_area->host);
+  sfree (prep_area->plugin);
+  sfree (prep_area->db_name);
 
-  for (r = q->results; r != NULL; r = r->next)
-    udb_result_finish_result (r);
+  prep_area->interval = -1;
+
+  for (r = q->results, r_area = prep_area->result_prep_areas;
+      r != NULL; r = r->next, r_area = r_area->next)
+  {
+    /* this may happen during error conditions of the caller */
+    if (r_area == NULL)
+      break;
+    udb_result_finish_result (r, r_area);
+  }
 } /* }}} void udb_query_finish_result */
 
-int udb_query_handle_result (udb_query_t *q, char **column_values) /* {{{ */
+int udb_query_handle_result (const udb_query_t const *q, /* {{{ */
+    udb_query_preparation_area_t *prep_area, char **column_values)
 {
+  udb_result_preparation_area_t *r_area;
   udb_result_t *r;
   int success;
   int status;
 
-  if (q == NULL)
+  if ((q == NULL) || (prep_area == NULL))
     return (-EINVAL);
 
-  if ((q->column_num < 1) || (q->host == NULL) || (q->plugin == NULL)
-      || (q->db_name == NULL))
+  if ((prep_area->column_num < 1) || (prep_area->host == NULL)
+      || (prep_area->plugin == NULL) || (prep_area->db_name == NULL))
   {
     ERROR ("db query utils: Query `%s': Query is not prepared; "
         "can't handle result.", q->name);
@@ -1067,19 +1115,21 @@ int udb_query_handle_result (udb_query_t *q, char **column_values) /* {{{ */
   {
     size_t i;
 
-    for (i = 0; i < q->column_num; i++)
+    for (i = 0; i < prep_area->column_num; i++)
     {
       DEBUG ("db query utils: udb_query_handle_result (%s, %s): "
           "column[%zu] = %s;",
-          q->db_name, q->name, i, column_values[i]);
+          prep_area->db_name, q->name, i, column_values[i]);
     }
   } while (0);
 #endif /* }}} */
 
   success = 0;
-  for (r = q->results; r != NULL; r = r->next)
+  for (r = q->results, r_area = prep_area->result_prep_areas;
+      r != NULL; r = r->next, r_area = r_area->next)
   {
-    status = udb_result_handle_result (r, q, column_values);
+    status = udb_result_handle_result (r, prep_area, r_area,
+        q, column_values);
     if (status == 0)
       success++;
   }
@@ -1087,34 +1137,39 @@ int udb_query_handle_result (udb_query_t *q, char **column_values) /* {{{ */
   if (success == 0)
   {
     ERROR ("db query utils: udb_query_handle_result (%s, %s): "
-        "All results failed.", q->db_name, q->name);
+        "All results failed.", prep_area->db_name, q->name);
     return (-1);
   }
 
   return (0);
 } /* }}} int udb_query_handle_result */
 
-int udb_query_prepare_result (udb_query_t *q, /* {{{ */
+int udb_query_prepare_result (const udb_query_t const *q, /* {{{ */
+    udb_query_preparation_area_t *prep_area,
     const char *host, const char *plugin, const char *db_name,
-    char **column_names, size_t column_num)
+    char **column_names, size_t column_num, int interval)
 {
+  udb_result_preparation_area_t *r_area;
   udb_result_t *r;
   int status;
 
-  if (q == NULL)
+  if ((q == NULL) || (prep_area == NULL))
     return (-EINVAL);
 
-  udb_query_finish_result (q);
+  udb_query_finish_result (q, prep_area);
+
+  prep_area->column_num = column_num;
+  prep_area->host = strdup (host);
+  prep_area->plugin = strdup (plugin);
+  prep_area->db_name = strdup (db_name);
 
-  q->column_num = column_num;
-  q->host = strdup (host);
-  q->plugin = strdup (plugin);
-  q->db_name = strdup (db_name);
+  prep_area->interval = interval;
 
-  if ((q->host == NULL) || (q->plugin == NULL) || (q->db_name == NULL))
+  if ((prep_area->host == NULL) || (prep_area->plugin == NULL)
+      || (prep_area->db_name == NULL))
   {
     ERROR ("db query utils: Query `%s': Prepare failed: Out of memory.", q->name);
-    udb_query_finish_result (q);
+    udb_query_finish_result (q, prep_area);
     return (-ENOMEM);
   }
 
@@ -1132,12 +1187,21 @@ int udb_query_prepare_result (udb_query_t *q, /* {{{ */
   } while (0);
 #endif
 
-  for (r = q->results; r != NULL; r = r->next)
+  for (r = q->results, r_area = prep_area->result_prep_areas;
+      r != NULL; r = r->next, r_area = r_area->next)
   {
-    status = udb_result_prepare_result (r, column_names, column_num);
+    if (! r_area)
+    {
+      ERROR ("db query utils: Query `%s': Invalid number of result "
+          "preparation areas.", q->name);
+      udb_query_finish_result (q, prep_area);
+      return (-EINVAL);
+    }
+
+    status = udb_result_prepare_result (r, r_area, column_names, column_num);
     if (status != 0)
     {
-      udb_query_finish_result (q);
+      udb_query_finish_result (q, prep_area);
       return (status);
     }
   }
@@ -1145,4 +1209,72 @@ int udb_query_prepare_result (udb_query_t *q, /* {{{ */
   return (0);
 } /* }}} int udb_query_prepare_result */
 
+udb_query_preparation_area_t *
+udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */
+{
+  udb_query_preparation_area_t   *q_area;
+  udb_result_preparation_area_t **next_r_area;
+  udb_result_t *r;
+
+  q_area = (udb_query_preparation_area_t *)malloc (sizeof (*q_area));
+  if (q_area == NULL)
+    return NULL;
+
+  memset (q_area, 0, sizeof (*q_area));
+
+  next_r_area = &q_area->result_prep_areas;
+  for (r = q->results; r != NULL; r = r->next)
+  {
+    udb_result_preparation_area_t *r_area;
+
+    r_area = (udb_result_preparation_area_t *)malloc (sizeof (*r_area));
+    if (r_area == NULL)
+    {
+      for (r_area = q_area->result_prep_areas;
+          r_area != NULL; r_area = r_area->next)
+      {
+        free (r_area);
+      }
+      free (q_area);
+      return NULL;
+    }
+
+    memset (r_area, 0, sizeof (*r_area));
+
+    *next_r_area = r_area;
+    next_r_area  = &r_area->next;
+  }
+
+  return (q_area);
+} /* }}} udb_query_preparation_area_t *udb_query_allocate_preparation_area */
+
+void
+udb_query_delete_preparation_area (udb_query_preparation_area_t *q_area) /* {{{ */
+{
+  udb_result_preparation_area_t *r_area;
+
+  if (q_area == NULL)
+    return;
+
+  r_area = q_area->result_prep_areas;
+  while (r_area != NULL)
+  {
+    udb_result_preparation_area_t *area = r_area;
+
+    r_area = r_area->next;
+
+    sfree (area->instances_pos);
+    sfree (area->values_pos);
+    sfree (area->instances_buffer);
+    sfree (area->values_buffer);
+    free (area);
+  }
+
+  sfree (q_area->host);
+  sfree (q_area->plugin);
+  sfree (q_area->db_name);
+
+  free (q_area);
+} /* }}} void udb_query_delete_preparation_area */
+
 /* vim: set sw=2 sts=2 et fdm=marker : */
index 6703e92..fa2b288 100644 (file)
@@ -30,6 +30,9 @@
 struct udb_query_s;
 typedef struct udb_query_s udb_query_t;
 
+struct udb_query_preparation_area_s;
+typedef struct udb_query_preparation_area_s udb_query_preparation_area_t;
+
 typedef int (*udb_query_create_callback_t) (udb_query_t *q,
     oconfig_item_t *ci);
 
@@ -62,11 +65,19 @@ void *udb_query_get_user_data (udb_query_t *q);
  */
 int udb_query_check_version (udb_query_t *q, unsigned int version);
 
-int udb_query_prepare_result (udb_query_t *q,
+int udb_query_prepare_result (const udb_query_t const *q,
+    udb_query_preparation_area_t *prep_area,
     const char *host, const char *plugin, const char *db_name,
-    char **column_names, size_t column_num);
-int udb_query_handle_result (udb_query_t *q, char **column_values);
-void udb_query_finish_result (udb_query_t *q);
+    char **column_names, size_t column_num, int interval);
+int udb_query_handle_result (const udb_query_t const *q,
+    udb_query_preparation_area_t *prep_area, char **column_values);
+void udb_query_finish_result (const udb_query_t const *q,
+    udb_query_preparation_area_t *prep_area);
+
+udb_query_preparation_area_t *
+udb_query_allocate_preparation_area (udb_query_t *q);
+void
+udb_query_delete_preparation_area (udb_query_preparation_area_t *q_area);
 
 #endif /* UTILS_DB_QUERY_H */
 /* vim: set sw=2 sts=2 et : */
index a919316..ac88c0f 100644 (file)
@@ -23,6 +23,7 @@
 #include "plugin.h"
 #include "common.h"
 
+#include "utils_cache.h"
 #include "utils_format_json.h"
 
 static int escape_string (char *buffer, size_t buffer_size, /* {{{ */
@@ -72,10 +73,11 @@ static int escape_string (char *buffer, size_t buffer_size, /* {{{ */
 } /* }}} int buffer_add_string */
 
 static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
-                const data_set_t *ds, const value_list_t *vl)
+                const data_set_t *ds, const value_list_t *vl, int store_rates)
 {
   size_t offset = 0;
   int i;
+  gauge_t *rates = NULL;
 
   memset (buffer, 0, buffer_size);
 
@@ -84,11 +86,17 @@ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
   status = ssnprintf (buffer + offset, buffer_size - offset, \
       __VA_ARGS__); \
   if (status < 1) \
+  { \
+    sfree(rates); \
     return (-1); \
+  } \
   else if (((size_t) status) >= (buffer_size - offset)) \
+  { \
+    sfree(rates); \
     return (-ENOMEM); \
+  } \
   else \
-  offset += ((size_t) status); \
+    offset += ((size_t) status); \
 } while (0)
 
   BUFFER_ADD ("[");
@@ -98,7 +106,28 @@ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
       BUFFER_ADD (",");
 
     if (ds->ds[i].type == DS_TYPE_GAUGE)
-      BUFFER_ADD ("%g", vl->values[i].gauge);
+    {
+      if(isfinite (vl->values[i].gauge))
+        BUFFER_ADD ("%g", vl->values[i].gauge);
+      else
+        BUFFER_ADD ("null");
+    }
+    else if (store_rates)
+    {
+      if (rates == NULL)
+        rates = uc_get_rate (ds, vl);
+      if (rates == NULL)
+      {
+        WARNING ("utils_format_json: uc_get_rate failed.");
+        sfree(rates);
+        return (-1);
+      }
+
+      if(isfinite (rates[i]))
+        BUFFER_ADD ("%g", rates[i]);
+      else
+        BUFFER_ADD ("null");
+    }
     else if (ds->ds[i].type == DS_TYPE_COUNTER)
       BUFFER_ADD ("%llu", vl->values[i].counter);
     else if (ds->ds[i].type == DS_TYPE_DERIVE)
@@ -109,6 +138,7 @@ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
     {
       ERROR ("format_json: Unknown data source type: %i",
           ds->ds[i].type);
+      sfree (rates);
       return (-1);
     }
   } /* for ds->ds_num */
@@ -117,13 +147,87 @@ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */
 #undef BUFFER_ADD
 
   DEBUG ("format_json: values_to_json: buffer = %s;", buffer);
-
+  sfree(rates);
   return (0);
 } /* }}} int values_to_json */
 
-static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
+static int dstypes_to_json (char *buffer, size_t buffer_size, /* {{{ */
+                const data_set_t *ds, const value_list_t *vl)
+{
+  size_t offset = 0;
+  int i;
+
+  memset (buffer, 0, buffer_size);
+
+#define BUFFER_ADD(...) do { \
+  int status; \
+  status = ssnprintf (buffer + offset, buffer_size - offset, \
+      __VA_ARGS__); \
+  if (status < 1) \
+    return (-1); \
+  else if (((size_t) status) >= (buffer_size - offset)) \
+    return (-ENOMEM); \
+  else \
+    offset += ((size_t) status); \
+} while (0)
+
+  BUFFER_ADD ("[");
+  for (i = 0; i < ds->ds_num; i++)
+  {
+    if (i > 0)
+      BUFFER_ADD (",");
+
+    BUFFER_ADD ("\"%s\"", DS_TYPE_TO_STRING (ds->ds[i].type));
+  } /* for ds->ds_num */
+  BUFFER_ADD ("]");
+
+#undef BUFFER_ADD
+
+  DEBUG ("format_json: dstypes_to_json: buffer = %s;", buffer);
+
+  return (0);
+} /* }}} int dstypes_to_json */
+
+static int dsnames_to_json (char *buffer, size_t buffer_size, /* {{{ */
                 const data_set_t *ds, const value_list_t *vl)
 {
+  size_t offset = 0;
+  int i;
+
+  memset (buffer, 0, buffer_size);
+
+#define BUFFER_ADD(...) do { \
+  int status; \
+  status = ssnprintf (buffer + offset, buffer_size - offset, \
+      __VA_ARGS__); \
+  if (status < 1) \
+    return (-1); \
+  else if (((size_t) status) >= (buffer_size - offset)) \
+    return (-ENOMEM); \
+  else \
+    offset += ((size_t) status); \
+} while (0)
+
+  BUFFER_ADD ("[");
+  for (i = 0; i < ds->ds_num; i++)
+  {
+    if (i > 0)
+      BUFFER_ADD (",");
+
+    BUFFER_ADD ("\"%s\"", ds->ds[i].name);
+  } /* for ds->ds_num */
+  BUFFER_ADD ("]");
+
+#undef BUFFER_ADD
+
+  DEBUG ("format_json: dsnames_to_json: buffer = %s;", buffer);
+
+  return (0);
+} /* }}} int dsnames_to_json */
+
+static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
+                const data_set_t *ds, const value_list_t *vl, int store_rates)
+{
   char temp[512];
   size_t offset = 0;
   int status;
@@ -145,11 +249,21 @@ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
    * a square bracket in `format_json_finalize'. */
   BUFFER_ADD (",{");
 
-  status = values_to_json (temp, sizeof (temp), ds, vl);
+  status = values_to_json (temp, sizeof (temp), ds, vl, store_rates);
   if (status != 0)
     return (status);
   BUFFER_ADD ("\"values\":%s", temp);
 
+  status = dstypes_to_json (temp, sizeof (temp), ds, vl);
+  if (status != 0)
+    return (status);
+  BUFFER_ADD (",\"dstypes\":%s", temp);
+
+  status = dsnames_to_json (temp, sizeof (temp), ds, vl);
+  if (status != 0)
+    return (status);
+  BUFFER_ADD (",\"dsnames\":%s", temp);
+
   BUFFER_ADD (",\"time\":%lu", (unsigned long) vl->time);
   BUFFER_ADD (",\"interval\":%i", vl->interval);
 
@@ -179,12 +293,12 @@ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */
 static int format_json_value_list_nocheck (char *buffer, /* {{{ */
     size_t *ret_buffer_fill, size_t *ret_buffer_free,
     const data_set_t *ds, const value_list_t *vl,
-    size_t temp_size)
+    int store_rates, size_t temp_size)
 {
   char temp[temp_size];
   int status;
 
-  status = value_list_to_json (temp, sizeof (temp), ds, vl);
+  status = value_list_to_json (temp, sizeof (temp), ds, vl, store_rates);
   if (status != 0)
     return (status);
   temp_size = strlen (temp);
@@ -250,7 +364,7 @@ int format_json_finalize (char *buffer, /* {{{ */
 
 int format_json_value_list (char *buffer, /* {{{ */
     size_t *ret_buffer_fill, size_t *ret_buffer_free,
-    const data_set_t *ds, const value_list_t *vl)
+    const data_set_t *ds, const value_list_t *vl, int store_rates)
 {
   if ((buffer == NULL)
       || (ret_buffer_fill == NULL) || (ret_buffer_free == NULL)
@@ -262,7 +376,7 @@ int format_json_value_list (char *buffer, /* {{{ */
 
   return (format_json_value_list_nocheck (buffer,
         ret_buffer_fill, ret_buffer_free, ds, vl,
-        (*ret_buffer_free) - 2));
+        store_rates, (*ret_buffer_free) - 2));
 } /* }}} int format_json_value_list */
 
 /* vim: set sw=2 sts=2 et fdm=marker : */
index 880a79d..c902e27 100644 (file)
@@ -29,7 +29,7 @@ int format_json_initialize (char *buffer,
     size_t *ret_buffer_fill, size_t *ret_buffer_free);
 int format_json_value_list (char *buffer,
     size_t *ret_buffer_fill, size_t *ret_buffer_free,
-    const data_set_t *ds, const value_list_t *vl);
+    const data_set_t *ds, const value_list_t *vl, int store_rates);
 int format_json_finalize (char *buffer,
     size_t *ret_buffer_fill, size_t *ret_buffer_free);
 
index 1ecd07e..086649a 100644 (file)
@@ -172,7 +172,7 @@ int c_heap_insert (c_heap_t *h, void *ptr)
   return (0);
 } /* int c_heap_insert */
 
-void *c_head_get_root (c_heap_t *h)
+void *c_heap_get_root (c_heap_t *h)
 {
   void *ret = NULL;
 
@@ -218,6 +218,6 @@ void *c_head_get_root (c_heap_t *h)
   pthread_mutex_unlock (&h->lock);
 
   return (ret);
-} /* void *c_head_get_root */
+} /* void *c_heap_get_root */
 
 /* vim: set sw=2 sts=2 et fdm=marker : */
index dd0f486..6428006 100644 (file)
@@ -78,7 +78,7 @@ int c_heap_insert (c_heap_t *h, void *ptr);
 
 /*
  * NAME
- *   c_head_get_root
+ *   c_heap_get_root
  *
  * DESCRIPTION
  *   Removes the value at the root of the heap and returns both, key and value.
@@ -90,7 +90,7 @@ int c_heap_insert (c_heap_t *h, void *ptr);
  *   The pointer passed to `c_heap_insert' or NULL if there are no more
  *   elements in the heap (or an error occurred).
  */
-void *c_head_get_root (c_heap_t *h);
+void *c_heap_get_root (c_heap_t *h);
 
 #endif /* UTILS_HEAP_H */
 /* vim: set sw=2 sts=2 et : */
index bf5f7e4..6a0c6f0 100644 (file)
@@ -139,17 +139,36 @@ int llist_size (llist_t *l)
        return (l ? l->size : 0);
 }
 
+static int llist_strcmp (llentry_t *e, void *ud)
+{
+       if ((e == NULL) || (ud == NULL))
+               return (-1);
+       return (strcmp (e->key, (const char *)ud));
+}
+
 llentry_t *llist_search (llist_t *l, const char *key)
 {
+       return (llist_search_custom (l, llist_strcmp, (void *)key));
+}
+
+llentry_t *llist_search_custom (llist_t *l,
+               int (*compare) (llentry_t *, void *), void *user_data)
+{
        llentry_t *e;
 
        if (l == NULL)
                return (NULL);
 
-       for (e = l->head; e != NULL; e = e->next)
-               if (strcmp (key, e->key) == 0)
+       e = l->head;
+       while (e != NULL) {
+               llentry_t *next = e->next;
+
+               if (compare (e, user_data) == 0)
                        break;
 
+               e = next;
+       }
+
        return (e);
 }
 
index c3753d8..19d8d94 100644 (file)
@@ -54,6 +54,8 @@ void llist_remove (llist_t *l, llentry_t *e);
 int llist_size (llist_t *l);
 
 llentry_t *llist_search (llist_t *l, const char *key);
+llentry_t *llist_search_custom (llist_t *l,
+               int (*compare) (llentry_t *, void *), void *user_data);
 
 llentry_t *llist_head (llist_t *l);
 llentry_t *llist_tail (llist_t *l);
index bdbad3f..0f87bc0 100644 (file)
 #include <regex.h>
 
 #define UTILS_MATCH_FLAGS_FREE_USER_DATA 0x01
+#define UTILS_MATCH_FLAGS_EXCLUDE_REGEX 0x02
 
 struct cu_match_s
 {
   regex_t regex;
+  regex_t excluderegex;
   int flags;
 
   int (*callback) (const char *str, char * const *matches, size_t matches_num,
@@ -210,7 +212,7 @@ static int default_callback (const char __attribute__((unused)) *str,
 /*
  * Public functions
  */
-cu_match_t *match_create_callback (const char *regex,
+cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
                int (*callback) (const char *str,
                  char * const *matches, size_t matches_num, void *user_data),
                void *user_data)
@@ -218,7 +220,8 @@ cu_match_t *match_create_callback (const char *regex,
   cu_match_t *obj;
   int status;
 
-  DEBUG ("utils_match: match_create_callback: regex = %s", regex);
+  DEBUG ("utils_match: match_create_callback: regex = %s, excluderegex = %s",
+        regex, excluderegex);
 
   obj = (cu_match_t *) malloc (sizeof (cu_match_t));
   if (obj == NULL)
@@ -233,13 +236,26 @@ cu_match_t *match_create_callback (const char *regex,
     return (NULL);
   }
 
+  if (excluderegex && strcmp(excluderegex, "") != 0) {
+    status = regcomp (&obj->excluderegex, excluderegex, REG_EXTENDED);
+    if (status != 0)
+    {
+       ERROR ("Compiling the excluding regular expression \"%s\" failed.",
+              excluderegex);
+       sfree (obj);
+       return (NULL);
+    }
+    obj->flags |= UTILS_MATCH_FLAGS_EXCLUDE_REGEX;
+  }
+
   obj->callback = callback;
   obj->user_data = user_data;
 
   return (obj);
 } /* cu_match_t *match_create_callback */
 
-cu_match_t *match_create_simple (const char *regex, int match_ds_type)
+cu_match_t *match_create_simple (const char *regex,
+                                const char *excluderegex, int match_ds_type)
 {
   cu_match_value_t *user_data;
   cu_match_t *obj;
@@ -250,7 +266,8 @@ cu_match_t *match_create_simple (const char *regex, int match_ds_type)
   memset (user_data, '\0', sizeof (cu_match_value_t));
   user_data->ds_type = match_ds_type;
 
-  obj = match_create_callback (regex, default_callback, user_data);
+  obj = match_create_callback (regex, excluderegex,
+                              default_callback, user_data);
   if (obj == NULL)
   {
     sfree (user_data);
@@ -286,6 +303,17 @@ int match_apply (cu_match_t *obj, const char *str)
   if ((obj == NULL) || (str == NULL))
     return (-1);
 
+  if (obj->flags & UTILS_MATCH_FLAGS_EXCLUDE_REGEX) {
+    status = regexec (&obj->excluderegex, str,
+                     STATIC_ARRAY_SIZE (re_match), re_match,
+                     /* eflags = */ 0);
+    /* Regex did match, so exclude this line */
+    if (status == 0) {
+      DEBUG("ExludeRegex matched, don't count that line\n");
+      return (0);
+    }
+  }
+
   status = regexec (&obj->regex, str,
       STATIC_ARRAY_SIZE (re_match), re_match,
       /* eflags = */ 0);
index 5a0c033..9e47d5c 100644 (file)
@@ -82,8 +82,10 @@ typedef struct cu_match_value_s cu_match_value_t;
  *  then only the submatch (the part in the parenthesis) will be passed to the
  *  callback. If there is no submatch, then the entire string is passed to the
  *  callback.
+ *  The optional `excluderegex' allows to exclude the line from the match, if
+ *  the excluderegex matches.
  */
-cu_match_t *match_create_callback (const char *regex,
+cu_match_t *match_create_callback (const char *regex, const char *excluderegex,
                int (*callback) (const char *str,
                  char * const *matches, size_t matches_num, void *user_data),
                void *user_data);
@@ -112,7 +114,8 @@ cu_match_t *match_create_callback (const char *regex,
  *    The function will not search for anything in the string and increase
  *    value.counter by one.
  */
-cu_match_t *match_create_simple (const char *regex, int ds_type);
+cu_match_t *match_create_simple (const char *regex,
+                                const char *excluderegex, int ds_type);
 
 /*
  * NAME
index 22c7917..8ae2208 100644 (file)
@@ -191,7 +191,7 @@ int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
 } /* int tail_match_add_match */
 
 int tail_match_add_match_simple (cu_tail_match_t *obj,
-    const char *regex, int ds_type,
+    const char *regex, const char *excluderegex, int ds_type,
     const char *plugin, const char *plugin_instance,
     const char *type, const char *type_instance)
 {
@@ -199,7 +199,7 @@ int tail_match_add_match_simple (cu_tail_match_t *obj,
   cu_tail_match_simple_t *user_data;
   int status;
 
-  match = match_create_simple (regex, ds_type);
+  match = match_create_simple (regex, excluderegex, ds_type);
   if (match == NULL)
     return (-1);
 
index d08c728..7659745 100644 (file)
@@ -97,12 +97,13 @@ int tail_match_add_match (cu_tail_match_t *obj, cu_match_t *match,
  *  The values gathered are dispatched by the tail_match module in this case. The
  *  passed `plugin', `plugin_instance', `type', and `type_instance' are
  *  directly used when submitting these values.
+ *  With excluderegex it is possible to exlude lines from the match.
  *
  * RETURN VALUE
  *   Zero upon success, non-zero otherwise.
  */
 int tail_match_add_match_simple (cu_tail_match_t *obj,
-    const char *regex, int ds_type,
+    const char *regex, const char *excluderegex, int ds_type,
     const char *plugin, const char *plugin_instance,
     const char *type, const char *type_instance);
 
index f14636b..ab8757e 100644 (file)
@@ -49,6 +49,7 @@ struct wh_callback_s
         int   verify_peer;
         int   verify_host;
         char *cacert;
+        int   store_rates;
 
 #define WH_FORMAT_COMMAND 0
 #define WH_FORMAT_JSON    1
@@ -142,7 +143,7 @@ static int wh_callback_init (wh_callback_t *cb) /* {{{ */
                 ssnprintf (cb->credentials, credentials_size, "%s:%s",
                                 cb->user, (cb->pass == NULL) ? "" : cb->pass);
                 curl_easy_setopt (cb->curl, CURLOPT_USERPWD, cb->credentials);
-                curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
+                curl_easy_setopt (cb->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
         }
 
         curl_easy_setopt (cb->curl, CURLOPT_SSL_VERIFYPEER, cb->verify_peer);
@@ -271,11 +272,13 @@ static void wh_callback_free (void *data) /* {{{ */
 
 static int wh_value_list_to_string (char *buffer, /* {{{ */
                 size_t buffer_size,
-                const data_set_t *ds, const value_list_t *vl)
+                const data_set_t *ds, const value_list_t *vl,
+                wh_callback_t *cb)
 {
         size_t offset = 0;
         int status;
         int i;
+        gauge_t *rates = NULL;
 
         assert (0 == strcmp (ds->type, vl->type));
 
@@ -285,9 +288,15 @@ static int wh_value_list_to_string (char *buffer, /* {{{ */
         status = ssnprintf (buffer + offset, buffer_size - offset, \
                         __VA_ARGS__); \
         if (status < 1) \
+        { \
+                sfree (rates); \
                 return (-1); \
+        } \
         else if (((size_t) status) >= (buffer_size - offset)) \
+        { \
+                sfree (rates); \
                 return (-1); \
+        } \
         else \
                 offset += ((size_t) status); \
 } while (0)
@@ -295,26 +304,40 @@ static int wh_value_list_to_string (char *buffer, /* {{{ */
         BUFFER_ADD ("%lu", (unsigned long) vl->time);
 
         for (i = 0; i < ds->ds_num; i++)
-{
-        if (ds->ds[i].type == DS_TYPE_GAUGE)
-                BUFFER_ADD (":%f", vl->values[i].gauge);
-        else if (ds->ds[i].type == DS_TYPE_COUNTER)
-                BUFFER_ADD (":%llu", vl->values[i].counter);
-        else if (ds->ds[i].type == DS_TYPE_DERIVE)
-                BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
-        else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
-                BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
-        else
         {
-                ERROR ("write_http plugin: Unknown data source type: %i",
-                                ds->ds[i].type);
-                return (-1);
-        }
-} /* for ds->ds_num */
+                if (ds->ds[i].type == DS_TYPE_GAUGE)
+                        BUFFER_ADD (":%f", vl->values[i].gauge);
+                else if (cb->store_rates)
+                {
+                        if (rates == NULL)
+                                rates = uc_get_rate (ds, vl);
+                        if (rates == NULL)
+                        {
+                                WARNING ("write_http plugin: "
+                                                "uc_get_rate failed.");
+                                return (-1);
+                        }
+                        BUFFER_ADD (":%g", rates[i]);
+                }
+                else if (ds->ds[i].type == DS_TYPE_COUNTER)
+                        BUFFER_ADD (":%llu", vl->values[i].counter);
+                else if (ds->ds[i].type == DS_TYPE_DERIVE)
+                        BUFFER_ADD (":%"PRIi64, vl->values[i].derive);
+                else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
+                        BUFFER_ADD (":%"PRIu64, vl->values[i].absolute);
+                else
+                {
+                        ERROR ("write_http plugin: Unknown data source type: %i",
+                                        ds->ds[i].type);
+                        sfree (rates);
+                        return (-1);
+                }
+        } /* for ds->ds_num */
 
 #undef BUFFER_ADD
 
-return (0);
+        sfree (rates);
+        return (0);
 } /* }}} int wh_value_list_to_string */
 
 static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{{ */
@@ -343,7 +366,7 @@ static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{
 
         /* Convert the values to an ASCII representation and put that into
          * `values'. */
-        status = wh_value_list_to_string (values, sizeof (values), ds, vl);
+        status = wh_value_list_to_string (values, sizeof (values), ds, vl, cb);
         if (status != 0) {
                 ERROR ("write_http plugin: error with "
                                 "wh_value_list_to_string");
@@ -423,7 +446,7 @@ static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ *
         status = format_json_value_list (cb->send_buffer,
                         &cb->send_buffer_fill,
                         &cb->send_buffer_free,
-                        ds, vl);
+                        ds, vl, cb->store_rates);
         if (status == (-ENOMEM))
         {
                 status = wh_flush_nolock (/* timeout = */ -1, cb);
@@ -437,7 +460,7 @@ static int wh_write_json (const data_set_t *ds, const value_list_t *vl, /* {{{ *
                 status = format_json_value_list (cb->send_buffer,
                                 &cb->send_buffer_fill,
                                 &cb->send_buffer_free,
-                                ds, vl);
+                                ds, vl, cb->store_rates);
         }
         if (status != 0)
         {
@@ -589,6 +612,8 @@ static int wh_config_url (oconfig_item_t *ci) /* {{{ */
                         config_set_string (&cb->cacert, child);
                 else if (strcasecmp ("Format", child->key) == 0)
                         config_set_format (cb, child);
+                else if (strcasecmp ("StoreRates", child->key) == 0)
+                        config_set_boolean (&cb->store_rates, child);
                 else
                 {
                         ERROR ("write_http plugin: Invalid configuration "
index e0114d0..2a93ab7 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-DEFAULT_VERSION="4.8.1.git"
+DEFAULT_VERSION="4.9.2.git"
 
 VERSION="`git describe 2> /dev/null | sed -e 's/^collectd-//'`"
 
@@ -10,4 +10,8 @@ fi
 
 VERSION="`echo \"$VERSION\" | sed -e 's/-/./g'`"
 
-echo -n "$VERSION"
+if test "x`uname -s`" = "xAIX" ; then 
+       echo "$VERSION\c"
+else 
+       echo -n "$VERSION"
+fi