Merge branch 'collectd-4.4'
authorFlorian Forster <octo@noris.net>
Mon, 14 Jul 2008 07:46:27 +0000 (09:46 +0200)
committerFlorian Forster <octo@noris.net>
Mon, 14 Jul 2008 07:46:27 +0000 (09:46 +0200)
Conflicts:

configure.in

87 files changed:
README
bindings/perl/Collectd/Unixsock.pm
configure.in
contrib/collection.cgi
contrib/cussh.pl
src/Makefile.am
src/apache.c
src/apcups.c
src/apple_sensors.c
src/ascent.c
src/battery.c
src/collectd-perl.pod
src/collectd-unixsock.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/cpu.c
src/cpufreq.c
src/csv.c
src/df.c
src/disk.c
src/dns.c
src/email.c
src/entropy.c
src/exec.c
src/hddtemp.c
src/interface.c
src/ipmi.c
src/iptables.c
src/ipvs.c
src/irq.c
src/libvirt.c
src/load.c
src/logfile.c
src/mbmon.c
src/memcached.c
src/memory.c
src/multimeter.c
src/mysql.c
src/netlink.c
src/network.c
src/nfs.c
src/nginx.c
src/notify_desktop.c [new file with mode: 0644]
src/notify_email.c [new file with mode: 0644]
src/ntpd.c
src/nut.c
src/perl.c
src/ping.c
src/plugin.c
src/plugin.h
src/postgresql.c [new file with mode: 0644]
src/powerdns.c
src/processes.c
src/qmail.c [new file with mode: 0644]
src/rrdtool.c
src/sensors.c
src/serial.c
src/snmp.c
src/swap.c
src/tape.c
src/tcpconns.c
src/teamspeak2.c
src/thermal.c [new file with mode: 0644]
src/types.db
src/types_list.c
src/unixsock.c
src/users.c
src/utils_cache.c
src/utils_cmd_flush.c
src/utils_cmd_putval.c
src/utils_complain.c [new file with mode: 0644]
src/utils_complain.h [new file with mode: 0644]
src/utils_dns.c
src/utils_ignorelist.c
src/utils_mount.c
src/utils_tail_match.c
src/utils_threshold.c
src/uuid.c
src/vmem.c
src/vserver.c
src/wireless.c
src/xmms.c

diff --git a/README b/README
index 904e7c7..97119ac 100644 (file)
--- a/README
+++ b/README
@@ -141,6 +141,10 @@ Features
       Network latency: Time to reach the default gateway or another given
       host.
 
+    - postgresql
+      PostgreSQL database statistics: active server connections, transaction
+      numbers, block IO, table row manipulations.
+
     - processes
       Process counts: Number of running, sleeping, zombie, ... processes.
 
@@ -230,6 +234,16 @@ Features
 
   * Notifications can be handled by the following plugins:
 
+    - notify_desktop
+      Send a desktop notification to a notification daemon, as defined in
+      the Desktop Notification Specification. To actually display the
+      notifications, notification-daemon is required.
+      See http://www.galago-project.org/specs/notification/.
+
+    - notify_email
+      Send an E-mail with the notification message to the configured
+      recipients.
+
     - exec
       Execute a program or script to handle the notification.
       See collectd-exec(5).
@@ -315,9 +329,16 @@ Prerequisites
     used and should be found in various implementations for hopefully all
     platforms.
 
+  * CoreFoundation.framework and IOKit.framework (optional)
+    For compiling on Darwin in general and the `apple_sensors' plugin in
+    particular.
+
   * libcurl (optional)
     If you want to use the `apache', `ascent', or `nginx' plugin.
 
+  * libesmtp (optional)
+    For the `notify_email' plugin.
+
   * libhal (optional)
     If present, the uuid plugin will check for UUID from HAL.
 
@@ -333,6 +354,9 @@ Prerequisites
   * libnetsnmp (optional)
     For the `snmp' plugin.
 
+  * libnotify (optional)
+    For the `notify_desktop' plugin.
+
   * liboping (optional, if not found a version shipped with this distribution
     can be used)
     Used by the `ping' plugin to send and receive ICMP packets.
@@ -344,38 +368,37 @@ Prerequisites
     Obviously used by the `perl' plugin. The library has to be compiled with
     ithread support (introduced in Perl 5.6.0).
 
+  * libpq (optional)
+    The PostgreSQL C client library used by the `postgresql' plugin.
+
   * librrd (optional; headers and library; rrdtool 1.0 and 1.2 both work fine)
     If built without `librrd' the resulting binary will be `client only', i.e.
     will send its values via multicast and not create any RRD files itself.
     Alternatively you can chose to write CSV-files (Comma Separated Values)
     instead.
 
+  * librt, libsocket, libkstat, libdevinfo (optional)
+    Various standard Solaris libraries which provide system functions.
+
   * libsensors (optional)
     To read from `lm_sensors', see the `sensors' plugin.
 
-  * libstatgrab may be used to collect statistics on systems other than Linux
-    and/or Solaris. Note that CPU- and disk-statistics, while being provided
-    by this library, are not supported in collectd right now..
+  * libstatgrab (optional) may be used to collect statistics on systems other
+    than Linux and/or Solaris. Note that CPU- and disk-statistics, while being
+    provided by this library, are not supported in collectd right now..
     <http://www.i-scream.org/libstatgrab/> 
 
   * libupsclient/nut (optional)
     For the `nut' plugin which queries nut's `upsd'.
 
-  * libxmms (optional)
-
-  * librt, libsocket, libkstat, libdevinfo
-    Various standard Solaris libraries which provide system functions.
-
-  * CoreFoundation.framework and IOKit.framework
-    For compiling on Darwin in general and the `apple_sensors' plugin in
-    particular.
-
   * libvirt (optional)
     Collect statistics from virtual machines.
 
   * libxml2 (optional)
     Parse XML data. This is needed for the `ascent' and `libvirt' plugins.
 
+  * libxmms (optional)
+
 
 Configuring / Compiling / Installing
 ------------------------------------
index f21ebfe..af274a5 100644 (file)
@@ -71,7 +71,7 @@ sub _create_socket
        return ($sock);
 } # _create_socket
 
-=head1 VALUE IDENTIFIER
+=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
@@ -219,8 +219,8 @@ sub getval
 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 IDENTIFIER>), though this
-is checked by the daemon, not the Perl module. Also, gauge data-sources
+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.
 
@@ -400,7 +400,7 @@ sub putnotif
        return;
 } # putnotif
 
-=item I<$obj>-E<gt>B<flush> (B<timeout> =E<gt> I<$timeout>, B<plugins> =E<gt> [...]);
+=item I<$obj>-E<gt>B<flush> (B<timeout> =E<gt> I<$timeout>, B<plugins> =E<gt> [...], B<identifier>  =E<gt> [...]);
 
 Flush cached data.
 
@@ -415,7 +415,14 @@ flushed.
 
 =item B<plugins>
 
-If this option is specified, only the selected plugins will be flushed. 
+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
 
@@ -431,7 +438,7 @@ sub flush
        my $status = 0;
        my $msg    = "FLUSH";
 
-       if ($args{'timeout'})
+       if (defined ($args{'timeout'}))
        {
                $msg .= " timeout=" . $args{'timeout'};
        }
@@ -444,6 +451,31 @@ sub flush
                }
        }
 
+       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=$ident_str";
+               }
+       }
+
        $msg .= "\n";
 
        send ($fh, $msg, 0) or confess ("send: $!");
index 41ee955..2be53f6 100644 (file)
@@ -57,7 +57,7 @@ AC_HEADER_STDC
 AC_HEADER_SYS_WAIT
 AC_HEADER_DIRENT
 
-AC_CHECK_HEADERS(stdint.h 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)
+AC_CHECK_HEADERS(stdio.h stdint.h stdbool.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, [], [],
@@ -1488,6 +1488,44 @@ AC_DEFINE_UNQUOTED(COLLECT_LIBPCAP, [$collect_libpcap],
        [Wether or not to use the pcap library])
 AM_CONDITIONAL(BUILD_WITH_LIBPCAP, test "x$with_libpcap" = "xyes")
 
+AC_ARG_WITH(libesmtp, [AS_HELP_STRING([--with-libesmtp@<:@=PREFIX@:>@], [Path to libesmtp.])],
+[
+       if test "x$withval" != "xno" && test "x$withval" != "xyes"
+       then
+               LDFLAGS="$LDFLAGS -L$withval/lib"
+               CPPFLAGS="$CPPFLAGS -I$withval/include -D_THREAD_SAFE"
+               with_libesmtp="yes"
+       else
+               with_libesmtp="$withval"
+       fi
+],
+[
+       with_libesmtp="yes"
+])
+if test "x$with_libesmtp" = "xyes"
+then
+       AC_CHECK_LIB(esmtp, smtp_create_session,
+       [
+               AC_DEFINE(HAVE_LIBESMTP, 1, [Define to 1 if you have the esmtp library (-lesmtp).])
+       ], [with_libesmtp="no (libesmtp not found)"])
+fi
+if test "x$with_libesmtp" = "xyes"
+then
+       AC_CHECK_HEADERS(libesmtp.h,
+       [
+               AC_DEFINE(HAVE_LIBESMTP_H, 1, [Define to 1 if you have the <libesmtp.h> header file.])
+       ], [with_libesmtp="no (libesmtp.h not found)"])
+fi
+if test "x$with_libesmtp" = "xyes"
+then
+       collect_libesmtp=1
+else
+       collect_libesmtp=0
+fi
+AC_DEFINE_UNQUOTED(COLLECT_LIBESMTP, [$collect_libesmtp],
+       [Wether or not to use the esmtp library])
+AM_CONDITIONAL(BUILD_WITH_LIBESMTP, test "x$with_libesmtp" = "xyes")
+
 perl_interpreter="perl"
 AC_ARG_WITH(libperl, [AS_HELP_STRING([--with-libperl@<:@=PREFIX@:>@], [Path to libperl.])],
 [
@@ -1737,6 +1775,10 @@ then
 fi
 AM_CONDITIONAL(BUILD_WITH_LIBNETSNMP, test "x$with_libnetsnmp" = "xyes")
 
+PKG_CHECK_MODULES([LIBNOTIFY], [libnotify],
+               [with_libnotify="yes"],
+               [with_libnotify="no ($LIBNOTIFY_PKG_ERRORS)"])
+
 with_libupsclient="no (pkg-config isn't available)"
 with_libupsclient_cflags=""
 with_libupsclient_libs=""
@@ -2060,6 +2102,93 @@ then
        AC_SUBST(BUILD_WITH_OPENIPMI_LIBS)
 fi
 
+dnl Check for libpq.
+with_pg_config="pg_config"
+with_libpq_includedir=""
+with_libpq_libdir=""
+with_libpq_cppflags=""
+with_libpq_ldflags=""
+AC_ARG_WITH(libpq, [AS_HELP_STRING([--with-libpq@<:@=PREFIX@:>@],
+       [Path to libpq.])],
+[
+       if test "x$withval" = "xno"
+       then
+               with_libpq="no"
+       else if test "x$withval" = "xyes"
+       then
+               with_libpq="yes"
+       else
+               if test -f "$withval" && test -x "$withval";
+               then
+                       with_pg_config="$withval"
+               else if test -x "$withval/bin/pg_config"
+               then
+                       with_pg_config="$withval/bin/pg_config"
+               fi; fi
+               with_libpq="yes"
+       fi; fi
+],
+[
+       with_libpq="yes"
+])
+if test "x$with_libpq" = "xyes"
+then
+       with_libpq_includedir=`$with_pg_config --includedir 2> /dev/null`
+       pg_config_status=$?
+
+       if test $pg_config_status -eq 0
+       then
+               if test -n "$with_libpq_includedir"; then
+                       for dir in $with_libpq_includedir; do
+                               with_libpq_cppflags="$with_libpq_cppflags -I$dir"
+                       done
+               fi
+       else
+               AC_MSG_WARN([$with_pg_config returned with status $pg_config_status])
+       fi
+
+       SAVE_CPPFLAGS="$CPPFLAGS"
+       CPPFLAGS="$CPPFLAGS $with_libpq_cppflags"
+
+       AC_CHECK_HEADERS(libpq-fe.h, [],
+               [with_libpq="no (libpq-fe.h not found)"], [])
+
+       CPPFLAGS="$SAVE_CPPFLAGS"
+fi
+if test "x$with_libpq" = "xyes"
+then
+       with_libpq_libdir=`$with_pg_config --libdir 2> /dev/null`
+       pg_config_status=$?
+
+       if test $pg_config_status -eq 0
+       then
+               if test -n "$with_libpq_libdir"; then
+                       for dir in $with_libpq_libdir; do
+                               with_libpq_ldflags="$with_libpq_ldflags -L$dir"
+                       done
+               fi
+       else
+               AC_MSG_WARN([$with_pg_config returned with status $pg_config_status])
+       fi
+
+       SAVE_LDFLAGS="$LDFLAGS"
+       LDFLAGS="$LDFLAGS $with_libpq_ldflags"
+
+       AC_CHECK_LIB(pq, PQconnectdb,
+               [with_libpq="yes"],
+               [with_libpq="no (symbol 'PQconnectdb' not found)"])
+
+       LDFLAGS="$SAVE_LDFLAGS"
+fi
+if test "x$with_libpq" = "xyes"
+then
+       BUILD_WITH_LIBPQ_CPPFLAGS="$with_libpq_cppflags"
+       BUILD_WITH_LIBPQ_LDFLAGS="$with_libpq_ldflags"
+       AC_SUBST(BUILD_WITH_LIBPQ_CPPFLAGS)
+       AC_SUBST(BUILD_WITH_LIBPQ_LDFLAGS)
+fi
+AM_CONDITIONAL(BUILD_WITH_LIBPQ, test "x$with_libpq" = "xyes")
+
 dnl Check for libvirt and libxml2 libraries.
 with_libxml2="no (pkg-config isn't available)"
 with_libxml2_cflags=""
@@ -2303,6 +2432,7 @@ plugin_serial="no"
 plugin_swap="no"
 plugin_tape="no"
 plugin_tcpconns="no"
+plugin_thermal="no"
 plugin_users="no"
 plugin_vmem="no"
 plugin_vserver="no"
@@ -2325,6 +2455,7 @@ then
        plugin_serial="yes"
        plugin_swap="yes"
        plugin_tcpconns="yes"
+       plugin_thermal="yes"
        plugin_vmem="yes"
        plugin_vserver="yes"
        plugin_wireless="yes"
@@ -2433,6 +2564,7 @@ fi
 
 if test "x$with_libkvm" = "xyes"
 then
+       plugin_processes="yes"
        plugin_swap="yes"
 fi
 
@@ -2458,6 +2590,7 @@ AC_PLUGIN([battery],     [$plugin_battery],    [Battery statistics])
 AC_PLUGIN([cpu],         [$plugin_cpu],        [CPU usage statistics])
 AC_PLUGIN([cpufreq],     [$plugin_cpufreq],    [CPU frequency statistics])
 AC_PLUGIN([csv],         [yes],                [CSV output plugin])
+AC_PLUGIN([notify_desktop], [$with_libnotify], [Desktop notifications])
 AC_PLUGIN([df],          [$plugin_df],         [Filesystem usage statistics])
 AC_PLUGIN([disk],        [$plugin_disk],       [Disk usage statistics])
 AC_PLUGIN([dns],         [$with_libpcap],      [DNS traffic analysis])
@@ -2482,12 +2615,15 @@ AC_PLUGIN([netlink],     [$with_libnetlink],   [Enhanced Linux network statistic
 AC_PLUGIN([network],     [yes],                [Network communication plugin])
 AC_PLUGIN([nfs],         [$plugin_nfs],        [NFS statistics])
 AC_PLUGIN([nginx],       [$with_libcurl],      [nginx statistics])
+AC_PLUGIN([notify_email], [$with_libesmtp],    [Email notifier])
 AC_PLUGIN([ntpd],        [yes],                [NTPd statistics])
 AC_PLUGIN([nut],         [$with_libupsclient], [Network UPS tools statistics])
 AC_PLUGIN([perl],        [$plugin_perl],       [Embed a Perl interpreter])
 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([qmail],       [yes],                [QMail queue statistics])
 AC_PLUGIN([rrdtool],     [$with_rrdtool],      [RRDTool output plugin])
 AC_PLUGIN([sensors],     [$with_lm_sensors],   [lm_sensors statistics])
 AC_PLUGIN([serial],      [$plugin_serial],     [serial port traffic])
@@ -2498,6 +2634,7 @@ AC_PLUGIN([tail],        [yes],                [Parsing of logfiles])
 AC_PLUGIN([tape],        [$plugin_tape],       [Tape drive statistics])
 AC_PLUGIN([tcpconns],    [$plugin_tcpconns],   [TCP connection statistics])
 AC_PLUGIN([teamspeak2],  [yes],                [TeamSpeak2 server statistics])
+AC_PLUGIN([thermal],     [$plugin_thermal],    [Linux ACPI thermal zone statistics])
 AC_PLUGIN([unixsock],    [yes],                [Unixsock communication plugin])
 AC_PLUGIN([users],       [$plugin_users],      [User statistics])
 AC_PLUGIN([uuid],        [yes],                [UUID as hostname plugin])
@@ -2579,90 +2716,98 @@ cat <<EOF;
 
 Configuration:
   Libraries:
-    libcurl . . . . . . $with_libcurl
-    libiokit  . . . . . $with_libiokit
-    libiptc . . . . . . $with_libiptc
-    libkstat  . . . . . $with_kstat
-    libkvm  . . . . . . $with_libkvm
-    libmysql  . . . . . $with_libmysql
-    libnetlink  . . . . $with_libnetlink
-    libnetsnmp  . . . . $with_libnetsnmp
-    liboconfig  . . . . $with_liboconfig
-    libopenipmi . . . . $with_libopenipmipthread
-    liboping  . . . . . $with_liboping
-    libpcap . . . . . . $with_libpcap
-    libperl . . . . . . $with_libperl
-    libpthread  . . . . $with_libpthread
-    librrd  . . . . . . $with_rrdtool
-    libsensors  . . . . $with_lm_sensors
-    libstatgrab . . . . $with_libstatgrab
-    libupsclient  . . . $with_libupsclient
-    libvirt . . . . . . $with_libvirt
-    libxml2 . . . . . . $with_libxml2
-    libxmms . . . . . . $with_libxmms
+    libcurl . . . . . . . $with_libcurl
+    libesmtp  . . . . . . $with_libesmtp
+    libiokit  . . . . . . $with_libiokit
+    libiptc . . . . . . . $with_libiptc
+    libkstat  . . . . . . $with_kstat
+    libkvm  . . . . . . . $with_libkvm
+    libmysql  . . . . . . $with_libmysql
+    libnetlink  . . . . . $with_libnetlink
+    libnetsnmp  . . . . . $with_libnetsnmp
+    libnotify . . . . . . $with_libnotify
+    liboconfig  . . . . . $with_liboconfig
+    libopenipmi . . . . . $with_libopenipmipthread
+    liboping  . . . . . . $with_liboping
+    libpcap . . . . . . . $with_libpcap
+    libperl . . . . . . . $with_libperl
+    libpthread  . . . . . $with_libpthread
+    libpq . . . . . . . . $with_libpq
+    librrd  . . . . . . . $with_rrdtool
+    libsensors  . . . . . $with_lm_sensors
+    libstatgrab . . . . . $with_libstatgrab
+    libupsclient  . . . . $with_libupsclient
+    libvirt . . . . . . . $with_libvirt
+    libxml2 . . . . . . . $with_libxml2
+    libxmms . . . . . . . $with_libxmms
 
   Features:
-    daemon mode . . . . $enable_daemon
-    debug . . . . . . . $enable_debug
+    daemon mode . . . . $enable_daemon
+    debug . . . . . . . $enable_debug
 
   Bindings:
-    perl  . . . . . . . $with_perl_bindings
+    perl  . . . . . . . $with_perl_bindings
 
   Modules:
-    apache  . . . . . . $enable_apache
-    apcups  . . . . . . $enable_apcups
-    apple_sensors . . . $enable_apple_sensors
-    ascent  . . . . . . $enable_ascent
-    battery . . . . . . $enable_battery
-    cpu . . . . . . . . $enable_cpu
-    cpufreq . . . . . . $enable_cpufreq
-    csv . . . . . . . . $enable_csv
-    df  . . . . . . . . $enable_df
-    disk  . . . . . . . $enable_disk
-    dns . . . . . . . . $enable_dns
-    email . . . . . . . $enable_email
-    entropy . . . . . . $enable_entropy
-    exec  . . . . . . . $enable_exec
-    hddtemp . . . . . . $enable_hddtemp
-    interface . . . . . $enable_interface
-    iptables  . . . . . $enable_iptables
-    ipmi  . . . . . . . $enable_ipmi
-    ipvs  . . . . . . . $enable_ipvs
-    irq . . . . . . . . $enable_irq
-    libvirt . . . . . . $enable_libvirt
-    load  . . . . . . . $enable_load
-    logfile . . . . . . $enable_logfile
-    mbmon . . . . . . . $enable_mbmon
-    memcached . . . . . $enable_memcached
-    memory  . . . . . . $enable_memory
-    multimeter  . . . . $enable_multimeter
-    mysql . . . . . . . $enable_mysql
-    netlink . . . . . . $enable_netlink
-    network . . . . . . $enable_network
-    nfs . . . . . . . . $enable_nfs
-    nginx . . . . . . . $enable_nginx
-    ntpd  . . . . . . . $enable_ntpd
-    nut . . . . . . . . $enable_nut
-    perl  . . . . . . . $enable_perl
-    ping  . . . . . . . $enable_ping
-    powerdns  . . . . . $enable_powerdns
-    processes . . . . . $enable_processes
-    rrdtool . . . . . . $enable_rrdtool
-    sensors . . . . . . $enable_sensors
-    serial  . . . . . . $enable_serial
-    snmp  . . . . . . . $enable_snmp
-    swap  . . . . . . . $enable_swap
-    syslog  . . . . . . $enable_syslog
-    tail  . . . . . . . $enable_tail
-    tape  . . . . . . . $enable_tape
-    tcpconns  . . . . . $enable_tcpconns
-    teamspeak2  . . . . $enable_teamspeak2
-    unixsock  . . . . . $enable_unixsock
-    users . . . . . . . $enable_users
-    uuid  . . . . . . . $enable_uuid
-    vmem  . . . . . . . $enable_vmem
-    vserver . . . . . . $enable_vserver
-    wireless  . . . . . $enable_wireless
-    xmms  . . . . . . . $enable_xmms
+    apache  . . . . . . . $enable_apache
+    apcups  . . . . . . . $enable_apcups
+    apple_sensors . . . . $enable_apple_sensors
+    ascent  . . . . . . . $enable_ascent
+    battery . . . . . . . $enable_battery
+    cpu . . . . . . . . . $enable_cpu
+    cpufreq . . . . . . . $enable_cpufreq
+    csv . . . . . . . . . $enable_csv
+    df  . . . . . . . . . $enable_df
+    disk  . . . . . . . . $enable_disk
+    dns . . . . . . . . . $enable_dns
+    email . . . . . . . . $enable_email
+    entropy . . . . . . . $enable_entropy
+    exec  . . . . . . . . $enable_exec
+    hddtemp . . . . . . . $enable_hddtemp
+    interface . . . . . . $enable_interface
+    iptables  . . . . . . $enable_iptables
+    ipmi  . . . . . . . . $enable_ipmi
+    ipvs  . . . . . . . . $enable_ipvs
+    irq . . . . . . . . . $enable_irq
+    libvirt . . . . . . . $enable_libvirt
+    load  . . . . . . . . $enable_load
+    logfile . . . . . . . $enable_logfile
+    mbmon . . . . . . . . $enable_mbmon
+    memcached . . . . . . $enable_memcached
+    memory  . . . . . . . $enable_memory
+    multimeter  . . . . . $enable_multimeter
+    mysql . . . . . . . . $enable_mysql
+    netlink . . . . . . . $enable_netlink
+    network . . . . . . . $enable_network
+    nfs . . . . . . . . . $enable_nfs
+    nginx . . . . . . . . $enable_nginx
+    notify_desktop  . . . $enable_notify_desktop
+    notify_email  . . . . $enable_notify_email
+    ntpd  . . . . . . . . $enable_ntpd
+    nut . . . . . . . . . $enable_nut
+    perl  . . . . . . . . $enable_perl
+    ping  . . . . . . . . $enable_ping
+    postgresql  . . . . . $enable_postgresql
+    powerdns  . . . . . . $enable_powerdns
+    processes . . . . . . $enable_processes
+    qmail . . . . . . . . $enable_qmail
+    rrdtool . . . . . . . $enable_rrdtool
+    sensors . . . . . . . $enable_sensors
+    serial  . . . . . . . $enable_serial
+    snmp  . . . . . . . . $enable_snmp
+    swap  . . . . . . . . $enable_swap
+    syslog  . . . . . . . $enable_syslog
+    tail  . . . . . . . . $enable_tail
+    tape  . . . . . . . . $enable_tape
+    tcpconns  . . . . . . $enable_tcpconns
+    teamspeak2  . . . . . $enable_teamspeak2
+    thermal . . . . . . . $enable_thermal
+    unixsock  . . . . . . $enable_unixsock
+    users . . . . . . . . $enable_users
+    uuid  . . . . . . . . $enable_uuid
+    vmem  . . . . . . . . $enable_vmem
+    vserver . . . . . . . $enable_vserver
+    wireless  . . . . . . $enable_wireless
+    xmms  . . . . . . . . $enable_xmms
 
 EOF
index b8a0813..cab7543 100755 (executable)
@@ -1953,6 +1953,66 @@ sub load_graph_definitions
     'GPRINT:ping_avg:AVERAGE:%4.1lf ms Avg,',
     'GPRINT:ping_max:MAX:%4.1lf ms Max,',
     'GPRINT:ping_avg:LAST:%4.1lf ms Last'],
+    pg_blks => ['DEF:pg_blks_avg={file}:value:AVERAGE',
+    'DEF:pg_blks_min={file}:value:MIN',
+    'DEF:pg_blks_max={file}:value:MAX',
+    "AREA:pg_blks_max#$HalfBlue",
+    "AREA:pg_blks_min#$Canvas",
+    "LINE1:pg_blks_avg#$FullBlue:Blocks",
+    'GPRINT:pg_blks_min:MIN:%4.1lf%s Min,',
+    'GPRINT:pg_blks_avg:AVERAGE:%4.1lf%s Avg,',
+    'GPRINT:pg_blks_max:MAX:%4.1lf%s Max,',
+    'GPRINT:pg_blks_avg:LAST:%4.1lf%s Last'],
+    pg_n_tup_c => ['DEF:pg_n_tup_avg={file}:value:AVERAGE',
+    'DEF:pg_n_tup_min={file}:value:MIN',
+    'DEF:pg_n_tup_max={file}:value:MAX',
+    "AREA:pg_n_tup_max#$HalfBlue",
+    "AREA:pg_n_tup_min#$Canvas",
+    "LINE1:pg_n_tup_avg#$FullBlue:Tuples",
+    'GPRINT:pg_n_tup_min:MIN:%4.1lf%s Min,',
+    'GPRINT:pg_n_tup_avg:AVERAGE:%4.1lf%s Avg,',
+    'GPRINT:pg_n_tup_max:MAX:%4.1lf%s Max,',
+    'GPRINT:pg_n_tup_avg:LAST:%4.1lf%s Last'],
+    pg_n_tup_g => ['DEF:pg_n_tup_avg={file}:value:AVERAGE',
+    'DEF:pg_n_tup_min={file}:value:MIN',
+    'DEF:pg_n_tup_max={file}:value:MAX',
+    "AREA:pg_n_tup_max#$HalfBlue",
+    "AREA:pg_n_tup_min#$Canvas",
+    "LINE1:pg_n_tup_avg#$FullBlue:Tuples",
+    'GPRINT:pg_n_tup_min:MIN:%4.1lf%s Min,',
+    'GPRINT:pg_n_tup_avg:AVERAGE:%4.1lf%s Avg,',
+    'GPRINT:pg_n_tup_max:MAX:%4.1lf%s Max,',
+    'GPRINT:pg_n_tup_avg:LAST:%4.1lf%s Last'],
+    pg_numbackends => ['DEF:pg_numbackends_avg={file}:value:AVERAGE',
+    'DEF:pg_numbackends_min={file}:value:MIN',
+    'DEF:pg_numbackends_max={file}:value:MAX',
+    "AREA:pg_numbackends_max#$HalfBlue",
+    "AREA:pg_numbackends_min#$Canvas",
+    "LINE1:pg_numbackends_avg#$FullBlue:Backends",
+    'GPRINT:pg_numbackends_min:MIN:%4.1lf%s Min,',
+    'GPRINT:pg_numbackends_avg:AVERAGE:%4.1lf%s Avg,',
+    'GPRINT:pg_numbackends_max:MAX:%4.1lf%s Max,',
+    'GPRINT:pg_numbackends_avg:LAST:%4.1lf%s Last'],
+    pg_scan => ['DEF:pg_scan_avg={file}:value:AVERAGE',
+    'DEF:pg_scan_min={file}:value:MIN',
+    'DEF:pg_scan_max={file}:value:MAX',
+    "AREA:pg_scan_max#$HalfBlue",
+    "AREA:pg_scan_min#$Canvas",
+    "LINE1:pg_scan_avg#$FullBlue:Scans",
+    'GPRINT:pg_scan_min:MIN:%4.1lf%s Min,',
+    'GPRINT:pg_scan_avg:AVERAGE:%4.1lf%s Avg,',
+    'GPRINT:pg_scan_max:MAX:%4.1lf%s Max,',
+    'GPRINT:pg_scan_avg:LAST:%4.1lf%s Last'],
+    pg_xact => ['DEF:pg_xact_avg={file}:value:AVERAGE',
+    'DEF:pg_xact_min={file}:value:MIN',
+    'DEF:pg_xact_max={file}:value:MAX',
+    "AREA:pg_xact_max#$HalfBlue",
+    "AREA:pg_xact_min#$Canvas",
+    "LINE1:pg_xact_avg#$FullBlue:Transactions",
+    'GPRINT:pg_xact_min:MIN:%4.1lf%s Min,',
+    'GPRINT:pg_xact_avg:AVERAGE:%4.1lf%s Avg,',
+    'GPRINT:pg_xact_max:MAX:%4.1lf%s Max,',
+    'GPRINT:pg_xact_avg:LAST:%4.1lf%s Last'],
     power => ['-v', 'Watt',
     'DEF:avg={file}:value:AVERAGE',
     'DEF:min={file}:value:MIN',
index f95d54d..2ec6308 100755 (executable)
@@ -243,6 +243,15 @@ sub flush {
                        elsif ($option eq "timeout") {
                                $args{"timeout"} = $value;
                        }
+                       elsif ($option eq "identifier") {
+                               my $id = getid (\$value);
+                               if (!$id)
+                               {
+                                       print STDERR "Not a valid identifier: \"$value\"\n";
+                                       next;
+                               }
+                               push @{$args{"identifier"}}, $id;
+                       }
                        else {
                                print STDERR "Invalid option \"$option\".\n";
                                return;
index 2102249..3d8b146 100644 (file)
@@ -31,6 +31,7 @@ collectd_SOURCES = collectd.c collectd.h \
                   plugin.c plugin.h \
                   utils_avltree.c utils_avltree.h \
                   utils_cache.c utils_cache.h \
+                  utils_complain.c utils_complain.h \
                   utils_ignorelist.c utils_ignorelist.h \
                   utils_llist.c utils_llist.h \
                   utils_tail_match.c utils_tail_match.h \
@@ -41,7 +42,7 @@ collectd_SOURCES = collectd.c collectd.h \
                   types_list.c types_list.h
 
 collectd_CPPFLAGS =  $(AM_CPPFLAGS) $(LTDLINCL)
-collectd_CFLAGS =
+collectd_CFLAGS = $(AM_CFLAGS)
 collectd_LDFLAGS = -export-dynamic
 collectd_LDADD =
 collectd_DEPENDENCIES =
@@ -95,7 +96,7 @@ if BUILD_PLUGIN_APACHE
 pkglib_LTLIBRARIES += apache.la
 apache_la_SOURCES = apache.c
 apache_la_LDFLAGS = -module -avoid-version
-apache_la_CFLAGS =
+apache_la_CFLAGS = $(AM_CFLAGS)
 apache_la_LIBADD =
 collectd_LDADD += "-dlopen" apache.la
 if BUILD_WITH_LIBCURL
@@ -128,7 +129,8 @@ if BUILD_PLUGIN_ASCENT
 pkglib_LTLIBRARIES += ascent.la
 ascent_la_SOURCES = ascent.c
 ascent_la_LDFLAGS = -module -avoid-version
-ascent_la_CFLAGS = $(BUILD_WITH_LIBCURL_CFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS)
+ascent_la_CFLAGS = $(AM_CFLAGS) \
+               $(BUILD_WITH_LIBCURL_CFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS)
 ascent_la_LIBADD = $(BUILD_WITH_LIBCURL_LIBS) $(BUILD_WITH_LIBXML2_LIBS)
 collectd_LDADD += "-dlopen" apache.la
 collectd_DEPENDENCIES += ascent.la
@@ -148,7 +150,7 @@ endif
 if BUILD_PLUGIN_CPU
 pkglib_LTLIBRARIES += cpu.la
 cpu_la_SOURCES = cpu.c
-cpu_la_CFLAGS =
+cpu_la_CFLAGS = $(AM_CFLAGS)
 cpu_la_LDFLAGS = -module -avoid-version
 cpu_la_LIBADD = 
 if BUILD_WITH_LIBKSTAT
@@ -192,7 +194,7 @@ endif
 if BUILD_PLUGIN_DISK
 pkglib_LTLIBRARIES += disk.la
 disk_la_SOURCES = disk.c
-disk_la_CFLAGS =
+disk_la_CFLAGS = $(AM_CFLAGS)
 disk_la_LDFLAGS = -module -avoid-version
 disk_la_LIBADD = 
 if BUILD_WITH_LIBKSTAT
@@ -266,7 +268,7 @@ endif
 if BUILD_PLUGIN_INTERFACE
 pkglib_LTLIBRARIES += interface.la
 interface_la_SOURCES = interface.c
-interface_la_CFLAGS =
+interface_la_CFLAGS = $(AM_CFLAGS)
 interface_la_LDFLAGS = -module -avoid-version
 interface_la_LIBADD =
 collectd_LDADD += "-dlopen" interface.la
@@ -301,7 +303,7 @@ endif
 if BUILD_PLUGIN_IPMI
 pkglib_LTLIBRARIES += ipmi.la
 ipmi_la_SOURCES = ipmi.c
-ipmi_la_CFLAGS = $(BUILD_WITH_OPENIPMI_CFLAGS)
+ipmi_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_OPENIPMI_CFLAGS)
 ipmi_la_LDFLAGS = -module -avoid-version
 ipmi_la_LIBADD = $(BUILD_WITH_OPENIPMI_LIBS)
 collectd_LDADD += "-dlopen" ipmi.la
@@ -327,7 +329,8 @@ endif
 if BUILD_PLUGIN_LIBVIRT
 pkglib_LTLIBRARIES += libvirt.la
 libvirt_la_SOURCES = libvirt.c
-libvirt_la_CFLAGS = $(BUILD_WITH_LIBVIRT_CFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS)
+libvirt_la_CFLAGS = $(AM_CFLAGS) \
+               $(BUILD_WITH_LIBVIRT_CFLAGS) $(BUILD_WITH_LIBXML2_CFLAGS)
 libvirt_la_LIBADD = $(BUILD_WITH_LIBVIRT_LIBS) $(BUILD_WITH_LIBXML2_LIBS)
 libvirt_la_LDFLAGS = -module -avoid-version
 collectd_LDADD += "-dlopen" libvirt.la
@@ -337,7 +340,7 @@ endif
 if BUILD_PLUGIN_LOAD
 pkglib_LTLIBRARIES += load.la
 load_la_SOURCES = load.c
-load_la_CFLAGS =
+load_la_CFLAGS = $(AM_CFLAGS)
 load_la_LDFLAGS = -module -avoid-version
 load_la_LIBADD =
 collectd_LDADD += "-dlopen" load.la
@@ -371,7 +374,6 @@ if BUILD_PLUGIN_MEMCACHED
 pkglib_LTLIBRARIES += memcached.la
 memcached_la_SOURCES = memcached.c
 memcached_la_LDFLAGS = -module -avoid-version
-memcached_la_CFLAGS =
 memcached_la_LIBADD =
 if BUILD_WITH_LIBSOCKET
 memcached_la_LDFLAGS += -lsocket
@@ -383,7 +385,7 @@ endif
 if BUILD_PLUGIN_MEMORY
 pkglib_LTLIBRARIES += memory.la
 memory_la_SOURCES = memory.c
-memory_la_CFLAGS =
+memory_la_CFLAGS = $(AM_CFLAGS)
 memory_la_LDFLAGS = -module -avoid-version
 memory_la_LIBADD =
 collectd_LDADD += "-dlopen" memory.la
@@ -412,7 +414,7 @@ if BUILD_PLUGIN_MYSQL
 pkglib_LTLIBRARIES += mysql.la
 mysql_la_SOURCES = mysql.c
 mysql_la_LDFLAGS = -module -avoid-version
-mysql_la_CFLAGS =
+mysql_la_CFLAGS = $(AM_CFLAGS)
 mysql_la_LIBADD =
 collectd_LDADD += "-dlopen" mysql.la
 if BUILD_WITH_LIBMYSQL
@@ -426,7 +428,7 @@ if BUILD_PLUGIN_NETLINK
 pkglib_LTLIBRARIES += netlink.la
 netlink_la_SOURCES = netlink.c
 netlink_la_LDFLAGS = -module -avoid-version
-netlink_la_CFLAGS = $(BUILD_WITH_LIBNETLINK_CFLAGS)
+netlink_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBNETLINK_CFLAGS)
 netlink_la_LIBADD = $(BUILD_WITH_LIBNETLINK_LIBS)
 collectd_LDADD += "-dlopen" netlink.la
 collectd_DEPENDENCIES += netlink.la
@@ -468,6 +470,23 @@ collectd_LDADD += "-dlopen" nginx.la
 collectd_DEPENDENCIES += nginx.la
 endif
 
+if BUILD_PLUGIN_NOTIFY_DESKTOP
+pkglib_LTLIBRARIES += notify_desktop.la
+notify_desktop_la_SOURCES = notify_desktop.c
+notify_desktop_la_CFLAGS = $(AM_CFLAGS) $(LIBNOTIFY_CFLAGS)
+notify_desktop_la_LDFLAGS = -module -avoid-version $(LIBNOTIFY_LIBS)
+collectd_LDADD += "-dlopen" notify_desktop.la
+collectd_DEPENDENCIES += notify_desktop.la
+endif
+
+if BUILD_PLUGIN_NOTIFY_EMAIL
+pkglib_LTLIBRARIES += notify_email.la
+notify_email_la_SOURCES = notify_email.c
+notify_email_la_LDFLAGS = -L/usr/local/lib -lesmtp -lssl -lcrypto -pthread -module -avoid-version
+collectd_LDADD += "-dlopen" notify_email.la
+collectd_DEPENDENCIES += notify_email.la
+endif
+
 if BUILD_PLUGIN_NTPD
 pkglib_LTLIBRARIES += ntpd.la
 ntpd_la_SOURCES = ntpd.c
@@ -491,11 +510,14 @@ endif
 if BUILD_PLUGIN_PERL
 pkglib_LTLIBRARIES += perl.la
 perl_la_SOURCES = perl.c
+# Despite C99 providing the "bool" type thru stdbool.h, Perl defines its own
+# version of that type if HAS_BOOL is not defined... *sigh*
+perl_la_CPPFLAGS = -DHAS_BOOL=1
 perl_la_CFLAGS  = $(AM_CFLAGS) \
-                 $(PERL_CFLAGS) \
-                 -DXS_VERSION=\"$(VERSION)\" -DVERSION=\"$(VERSION)\"
+               $(PERL_CFLAGS) \
+               -DXS_VERSION=\"$(VERSION)\" -DVERSION=\"$(VERSION)\"
 perl_la_LDFLAGS = -module -avoid-version \
-                 $(PERL_LDFLAGS)
+               $(PERL_LDFLAGS)
 collectd_LDADD += "-dlopen" perl.la
 collectd_DEPENDENCIES += perl.la
 endif
@@ -514,6 +536,16 @@ collectd_LDADD += "-dlopen" ping.la
 collectd_DEPENDENCIES += ping.la
 endif
 
+if BUILD_PLUGIN_POSTGRESQL
+pkglib_LTLIBRARIES += postgresql.la
+postgresql_la_SOURCES = postgresql.c
+postgresql_la_CPPFLAGS = $(BUILD_WITH_LIBPQ_CPPFLAGS)
+postgresql_la_LDFLAGS = -module -avoid-version \
+               $(BUILD_WITH_LIBPQ_LDFLAGS) -lpq
+collectd_LDADD += "-dlopen" postgresql.la
+collectd_DEPENDENCIES += postgresql.la
+endif
+
 if BUILD_PLUGIN_POWERDNS
 pkglib_LTLIBRARIES += powerdns.la
 powerdns_la_SOURCES = powerdns.c
@@ -530,11 +562,19 @@ collectd_LDADD += "-dlopen" processes.la
 collectd_DEPENDENCIES += processes.la
 endif
 
+if BUILD_PLUGIN_QMAIL
+pkglib_LTLIBRARIES += qmail.la
+qmail_la_SOURCES = qmail.c
+qmail_la_LDFLAGS = -module -avoid-version
+collectd_LDADD += "-dlopen" qmail.la
+collectd_DEPENDENCIES += qmail.la
+endif
+
 if BUILD_PLUGIN_RRDTOOL
 pkglib_LTLIBRARIES += rrdtool.la
 rrdtool_la_SOURCES = rrdtool.c
 rrdtool_la_LDFLAGS = -module -avoid-version
-rrdtool_la_CFLAGS = $(BUILD_WITH_LIBRRD_CFLAGS)
+rrdtool_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBRRD_CFLAGS)
 rrdtool_la_LIBADD = $(BUILD_WITH_LIBRRD_LDFLAGS)
 collectd_LDADD += "-dlopen" rrdtool.la
 collectd_DEPENDENCIES += rrdtool.la
@@ -543,7 +583,7 @@ endif
 if BUILD_PLUGIN_SENSORS
 pkglib_LTLIBRARIES += sensors.la
 sensors_la_SOURCES = sensors.c
-sensors_la_CFLAGS = $(BUILD_WITH_LIBSENSORS_CFLAGS)
+sensors_la_CFLAGS = $(AM_CFLAGS) $(BUILD_WITH_LIBSENSORS_CFLAGS)
 sensors_la_LDFLAGS = -module -avoid-version $(BUILD_WITH_LIBSENSORS_LDFLAGS)
 sensors_la_LIBADD = -lsensors
 collectd_LDADD += "-dlopen" sensors.la
@@ -562,7 +602,7 @@ if BUILD_PLUGIN_SNMP
 pkglib_LTLIBRARIES += snmp.la
 snmp_la_SOURCES = snmp.c
 snmp_la_LDFLAGS = -module -avoid-version
-snmp_la_CFLAGS =
+snmp_la_CFLAGS = $(AM_CFLAGS)
 snmp_la_LIBADD =
 if BUILD_WITH_LIBNETSNMP
 snmp_la_CFLAGS += $(BUILD_WITH_LIBSNMP_CFLAGS)
@@ -578,7 +618,7 @@ endif
 if BUILD_PLUGIN_SWAP
 pkglib_LTLIBRARIES += swap.la
 swap_la_SOURCES = swap.c
-swap_la_CFLAGS =
+swap_la_CFLAGS = $(AM_CFLAGS)
 swap_la_LDFLAGS = -module -avoid-version
 swap_la_LIBADD =
 collectd_LDADD += "-dlopen" swap.la
@@ -638,6 +678,14 @@ collectd_LDADD += "-dlopen" teamspeak2.la
 collectd_DEPENDENCIES += teamspeak2.la
 endif
 
+if BUILD_PLUGIN_THERMAL
+pkglib_LTLIBRARIES += thermal.la
+thermal_la_SOURCES = thermal.c
+thermal_la_LDFLAGS = -module -avoid-version
+collectd_LDADD += "-dlopen" thermal.la
+collectd_DEPENDENCIES += thermal.la
+endif
+
 if BUILD_PLUGIN_UNIXSOCK
 pkglib_LTLIBRARIES += unixsock.la
 unixsock_la_SOURCES = unixsock.c \
@@ -654,7 +702,7 @@ endif
 if BUILD_PLUGIN_USERS
 pkglib_LTLIBRARIES += users.la
 users_la_SOURCES = users.c
-users_la_CFLAGS =
+users_la_CFLAGS = $(AM_CFLAGS)
 users_la_LDFLAGS = -module -avoid-version
 users_la_LIBADD =
 if BUILD_WITH_LIBSTATGRAB
index a2f41f4..c6bf8ad 100644 (file)
@@ -146,7 +146,7 @@ static int init (void)
        {
                int status;
 
-               status = snprintf (credentials, sizeof (credentials), "%s:%s",
+               status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
                                user, (pass == NULL) ? "" : pass);
                if (status >= sizeof (credentials))
                {
@@ -155,7 +155,6 @@ static int init (void)
                                        "truncated.");
                        return (-1);
                }
-               credentials[sizeof (credentials) - 1] = '\0';
 
                curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
        }
@@ -202,15 +201,13 @@ static void submit_counter (const char *type, const char *type_instance,
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
        if (type_instance != NULL)
-       {
-               strncpy (vl.type_instance, type_instance,
+               sstrncpy (vl.type_instance, type_instance,
                                sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void submit_counter */
 
 static void submit_gauge (const char *type, const char *type_instance,
@@ -227,15 +224,13 @@ static void submit_gauge (const char *type, const char *type_instance,
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "apache", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
        if (type_instance != NULL)
-       {
-               strncpy (vl.type_instance, type_instance,
+               sstrncpy (vl.type_instance, type_instance,
                                sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void submit_counter */
 
 static void submit_scoreboard (char *buf)
index c948f5a..227c703 100644 (file)
@@ -124,8 +124,7 @@ static int net_open (char *host, int port)
        assert ((port > 0x00000000) && (port <= 0x0000FFFF));
 
        /* Convert the port to a string */
-       snprintf (port_str, 8, "%i", port);
-       port_str[7] = '\0';
+       ssnprintf (port_str, sizeof (port_str), "%i", port);
 
        /* Resolve name */
        memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
@@ -376,9 +375,10 @@ static void apc_submit_generic (char *type, char *type_inst, double value)
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "apcups", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
-       strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static void apc_submit (struct apc_detail_s *apcups_detail)
index 5e957b6..6296335 100644 (file)
@@ -91,9 +91,10 @@ static void as_submit (const char *type, const char *type_instance,
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "apple_sensors", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type))
        sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int as_read (void)
@@ -146,10 +147,10 @@ static int as_read (void)
                if (CFGetTypeID (property) != CFStringGetTypeID ())
                        continue;
                if (!CFStringGetCString (property,
-                                       type, 128,
+                                       type, sizeof (type),
                                        kCFStringEncodingASCII))
                        continue;
-               type[127] = '\0';
+               type[sizeof (type) - 1] = '\0';
 
                /* Copy the sensor location. This will be used as `instance'. */
                property = NULL;
@@ -160,10 +161,10 @@ static int as_read (void)
                if (CFGetTypeID (property) != CFStringGetTypeID ())
                        continue;
                if (!CFStringGetCString (property,
-                                       inst, 128,
+                                       inst, sizeof (inst),
                                        kCFStringEncodingASCII))
                        continue;
-               inst[127] = '\0';
+               inst[sizeof (inst) - 1] = '\0';
                for (i = 0; i < 128; i++)
                {
                        if (inst[i] == '\0')
index 8c0bbec..2ad641d 100644 (file)
@@ -91,10 +91,12 @@ struct player_info_s
 typedef struct player_info_s player_info_t;
 #define PLAYER_INFO_STATIC_INIT { -1, -1, -1, -1, -1 }
 
-static char *url    = NULL;
-static char *user   = NULL;
-static char *pass   = NULL;
-static char *cacert = NULL;
+static char *url         = NULL;
+static char *user        = NULL;
+static char *pass        = NULL;
+static char *verify_peer = NULL;
+static char *verify_host = NULL;
+static char *cacert      = NULL;
 
 static CURL *curl = NULL;
 
@@ -108,6 +110,8 @@ static const char *config_keys[] =
   "URL",
   "User",
   "Password",
+  "VerifyPeer",
+  "VerifyHost",
   "CACert"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
@@ -130,10 +134,12 @@ static int ascent_submit_gauge (const char *plugin_instance, /* {{{ */
     sstrncpy (vl.plugin_instance, plugin_instance,
         sizeof (vl.plugin_instance));
 
+  sstrncpy (vl.type, type, sizeof (vl.type));
+
   if (type_instance != NULL)
     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
   return (0);
 } /* }}} int ascent_submit_gauge */
 
@@ -498,6 +504,10 @@ static int ascent_config (const char *key, const char *value) /* {{{ */
     return (config_set (&user, value));
   else if (strcasecmp (key, "Password") == 0)
     return (config_set (&pass, value));
+  else if (strcasecmp (key, "VerifyPeer") == 0)
+    return (config_set (&verify_peer, value));
+  else if (strcasecmp (key, "VerifyHost") == 0)
+    return (config_set (&verify_host, value));
   else if (strcasecmp (key, "CACert") == 0)
     return (config_set (&cacert, value));
   else
@@ -534,7 +544,7 @@ static int ascent_init (void) /* {{{ */
   {
     int status;
 
-    status = snprintf (credentials, sizeof (credentials), "%s:%s",
+    status = ssnprintf (credentials, sizeof (credentials), "%s:%s",
         user, (pass == NULL) ? "" : pass);
     if (status >= sizeof (credentials))
     {
@@ -542,13 +552,22 @@ static int ascent_init (void) /* {{{ */
           "credentials have been truncated.");
       return (-1);
     }
-    credentials[sizeof (credentials) - 1] = '\0';
 
     curl_easy_setopt (curl, CURLOPT_USERPWD, credentials);
   }
 
   curl_easy_setopt (curl, CURLOPT_URL, url);
 
+  if ((verify_peer == NULL) || (strcmp (verify_peer, "true") == 0))
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1);
+  else
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
+
+  if ((verify_host == NULL) || (strcmp (verify_host, "true") == 0))
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 2);
+  else
+    curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
+
   if (cacert != NULL)
     curl_easy_setopt (curl, CURLOPT_CAINFO, cacert);
 
index badd8e7..9f1bee6 100644 (file)
@@ -61,6 +61,7 @@
 #elif KERNEL_LINUX
 static int   battery_pmu_num = 0;
 static char *battery_pmu_file = "/proc/pmu/battery_%i";
+static const char *battery_acpi_dir = "/proc/acpi/battery";
 #endif /* KERNEL_LINUX */
 
 static int battery_init (void)
@@ -75,7 +76,7 @@ static int battery_init (void)
 
        for (battery_pmu_num = 0; ; battery_pmu_num++)
        {
-               len = snprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
+               len = ssnprintf (filename, sizeof (filename), battery_pmu_file, battery_pmu_num);
 
                if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
                        break;
@@ -101,8 +102,9 @@ static void battery_submit (const char *plugin_instance, const char *type, doubl
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "battery", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void battery_submit */
 
 #if HAVE_IOKIT_PS_IOPOWERSOURCES_H || HAVE_IOKIT_IOKITLIB_H
@@ -312,6 +314,99 @@ static void get_via_generic_iokit (double *ret_charge,
 }
 #endif /* HAVE_IOKIT_IOKITLIB_H */
 
+#if KERNEL_LINUX
+static int battery_read_acpi (const char *name)
+{
+       double  current = INVALID_VALUE;
+       double  voltage = INVALID_VALUE;
+       double  charge  = INVALID_VALUE;
+       double *valptr = NULL;
+       int charging = 0;
+
+       char filename[256];
+       FILE *fh;
+
+       char buffer[1024];
+       char *fields[8];
+       int numfields;
+       char *endptr;
+       int len;
+
+       len = ssnprintf (filename, sizeof (filename), "%s/%s/state", battery_acpi_dir, name);
+
+       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
+               return -1;
+
+       if ((fh = fopen (filename, "r")) == NULL) {
+               char errbuf[1024];
+               ERROR ("Cannot open `%s': %s", filename,
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return -1;
+       }
+
+       /*
+        * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
+        * [11:00] <@tokkee> present:                 yes
+        * [11:00] <@tokkee> capacity state:          ok
+        * [11:00] <@tokkee> charging state:          charging
+        * [11:00] <@tokkee> present rate:            1724 mA
+        * [11:00] <@tokkee> remaining capacity:      4136 mAh
+        * [11:00] <@tokkee> present voltage:         12428 mV
+        */
+       while (fgets (buffer, sizeof (buffer), fh) != NULL)
+       {
+               numfields = strsplit (buffer, fields, 8);
+
+               if (numfields < 3)
+                       continue;
+
+               if ((strcmp (fields[0], "charging") == 0)
+                               && (strcmp (fields[1], "state:") == 0))
+               {
+                       if (strcmp (fields[2], "charging") == 0)
+                               charging = 1;
+                       else
+                               charging = 0;
+                       continue;
+               }
+
+               if ((strcmp (fields[0], "present") == 0)
+                               && (strcmp (fields[1], "rate:") == 0))
+                       valptr = &current;
+               else if ((strcmp (fields[0], "remaining") == 0)
+                               && (strcmp (fields[1], "capacity:") == 0))
+                       valptr = &charge;
+               else if ((strcmp (fields[0], "present") == 0)
+                               && (strcmp (fields[1], "voltage:") == 0))
+                       valptr = &voltage;
+               else
+                       continue;
+
+               endptr = NULL;
+               errno  = 0;
+               *valptr = strtod (fields[2], &endptr) / 1000.0;
+
+               if ((fields[2] == endptr) || (errno != 0))
+                       *valptr = INVALID_VALUE;
+       } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
+
+       fclose (fh);
+
+       if ((current != INVALID_VALUE) && (charging == 0))
+                       current *= -1;
+
+       if (charge != INVALID_VALUE)
+               battery_submit ("0", "charge", charge);
+       if (current != INVALID_VALUE)
+               battery_submit ("0", "current", current);
+       if (voltage != INVALID_VALUE)
+               battery_submit ("0", "voltage", voltage);
+
+       return 0;
+}
+#endif /* KERNEL_LINUX */
+
+
 static int battery_read (void)
 {
 #if HAVE_IOKIT_IOKITLIB_H || HAVE_IOKIT_PS_IOPOWERSOURCES_H
@@ -359,11 +454,11 @@ static int battery_read (void)
                double  charge  = INVALID_VALUE;
                double *valptr = NULL;
 
-               len = snprintf (filename, sizeof (filename), battery_pmu_file, i);
+               len = ssnprintf (filename, sizeof (filename), battery_pmu_file, i);
                if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
                        continue;
 
-               len = snprintf (batnum_str, sizeof (batnum_str), "%i", i);
+               len = ssnprintf (batnum_str, sizeof (batnum_str), "%i", i);
                if ((len < 0) || ((unsigned int)len >= sizeof (batnum_str)))
                        continue;
 
@@ -411,111 +506,8 @@ static int battery_read (void)
                        battery_submit ("0", "voltage", voltage);
        }
 
-       if (access ("/proc/acpi/battery", R_OK | X_OK) == 0)
-       {
-               double  current = INVALID_VALUE;
-               double  voltage = INVALID_VALUE;
-               double  charge  = INVALID_VALUE;
-               double *valptr = NULL;
-               int charging = 0;
-
-               struct dirent *ent;
-               DIR *dh;
+       walk_directory (battery_acpi_dir, battery_read_acpi);
 
-               if ((dh = opendir ("/proc/acpi/battery")) == NULL)
-               {
-                       char errbuf[1024];
-                       ERROR ("Cannot open `/proc/acpi/battery': %s",
-                                       sstrerror (errno, errbuf, sizeof (errbuf)));
-                       return (-1);
-               }
-
-               while ((ent = readdir (dh)) != NULL)
-               {
-                       if (ent->d_name[0] == '.')
-                               continue;
-
-                       len = snprintf (filename, sizeof (filename),
-                                       "/proc/acpi/battery/%s/state",
-                                       ent->d_name);
-                       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
-                               continue;
-
-                       if ((fh = fopen (filename, "r")) == NULL)
-                       {
-                               char errbuf[1024];
-                               ERROR ("Cannot open `%s': %s", filename,
-                                               sstrerror (errno, errbuf,
-                                                       sizeof (errbuf)));
-                               continue;
-                       }
-
-                       /*
-                        * [11:00] <@tokkee> $ cat /proc/acpi/battery/BAT1/state
-                        * [11:00] <@tokkee> present:                 yes
-                        * [11:00] <@tokkee> capacity state:          ok
-                        * [11:00] <@tokkee> charging state:          charging
-                        * [11:00] <@tokkee> present rate:            1724 mA
-                        * [11:00] <@tokkee> remaining capacity:      4136 mAh
-                        * [11:00] <@tokkee> present voltage:         12428 mV
-                        */
-                       while (fgets (buffer, sizeof (buffer), fh) != NULL)
-                       {
-                               numfields = strsplit (buffer, fields, 8);
-
-                               if (numfields < 3)
-                                       continue;
-
-                               if ((strcmp (fields[0], "present") == 0)
-                                               && (strcmp (fields[1], "rate:") == 0))
-                                       valptr = &current;
-                               else if ((strcmp (fields[0], "remaining") == 0)
-                                               && (strcmp (fields[1], "capacity:") == 0))
-                                       valptr = &charge;
-                               else if ((strcmp (fields[0], "present") == 0)
-                                               && (strcmp (fields[1], "voltage:") == 0))
-                                       valptr = &voltage;
-                               else
-                                       valptr = NULL;
-
-                               if ((strcmp (fields[0], "charging") == 0)
-                                               && (strcmp (fields[1], "state:") == 0))
-                               {
-                                       if (strcmp (fields[2], "charging") == 0)
-                                               charging = 1;
-                                       else
-                                               charging = 0;
-                               }
-
-                               if (valptr != NULL)
-                               {
-                                       char *endptr;
-
-                                       endptr = NULL;
-                                       errno  = 0;
-
-                                       *valptr = strtod (fields[2], &endptr) / 1000.0;
-
-                                       if ((fields[2] == endptr) || (errno != 0))
-                                               *valptr = INVALID_VALUE;
-                               }
-                       } /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
-
-                       fclose (fh);
-
-                       if ((current != INVALID_VALUE) && (charging == 0))
-                                       current *= -1;
-
-                       if (charge != INVALID_VALUE)
-                               battery_submit ("0", "charge", charge);
-                       if (current != INVALID_VALUE)
-                               battery_submit ("0", "current", current);
-                       if (voltage != INVALID_VALUE)
-                               battery_submit ("0", "voltage", voltage);
-               }
-
-               closedir (dh);
-       }
 #endif /* KERNEL_LINUX */
 
        return (0);
index 8cdf08b..b7ae9ca 100644 (file)
@@ -166,6 +166,7 @@ layout looks like this:
     time   => time (),
     host   => $hostname_g,
     plugin => 'myplugin',
+    type   => 'myplugin',
     plugin_instance => '',
     type_instance   => ''
   }
@@ -285,13 +286,19 @@ data type.
 Removes a callback or data-set from collectd's internal list of
 functionsE<nbsp>/ datasets.
 
-=item B<plugin_dispatch_values> (I<type>, I<value-list>)
+=item B<plugin_dispatch_values> (I<value-list>)
 
-Submits a I<value-list> of type I<type> to the daemon. If the data-set I<type>
+Submits a I<value-list> to the daemon. If the data-set identified by
+I<value-list>->{I<type>}
 is found (and the number of values matches the number of data-sources) then the
 type, data-set and value-list is passed to all write-callbacks that are
 registered with the daemon.
 
+B<Note>: Prior to version 4.4 of collectd, the data-set type used to be passed
+as the first argument to B<plugin_register>. This syntax is still supported
+for backwards compatibility but has been deprecated and will be removed in
+some future version of collectd.
+
 =item B<plugin_flush> ([B<timeout> => I<timeout>,] [B<plugins> => I<...>])
 
 Flush one or more plugins. I<timeout> is passed on to the registered
index 971cb36..1e00baf 100644 (file)
@@ -183,19 +183,25 @@ Example:
   -> | PUTNOTIF type=temperature severity=warning time=1201094702 message=The roof is on fire!
   <- | 0 Success
 
-=item B<FLUSH> [B<timeout=>I<Timeout>] [B<plugin=>I<Plugin> [...]]
+=item B<FLUSH> [B<timeout=>I<Timeout>] [B<plugin=>I<Plugin> [...]] [B<identifier=>I<Ident> [...]]
 
 Flushes all cached data older than I<Timeout> seconds. If no timeout has been
-specified, it defaults to -1 which causes all data to be flushed. B<timeout>
-may be specified multiple times - each occurrence applies to plugins listed
-afterwards.
+specified, it defaults to -1 which causes all data to be flushed.
 
-If specified, only specific plugins are flushed. Otherwise all plugins
-providing a flush callback are flushed.
+If the B<plugin> option has been specified, only the I<Plugin> plugin will be
+flushed. You can have multiple B<plugin> options to flush multiple plugins in
+one go. If the B<plugin> option is not given all plugins providing a flush
+callback will be flushed.
+
+If the B<identifier> option is given only the specified values will be flushed.
+This is meant to be used by graphing or displaying frontends which want to have
+the lastest values for a specific graph. Again, you can specify the
+B<identifier> option multiple times to flush several values. If this option is
+not specified at all, all values will be flushed.
 
 Example:
-  -> | FLUSH
-  <- | 0 Done
+  -> | FLUSH plugin=rrdtool identifier=localhost/df/df-root identifier=localhost/df/df-var
+  <- | 0 Done: 2 successful, 0 errors
 
 =back
 
index a3f63b4..5484322 100644 (file)
@@ -90,8 +90,7 @@ static int init_hostname (void)
        str = global_option_get ("Hostname");
        if (str != NULL)
        {
-               strncpy (hostname_g, str, sizeof (hostname_g));
-               hostname_g[sizeof (hostname_g) - 1] = '\0';
+               sstrncpy (hostname_g, str, sizeof (hostname_g));
                return (0);
        }
 
@@ -127,8 +126,7 @@ static int init_hostname (void)
                if (ai_ptr->ai_canonname == NULL)
                        continue;
 
-               strncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
-               hostname_g[sizeof (hostname_g) - 1] = '\0';
+               sstrncpy (hostname_g, ai_ptr->ai_canonname, sizeof (hostname_g));
                break;
        }
 
index f4e04d3..7c36643 100644 (file)
@@ -45,10 +45,13 @@ FQDNLookup   true
 @BUILD_PLUGIN_NETWORK_TRUE@LoadPlugin network
 @BUILD_PLUGIN_NFS_TRUE@LoadPlugin nfs
 @BUILD_PLUGIN_NGINX_TRUE@LoadPlugin nginx
+@BUILD_PLUGIN_NOTIFY_DESKTOP_TRUE@LoadPlugin notify_desktop
+@BUILD_PLUGIN_NOTIFY_EMAIL_TRUE@LoadPlugin notify_email
 @BUILD_PLUGIN_NTPD_TRUE@LoadPlugin ntpd
 @BUILD_PLUGIN_NUT_TRUE@LoadPlugin nut
 @BUILD_PLUGIN_PERL_TRUE@LoadPlugin perl
 @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_RRDTOOL_TRUE@LoadPlugin rrdtool
@@ -61,6 +64,7 @@ FQDNLookup   true
 @BUILD_PLUGIN_TAPE_TRUE@LoadPlugin tape
 @BUILD_PLUGIN_TCPCONNS_TRUE@LoadPlugin tcpconns
 @BUILD_PLUGIN_TEAMSPEAK2_TRUE@LoadPlugin teamspeak2
+@BUILD_PLUGIN_THERMAL_TRUE@LoadPlugin thermal
 @BUILD_PLUGIN_UNIXSOCK_TRUE@LoadPlugin unixsock
 @BUILD_PLUGIN_USERS_TRUE@LoadPlugin users
 @BUILD_PLUGIN_UUID_TRUE@LoadPlugin uuid
@@ -204,6 +208,24 @@ FQDNLookup   true
 #      CACert "/etc/ssl/ca.crt"
 #</Plugin>
 
+#<Plugin notify_desktop>
+#      OkayTimeout 1000
+#      WarningTimeout 5000
+#      FailureTimeout 0
+#</Plugin>
+
+#<Plugin notify_email>
+#       SMTPServer "localhost"
+#      SMTPPort 25
+#      SMTPUser "my-username"
+#      SMTPPassword "my-password"
+#      From "collectd@main0server.com"
+#      # <WARNING/FAILURE/OK> on <hostname>. beware! do not use not more than two %s in this string!!!
+#      Subject "Aaaaaa!! %s on %s!!!!!"
+#      Recipient "email1@domain1.net"
+#      Recipient "email2@domain2.com"
+#</Plugin>
+
 #<Plugin ntpd>
 #      Host "localhost"
 #      Port 123
@@ -226,6 +248,22 @@ FQDNLookup   true
 #      TTL 255
 #</Plugin>
 
+#<Plugin postgresql>
+#      <Database foo>
+#              Host "hostname"
+#              Port 5432
+#              User "username"
+#              Password "secret"
+#
+#              SSLMode "prefer"
+#              KRBSrvName "kerberos_service_name"
+#      </Database>
+#
+#      <Database bar>
+#              Service "service_name"
+#      </Database>
+#</Plugin>
+
 #<Plugin powerdns>
 #  <Server "server_name">
 #    Collect "latency"
@@ -333,6 +371,12 @@ FQDNLookup   true
 #      Server "8767"
 #</Plugin>
 
+#<Plugin thermal>
+#      ForceUseProcfs false
+#      Device "THRM"
+#      IgnoreSelected false
+#</Plugin>
+
 #<Plugin unixsock>
 #      SocketFile "@prefix@/var/run/@PACKAGE_NAME@-unixsock"
 #      SocketGroup "collectd"
index afa0571..faff2ff 100644 (file)
@@ -232,6 +232,19 @@ Optional user name needed for authentication.
 
 Optional password needed for authentication.
 
+=item B<VerifyPeer> B<true|false>
+
+Enable or disable peer SSL certificate verification. See
+L<http://curl.haxx.se/docs/sslcerts.html> for details. Enabled by default.
+
+=item B<VerifyHost> B<true|false>
+
+Enable or disable peer host name verification. If enabled, the plugin checks
+if the C<Common Name> or a C<Subject Alternate Name> field of the SSL
+certificate matches the host name provided by the B<URL> option. If this
+identity check fails, the connection is aborted. Obviously, only works when
+connecting to a SSL enabled server. Enabled by default.
+
 =item B<CACert> I<File>
 
 File that holds one or more SSL certificates. If you want to use HTTPS you will
@@ -857,6 +870,32 @@ and are checked by default depends on the distribution you use.
 
 =back
 
+=head2 Plugin C<notify_desktop>
+
+This plugin sends a desktop notification to a notification daemon, as defined
+in the Desktop Notification Specification. To actually display the
+notifications, B<notification-daemon> is required and B<collectd> has to be
+able to access the X server.
+
+The Desktop Notification Specification can be found at
+L<http://www.galago-project.org/specs/notification/>.
+
+=over 4
+
+=item B<OkayTimeout> I<timeout>
+
+=item B<WarningTimeout> I<timeout>
+
+=item B<FailureTimeout> I<timeout>
+
+Set the I<timeout>, in milliseconds, after which to expire the notification
+for C<OKAY>, C<WARNING> and C<FAILURE> severities respectively. If zero has
+been specified, the displayed notification will not be closed at all - the
+user has to do so herself. These options default to 5000. If a negative number
+has been specified, the default is used as well.
+
+=back
+
 =head2 Plugin C<ntpd>
 
 =over 4
@@ -909,6 +948,110 @@ Sets the Time-To-Live of generated ICMP packets.
 
 =back
 
+=head2 Plugin C<postgresql>
+
+The C<postgresql> plugin queries statistics from PostgreSQL databases. It
+keeps a persistent connection to all configured databases and tries to
+reconnect if the connection has been interrupted. The statistics are collected
+from PostgreSQL's B<statistics collector> which thus has to be enabled for
+this plugin to work correctly. This should usually be the case by default.
+See the section "The Statistics Collector" of the B<PostgreSQL Documentation>
+for details.
+
+The B<PostgreSQL Documentation> manual can be found at
+L<http://www.postgresql.org/docs/manuals/>.
+
+  <Plugin postgresql>
+    <Database foo>
+      Host "hostname"
+      Port "5432"
+      User "username"
+      Password "secret"
+      SSLMode "prefer"
+      KRBSrvName "kerberos_service_name"
+    </Database>
+    <Database bar>
+      Service "service_name"
+    </Database>
+  </Plugin>
+
+=over 4
+
+=item B<Database> block
+
+The B<Database> block defines one PostgreSQL database for which to collect
+statistics. It accepts a single mandatory argument which specifies the
+database name. None of the other options are required. PostgreSQL will use
+default values as documented in the section "CONNECTING TO A DATABASE" in the
+L<psql(1)> manpage. However, be aware that those defaults may be influenced by
+the user collectd is run as and special environment variables. See the manpage
+for details.
+
+=item B<Host> I<hostname>
+
+Specify the hostname or IP of the PostgreSQL server to connect to. If the
+value begins with a slash, it is interpreted as the directory name in which to
+look for the UNIX domain socket.
+
+This option is also used to determine the hostname that is associated with a
+collected data set. If it has been omitted or either begins with with a slash
+or equals B<localhost> it will be replaced with the global hostname definition
+of collectd. Any other value will be passed literally to collectd when
+dispatching values. Also see the global B<Hostname> and B<FQDNLookup> options.
+
+=item B<Port> I<port>
+
+Specify the TCP port or the local UNIX domain socket file extension of the
+server.
+
+=item B<User> I<username>
+
+Specify the username to be used when connecting to the server.
+
+=item B<Password> I<password>
+
+Specify the password to be used when connecting to the server.
+
+=item B<SSLMode> I<disable>|I<allow>|I<prefer>|I<require>
+
+Specify whether to use an SSL connection when contacting the server. The
+following modes are supported:
+
+=over 4
+
+=item I<disable>
+
+Do not use SSL at all.
+
+=item I<allow>
+
+First, try to connect without using SSL. If that fails, try using SSL.
+
+=item I<prefer> (default)
+
+First, try to connect using SSL. If that fails, try without using SSL.
+
+=item I<require>
+
+Use SSL only.
+
+=back
+
+=item B<KRBSrvName> I<kerberos_service_name>
+
+Specify the Kerberos service name to use when authenticating with Kerberos 5
+or GSSAPI. See the sections "Kerberos authentication" and "GSSAPI" of the
+B<PostgreSQL Documentation> for details.
+
+=item B<Service> I<service_name>
+
+Specify the PostgreSQL service name to use for additional parameters. That
+service has to be defined in F<pg_service.conf> and holds additional
+connection parameters. See the section "The Connection Service File" in the
+B<PostgreSQL Documentation> for details.
+
+=back
+
 =head2 Plugin C<powerdns>
 
 The C<powerdns> plugin queries statistics from an authoritative PowerDNS
@@ -1363,6 +1506,32 @@ port in numeric form.
 
 =back
 
+=head2 Plugin C<thermal>
+
+=over 4
+
+=item B<ForceUseProcfs> I<true>|I<false>
+
+By default, the C<thermal> plugin tries to read the statistics from the Linux
+C<sysfs> interface. If that is not available, the plugin falls back to the
+C<procfs> interface. By setting this option to I<true>, you can force the
+plugin to use the latter. This option defaults to I<false>.
+
+=item B<Device> I<Device>
+
+Selects the name of the thermal device that you want to collect or ignore,
+depending on the value of the B<IgnoreSelected> option. This option may be
+used multiple times to specify a list of devices.
+
+=item B<IgnoreSelected> I<true>|I<false>
+
+Invert the selection: If set to true, all devices B<except> the ones that
+match the device names specified by the B<Device> option are collected. By
+default only selected devices are collected if a selection is made. If no
+selection is configured at all, B<all> devices are selected.
+
+=back
+
 =head2 Plugin C<unixsock>
 
 =over 4
@@ -1587,6 +1756,7 @@ L<types.db(5)>,
 L<hddtemp(8)>,
 L<kstat(3KSTAT)>,
 L<mbmon(1)>,
+L<psql(1)>,
 L<rrdtool(1)>,
 L<sensors(1)>
 
index 337da46..5912fe6 100644 (file)
@@ -56,6 +56,9 @@
 #if HAVE_STDINT_H
 # include <stdint.h>
 #endif
+#if HAVE_STDBOOL_H
+# include <stdbool.h>
+#endif
 #if HAVE_UNISTD_H
 # include <unistd.h>
 #endif
index 82b4aaa..fd7c199 100644 (file)
@@ -61,6 +61,19 @@ char *sstrncpy (char *dest, const char *src, size_t n)
        return (dest);
 } /* char *sstrncpy */
 
+int ssnprintf (char *dest, size_t n, const char *format, ...)
+{
+       int ret = 0;
+       va_list ap;
+
+       va_start (ap, format);
+       ret = vsnprintf (dest, n, format, ap);
+       dest[n - 1] = '\0';
+       va_end (ap);
+
+       return (ret);
+} /* int ssnprintf */
+
 char *sstrdup (const char *s)
 {
        char *r;
@@ -91,7 +104,7 @@ char *sstrerror (int errnum, char *buf, size_t buflen)
                pthread_mutex_lock (&strerror_r_lock);
 
                temp = strerror (errnum);
-               strncpy (buf, temp, buflen);
+               sstrncpy (buf, temp, buflen);
 
                pthread_mutex_unlock (&strerror_r_lock);
        }
@@ -104,9 +117,9 @@ char *sstrerror (int errnum, char *buf, size_t buflen)
                if (buf[0] == '\0')
                {
                        if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
-                               strncpy (buf, temp, buflen);
+                               sstrncpy (buf, temp, buflen);
                        else
-                               strncpy (buf, "strerror_r did not return "
+                               sstrncpy (buf, "strerror_r did not return "
                                                "an error message", buflen);
                }
        }
@@ -115,13 +128,12 @@ char *sstrerror (int errnum, char *buf, size_t buflen)
 #else
        if (strerror_r (errnum, buf, buflen) != 0)
        {
-               snprintf (buf, buflen, "Error #%i; "
+               ssnprintf (buf, buflen, "Error #%i; "
                                "Additionally, strerror_r failed.",
                                errnum);
        }
 #endif /* STRERROR_R_CHAR_P */
 
-       buf[buflen - 1] = '\0';
        return (buf);
 } /* char *sstrerror */
 
@@ -376,7 +388,7 @@ int check_create_dir (const char *file_orig)
 
        if ((len = strlen (file_orig)) < 1)
                return (-1);
-       else if (len >= 512)
+       else if (len >= sizeof (file_copy))
                return (-1);
 
        /*
@@ -391,8 +403,7 @@ int check_create_dir (const char *file_orig)
        /*
         * Create a copy for `strtok_r' to destroy
         */
-       strncpy (file_copy, file_orig, 512);
-       file_copy[511] = '\0';
+       sstrncpy (file_copy, file_orig, sizeof (file_copy));
 
        /*
         * Break into components. This will eat up several slashes in a row and
@@ -477,8 +488,7 @@ int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)
        if (kc == NULL)
                return (-1);
 
-       snprintf (ident, 128, "%s,%i,%s", module, instance, name);
-       ident[127] = '\0';
+       ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name);
 
        if (*ksp_ptr == NULL)
        {
@@ -663,21 +673,21 @@ int format_name (char *ret, int ret_len,
        if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
        {
                if ((type_instance == NULL) || (strlen (type_instance) == 0))
-                       status = snprintf (ret, ret_len, "%s/%s/%s",
+                       status = ssnprintf (ret, ret_len, "%s/%s/%s",
                                        hostname, plugin, type);
                else
-                       status = snprintf (ret, ret_len, "%s/%s/%s-%s",
+                       status = ssnprintf (ret, ret_len, "%s/%s/%s-%s",
                                        hostname, plugin, type,
                                        type_instance);
        }
        else
        {
                if ((type_instance == NULL) || (strlen (type_instance) == 0))
-                       status = snprintf (ret, ret_len, "%s/%s-%s/%s",
+                       status = ssnprintf (ret, ret_len, "%s/%s-%s/%s",
                                        hostname, plugin, plugin_instance,
                                        type);
                else
-                       status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
+                       status = ssnprintf (ret, ret_len, "%s/%s-%s/%s-%s",
                                        hostname, plugin, plugin_instance,
                                        type, type_instance);
        }
@@ -838,26 +848,63 @@ int notification_init (notification_t *n, int severity, const char *message,
        n->severity = severity;
 
        if (message != NULL)
-               strncpy (n->message, message, sizeof (n->message));
+               sstrncpy (n->message, message, sizeof (n->message));
        if (host != NULL)
-               strncpy (n->host, host, sizeof (n->host));
+               sstrncpy (n->host, host, sizeof (n->host));
        if (plugin != NULL)
-               strncpy (n->plugin, plugin, sizeof (n->plugin));
+               sstrncpy (n->plugin, plugin, sizeof (n->plugin));
        if (plugin_instance != NULL)
-               strncpy (n->plugin_instance, plugin_instance,
+               sstrncpy (n->plugin_instance, plugin_instance,
                                sizeof (n->plugin_instance));
        if (type != NULL)
-               strncpy (n->type, type, sizeof (n->type));
+               sstrncpy (n->type, type, sizeof (n->type));
        if (type_instance != NULL)
-               strncpy (n->type_instance, type_instance,
+               sstrncpy (n->type_instance, type_instance,
                                sizeof (n->type_instance));
 
-       n->message[sizeof (n->message) - 1] = '\0';
-       n->host[sizeof (n->host) - 1] = '\0';
-       n->plugin[sizeof (n->plugin) - 1] = '\0';
-       n->plugin_instance[sizeof (n->plugin_instance) - 1] = '\0';
-       n->type[sizeof (n->type) - 1] = '\0';
-       n->type_instance[sizeof (n->type_instance) - 1] = '\0';
-
        return (0);
 } /* int notification_init */
+
+int walk_directory (const char *dir, dirwalk_callback_f callback)
+{
+       struct dirent *ent;
+       DIR *dh;
+       int ok = 0;
+
+       if ((dh = opendir (dir)) == NULL)
+       {
+               char errbuf[1024];
+               ERROR ("Cannot open '%s': %s", dir,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               return -1;
+       }
+
+       while ((ent = readdir (dh)) != NULL)
+       {
+               if (ent->d_name[0] == '.')
+                       continue;
+
+               if (!callback(ent->d_name))
+                       ++ok;
+       }
+
+       closedir (dh);
+
+       return ok ? 0 : -1;
+}
+
+int read_file_contents (const char *filename, char *buf, int bufsize)
+{
+       FILE *fh;
+       int n;
+
+       if ((fh = fopen (filename, "r")) == NULL)
+               return -1;
+
+       n = fread(buf, 1, bufsize, fh);
+       fclose(fh);
+
+       return n;
+}
+
+
index e99aea6..a5e2167 100644 (file)
@@ -39,6 +39,7 @@
 #define STATIC_ARRAY_SIZE(a) (sizeof (a) / sizeof (*(a)))
 
 char *sstrncpy (char *dest, const char *src, size_t n);
+int ssnprintf (char *dest, size_t n, const char *format, ...);
 char *sstrdup(const char *s);
 void *smalloc(size_t size);
 char *sstrerror (int errnum, char *buf, size_t buflen);
@@ -199,4 +200,9 @@ int notification_init (notification_t *n, int severity, const char *message,
        notification_init (n, NOTIF_FAILURE, NULL, \
                        (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
                        (ds)->type, (vl)->type_instance)
+
+typedef int (*dirwalk_callback_f)(const char *filename);
+int walk_directory (const char *dir, dirwalk_callback_f callback);
+int read_file_contents (const char *filename, char *buf, int bufsize);
+
 #endif /* COMMON_H */
index 4a9789a..2afef4f 100644 (file)
@@ -182,8 +182,7 @@ static int dispatch_global_option (const oconfig_item_t *ci)
        else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
        {
                char tmp[128];
-               snprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
-               tmp[127] = '\0';
+               ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
                return (global_option_set (ci->key, tmp));
        }
        else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
@@ -258,13 +257,13 @@ static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
                int status = -1;
 
                if (ci->values[i].type == OCONFIG_TYPE_STRING)
-                       status = snprintf (buffer_ptr, buffer_free, " %s",
+                       status = ssnprintf (buffer_ptr, buffer_free, " %s",
                                        ci->values[i].value.string);
                else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
-                       status = snprintf (buffer_ptr, buffer_free, " %lf",
+                       status = ssnprintf (buffer_ptr, buffer_free, " %lf",
                                        ci->values[i].value.number);
                else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
-                       status = snprintf (buffer_ptr, buffer_free, " %s",
+                       status = ssnprintf (buffer_ptr, buffer_free, " %s",
                                        ci->values[i].value.boolean
                                        ? "true" : "false");
 
@@ -548,7 +547,7 @@ static oconfig_item_t *cf_read_dir (const char *dir, int depth)
                if ((de->d_name[0] == '.') || (de->d_name[0] == '\0'))
                        continue;
 
-               status = snprintf (name, sizeof (name), "%s/%s",
+               status = ssnprintf (name, sizeof (name), "%s/%s",
                                dir, de->d_name);
                if (status >= sizeof (name))
                {
index 0fadc06..e9ab783 100644 (file)
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -182,12 +182,12 @@ static void submit (int cpu_num, const char *type_instance, counter_t value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin));
-       snprintf (vl.plugin_instance, sizeof (vl.type_instance),
+       ssnprintf (vl.plugin_instance, sizeof (vl.type_instance),
                        "%i", cpu_num);
-       vl.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
+       sstrncpy (vl.type, "cpu", sizeof (vl.type));
        sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("cpu", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int cpu_read (void)
index 74e542b..3738b54 100644 (file)
@@ -37,7 +37,7 @@ static int cpufreq_init (void)
 
        while (1)
        {
-               status = snprintf (filename, sizeof (filename),
+               status = ssnprintf (filename, sizeof (filename),
                                "/sys/devices/system/cpu/cpu%d/cpufreq/"
                                "scaling_cur_freq", num_cpu);
                if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
@@ -70,10 +70,11 @@ static void cpufreq_submit (int cpu_num, double value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "cpufreq", sizeof (vl.plugin));
-       snprintf (vl.type_instance, sizeof (vl.type_instance),
+       sstrncpy (vl.type, "cpufreq", sizeof (vl.type));
+       ssnprintf (vl.type_instance, sizeof (vl.type_instance),
                        "%i", cpu_num);
 
-       plugin_dispatch_values ("cpufreq", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int cpufreq_read (void)
@@ -87,7 +88,7 @@ static int cpufreq_read (void)
 
        for (i = 0; i < num_cpu; i++)
        {
-               status = snprintf (filename, sizeof (filename),
+               status = ssnprintf (filename, sizeof (filename),
                                "/sys/devices/system/cpu/cpu%d/cpufreq/"
                                "scaling_cur_freq", i);
                if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
index ff59f91..a94b700 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
@@ -45,9 +45,11 @@ static int value_list_to_string (char *buffer, int buffer_len,
        int i;
        gauge_t *rates = NULL;
 
+       assert (0 == strcmp (ds->type, vl->type));
+
        memset (buffer, '\0', buffer_len);
 
-       status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
+       status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
        if ((status < 1) || (status >= buffer_len))
                return (-1);
        offset = status;
@@ -62,7 +64,7 @@ static int value_list_to_string (char *buffer, int buffer_len,
                {
                        if (store_rates == 0)
                        {
-                               status = snprintf (buffer + offset,
+                               status = ssnprintf (buffer + offset,
                                                buffer_len - offset,
                                                ",%llu",
                                                vl->values[i].counter);
@@ -77,14 +79,14 @@ static int value_list_to_string (char *buffer, int buffer_len,
                                                        "uc_get_rate failed.");
                                        return (-1);
                                }
-                               status = snprintf (buffer + offset,
+                               status = ssnprintf (buffer + offset,
                                                buffer_len - offset,
                                                ",%lf", rates[i]);
                        }
                }
                else /* if (ds->ds[i].type == DS_TYPE_GAUGE) */
                {
-                       status = snprintf (buffer + offset, buffer_len - offset,
+                       status = ssnprintf (buffer + offset, buffer_len - offset,
                                        ",%lf", vl->values[i].gauge);
                }
 
@@ -107,37 +109,39 @@ static int value_list_to_filename (char *buffer, int buffer_len,
        int offset = 0;
        int status;
 
+       assert (0 == strcmp (ds->type, vl->type));
+
        if (datadir != NULL)
        {
-               status = snprintf (buffer + offset, buffer_len - offset,
+               status = ssnprintf (buffer + offset, buffer_len - offset,
                                "%s/", datadir);
                if ((status < 1) || (status >= buffer_len - offset))
                        return (-1);
                offset += status;
        }
 
-       status = snprintf (buffer + offset, buffer_len - offset,
+       status = ssnprintf (buffer + offset, buffer_len - offset,
                        "%s/", vl->host);
        if ((status < 1) || (status >= buffer_len - offset))
                return (-1);
        offset += status;
 
        if (strlen (vl->plugin_instance) > 0)
-               status = snprintf (buffer + offset, buffer_len - offset,
+               status = ssnprintf (buffer + offset, buffer_len - offset,
                                "%s-%s/", vl->plugin, vl->plugin_instance);
        else
-               status = snprintf (buffer + offset, buffer_len - offset,
+               status = ssnprintf (buffer + offset, buffer_len - offset,
                                "%s/", vl->plugin);
        if ((status < 1) || (status >= buffer_len - offset))
                return (-1);
        offset += status;
 
        if (strlen (vl->type_instance) > 0)
-               status = snprintf (buffer + offset, buffer_len - offset,
-                               "%s-%s", ds->type, vl->type_instance);
+               status = ssnprintf (buffer + offset, buffer_len - offset,
+                               "%s-%s", vl->type, vl->type_instance);
        else
-               status = snprintf (buffer + offset, buffer_len - offset,
-                               "%s", ds->type);
+               status = ssnprintf (buffer + offset, buffer_len - offset,
+                               "%s", vl->type);
        if ((status < 1) || (status >= buffer_len - offset))
                return (-1);
        offset += status;
@@ -242,6 +246,11 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        struct flock fl;
        int          status;
 
+       if (0 != strcmp (ds->type, vl->type)) {
+               ERROR ("csv plugin: DS type does not match value list type");
+               return -1;
+       }
+
        if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
                return (-1);
 
index 6bd9250..26d2382 100644 (file)
--- a/src/df.c
+++ b/src/df.c
@@ -128,9 +128,10 @@ static void df_submit (char *df_name,
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "df", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
-       strncpy (vl.type_instance, df_name, sizeof (vl.type_instance));
+       sstrncpy (vl.type, "df", sizeof (vl.host));
+       sstrncpy (vl.type_instance, df_name, sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("df", &vl);
+       plugin_dispatch_values (&vl);
 } /* void df_submit */
 
 static int df_read (void)
@@ -173,13 +174,13 @@ static int df_read (void)
 
                if (strcmp (mnt_ptr->dir, "/") == 0)
                {
-                       strncpy (mnt_name, "root", sizeof (mnt_name));
+                       sstrncpy (mnt_name, "root", sizeof (mnt_name));
                }
                else
                {
                        int i, len;
 
-                       strncpy (mnt_name, mnt_ptr->dir + 1, sizeof (mnt_name));
+                       sstrncpy (mnt_name, mnt_ptr->dir + 1, sizeof (mnt_name));
                        len = strlen (mnt_name);
 
                        for (i = 0; i < len; i++)
index 20afd30..29aa979 100644 (file)
@@ -215,10 +215,11 @@ static void disk_submit (const char *plugin_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "disk", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, plugin_instance,
+       sstrncpy (vl.plugin_instance, plugin_instance,
                        sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void disk_submit */
 
 #if HAVE_IOKIT_IOKITLIB_H
@@ -375,7 +376,8 @@ static int disk_read (void)
                write_tme = dict_get_value (stats_dict,
                                kIOBlockStorageDriverStatisticsTotalWriteTimeKey);
 
-               if (snprintf (disk_name, 64, "%i-%i", disk_major, disk_minor) >= 64)
+               if (ssnprintf (disk_name, sizeof (disk_name),
+                               "%i-%i", disk_major, disk_minor) >= sizeof (disk_name))
                {
                        DEBUG ("snprintf (major, minor) failed.");
                        CFRelease (child_dict);
index 69cb192..c04169f 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -301,9 +301,10 @@ static void submit_counter (const char *type, const char *type_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void submit_counter */
 
 static void submit_octets (counter_t queries, counter_t responses)
@@ -319,8 +320,9 @@ static void submit_octets (counter_t queries, counter_t responses)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
+       sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
 
-       plugin_dispatch_values ("dns_octets", &vl);
+       plugin_dispatch_values (&vl);
 } /* void submit_counter */
 
 static int dns_read (void)
index 0882ad4..e208f13 100644 (file)
@@ -390,9 +390,7 @@ static void *open_connection (void *arg)
        }
 
        addr.sun_family = AF_UNIX;
-
-       strncpy (addr.sun_path, path, (size_t)(UNIX_PATH_MAX - 1));
-       addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
+       sstrncpy (addr.sun_path, path, (size_t)(UNIX_PATH_MAX - 1));
 
        errno = 0;
        if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
@@ -655,9 +653,10 @@ static void email_submit (const char *type, const char *type_instance, gauge_t v
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "email", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void email_submit */
 
 /* Copy list l1 to list l2. l2 may partly exist already, but it is assumed
index 69ce314..2e20a67 100644 (file)
@@ -41,10 +41,9 @@ static void entropy_submit (double entropy)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "entropy", sizeof (vl.plugin));
-       sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
-       sstrncpy (vl.type_instance, "", sizeof (vl.type_instance));
+       sstrncpy (vl.type, "entropy", sizeof (vl.type));
 
-       plugin_dispatch_values ("entropy", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int entropy_read (void)
index 07c35c9..a80de1e 100644 (file)
@@ -170,10 +170,9 @@ static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
   {
     char *tmp = strrchr (ci->values[1].value.string, '/');
     if (tmp == NULL)
-      strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
+      sstrncpy (buffer, ci->values[1].value.string, sizeof (buffer));
     else
-      strncpy (buffer, tmp + 1, sizeof (buffer));
-    buffer[sizeof (buffer) - 1] = '\0';
+      sstrncpy (buffer, tmp + 1, sizeof (buffer));
   }
   pl->argv[0] = strdup (buffer);
   if (pl->argv[0] == NULL)
@@ -196,17 +195,16 @@ static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
     {
       if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
       {
-       snprintf (buffer, sizeof (buffer), "%lf",
+       ssnprintf (buffer, sizeof (buffer), "%lf",
            ci->values[i + 1].value.number);
       }
       else
       {
        if (ci->values[i + 1].value.boolean)
-         strncpy (buffer, "true", sizeof (buffer));
+         sstrncpy (buffer, "true", sizeof (buffer));
        else
-         strncpy (buffer, "false", sizeof (buffer));
+         sstrncpy (buffer, "false", sizeof (buffer));
       }
-      buffer[sizeof (buffer) - 1] = '\0';
 
       pl->argv[i] = strdup (buffer);
     }
@@ -639,7 +637,8 @@ static void *exec_read_one (void *arg) /* {{{ */
 static void *exec_notification_one (void *arg) /* {{{ */
 {
   program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
-  const notification_t *n = &((program_list_and_notification_t *) arg)->n;
+  notification_t *n = &((program_list_and_notification_t *) arg)->n;
+  notification_meta_t *meta;
   int fd;
   FILE *fh;
   int pid;
@@ -688,6 +687,21 @@ static void *exec_notification_one (void *arg) /* {{{ */
   if (strlen (n->type_instance) > 0)
     fprintf (fh, "TypeInstance: %s\n", n->type_instance);
 
+  for (meta = n->meta; meta != NULL; meta = meta->next)
+  {
+    if (meta->type == NM_TYPE_STRING)
+      fprintf (fh, "%s: %s\n", meta->name, meta->value_string);
+    else if (meta->type == NM_TYPE_SIGNED_INT)
+      fprintf (fh, "%s: %"PRIi64"\n", meta->name, meta->value_signed_int);
+    else if (meta->type == NM_TYPE_UNSIGNED_INT)
+      fprintf (fh, "%s: %"PRIu64"\n", meta->name, meta->value_unsigned_int);
+    else if (meta->type == NM_TYPE_DOUBLE)
+      fprintf (fh, "%s: %e\n", meta->name, meta->value_double);
+    else if (meta->type == NM_TYPE_BOOLEAN)
+      fprintf (fh, "%s: %s\n", meta->name,
+         meta->value_boolean ? "true" : "false");
+  }
+
   fprintf (fh, "\n%s\n", n->message);
 
   fflush (fh);
@@ -698,6 +712,7 @@ static void *exec_notification_one (void *arg) /* {{{ */
   DEBUG ("exec plugin: Child %i exited with status %i.",
       pid, status);
 
+  plugin_notification_meta_free (n);
   sfree (arg);
   pthread_exit ((void *) 0);
   return (NULL);
@@ -774,6 +789,11 @@ static int exec_notification (const notification_t *n)
     pln->pl = pl;
     memcpy (&pln->n, n, sizeof (notification_t));
 
+    /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
+     * will run into an endless loop. */
+    pln->n.meta = NULL;
+    plugin_notification_meta_copy (&pln->n, n);
+
     pthread_attr_init (&attr);
     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
     pthread_create (&t, &attr, exec_notification_one, (void *) pln);
index cb49577..9f4a725 100644 (file)
@@ -223,11 +223,10 @@ static int hddtemp_config (const char *key, const char *value)
        {
                int port = (int) (atof (value));
                if ((port > 0) && (port <= 65535))
-                       snprintf (hddtemp_port, sizeof (hddtemp_port),
+                       ssnprintf (hddtemp_port, sizeof (hddtemp_port),
                                        "%i", port);
                else
-                       strncpy (hddtemp_port, value, sizeof (hddtemp_port));
-               hddtemp_port[sizeof (hddtemp_port) - 1] = '\0';
+                       sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
        }
        else if (strcasecmp (key, "TranslateDevicename") == 0)
        {
@@ -430,7 +429,7 @@ static char *hddtemp_get_major_minor (char *drive)
        if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
                return (NULL);
 
-       if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
+       if (ssnprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
        {
                free (ret);
                return (NULL);
@@ -451,9 +450,10 @@ static void hddtemp_submit (char *type_instance, double value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.type, "temperature", sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("temperature", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int hddtemp_read (void)
index 2add89d..806a336 100644 (file)
@@ -199,9 +199,10 @@ static void if_submit (const char *dev, const char *type,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
-       strncpy (vl.type_instance, dev, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void if_submit */
 
 static int interface_read (void)
index 95e2e7c..b9cfa19 100644 (file)
@@ -73,7 +73,7 @@ static void c_ipmi_error (const char *func, int status)
 
   if (IPMI_IS_OS_ERR (status))
   {
-    sstrerror_r (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
+    sstrerror (IPMI_GET_OS_ERR (status), errbuf, sizeof (errbuf));
   }
   else if (IPMI_IS_IPMI_ERR (status))
   {
@@ -193,9 +193,10 @@ static void sensor_read_handler (ipmi_sensor_t *sensor,
 
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "ipmi", sizeof (vl.plugin));
+  sstrncpy (vl.type, type, sizeof (vl.type));
   sstrncpy (vl.type_instance, sensor_name_ptr, sizeof (vl.type_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* void sensor_read_handler */
 
 static int sensor_list_add (ipmi_sensor_t *sensor)
index ea8c65e..4d15c6e 100644 (file)
@@ -114,8 +114,7 @@ static int iptables_config (const char *key, const char *value)
                        free (value_copy);
                        return (1);
                }
-               strncpy (temp.table, table, table_len);
-               temp.table[table_len] = '\0';
+               sstrncpy (temp.table, table, table_len);
 
                chain_len = strlen (chain);
                if ((unsigned int)chain_len >= sizeof(temp.chain))
@@ -124,8 +123,7 @@ static int iptables_config (const char *key, const char *value)
                        free (value_copy);
                        return (1);
                }
-               strncpy (temp.chain, chain, chain_len);
-               temp.chain[chain_len] = '\0'; 
+               sstrncpy (temp.chain, chain, chain_len);
 
                if (fields_num >= 3)
                {
@@ -154,7 +152,7 @@ static int iptables_config (const char *key, const char *value)
                }
 
                if (fields_num >= 4)
-                   strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
+                   sstrncpy (temp.name, fields[3], sizeof (temp.name));
 
                free (value_copy);
                value_copy = NULL;
@@ -224,31 +222,32 @@ static int submit_match (const struct ipt_entry_match *match,
     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
     sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
 
-    status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
+    status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
            "%s-%s", chain->table, chain->chain);
     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
        return (0);
 
     if (chain->name[0] != '\0')
     {
-       strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
+       sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
     }
     else
     {
        if (chain->rule_type == RTYPE_NUM)
-           snprintf (vl.type_instance, sizeof (vl.type_instance),
+           ssnprintf (vl.type_instance, sizeof (vl.type_instance),
                    "%i", chain->rule.num);
        else
-           strncpy (vl.type_instance, (char *) match->data,
+           sstrncpy (vl.type_instance, (char *) match->data,
                    sizeof (vl.type_instance));
     }
-    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
 
+    sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
     values[0].counter = (counter_t) entry->counters.bcnt;
-    plugin_dispatch_values ("ipt_bytes", &vl);
+    plugin_dispatch_values (&vl);
 
+    sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
     values[0].counter = (counter_t) entry->counters.pcnt;
-    plugin_dispatch_values ("ipt_packets", &vl);
+    plugin_dispatch_values (&vl);
 
     return (0);
 } /* void submit_match */
index 31e9741..10c4d15 100644 (file)
@@ -191,7 +191,7 @@ static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size)
 
        /* inet_ntoa() returns a pointer to a statically allocated buffer
         * I hope non-glibc systems behave the same */
-       len = snprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
+       len = ssnprintf (pi, size, "%s_%s%u", inet_ntoa (addr),
                        (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP",
                        ntohs (se->port));
 
@@ -215,7 +215,7 @@ static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size)
 
        /* inet_ntoa() returns a pointer to a statically allocated buffer
         * I hope non-glibc systems behave the same */
-       len = snprintf (ti, size, "%s_%u", inet_ntoa (addr),
+       len = ssnprintf (ti, size, "%s_%u", inet_ntoa (addr),
                        ntohs (de->port));
 
        if ((0 > len) || (size <= len)) {
@@ -241,9 +241,11 @@ static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
-       sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total", sizeof (vl.type_instance));
+       sstrncpy (vl.type, "connections", sizeof (vl.type));
+       sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
+               sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("connections", &vl);
+       plugin_dispatch_values (&vl);
        return;
 } /* cipvs_submit_connections */
 
@@ -265,9 +267,11 @@ static void cipvs_submit_if (char *pi, char *t, char *ti,
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance));
-       sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total", sizeof (vl.type_instance));
+       sstrncpy (vl.type, t, sizeof (vl.type));
+       sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total",
+               sizeof (vl.type_instance));
 
-       plugin_dispatch_values (t, &vl);
+       plugin_dispatch_values (&vl);
        return;
 } /* cipvs_submit_if */
 
index 93c6076..9b7e618 100644 (file)
--- a/src/irq.c
+++ b/src/irq.c
@@ -134,13 +134,14 @@ static void irq_submit (unsigned int irq, counter_t value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "irq", sizeof (vl.plugin));
+       sstrncpy (vl.type, "irq", sizeof (vl.type));
 
-       status = snprintf (vl.type_instance, sizeof (vl.type_instance),
+       status = ssnprintf (vl.type_instance, sizeof (vl.type_instance),
                        "%u", irq);
        if ((status < 1) || ((unsigned int)status >= sizeof (vl.type_instance)))
                return;
 
-       plugin_dispatch_values ("irq", &vl);
+       plugin_dispatch_values (&vl);
 } /* void irq_submit */
 
 static int irq_read (void)
index 81d7f1c..5acff29 100644 (file)
@@ -634,7 +634,7 @@ ignore_device_match (ignorelist_t *il, const char *domname, const char *devpath)
         ERROR ("libvirt plugin: malloc failed.");
         return 0;
     }
-    snprintf (name, n, "%s:%s", domname, devpath);
+    ssnprintf (name, n, "%s:%s", domname, devpath);
     r = ignorelist_match (il, name);
     free (name);
     return r;
@@ -652,8 +652,7 @@ init_value_list (value_list_t *vl, time_t t, virDomainPtr dom)
     vl->time = t;
     vl->interval = interval_g;
 
-    strncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
-    vl->plugin[sizeof (vl->plugin) - 1] = '\0';
+    sstrncpy (vl->plugin, "libvirt", sizeof (vl->plugin));
 
     vl->host[0] = '\0';
     host_ptr = vl->host;
@@ -706,7 +705,9 @@ cpu_submit (unsigned long long cpu_time,
     vl.values = values;
     vl.values_len = 1;
 
-    plugin_dispatch_values (type, &vl);
+    sstrncpy (vl.type, type, sizeof (vl.type));
+
+    plugin_dispatch_values (&vl);
 }
 
 static void
@@ -723,10 +724,10 @@ vcpu_submit (counter_t cpu_time,
     vl.values = values;
     vl.values_len = 1;
 
-    snprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
-    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+    sstrncpy (vl.type, type, sizeof (vl.type));
+    ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%d", vcpu_nr);
 
-    plugin_dispatch_values (type, &vl);
+    plugin_dispatch_values (&vl);
 }
 
 static void
@@ -744,10 +745,10 @@ submit_counter2 (const char *type, counter_t v0, counter_t v1,
     vl.values = values;
     vl.values_len = 2;
 
-    strncpy (vl.type_instance, devname, sizeof (vl.type_instance));
-    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+    sstrncpy (vl.type, type, sizeof (vl.type));
+    sstrncpy (vl.type_instance, devname, sizeof (vl.type_instance));
 
-    plugin_dispatch_values (type, &vl);
+    plugin_dispatch_values (&vl);
 } /* void submit_counter2 */
 
 static int
index e0e0c0e..72c7756 100644 (file)
@@ -53,8 +53,9 @@ static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "load", sizeof (vl.plugin));
+       sstrncpy (vl.type, "load", sizeof (vl.type));
 
-       plugin_dispatch_values ("load", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int load_read (void)
index 36ac58d..382386b 100644 (file)
@@ -158,7 +158,7 @@ static int logfile_notification (const notification_t *n)
        int   buf_len = sizeof (buf);
        int status;
 
-       status = snprintf (buf_ptr, buf_len, "Notification: severity = %s",
+       status = ssnprintf (buf_ptr, buf_len, "Notification: severity = %s",
                        (n->severity == NOTIF_FAILURE) ? "FAILURE"
                        : ((n->severity == NOTIF_WARNING) ? "WARNING"
                                : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
@@ -170,7 +170,7 @@ static int logfile_notification (const notification_t *n)
 
 #define APPEND(bufptr, buflen, key, value) \
        if ((buflen > 0) && (strlen (value) > 0)) { \
-               int status = snprintf (bufptr, buflen, ", %s = %s", key, value); \
+               int status = ssnprintf (bufptr, buflen, ", %s = %s", key, value); \
                if (status > 0) { \
                        bufptr += status; \
                        buflen -= status; \
index 22a26da..344cadd 100644 (file)
@@ -230,9 +230,10 @@ static void mbmon_submit (const char *type, const char *type_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void mbmon_submit */
 
 /* Trim trailing whitespace from a string. */
index c830598..ddea2ea 100644 (file)
@@ -200,11 +200,10 @@ static int memcached_config (const char *key, const char *value) /* {{{ */
        } else if (strcasecmp (key, "Port") == 0) {
                int port = (int) (atof (value));
                if ((port > 0) && (port <= 65535)) {
-                       snprintf (memcached_port, sizeof (memcached_port), "%i", port);
+                       ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
                } else {
-                       strncpy (memcached_port, value, sizeof (memcached_port));
+                       sstrncpy (memcached_port, value, sizeof (memcached_port));
                }
-               memcached_port[sizeof (memcached_port) - 1] = '\0';
        } else {
                return -1;
        }
@@ -226,13 +225,11 @@ static void submit_counter (const char *type, const char *type_inst,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void memcached_submit_cmd */
 /* }}} */
 
@@ -250,13 +247,11 @@ static void submit_counter2 (const char *type, const char *type_inst,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void memcached_submit_cmd */
 /* }}} */
 
@@ -273,13 +268,11 @@ static void submit_gauge (const char *type, const char *type_inst,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 }
 /* }}} */
 
@@ -297,13 +290,11 @@ static void submit_gauge2 (const char *type, const char *type_inst,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "memcached", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 }
 /* }}} */
 
index 583f71a..c1564c8 100644 (file)
@@ -111,10 +111,10 @@ static void memory_submit (const char *type_instance, gauge_t value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "memory", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-       vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+       sstrncpy (vl.type, "memory", sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("memory", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int memory_read (void)
index c6b070a..994747f 100644 (file)
@@ -218,8 +218,9 @@ static void multimeter_submit (double value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "multimeter", sizeof (vl.plugin));
+       sstrncpy (vl.type, "multimeter", sizeof (vl.type));
 
-       plugin_dispatch_values ("multimeter", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int multimeter_read (void)
index aa585d6..312a819 100644 (file)
@@ -128,9 +128,10 @@ static void counter_submit (const char *type, const char *type_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void counter_submit */
 
 static void qcache_submit (counter_t hits, counter_t inserts,
@@ -151,8 +152,9 @@ static void qcache_submit (counter_t hits, counter_t inserts,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
+       sstrncpy (vl.type, "mysql_qcache", sizeof (vl.type));
 
-       plugin_dispatch_values ("mysql_qcache", &vl);
+       plugin_dispatch_values (&vl);
 } /* void qcache_submit */
 
 static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
@@ -171,8 +173,9 @@ static void threads_submit (gauge_t running, gauge_t connected, gauge_t cached,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
+       sstrncpy (vl.type, "mysql_threads", sizeof (vl.type));
 
-       plugin_dispatch_values ("mysql_threads", &vl);
+       plugin_dispatch_values (&vl);
 } /* void threads_submit */
 
 static void traffic_submit (counter_t rx, counter_t tx)
@@ -188,8 +191,9 @@ static void traffic_submit (counter_t rx, counter_t tx)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "mysql", sizeof (vl.plugin));
+       sstrncpy (vl.type, "mysql_octets", sizeof (vl.type));
 
-       plugin_dispatch_values ("mysql_octets", &vl);
+       plugin_dispatch_values (&vl);
 } /* void traffic_submit */
 
 static int mysql_read (void)
index e54365d..c2e4c9b 100644 (file)
@@ -173,12 +173,13 @@ static void submit_one (const char *dev, const char *type,
   vl.time = time (NULL);
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
-  strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
+  sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, type, sizeof (vl.type));
 
   if (type_instance != NULL)
-    strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+    sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* void submit_one */
 
 static void submit_two (const char *dev, const char *type,
@@ -196,16 +197,17 @@ static void submit_two (const char *dev, const char *type,
   vl.time = time (NULL);
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
-  strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
+  sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, type, sizeof (vl.type));
 
   if (type_instance != NULL)
-    strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+    sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* void submit_two */
 
 static int link_filter (const struct sockaddr_nl *sa,
-    const struct nlmsghdr *nmh, void *args)
+    struct nlmsghdr *nmh, void *args)
 {
   struct ifinfomsg *msg;
   int msg_len;
@@ -315,7 +317,7 @@ static int link_filter (const struct sockaddr_nl *sa,
 } /* int link_filter */
 
 static int qos_filter (const struct sockaddr_nl *sa,
-    const struct nlmsghdr *nmh, void *args)
+    struct nlmsghdr *nmh, void *args)
 {
   struct tcmsg *msg;
   int msg_len;
@@ -395,11 +397,10 @@ static int qos_filter (const struct sockaddr_nl *sa,
     if (strcmp (tc_type, "filter") == 0)
       numberic_id = msg->tcm_parent;
 
-    snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
+    ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
        (const char *) RTA_DATA (attrs[TCA_KIND]),
        numberic_id >> 16,
        numberic_id & 0x0000FFFF);
-    tc_inst[sizeof (tc_inst) - 1] = '\0';
   }
 
   DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
@@ -421,9 +422,8 @@ static int qos_filter (const struct sockaddr_nl *sa,
       struct gnet_stats_basic bs;
       char type_instance[DATA_MAX_NAME_LEN];
 
-      snprintf (type_instance, sizeof (type_instance), "%s-%s",
+      ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
          tc_type, tc_inst);
-      type_instance[sizeof (type_instance) - 1] = '\0';
 
       memset (&bs, '\0', sizeof (bs));
       memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
@@ -443,9 +443,8 @@ static int qos_filter (const struct sockaddr_nl *sa,
     struct tc_stats ts;
     char type_instance[DATA_MAX_NAME_LEN];
 
-    snprintf (type_instance, sizeof (type_instance), "%s-%s",
+    ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
        tc_type, tc_inst);
-    type_instance[sizeof (type_instance) - 1] = '\0';
 
     memset(&ts, '\0', sizeof (ts));
     memcpy(&ts, RTA_DATA (attrs[TCA_STATS]),
index 0f558ca..39069aa 100644 (file)
@@ -246,7 +246,7 @@ static int cache_flush (void)
        return (0);
 } /* int cache_flush */
 
-static int cache_check (const char *type, const value_list_t *vl)
+static int cache_check (const value_list_t *vl)
 {
        char key[1024];
        time_t *value = NULL;
@@ -256,7 +256,7 @@ static int cache_check (const char *type, const value_list_t *vl)
                return (-1);
 
        if (format_name (key, sizeof (key), vl->host, vl->plugin,
-                               vl->plugin_instance, type, vl->type_instance))
+                               vl->plugin_instance, vl->type, vl->type_instance))
                return (-1);
 
        pthread_mutex_lock (&cache_lock);
@@ -678,14 +678,12 @@ static int parse_packet (void *buffer, int buffer_len)
        int status;
 
        value_list_t vl = VALUE_LIST_INIT;
-       char type[DATA_MAX_NAME_LEN];
        notification_t n;
 
        DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
                        buffer, buffer_len);
 
        memset (&vl, '\0', sizeof (vl));
-       memset (&type, '\0', sizeof (type));
        memset (&n, '\0', sizeof (n));
        status = 0;
 
@@ -722,10 +720,10 @@ static int parse_packet (void *buffer, int buffer_len)
                        if ((vl.time > 0)
                                        && (strlen (vl.host) > 0)
                                        && (strlen (vl.plugin) > 0)
-                                       && (strlen (type) > 0)
-                                       && (cache_check (type, &vl) == 0))
+                                       && (strlen (vl.type) > 0)
+                                       && (cache_check (&vl) == 0))
                        {
-                               plugin_dispatch_values (type, &vl);
+                               plugin_dispatch_values (&vl);
                        }
                        else
                        {
@@ -782,9 +780,9 @@ static int parse_packet (void *buffer, int buffer_len)
                else if (pkg_type == TYPE_TYPE)
                {
                        status = parse_part_string (&buffer, &buffer_len,
-                                       type, sizeof (type));
+                                       vl.type, sizeof (vl.type));
                        if (status == 0)
-                               sstrncpy (n.type, type, sizeof (n.type));
+                               sstrncpy (n.type, vl.type, sizeof (n.type));
                }
                else if (pkg_type == TYPE_TYPE_INSTANCE)
                {
@@ -1441,12 +1439,12 @@ static int add_to_buffer (char *buffer, int buffer_size,
                sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
        }
 
-       if (strcmp (type_def, ds->type) != 0)
+       if (strcmp (type_def, vl->type) != 0)
        {
                if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
-                                       ds->type, strlen (ds->type)) != 0)
+                                       vl->type, strlen (vl->type)) != 0)
                        return (-1);
-               sstrncpy (type_def, ds->type, sizeof (type_def));
+               sstrncpy (type_def, vl->type, sizeof (type_def));
        }
 
        if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
@@ -1483,7 +1481,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl)
        /* If the value is already in the cache, we have received it via the
         * network. We write it again if forwarding is activated. It's then in
         * the cache and should we receive it again we will ignore it. */
-       status = cache_check (ds->type, vl);
+       status = cache_check (vl);
        if ((network_config_forward == 0)
                        && (status != 0))
                return (0);
@@ -1767,7 +1765,14 @@ static int network_init (void)
        return (0);
 } /* int network_init */
 
-static int network_flush (int timeout)
+/* 
+ * The flush option of the network plugin cannot flush individual identifiers.
+ * All the values are added to a buffer and sent when the buffer is full, the
+ * requested value may or may not be in there, it's not worth finding out. We
+ * just send the buffer if `flush'  is called - if the requested value was in
+ * there, good. If not, well, then there is nothing to flush.. -octo
+ */
+static int network_flush (int timeout, const char *identifier)
 {
        pthread_mutex_lock (&send_buffer_lock);
 
index 91e6788..f2db895 100644 (file)
--- a/src/nfs.c
+++ b/src/nfs.c
@@ -190,18 +190,19 @@ static void nfs_procedures_submit (const char *plugin_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "nfs", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, plugin_instance,
+       sstrncpy (vl.plugin_instance, plugin_instance,
                        sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, "nfs_procedure", sizeof (vl.type));
 
        for (i = 0; i < len; i++)
        {
                values[0].counter = val[i];
-               strncpy (vl.type_instance, names[i],
+               sstrncpy (vl.type_instance, names[i],
                                sizeof (vl.type_instance));
                DEBUG ("%s-%s/nfs_procedure-%s = %llu",
                                vl.plugin, vl.plugin_instance,
                                vl.type_instance, val[i]);
-               plugin_dispatch_values ("nfs_procedure", &vl);
+               plugin_dispatch_values (&vl);
        }
 } /* void nfs_procedures_submit */
 
@@ -240,9 +241,8 @@ static void nfs_read_stats_file (FILE *fh, char *inst)
                                continue;
                        }
 
-                       snprintf (plugin_instance, sizeof (plugin_instance),
+                       ssnprintf (plugin_instance, sizeof (plugin_instance),
                                        "v2%s", inst);
-                       plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
 
                        values = (unsigned long long *) malloc (nfs2_procedures_names_num * sizeof (unsigned long long));
                        if (values == NULL)
@@ -277,9 +277,8 @@ static void nfs_read_stats_file (FILE *fh, char *inst)
                                continue;
                        }
 
-                       snprintf (plugin_instance, sizeof (plugin_instance),
+                       ssnprintf (plugin_instance, sizeof (plugin_instance),
                                        "v3%s", inst);
-                       plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
 
                        values = (unsigned long long *) malloc (nfs3_procedures_names_num * sizeof (unsigned long long));
                        if (values == NULL)
index 283e8f6..91bcf75 100644 (file)
@@ -122,7 +122,8 @@ static int init (void)
 
   if (user != NULL)
   {
-    if (snprintf (credentials, 1024, "%s:%s", user, pass == NULL ? "" : pass) >= 1024)
+    if (ssnprintf (credentials, sizeof (credentials),
+         "%s:%s", user, pass == NULL ? "" : pass) >= sizeof (credentials))
     {
       ERROR ("nginx plugin: Credentials would have been truncated.");
       return (-1);
@@ -180,14 +181,12 @@ static void submit (char *type, char *inst, long long value)
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "nginx", sizeof (vl.plugin));
   sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, type, sizeof (vl.type));
 
   if (inst != NULL)
-  {
-    strncpy (vl.type_instance, inst, sizeof (vl.type_instance));
-    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-  }
+    sstrncpy (vl.type_instance, inst, sizeof (vl.type_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* void submit */
 
 static int nginx_read (void)
diff --git a/src/notify_desktop.c b/src/notify_desktop.c
new file mode 100644 (file)
index 0000000..2e62e88
--- /dev/null
@@ -0,0 +1,161 @@
+/**
+ * collectd - src/notify_desktop.c
+ * Copyright (C) 2008  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>
+ **/
+
+/*
+ * This plugin sends desktop notifications to a notification daemon.
+ */
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
+
+#include <glib.h>
+#include <libnotify/notify.h>
+
+#define log_info(...) INFO ("notify_desktop: " __VA_ARGS__)
+#define log_warn(...) WARNING ("notify_desktop: " __VA_ARGS__)
+#define log_err(...) ERROR ("notify_desktop: " __VA_ARGS__)
+
+#define DEFAULT_TIMEOUT 5000
+
+static int okay_timeout = DEFAULT_TIMEOUT;
+static int warn_timeout = DEFAULT_TIMEOUT;
+static int fail_timeout = DEFAULT_TIMEOUT;
+
+static int set_timeout (oconfig_item_t *ci, int *timeout)
+{
+       if ((0 != ci->children_num) || (1 != ci->values_num)
+                       || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) {
+               log_err ("%s expects a single number argument.", ci->key);
+               return 1;
+       }
+
+       *timeout = (int)ci->values[0].value.number;
+       if (0 > *timeout)
+               *timeout = DEFAULT_TIMEOUT;
+       return 0;
+} /* set_timeout */
+
+static int c_notify_config (oconfig_item_t *ci)
+{
+       int i = 0;
+
+       for (i = 0; i < ci->children_num; ++i) {
+               oconfig_item_t *c = ci->children + i;
+
+               if (0 == strcasecmp (c->key, "OkayTimeout"))
+                       set_timeout (c, &okay_timeout);
+               else if (0 == strcasecmp (c->key, "WarningTimeout"))
+                       set_timeout (c, &warn_timeout);
+               else if (0 == strcasecmp (c->key, "FailureTimeout"))
+                       set_timeout (c, &fail_timeout);
+       }
+       return 0;
+} /* c_notify_config */
+
+static int c_notify (const notification_t *n)
+{
+       NotifyNotification *notification = NULL;
+       NotifyUrgency       urgency      = NOTIFY_URGENCY_LOW;
+       int                 timeout      = okay_timeout;
+
+       char summary[1024];
+
+       if (NOTIF_WARNING == n->severity) {
+               urgency = NOTIFY_URGENCY_NORMAL;
+               timeout = warn_timeout;
+       }
+       else if (NOTIF_FAILURE == n->severity) {
+               urgency = NOTIFY_URGENCY_CRITICAL;
+               timeout = fail_timeout;
+       }
+
+       ssnprintf (summary, sizeof (summary), "collectd %s notification",
+                       (NOTIF_FAILURE == n->severity) ? "FAILURE"
+                               : (NOTIF_WARNING == n->severity) ? "WARNING"
+                               : (NOTIF_OKAY == n->severity) ? "OKAY" : "UNKNOWN");
+
+       notification = notify_notification_new (summary, n->message, NULL, NULL);
+       if (NULL == notification) {
+               log_err ("Failed to create a new notification.");
+               return -1;
+       }
+
+       notify_notification_set_urgency (notification, urgency);
+       notify_notification_set_timeout (notification, timeout);
+
+       if (! notify_notification_show (notification, NULL))
+               log_err ("Failed to display notification.");
+
+       g_object_unref (G_OBJECT (notification));
+       return 0;
+} /* c_notify */
+
+static int c_notify_shutdown (void)
+{
+       plugin_unregister_init ("notify_desktop");
+       plugin_unregister_notification ("notify_desktop");
+       plugin_unregister_shutdown ("notify_desktop");
+
+       if (notify_is_initted ())
+               notify_uninit ();
+       return 0;
+} /* c_notify_shutdown */
+
+static int c_notify_init (void)
+{
+       char *name         = NULL;
+       char *vendor       = NULL;
+       char *version      = NULL;
+       char *spec_version = NULL;
+
+       if (! notify_init (PACKAGE_STRING)) {
+               log_err ("Failed to initialize libnotify.");
+               return -1;
+       }
+
+       if (! notify_get_server_info (&name, &vendor, &version, &spec_version))
+               log_warn ("Failed to get the notification server info. "
+                               "Check if you have a notification daemon running.");
+       else {
+               log_info ("Found notification daemon: %s (%s) %s (spec version %s)",
+                               name, vendor, version, spec_version);
+               free (name);
+               free (vendor);
+               free (version);
+               free (spec_version);
+       }
+
+       plugin_register_notification ("notify_desktop", c_notify);
+       plugin_register_shutdown ("notify_desktop", c_notify_shutdown);
+       return 0;
+} /* c_notify_init */
+
+void module_register (void)
+{
+       plugin_register_complex_config ("notify_desktop", c_notify_config);
+       plugin_register_init ("notify_desktop", c_notify_init);
+       return;
+} /* module_register */
+
+/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
+
diff --git a/src/notify_email.c b/src/notify_email.c
new file mode 100644 (file)
index 0000000..dc4e47f
--- /dev/null
@@ -0,0 +1,287 @@
+/**
+ * collectd - src/notify_email.c
+ * Copyright (C) 2008  Oleg King
+ *
+ * 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
+ *
+ * Authors:
+ *   Oleg King <king2 at kaluga.ru>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#include <auth-client.h>
+#include <libesmtp.h>
+
+#define MAXSTRING               256
+
+static const char *config_keys[] =
+{
+  "SMTPServer",
+  "SMTPPort",
+  "SMTPUser",
+  "SMTPPassword",
+  "From",
+  "Recipient",
+  "Subject"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+static char **recipients;
+static int recipients_len = 0;
+
+static smtp_session_t session;
+static smtp_message_t message;
+static auth_context_t authctx = NULL;
+
+static int smtp_port = 25;
+static char *smtp_host = NULL;
+static char *smtp_user = NULL;
+static char *smtp_password = NULL;
+static char *email_from = NULL;
+static char *email_subject = NULL;
+
+#define DEFAULT_SMTP_HOST      "localhost"
+#define DEFAULT_SMTP_FROM      "root@localhost"
+#define DEFAULT_SMTP_SUBJECT   "Collectd notify: %s@%s"
+
+/* Callback to get username and password */
+static int authinteract (auth_client_request_t request, char **result,
+    int fields, void *arg)
+{               
+  int i;
+  for (i = 0; i < fields; i++)
+  {
+    if (request[i].flags & AUTH_USER)
+      result[i] = smtp_user;
+    else if (request[i].flags & AUTH_PASS)
+      result[i] = smtp_password;
+    else
+      return 0;
+  }
+  return 1;
+} /* int authinteract */
+
+/* Callback to print the recipient status */
+static void print_recipient_status (smtp_recipient_t recipient,
+    const char *mailbox, void *arg)
+{
+  const smtp_status_t *status;
+
+  status = smtp_recipient_status (recipient);
+  if (status->text[strlen(status->text) - 2] == '\r')
+    status->text[strlen(status->text) - 2] = 0;
+  INFO ("notify_email: notify sent to %s: %d %s", mailbox, status->code,
+      status->text);
+} /* void print_recipient_status */
+
+/* Callback to monitor SMTP activity */
+static void monitor_cb (const char *buf, int buflen, int writing, void *arg)
+{
+  char log_str[MAXSTRING];
+
+  sstrncpy (log_str, buf, sizeof (log_str));
+  if (buflen > 2)
+    log_str[buflen - 2] = 0; /* replace \n with \0 */
+
+  if (writing == SMTP_CB_HEADERS) {
+    DEBUG ("notify_email plugin: SMTP --- H: %s", log_str);
+    return;
+  }
+  DEBUG (writing
+      ? "notify_email plugin: SMTP >>> C: %s"
+      : "notify_email plugin: SMTP <<< S: %s",
+      log_str);
+} /* void monitor_cb */
+
+static int notify_email_init (void)
+{
+  char server[MAXSTRING];
+
+  auth_client_init();
+  if (!(session = smtp_create_session ())) {
+    ERROR ("notify_email plugin: cannot create SMTP session");
+    return (-1);
+  }
+
+  smtp_set_monitorcb (session, monitor_cb, NULL, 1);
+  smtp_set_hostname (session, hostname_g);
+  ssnprintf(server, sizeof (server), "%s:%i",
+      (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host,
+      smtp_port);
+  smtp_set_server (session, server);
+
+  if (smtp_user && smtp_password) {
+    authctx = auth_create_context ();
+    auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
+    auth_set_interact_cb (authctx, authinteract, NULL);
+  }
+
+  if ( !smtp_auth_set_context (session, authctx)) {
+    ERROR ("notify_email plugin: cannot set SMTP auth context");
+    return (-1);   
+  }
+
+  return (0);
+} /* int notify_email_init */
+
+static int notify_email_shutdown (void)
+{
+  smtp_destroy_session (session);
+  auth_destroy_context (authctx);
+  auth_client_exit();
+  return (0);
+} /* int notify_email_shutdown */
+
+static int notify_email_config (const char *key, const char *value)
+{
+  if (strcasecmp (key, "Recipient") == 0)
+  {
+    char **tmp;
+
+    tmp = (char **) realloc ((void *) recipients, (recipients_len + 1) * sizeof (char *));
+    if (tmp == NULL) {
+      ERROR ("notify_email: realloc failed.");
+      return (-1);
+    }
+
+    recipients = tmp;
+    recipients[recipients_len] = strdup (value);
+    if (recipients[recipients_len] == NULL) {
+      ERROR ("notify_email: strdup failed.");
+      return (-1);
+    }
+    recipients_len++;
+  }
+  else if (0 == strcasecmp (key, "SMTPServer")) {
+    sfree (smtp_host);
+    smtp_host = strdup (value);
+  }
+  else if (0 == strcasecmp (key, "SMTPPort")) {
+    int port_tmp = atoi (value);
+    if (port_tmp < 1 || port_tmp > 65535)
+    {
+      WARNING ("notify_email plugin: Invalid SMTP port: %i", port_tmp);
+      return (1);
+    }
+    smtp_port = port_tmp;
+  }
+  else if (0 == strcasecmp (key, "SMTPUser")) {
+    sfree (smtp_user);
+    smtp_user = strdup (value);
+  }
+  else if (0 == strcasecmp (key, "SMTPPassword")) {
+    sfree (smtp_password);
+    smtp_password = strdup (value);
+  }
+  else if (0 == strcasecmp (key, "From")) {
+    sfree (email_from);
+    email_from = strdup (value);
+  }
+  else if (0 == strcasecmp (key, "Subject")) {
+    sfree (email_subject);
+    email_subject = strdup (value);
+  }
+  else {
+    return -1;
+  }
+  return 0;
+} /* int notify_email_config (const char *, const char *) */
+
+static int notify_email_notification (const notification_t *n)
+{
+  smtp_recipient_t recipient;
+
+  struct tm timestamp_tm;
+  char timestamp_str[64];
+
+  char severity[32];
+  char subject[MAXSTRING];
+
+  char buf[4096] = "";
+  int  buf_len = sizeof (buf);
+  int i;
+
+  ssnprintf (severity, sizeof (severity), "%s",
+      (n->severity == NOTIF_FAILURE) ? "FAILURE"
+      : ((n->severity == NOTIF_WARNING) ? "WARNING"
+        : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
+
+  ssnprintf (subject, sizeof (subject),
+      (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject,
+      severity, n->host);
+
+  localtime_r (&n->time, &timestamp_tm);
+  strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
+      &timestamp_tm);
+  timestamp_str[sizeof (timestamp_str) - 1] = '\0';
+
+  /* Let's make RFC822 message text with \r\n EOLs */
+  ssnprintf (buf, buf_len,
+      "MIME-Version: 1.0\r\n"
+      "Content-Type: text/plain;\r\n"
+      "Content-Transfer-Encoding: 8bit\r\n"
+      "Subject: %s\r\n"
+      "\r\n"
+      "%s - %s@%s\r\n"
+      "\r\n"
+      "Message: %s",
+      subject,
+      timestamp_str,
+      severity,
+      n->host,
+      n->message);
+
+  if (!(message = smtp_add_message (session))) {
+    ERROR ("notify_email plugin: cannot set SMTP message");
+    return (-1);   
+  }
+  smtp_set_reverse_path (message, email_from);
+  smtp_set_header (message, "To", NULL, NULL);
+  smtp_set_message_str (message, buf);
+
+  for (i = 0; i < recipients_len; i++)
+    recipient = smtp_add_recipient (message, recipients[i]);
+
+  /* Initiate a connection to the SMTP server and transfer the message. */
+  if (!smtp_start_session (session)) {
+    char buf[MAXSTRING];
+    ERROR ("notify_email plugin: SMTP server problem: %s",
+        smtp_strerror (smtp_errno (), buf, sizeof buf));
+    return (-1);
+  } else {
+    const smtp_status_t *status;
+    /* Report on the success or otherwise of the mail transfer. */
+    status = smtp_message_transfer_status (message);
+    DEBUG ("notify_email plugin: SMTP server report: %d %s",
+        status->code, (status->text != NULL) ? status->text : "\n");
+    smtp_enumerate_recipients (message, print_recipient_status, NULL);
+  }
+
+  return (0);
+} /* int notify_email_notification */
+
+void module_register (void)
+{
+  plugin_register_init ("notify_email", notify_email_init);
+  plugin_register_shutdown ("notify_email", notify_email_shutdown);
+  plugin_register_config ("notify_email", notify_email_config,
+      config_keys, config_keys_num);
+  plugin_register_notification ("notify_email", notify_email_notification);
+} /* void module_register (void) */
+
+/* vim: set sw=2 sts=2 ts=8 et : */
index b6214e8..1a0a07d 100644 (file)
@@ -269,11 +269,10 @@ static int ntpd_config (const char *key, const char *value)
        {
                int port = (int) (atof (value));
                if ((port > 0) && (port <= 65535))
-                       snprintf (ntpd_port, sizeof (ntpd_port),
+                       ssnprintf (ntpd_port, sizeof (ntpd_port),
                                        "%i", port);
                else
-                       strncpy (ntpd_port, value, sizeof (ntpd_port));
-               ntpd_port[sizeof (ntpd_port) - 1] = '\0';
+                       sstrncpy (ntpd_port, value, sizeof (ntpd_port));
        }
        else if (strcasecmp (key, "ReverseLookups") == 0)
        {
@@ -305,9 +304,10 @@ static void ntpd_submit (char *type, char *type_inst, double value)
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "ntpd", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
-       strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 }
 
 /* returns `tv0 - tv1' in milliseconds or 0 if `tv1 > tv0' */
@@ -874,7 +874,7 @@ static int ntpd_read (void)
 
                        if (refclock_id < refclock_names_num)
                        {
-                               strncpy (peername, refclock_names[refclock_id],
+                               sstrncpy (peername, refclock_names[refclock_id],
                                                sizeof (peername));
                        }
                        else
@@ -883,7 +883,7 @@ static int ntpd_read (void)
                                addr_obj.s_addr = ptr->srcadr;
                                addr_str = inet_ntoa (addr_obj);
 
-                               strncpy (peername, addr_str, sizeof (peername));
+                               sstrncpy (peername, addr_str, sizeof (peername));
                        }
                }
                else /* Normal network host. */
index 695d93b..8796d58 100644 (file)
--- a/src/nut.c
+++ b/src/nut.c
@@ -124,20 +124,17 @@ static void nut_submit (nut_ups_t *ups, const char *type,
   vl.values = values;
   vl.values_len = STATIC_ARRAY_SIZE (values);
   vl.time = time (NULL);
-  strncpy (vl.host,
+  sstrncpy (vl.host,
       (strcasecmp (ups->hostname, "localhost") == 0)
       ? hostname_g
       : ups->hostname,
       sizeof (vl.host));
   sstrncpy (vl.plugin, "nut", sizeof (vl.plugin));
-  strncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
-  strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+  sstrncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, type, sizeof (vl.type));
+  sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-  vl.host[sizeof (vl.host) - 1] = '\0';
-  vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
-  vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* void nut_submit */
 
 static int nut_read_one (nut_ups_t *ups)
index 597368f..541826f 100644 (file)
@@ -210,8 +210,7 @@ static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
                return -1;
 
        if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
-               strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
-               ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
+               sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
        }
        else {
                log_err ("hv2data_source: No DS name given.");
@@ -373,6 +372,10 @@ static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
                                newSVpv (vl->plugin_instance, 0), 0))
                        return -1;
 
+       if ('\0' != vl->type[0])
+               if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
+                       return -1;
+
        if ('\0' != vl->type_instance[0])
                if (NULL == hv_store (hash, "type_instance", 13,
                                newSVpv (vl->type_instance, 0), 0))
@@ -424,12 +427,11 @@ static int notification2hv (pTHX_ notification_t *n, HV *hash)
 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
        int status = 0;
        if (base_name[0] == '\0')
-               status = snprintf (buf, buf_len, "%s", module);
+               status = ssnprintf (buf, buf_len, "%s", module);
        else
-               status = snprintf (buf, buf_len, "%s::%s", base_name, module);
+               status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
        if ((status < 0) || ((unsigned int)status >= buf_len))
                return (NULL);
-       buf[buf_len - 1] = '\0';
        return (buf);
 } /* char *get_module_name */
 
@@ -475,8 +477,7 @@ static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
                                ds[i].name, ds[i].type, ds[i].min, ds[i].max);
        }
 
-       strncpy (set->type, name, DATA_MAX_NAME_LEN);
-       set->type[DATA_MAX_NAME_LEN - 1] = '\0';
+       sstrncpy (set->type, name, sizeof (set->type));
 
        set->ds_num = len + 1;
        set->ds = ds;
@@ -511,7 +512,7 @@ static int pplugin_unregister_data_set (char *name)
  *   type_instance   => $tinstance,
  * }
  */
-static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
+static int pplugin_dispatch_values (pTHX_ HV *values)
 {
        value_list_t list = VALUE_LIST_INIT;
        value_t      *val = NULL;
@@ -520,8 +521,15 @@ static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
 
        int ret = 0;
 
-       if ((NULL == name) || (NULL == values))
+       if (NULL == values)
+               return -1;
+
+       if (NULL == (tmp = hv_fetch (values, "type", 4, 0))) {
+               log_err ("pplugin_dispatch_values: No type given.");
                return -1;
+       }
+
+       sstrncpy (list.type, SvPV_nolen (*tmp), sizeof (list.type));
 
        if ((NULL == (tmp = hv_fetch (values, "values", 6, 0)))
                        || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
@@ -538,7 +546,8 @@ static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
 
                val = (value_t *)smalloc (len * sizeof (value_t));
 
-               list.values_len = av2value (aTHX_ name, (AV *)SvRV (*tmp), val, len);
+               list.values_len = av2value (aTHX_ list.type, (AV *)SvRV (*tmp),
+                               val, len);
                list.values = val;
 
                if (-1 == list.values_len) {
@@ -555,29 +564,24 @@ static int pplugin_dispatch_values (pTHX_ char *name, HV *values)
        }
 
        if (NULL != (tmp = hv_fetch (values, "host", 4, 0))) {
-               strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
-               list.host[DATA_MAX_NAME_LEN - 1] = '\0';
+               sstrncpy (list.host, SvPV_nolen (*tmp), sizeof (list.host));
        }
        else {
                sstrncpy (list.host, hostname_g, sizeof (list.host));
        }
 
-       if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0))) {
-               strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
-               list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
-       }
+       if (NULL != (tmp = hv_fetch (values, "plugin", 6, 0)))
+               sstrncpy (list.plugin, SvPV_nolen (*tmp), sizeof (list.plugin));
 
-       if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0))) {
-               strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
-               list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
-       }
+       if (NULL != (tmp = hv_fetch (values, "plugin_instance", 15, 0)))
+               sstrncpy (list.plugin_instance, SvPV_nolen (*tmp),
+                               sizeof (list.plugin_instance));
 
-       if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0))) {
-               strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
-               list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
-       }
+       if (NULL != (tmp = hv_fetch (values, "type_instance", 13, 0)))
+               sstrncpy (list.type_instance, SvPV_nolen (*tmp),
+                               sizeof (list.type_instance));
 
-       ret = plugin_dispatch_values (name, &list);
+       ret = plugin_dispatch_values (&list);
 
        sfree (val);
        return ret;
@@ -620,31 +624,25 @@ static int pplugin_dispatch_notification (pTHX_ HV *notif)
                n.time = time (NULL);
 
        if (NULL != (tmp = hv_fetch (notif, "message", 7, 0)))
-               strncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
-       n.message[sizeof (n.message) - 1] = '\0';
+               sstrncpy (n.message, SvPV_nolen (*tmp), sizeof (n.message));
 
        if (NULL != (tmp = hv_fetch (notif, "host", 4, 0)))
-               strncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
+               sstrncpy (n.host, SvPV_nolen (*tmp), sizeof (n.host));
        else
-               strncpy (n.host, hostname_g, sizeof (n.host));
-       n.host[sizeof (n.host) - 1] = '\0';
+               sstrncpy (n.host, hostname_g, sizeof (n.host));
 
        if (NULL != (tmp = hv_fetch (notif, "plugin", 6, 0)))
-               strncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
-       n.plugin[sizeof (n.plugin) - 1] = '\0';
+               sstrncpy (n.plugin, SvPV_nolen (*tmp), sizeof (n.plugin));
 
        if (NULL != (tmp = hv_fetch (notif, "plugin_instance", 15, 0)))
-               strncpy (n.plugin_instance, SvPV_nolen (*tmp),
+               sstrncpy (n.plugin_instance, SvPV_nolen (*tmp),
                                sizeof (n.plugin_instance));
-       n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
 
        if (NULL != (tmp = hv_fetch (notif, "type", 4, 0)))
-               strncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
-       n.type[sizeof (n.type) - 1] = '\0';
+               sstrncpy (n.type, SvPV_nolen (*tmp), sizeof (n.type));
 
        if (NULL != (tmp = hv_fetch (notif, "type_instance", 13, 0)))
-               strncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
-       n.type_instance[sizeof (n.type_instance) - 1] = '\0';
+               sstrncpy (n.type_instance, SvPV_nolen (*tmp), sizeof (n.type_instance));
        return plugin_dispatch_notification (&n);
 } /* static int pplugin_dispatch_notification (HV *) */
 
@@ -693,6 +691,7 @@ static int pplugin_call_all (pTHX_ int type, ...)
                 *   time   => $time,
                 *   host   => $hostname,
                 *   plugin => $plugin,
+                *   type   => $type,
                 *   plugin_instance => $instance,
                 *   type_instance   => $type_instance
                 * };
@@ -868,33 +867,43 @@ static XS (Collectd_plugin_unregister_ds)
  */
 static XS (Collectd_plugin_dispatch_values)
 {
-       SV *values = NULL;
+       SV *values     = NULL;
+       int values_idx = 0;
 
        int ret = 0;
 
        dXSARGS;
 
-       if (2 != items) {
-               log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
+       if (2 == items) {
+               log_warn ("Collectd::plugin_dispatch_values with two arguments "
+                               "is deprecated - pass the type through values->{type}.");
+               values_idx = 1;
+       }
+       else if (1 != items) {
+               log_err ("Usage: Collectd::plugin_dispatch_values(values)");
                XSRETURN_EMPTY;
        }
 
-       log_debug ("Collectd::plugin_dispatch_values: "
-                       "name = \"%s\", values=\"%s\"",
-                       SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
+       log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
+                       SvPV_nolen (ST (values_idx)));
 
-       values = ST (1);
+       values = ST (values_idx);
 
        if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
                log_err ("Collectd::plugin_dispatch_values: Invalid values.");
                XSRETURN_EMPTY;
        }
 
-       if ((NULL == ST (0)) || (NULL == values))
+       if (((2 == items) && (NULL == ST (0))) || (NULL == values))
                XSRETURN_EMPTY;
 
-       ret = pplugin_dispatch_values (aTHX_ SvPV_nolen (ST (0)),
-                       (HV *)SvRV (values));
+       if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
+                       newSVsv (ST (0)), 0))) {
+               log_err ("Collectd::plugin_dispatch_values: Could not store type.");
+               XSRETURN_EMPTY;
+       }
+
+       ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
 
        if (0 == ret)
                XSRETURN_YES;
@@ -1255,7 +1264,8 @@ static int perl_notify (const notification_t *notif)
        return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
 } /* static int perl_notify (const notification_t *) */
 
-static int perl_flush (const int timeout)
+/* TODO: Implement flushing of single identifiers. */
+static int perl_flush (int timeout, const char *identifier)
 {
        dTHX;
 
@@ -1352,8 +1362,7 @@ static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
 {
        char *pv = mg->mg_ptr;
-       strncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
-       pv[DATA_MAX_NAME_LEN - 1] = '\0';
+       sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
        return 0;
 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
 
@@ -1554,8 +1563,7 @@ static int perl_config_basename (pTHX_ oconfig_item_t *ci)
        value = ci->values[0].value.string;
 
        log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
-       strncpy (base_name, value, sizeof (base_name));
-       base_name[sizeof (base_name) - 1] = '\0';
+       sstrncpy (base_name, value, sizeof (base_name));
        return 0;
 } /* static int perl_config_basename (oconfig_item_it *) */
 
index 09de77c..ac5b0f8 100644 (file)
@@ -189,9 +189,10 @@ static void ping_submit (char *host, double latency)
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "ping", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
-       strncpy (vl.type_instance, host, sizeof (vl.type_instance));
+       sstrncpy (vl.type_instance, host, sizeof (vl.type_instance));
+       sstrncpy (vl.type, "ping", sizeof (vl.type));
 
-       plugin_dispatch_values ("ping", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int ping_read (void)
index e611b8c..bf8fb08 100644 (file)
@@ -21,6 +21,7 @@
  **/
 
 #include "collectd.h"
+#include "utils_complain.h"
 
 #include <ltdl.h>
 
@@ -328,7 +329,8 @@ int plugin_load (const char *type)
 
        /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
         * type when matching the filename */
-       if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
+       if (ssnprintf (typename, sizeof (typename),
+                       "%s.so", type) >= sizeof (typename))
        {
                WARNING ("snprintf: truncated: `%s.so'", type);
                return (-1);
@@ -348,7 +350,8 @@ int plugin_load (const char *type)
                if (strncasecmp (de->d_name, typename, typename_len))
                        continue;
 
-               if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
+               if (ssnprintf (filename, sizeof (filename),
+                               "%s/%s", dir, de->d_name) >= sizeof (filename))
                {
                        WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
                        continue;
@@ -439,7 +442,8 @@ int plugin_register_write (const char *name,
        return (register_callback (&list_write, name, (void *) callback));
 } /* int plugin_register_write */
 
-int plugin_register_flush (const char *name, int (*callback) (const int))
+int plugin_register_flush (const char *name,
+               int (*callback) (const int timeout, const char *identifier))
 {
        return (register_callback (&list_flush, name, (void *) callback));
 } /* int plugin_register_flush */
@@ -698,6 +702,29 @@ void plugin_flush_all (int timeout)
        }
 } /* void plugin_flush_all */
 
+int plugin_flush (const char *plugin, int timeout, const char *identifier)
+{
+  int (*callback) (int timeout, const char *identifier);
+  llentry_t *le;
+
+  if (list_flush == NULL)
+    return (0);
+
+  le = llist_head (list_flush);
+  while (le != NULL)
+  {
+    if ((plugin != NULL)
+       && (strcmp (plugin, le->key) != 0))
+      continue;
+
+    callback = (int (*) (int, const char *)) le->value;
+    le = le->next;
+
+    (*callback) (timeout, identifier);
+  }
+  return (0);
+} /* int plugin_flush */
+
 void plugin_shutdown_all (void)
 {
        int (*callback) (void);
@@ -723,20 +750,25 @@ void plugin_shutdown_all (void)
        }
 } /* void plugin_shutdown_all */
 
-int plugin_dispatch_values (const char *name, value_list_t *vl)
+int plugin_dispatch_values (value_list_t *vl)
 {
+       static c_complain_t no_write_complaint = C_COMPLAIN_INIT;
+
        int (*callback) (const data_set_t *, const value_list_t *);
        data_set_t *ds;
        llentry_t *le;
 
-       if (list_write == NULL)
-       {
-               ERROR ("plugin_dispatch_values: No write callback has been "
-                               "registered. Please load at least one plugin "
-                               "that provides a write function.");
+       if ((vl == NULL) || (*vl->type == '\0')) {
+               ERROR ("plugin_dispatch_values: Invalid value list.");
                return (-1);
        }
 
+       if (list_write == NULL)
+               c_complain_once (LOG_WARNING, &no_write_complaint,
+                               "plugin_dispatch_values: No write callback has been "
+                               "registered. Please load at least one output plugin, "
+                               "if you want the collected data to be stored.");
+
        if (data_sets == NULL)
        {
                ERROR ("plugin_dispatch_values: No data sets registered. "
@@ -745,9 +777,9 @@ int plugin_dispatch_values (const char *name, value_list_t *vl)
                return (-1);
        }
 
-       if (c_avl_get (data_sets, name, (void *) &ds) != 0)
+       if (c_avl_get (data_sets, vl->type, (void *) &ds) != 0)
        {
-               INFO ("plugin_dispatch_values: Dataset not found: %s", name);
+               INFO ("plugin_dispatch_values: Dataset not found: %s", vl->type);
                return (-1);
        }
 
@@ -758,7 +790,15 @@ int plugin_dispatch_values (const char *name, value_list_t *vl)
                        (unsigned int) vl->time, vl->interval,
                        vl->host,
                        vl->plugin, vl->plugin_instance,
-                       ds->type, vl->type_instance);
+                       vl->type, vl->type_instance);
+
+#if COLLECT_DEBUG
+       assert (0 == strcmp (ds->type, vl->type));
+#else
+       if (0 != strcmp (ds->type, vl->type))
+               WARNING ("plugin_dispatch_values: (ds->type = %s) != (vl->type = %s)",
+                               ds->type, vl->type);
+#endif
 
 #if COLLECT_DEBUG
        assert (ds->ds_num == vl->values_len);
@@ -776,6 +816,7 @@ int plugin_dispatch_values (const char *name, value_list_t *vl)
        escape_slashes (vl->host, sizeof (vl->host));
        escape_slashes (vl->plugin, sizeof (vl->plugin));
        escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance));
+       escape_slashes (vl->type, sizeof (vl->type));
        escape_slashes (vl->type_instance, sizeof (vl->type_instance));
 
        /* Update the value cache */
@@ -864,3 +905,179 @@ const data_set_t *plugin_get_ds (const char *name)
 
        return (ds);
 } /* data_set_t *plugin_get_ds */
+
+static int plugin_notification_meta_add (notification_t *n,
+               const char *name,
+               enum notification_meta_type_e type,
+               const void *value)
+{
+  notification_meta_t *meta;
+  notification_meta_t *tail;
+
+  if ((n == NULL) || (name == NULL) || (value == NULL))
+  {
+    ERROR ("plugin_notification_meta_add: A pointer is NULL!");
+    return (-1);
+  }
+
+  meta = (notification_meta_t *) malloc (sizeof (notification_meta_t));
+  if (meta == NULL)
+  {
+    ERROR ("plugin_notification_meta_add: malloc failed.");
+    return (-1);
+  }
+  memset (meta, 0, sizeof (notification_meta_t));
+
+  sstrncpy (meta->name, name, sizeof (meta->name));
+  meta->type = type;
+
+  switch (type)
+  {
+    case NM_TYPE_STRING:
+    {
+      meta->value_string = strdup ((const char *) value);
+      if (meta->value_string == NULL)
+      {
+        ERROR ("plugin_notification_meta_add: strdup failed.");
+        sfree (meta);
+        return (-1);
+      }
+      break;
+    }
+    case NM_TYPE_SIGNED_INT:
+    {
+      meta->value_signed_int = *((int64_t *) value);
+      break;
+    }
+    case NM_TYPE_UNSIGNED_INT:
+    {
+      meta->value_unsigned_int = *((uint64_t *) value);
+      break;
+    }
+    case NM_TYPE_DOUBLE:
+    {
+      meta->value_double = *((double *) value);
+      break;
+    }
+    case NM_TYPE_BOOLEAN:
+    {
+      meta->value_boolean = *((bool *) value);
+      break;
+    }
+    default:
+    {
+      ERROR ("plugin_notification_meta_add: Unknown type: %i", type);
+      sfree (meta);
+      return (-1);
+    }
+  } /* switch (type) */
+
+  meta->next = NULL;
+  tail = n->meta;
+  while ((tail != NULL) && (tail->next != NULL))
+    tail = tail->next;
+
+  if (tail == NULL)
+    n->meta = meta;
+  else
+    tail->next = meta;
+
+  return (0);
+} /* int plugin_notification_meta_add */
+
+int plugin_notification_meta_add_string (notification_t *n,
+    const char *name,
+    const char *value)
+{
+  return (plugin_notification_meta_add (n, name, NM_TYPE_STRING, value));
+}
+
+int plugin_notification_meta_add_signed_int (notification_t *n,
+    const char *name,
+    int64_t value)
+{
+  return (plugin_notification_meta_add (n, name, NM_TYPE_SIGNED_INT, &value));
+}
+
+int plugin_notification_meta_add_unsigned_int (notification_t *n,
+    const char *name,
+    uint64_t value)
+{
+  return (plugin_notification_meta_add (n, name, NM_TYPE_UNSIGNED_INT, &value));
+}
+
+int plugin_notification_meta_add_double (notification_t *n,
+    const char *name,
+    double value)
+{
+  return (plugin_notification_meta_add (n, name, NM_TYPE_DOUBLE, &value));
+}
+
+int plugin_notification_meta_add_boolean (notification_t *n,
+    const char *name,
+    bool value)
+{
+  return (plugin_notification_meta_add (n, name, NM_TYPE_BOOLEAN, &value));
+}
+
+int plugin_notification_meta_copy (notification_t *dst,
+    const notification_t *src)
+{
+  notification_meta_t *meta;
+
+  assert (dst != NULL);
+  assert (src != NULL);
+  assert (dst != src);
+  assert ((src->meta == NULL) || (src->meta != dst->meta));
+
+  for (meta = src->meta; meta != NULL; meta = meta->next)
+  {
+    if (meta->type == NM_TYPE_STRING)
+      plugin_notification_meta_add_string (dst, meta->name,
+         meta->value_string);
+    else if (meta->type == NM_TYPE_SIGNED_INT)
+      plugin_notification_meta_add_signed_int (dst, meta->name,
+         meta->value_signed_int);
+    else if (meta->type == NM_TYPE_UNSIGNED_INT)
+      plugin_notification_meta_add_unsigned_int (dst, meta->name,
+         meta->value_unsigned_int);
+    else if (meta->type == NM_TYPE_DOUBLE)
+      plugin_notification_meta_add_double (dst, meta->name,
+         meta->value_double);
+    else if (meta->type == NM_TYPE_BOOLEAN)
+      plugin_notification_meta_add_boolean (dst, meta->name,
+         meta->value_boolean);
+  }
+
+  return (0);
+} /* int plugin_notification_meta_copy */
+
+int plugin_notification_meta_free (notification_t *n)
+{
+  notification_meta_t *this;
+  notification_meta_t *next;
+
+  if (n == NULL)
+  {
+    ERROR ("plugin_notification_meta_free: n == NULL!");
+    return (-1);
+  }
+
+  this = n->meta;
+  n->meta = NULL;
+  while (this != NULL)
+  {
+    next = this->next;
+
+    if (this->type == NM_TYPE_STRING)
+    {
+      free ((char *)this->value_string);
+      this->value_string = NULL;
+    }
+    sfree (this);
+
+    this = next;
+  }
+
+  return (0);
+} /* int plugin_notification_meta_free */
index 318b547..3bf7e4c 100644 (file)
@@ -74,12 +74,13 @@ struct value_list_s
        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];
 };
 typedef struct value_list_s value_list_t;
 
-#define VALUE_LIST_INIT { NULL, 0, 0, interval_g, "localhost", "", "", "" }
-#define VALUE_LIST_STATIC { NULL, 0, 0, 0, "localhost", "", "", "" }
+#define VALUE_LIST_INIT { NULL, 0, 0, interval_g, "localhost", "", "", "", "" }
+#define VALUE_LIST_STATIC { NULL, 0, 0, 0, "localhost", "", "", "", "" }
 
 struct data_source_s
 {
@@ -98,6 +99,30 @@ struct data_set_s
 };
 typedef struct data_set_s data_set_t;
 
+enum notification_meta_type_e
+{
+       NM_TYPE_STRING,
+       NM_TYPE_SIGNED_INT,
+       NM_TYPE_UNSIGNED_INT,
+       NM_TYPE_DOUBLE,
+       NM_TYPE_BOOLEAN
+};
+
+typedef struct notification_meta_s
+{
+       char name[DATA_MAX_NAME_LEN];
+       enum notification_meta_type_e type;
+       union
+       {
+               const char *value_string;
+               int64_t value_signed_int;
+               uint64_t value_unsigned_int;
+               double value_double;
+               bool value_boolean;
+       };
+       struct notification_meta_s *next;
+} notification_meta_t;
+
 typedef struct notification_s
 {
        int    severity;
@@ -108,6 +133,7 @@ typedef struct notification_s
        char   plugin_instance[DATA_MAX_NAME_LEN];
        char   type[DATA_MAX_NAME_LEN];
        char   type_instance[DATA_MAX_NAME_LEN];
+       notification_meta_t *meta;
 } notification_t;
 
 /*
@@ -150,10 +176,11 @@ int plugin_load (const char *name);
 
 void plugin_init_all (void);
 void plugin_read_all (void);
-void plugin_flush_all (int timeout);
 void plugin_shutdown_all (void);
 
+void plugin_flush_all (int timeout);
 int plugin_flush_one (int timeout, const char *name);
+int plugin_flush (const char *plugin, int timeout, const char *identifier);
 
 /*
  * The `plugin_register_*' functions are used to make `config', `init',
@@ -172,7 +199,7 @@ int plugin_register_read (const char *name,
 int plugin_register_write (const char *name,
                int (*callback) (const data_set_t *ds, const value_list_t *vl));
 int plugin_register_flush (const char *name,
-               int (*callback) (const int));
+               int (*callback) (const int timeout, const char *identifier));
 int plugin_register_shutdown (char *name,
                int (*callback) (void));
 int plugin_register_data_set (const data_set_t *ds);
@@ -204,11 +231,10 @@ int plugin_unregister_notification (const char *name);
  *  write-functions.
  *
  * ARGUMENTS
- *  `name'      Name/type of the data-set that describe the values in `vl'.
  *  `vl'        Value list of the values that have been read by a `read'
  *              function.
  */
-int plugin_dispatch_values (const char *name, value_list_t *vl);
+int plugin_dispatch_values (value_list_t *vl);
 
 int plugin_dispatch_notification (const notification_t *notif);
 
@@ -227,4 +253,25 @@ void plugin_log (int level, const char *format, ...)
 
 const data_set_t *plugin_get_ds (const char *name);
 
+int plugin_notification_meta_add_string (notification_t *n,
+    const char *name,
+    const char *value);
+int plugin_notification_meta_add_signed_int (notification_t *n,
+    const char *name,
+    int64_t value);
+int plugin_notification_meta_add_unsigned_int (notification_t *n,
+    const char *name,
+    uint64_t value);
+int plugin_notification_meta_add_double (notification_t *n,
+    const char *name,
+    double value);
+int plugin_notification_meta_add_boolean (notification_t *n,
+    const char *name,
+    bool value);
+
+int plugin_notification_meta_copy (notification_t *dst,
+    const notification_t *src);
+
+int plugin_notification_meta_free (notification_t *n);
+
 #endif /* PLUGIN_H */
diff --git a/src/postgresql.c b/src/postgresql.c
new file mode 100644 (file)
index 0000000..40f8ec5
--- /dev/null
@@ -0,0 +1,519 @@
+/**
+ * collectd - src/postgresql.c
+ * Copyright (C) 2008  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>
+ **/
+
+/*
+ * This module collects PostgreSQL database statistics.
+ */
+
+#include "collectd.h"
+#include "common.h"
+
+#include "configfile.h"
+#include "plugin.h"
+
+#include "utils_complain.h"
+
+#include <pg_config_manual.h>
+#include <libpq-fe.h>
+
+#define log_err(...) ERROR ("postgresql: " __VA_ARGS__)
+#define log_warn(...) WARNING ("postgresql: " __VA_ARGS__)
+#define log_info(...) INFO ("postgresql: " __VA_ARGS__)
+
+/* Appends the (parameter, value) pair to the string
+ * pointed to by 'buf' suitable to be used as argument
+ * for PQconnectdb(). If value equals NULL, the pair
+ * is ignored. */
+#define C_PSQL_PAR_APPEND(buf, buf_len, parameter, value) \
+       if ((0 < (buf_len)) && (NULL != (value)) && ('\0' != *(value))) { \
+               int s = ssnprintf (buf, buf_len, " %s = '%s'", parameter, value); \
+               if (0 < s) { \
+                       buf     += s; \
+                       buf_len -= s; \
+               } \
+       }
+
+/* Returns the tuple (major, minor, patchlevel)
+ * for the given version number. */
+#define C_PSQL_SERVER_VERSION3(server_version) \
+       (server_version) / 10000, \
+       (server_version) / 100 - (int)((server_version) / 10000) * 100, \
+       (server_version) - (int)((server_version) / 100) * 100
+
+/* Returns true if the given host specifies a
+ * UNIX domain socket. */
+#define C_PSQL_IS_UNIX_DOMAIN_SOCKET(host) \
+       ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host)))
+
+/* Returns the tuple (host, delimiter, port) for a
+ * given (host, port) pair. Depending on the value of
+ * 'host' a UNIX domain socket or a TCP socket is
+ * assumed. */
+#define C_PSQL_SOCKET3(host, port) \
+       ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \
+       C_PSQL_IS_UNIX_DOMAIN_SOCKET (host) ? "/.s.PGSQL." : ":", \
+       port
+
+typedef struct {
+       PGconn      *conn;
+       c_complain_t conn_complaint;
+
+       /* user configuration */
+       char *host;
+       char *port;
+       char *database;
+       char *user;
+       char *password;
+
+       char *sslmode;
+
+       char *krbsrvname;
+
+       char *service;
+} c_psql_database_t;
+
+static c_psql_database_t *databases     = NULL;
+static int                databases_num = 0;
+
+static void submit (const c_psql_database_t *db,
+               const char *type, const char *type_instance,
+               value_t *values, size_t values_len)
+{
+       value_list_t vl = VALUE_LIST_INIT;
+
+       vl.values     = values;
+       vl.values_len = values_len;
+       vl.time       = time (NULL);
+
+       if (C_PSQL_IS_UNIX_DOMAIN_SOCKET (db->host)
+                       || (0 == strcmp (db->host, "localhost")))
+               sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       else
+               sstrncpy (vl.host, db->host, sizeof (vl.host));
+
+       sstrncpy (vl.plugin, "postgresql", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, db->database, sizeof (vl.plugin_instance));
+
+       sstrncpy (vl.type, type, sizeof (vl.type));
+
+       if (NULL != type_instance)
+               sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+
+       plugin_dispatch_values (&vl);
+       return;
+} /* submit */
+
+static void submit_counter (const c_psql_database_t *db,
+               const char *type, const char *type_instance,
+               const char *value)
+{
+       value_t values[1];
+
+       if ((NULL == value) || ('\0' == *value))
+               return;
+
+       values[0].counter = atoll (value);
+       submit (db, type, type_instance, values, 1);
+       return;
+} /* submit_counter */
+
+static void submit_gauge (const c_psql_database_t *db,
+               const char *type, const char *type_instance,
+               const char *value)
+{
+       value_t values[1];
+
+       if ((NULL == value) || ('\0' == *value))
+               return;
+
+       values[0].gauge = atof (value);
+       submit (db, type, type_instance, values, 1);
+       return;
+} /* submit_gauge */
+
+static int c_psql_check_connection (c_psql_database_t *db)
+{
+       /* "ping" */
+       PQclear (PQexec (db->conn, "SELECT 42;"));
+
+       if (CONNECTION_OK != PQstatus (db->conn)) {
+               PQreset (db->conn);
+
+               /* trigger c_release() */
+               if (0 == db->conn_complaint.interval)
+                       db->conn_complaint.interval = 1;
+
+               if (CONNECTION_OK != PQstatus (db->conn)) {
+                       c_complain (LOG_ERR, &db->conn_complaint,
+                                       "Failed to connect to database %s: %s",
+                                       db->database, PQerrorMessage (db->conn));
+                       return -1;
+               }
+       }
+
+       c_release (LOG_INFO, &db->conn_complaint,
+                       "Successfully reconnected to database %s", PQdb (db->conn));
+       return 0;
+} /* c_psql_check_connection */
+
+static int c_psql_stat_database (c_psql_database_t *db)
+{
+       const char *const query =
+               "SELECT numbackends, xact_commit, xact_rollback "
+                       "FROM pg_stat_database "
+                       "WHERE datname = $1;";
+
+       PGresult *res;
+
+       int n;
+
+       res = PQexecParams (db->conn, query, /* number of parameters */ 1,
+                       NULL, (const char *const *)&db->database, NULL, NULL,
+                       /* return text data */ 0);
+
+       if (PGRES_TUPLES_OK != PQresultStatus (res)) {
+               log_err ("Failed to execute SQL query: %s",
+                               PQerrorMessage (db->conn));
+               log_info ("SQL query was: %s", query);
+               PQclear (res);
+               return -1;
+       }
+
+       n = PQntuples (res);
+       if (1 < n) {
+               log_warn ("pg_stat_database has more than one entry "
+                               "for database %s - ignoring additional results.",
+                               db->database);
+       }
+       else if (1 > n) {
+               log_err ("pg_stat_database has no entry for database %s",
+                               db->database);
+               PQclear (res);
+               return -1;
+       }
+
+       submit_gauge (db, "pg_numbackends", NULL,  PQgetvalue (res, 0, 0));
+
+       submit_counter (db, "pg_xact", "commit",   PQgetvalue (res, 0, 1));
+       submit_counter (db, "pg_xact", "rollback", PQgetvalue (res, 0, 2));
+
+       PQclear (res);
+       return 0;
+} /* c_psql_stat_database */
+
+static int c_psql_stat_user_tables (c_psql_database_t *db)
+{
+       const char *const query =
+               "SELECT sum(seq_scan), sum(seq_tup_read), "
+                               "sum(idx_scan), sum(idx_tup_fetch), "
+                               "sum(n_tup_ins), sum(n_tup_upd), sum(n_tup_del), "
+                               "sum(n_tup_hot_upd), sum(n_live_tup), sum(n_dead_tup) "
+                       "FROM pg_stat_user_tables;";
+
+       PGresult *res;
+
+       int n;
+
+       res = PQexec (db->conn, query);
+
+       if (PGRES_TUPLES_OK != PQresultStatus (res)) {
+               log_err ("Failed to execute SQL query: %s",
+                               PQerrorMessage (db->conn));
+               log_info ("SQL query was: %s", query);
+               PQclear (res);
+               return -1;
+       }
+
+       n = PQntuples (res);
+       assert (1 >= n);
+
+       if (1 > n) /* no user tables */
+               return 0;
+
+       submit_counter (db, "pg_scan", "seq",           PQgetvalue (res, 0, 0));
+       submit_counter (db, "pg_scan", "seq_tup_read",  PQgetvalue (res, 0, 1));
+       submit_counter (db, "pg_scan", "idx",           PQgetvalue (res, 0, 2));
+       submit_counter (db, "pg_scan", "idx_tup_fetch", PQgetvalue (res, 0, 3));
+
+       submit_counter (db, "pg_n_tup_c", "ins",        PQgetvalue (res, 0, 4));
+       submit_counter (db, "pg_n_tup_c", "upd",        PQgetvalue (res, 0, 5));
+       submit_counter (db, "pg_n_tup_c", "del",        PQgetvalue (res, 0, 6));
+       submit_counter (db, "pg_n_tup_c", "hot_upd",    PQgetvalue (res, 0, 7));
+
+       submit_gauge (db, "pg_n_tup_g", "live",         PQgetvalue (res, 0, 8));
+       submit_gauge (db, "pg_n_tup_g", "dead",         PQgetvalue (res, 0, 9));
+
+       PQclear (res);
+       return 0;
+} /* c_psql_stat_user_tables */
+
+static int c_psql_statio_user_tables (c_psql_database_t *db)
+{
+       const char *const query =
+               "SELECT sum(heap_blks_read), sum(heap_blks_hit), "
+                               "sum(idx_blks_read), sum(idx_blks_hit), "
+                               "sum(toast_blks_read), sum(toast_blks_hit), "
+                               "sum(tidx_blks_read), sum(tidx_blks_hit) "
+                       "FROM pg_statio_user_tables;";
+
+       PGresult *res;
+
+       int n;
+
+       res = PQexec (db->conn, query);
+
+       if (PGRES_TUPLES_OK != PQresultStatus (res)) {
+               log_err ("Failed to execute SQL query: %s",
+                               PQerrorMessage (db->conn));
+               log_info ("SQL query was: %s", query);
+               PQclear (res);
+               return -1;
+       }
+
+       n = PQntuples (res);
+       assert (1 >= n);
+
+       if (1 > n) /* no user tables */
+               return 0;
+
+       submit_counter (db, "pg_blks", "heap_read",  PQgetvalue (res, 0, 0));
+       submit_counter (db, "pg_blks", "heap_hit",   PQgetvalue (res, 0, 1));
+
+       submit_counter (db, "pg_blks", "idx_read",   PQgetvalue (res, 0, 2));
+       submit_counter (db, "pg_blks", "idx_hit",    PQgetvalue (res, 0, 3));
+
+       submit_counter (db, "pg_blks", "toast_read", PQgetvalue (res, 0, 4));
+       submit_counter (db, "pg_blks", "toast_hit",  PQgetvalue (res, 0, 5));
+
+       submit_counter (db, "pg_blks", "tidx_read",  PQgetvalue (res, 0, 6));
+       submit_counter (db, "pg_blks", "tidx_hit",   PQgetvalue (res, 0, 7));
+
+       PQclear (res);
+       return 0;
+} /* c_psql_statio_user_tables */
+
+static int c_psql_read (void)
+{
+       int success = 0;
+       int i;
+
+       for (i = 0; i < databases_num; ++i) {
+               c_psql_database_t *db = databases + i;
+
+               assert (NULL != db->database);
+
+               if (0 != c_psql_check_connection (db))
+                       continue;
+
+               c_psql_stat_database (db);
+               c_psql_stat_user_tables (db);
+               c_psql_statio_user_tables (db);
+
+               ++success;
+       }
+
+       if (! success)
+               return -1;
+       return 0;
+} /* c_psql_read */
+
+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_t *db = databases + i;
+
+               PQfinish (db->conn);
+
+               sfree (db->database);
+               sfree (db->host);
+               sfree (db->port);
+               sfree (db->user);
+               sfree (db->password);
+
+               sfree (db->sslmode);
+
+               sfree (db->krbsrvname);
+
+               sfree (db->service);
+       }
+
+       sfree (databases);
+       databases_num = 0;
+       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;
+
+               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;
+
+               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),
+                               PQprotocolVersion (db->conn), PQbackendPID (db->conn));
+       }
+
+       plugin_register_read ("postgresql", c_psql_read);
+       plugin_register_shutdown ("postgresql", c_psql_shutdown);
+       return 0;
+} /* c_psql_init */
+
+static int config_set (char *name, char **var, const oconfig_item_t *ci)
+{
+       if ((0 != ci->children_num) || (1 != ci->values_num)
+                       || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
+               log_err ("%s expects a single string argument.", name);
+               return 1;
+       }
+
+       sfree (*var);
+       *var = sstrdup (ci->values[0].value.string);
+       return 0;
+} /* config_set */
+
+static int c_psql_config_database (oconfig_item_t *ci)
+{
+       c_psql_database_t *db;
+
+       int i;
+
+       if ((1 != ci->values_num)
+                       || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
+               log_err ("<Database> expects a single string argument.");
+               return 1;
+       }
+
+       ++databases_num;
+       if (NULL == (databases = (c_psql_database_t *)realloc (databases,
+                               databases_num * sizeof (*databases)))) {
+               log_err ("Out of memory.");
+               exit (5);
+       }
+
+       db = databases + (databases_num - 1);
+
+       db->conn = NULL;
+
+       db->conn_complaint.last     = 0;
+       db->conn_complaint.interval = 0;
+
+       db->database   = sstrdup (ci->values[0].value.string);
+       db->host       = NULL;
+       db->port       = NULL;
+       db->user       = NULL;
+       db->password   = NULL;
+
+       db->sslmode    = NULL;
+
+       db->krbsrvname = NULL;
+
+       db->service    = NULL;
+
+       for (i = 0; i < ci->children_num; ++i) {
+               oconfig_item_t *c = ci->children + i;
+
+               if (0 == strcasecmp (c->key, "Host"))
+                       config_set ("Host", &db->host, c);
+               else if (0 == strcasecmp (c->key, "Port"))
+                       config_set ("Port", &db->port, c);
+               else if (0 == strcasecmp (c->key, "User"))
+                       config_set ("User", &db->user, c);
+               else if (0 == strcasecmp (c->key, "Password"))
+                       config_set ("Password", &db->password, c);
+               else if (0 == strcasecmp (c->key, "SSLMode"))
+                       config_set ("SSLMode", &db->sslmode, c);
+               else if (0 == strcasecmp (c->key, "KRBSrvName"))
+                       config_set ("KRBSrvName", &db->krbsrvname, c);
+               else if (0 == strcasecmp (c->key, "Service"))
+                       config_set ("Service", &db->service, c);
+               else
+                       log_warn ("Ignoring unknown config key \"%s\".", c->key);
+       }
+       return 0;
+}
+
+static int c_psql_config (oconfig_item_t *ci)
+{
+       int i;
+
+       for (i = 0; i < ci->children_num; ++i) {
+               oconfig_item_t *c = ci->children + i;
+
+               if (0 == strcasecmp (c->key, "Database"))
+                       c_psql_config_database (c);
+               else
+                       log_warn ("Ignoring unknown config key \"%s\".", c->key);
+       }
+       return 0;
+} /* c_psql_config */
+
+void module_register (void)
+{
+       plugin_register_complex_config ("postgresql", c_psql_config);
+       plugin_register_init ("postgresql", c_psql_init);
+} /* module_register */
+
+/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
+
index 644dd56..b451073 100644 (file)
@@ -320,11 +320,12 @@ static void submit (const char *plugin_instance, /* {{{ */
   vl.time = time (NULL);
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "powerdns", sizeof (vl.plugin));
+  sstrncpy (vl.type, type, sizeof (vl.type));
   if (type_instance != NULL)
     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
   sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* }}} static void submit */
 
 static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
@@ -349,10 +350,9 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
 
   memset (&sa_unix, 0, sizeof (sa_unix));
   sa_unix.sun_family = AF_UNIX;
-  strncpy (sa_unix.sun_path,
+  sstrncpy (sa_unix.sun_path,
       (local_sockpath != NULL) ? local_sockpath : PDNS_LOCAL_SOCKPATH,
       sizeof (sa_unix.sun_path));
-  sa_unix.sun_path[sizeof (sa_unix.sun_path) - 1] = 0;
 
   status = unlink (sa_unix.sun_path);
   if ((status != 0) && (errno != ENOENT))
@@ -860,7 +860,8 @@ static int powerdns_config_add_server (oconfig_item_t *ci) /* {{{ */
     }
 
     item->sockaddr.sun_family = AF_UNIX;
-    sstrncpy (item->sockaddr.sun_path, socket_temp, UNIX_PATH_MAX);
+    sstrncpy (item->sockaddr.sun_path, socket_temp,
+      sizeof (item->sockaddr.sun_path));
 
     e = llentry_create (item->instance, item);
     if (e == NULL)
index 6ad4681..7531a6a 100644 (file)
@@ -1,7 +1,8 @@
 /**
  * collectd - src/processes.c
  * Copyright (C) 2005  Lyonel Vincent
- * Copyright (C) 2006-2007  Florian Forster (Mach code)
+ * Copyright (C) 2006-2008  Florian Forster (Mach code)
+ * Copyright (C) 2008  Oleg King
  *
  * 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
@@ -20,6 +21,7 @@
  * Authors:
  *   Lyonel Vincent <lyonel at ezix.org>
  *   Florian octo Forster <octo at verplant.org>
+ *   Oleg King <king2 at kaluga.ru>
  **/
 
 #include "collectd.h"
 #  endif
 /* #endif KERNEL_LINUX */
 
+#elif HAVE_KVM_H
+#  include <kvm.h>
+#  include <sys/user.h>
+#  include <sys/proc.h>
+#  if HAVE_SYS_SYSCTL_H
+#    include <sys/sysctl.h>
+#  endif
+/* #endif HAVE_KVM_H */
+
 #else
 # error "No applicable input method."
 #endif
 
+#if HAVE_REGEX_H
+# include <regex.h>
+#endif
+
 #define BUFSIZE 256
 
 static const char *config_keys[] =
 {
        "Process",
+       "ProcessMatch",
        NULL
 };
-static int config_keys_num = 1;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 typedef struct procstat_entry_s
 {
@@ -121,6 +137,9 @@ typedef struct procstat_entry_s
 typedef struct procstat
 {
        char          name[PROCSTAT_NAME_LEN];
+#if HAVE_REGEX_H
+       regex_t *re;
+#endif
 
        unsigned long num_proc;
        unsigned long num_lwp;
@@ -150,20 +169,71 @@ static mach_msg_type_number_t     pset_list_len;
 static long pagesize_g;
 #endif /* KERNEL_LINUX */
 
-static void ps_list_register (const char *name)
+/* put name of process from config to list_head_g tree
+   list_head_g is a list of 'procstat_t' structs with
+   processes names we want to watch */
+static void ps_list_register (const char *name, const char *regexp)
 {
        procstat_t *new;
        procstat_t *ptr;
+       int status;
 
-       if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL)
+       new = (procstat_t *) malloc (sizeof (procstat_t));
+       if (new == NULL)
+       {
+               ERROR ("processes plugin: ps_list_register: malloc failed.");
                return;
+       }
        memset (new, 0, sizeof (procstat_t));
-       strncpy (new->name, name, PROCSTAT_NAME_LEN);
+       sstrncpy (new->name, name, sizeof (new->name));
 
+#if HAVE_REGEX_H
+       if (regexp != NULL)
+       {
+               DEBUG ("ProcessMatch: adding \"%s\" as criteria to process %s.", regexp, name);
+               new->re = (regex_t *) malloc (sizeof (regex_t));
+               if (new->re == NULL)
+               {
+                       ERROR ("processes plugin: ps_list_register: malloc failed.");
+                       sfree (new);
+                       return;
+               }
+
+               status = regcomp (new->re, regexp, REG_EXTENDED | REG_NOSUB);
+               if (status != 0)
+               {
+                       DEBUG ("ProcessMatch: compiling the regular expression \"%s\" failed.", regexp);
+                       sfree(new->re);
+                       return;
+               }
+       }
+#else
+       if (regexp != NULL)
+       {
+               ERROR ("processes plugin: ps_list_register: "
+                               "Regular expression \"%s\" found in config "
+                               "file, but support for regular expressions "
+                               "has been dispabled at compile time.",
+                               regexp);
+               sfree (new);
+               return;
+       }
+#endif
+       
        for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
        {
                if (strcmp (ptr->name, name) == 0)
+               {
+                       WARNING ("processes plugin: You have configured more "
+                                       "than one `Process' or "
+                                       "`ProcessMatch' with the same name. "
+                                       "All but the first setting will be "
+                                       "ignored.");
+                       sfree (new->re);
+                       sfree (new);
                        return;
+               }
+
                if (ptr->next == NULL)
                        break;
        }
@@ -172,20 +242,40 @@ static void ps_list_register (const char *name)
                list_head_g = new;
        else
                ptr->next = new;
-}
+} /* void ps_list_register */
 
-static procstat_t *ps_list_search (const char *name)
+/* try to match name against entry, returns 1 if success */
+static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps)
 {
-       procstat_t *ptr;
+#if HAVE_REGEX_H
+       if (ps->re != NULL)
+       {
+               int status;
+               const char *str;
 
-       for (ptr = list_head_g; ptr != NULL; ptr = ptr->next)
-               if (strcmp (ptr->name, name) == 0)
-                       break;
+               str = cmdline;
+               if ((str == NULL) || (str[0] == 0))
+                       str = name;
 
-       return (ptr);
-}
+               assert (str != NULL);
+
+               status = regexec (ps->re, str,
+                               /* nmatch = */ 0,
+                               /* pmatch = */ NULL,
+                               /* eflags = */ 0);
+               if (status == 0)
+                       return (1);
+       }
+       else
+#endif
+       if (strcmp (ps->name, name) == 0)
+               return (1);
 
-static void ps_list_add (const char *name, procstat_entry_t *entry)
+       return (0);
+} /* int ps_list_match */
+
+/* add process entry to 'instances' of process 'name' (or refresh it) */
+static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t *entry)
 {
        procstat_t *ps;
        procstat_entry_t *pse;
@@ -193,115 +283,119 @@ static void ps_list_add (const char *name, procstat_entry_t *entry)
        if (entry->id == 0)
                return;
 
-       if ((ps = ps_list_search (name)) == NULL)
-               return;
-
-       for (pse = ps->instances; pse != NULL; pse = pse->next)
-               if ((pse->id == entry->id) || (pse->next == NULL))
-                       break;
-
-       if ((pse == NULL) || (pse->id != entry->id))
+       for (ps = list_head_g; ps != NULL; ps = ps->next)
        {
-               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 ((ps_list_match (name, cmdline, ps)) == 0)
+                       continue;
 
-               if (pse == NULL)
-                       ps->instances = new;
-               else
-                       pse->next = new;
+               for (pse = ps->instances; pse != NULL; pse = pse->next)
+                       if ((pse->id == entry->id) || (pse->next == NULL))
+                               break;
 
-               pse = new;
-       }
+               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
+                               pse->next = new;
 
-       pse->age = 0;
-       pse->num_proc = entry->num_proc;
-       pse->num_lwp  = entry->num_lwp;
-       pse->vmem_rss = entry->vmem_rss;
+                       pse = new;
+               }
 
-       ps->num_proc += pse->num_proc;
-       ps->num_lwp  += pse->num_lwp;
-       ps->vmem_rss += pse->vmem_rss;
+               pse->age = 0;
+               pse->num_proc = entry->num_proc;
+               pse->num_lwp  = entry->num_lwp;
+               pse->vmem_rss = entry->vmem_rss;
 
-       if ((entry->vmem_minflt_counter == 0)
-                       && (entry->vmem_majflt_counter == 0))
-       {
-               pse->vmem_minflt_counter += entry->vmem_minflt;
-               pse->vmem_minflt = entry->vmem_minflt;
+               ps->num_proc += pse->num_proc;
+               ps->num_lwp  += pse->num_lwp;
+               ps->vmem_rss += pse->vmem_rss;
 
-               pse->vmem_majflt_counter += entry->vmem_majflt;
-               pse->vmem_majflt = entry->vmem_majflt;
-       }
-       else
-       {
-               if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
+               if ((entry->vmem_minflt_counter == 0)
+                               && (entry->vmem_majflt_counter == 0))
                {
-                       pse->vmem_minflt = entry->vmem_minflt_counter
-                               + (ULONG_MAX - pse->vmem_minflt_counter);
-               }
-               else
-               {
-                       pse->vmem_minflt = entry->vmem_minflt_counter - pse->vmem_minflt_counter;
-               }
-               pse->vmem_minflt_counter = entry->vmem_minflt_counter;
+                       pse->vmem_minflt_counter += entry->vmem_minflt;
+                       pse->vmem_minflt = entry->vmem_minflt;
 
-               if (entry->vmem_majflt_counter < pse->vmem_majflt_counter)
-               {
-                       pse->vmem_majflt = entry->vmem_majflt_counter
-                               + (ULONG_MAX - pse->vmem_majflt_counter);
+                       pse->vmem_majflt_counter += entry->vmem_majflt;
+                       pse->vmem_majflt = entry->vmem_majflt;
                }
                else
                {
-                       pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
+                       if (entry->vmem_minflt_counter < pse->vmem_minflt_counter)
+                       {
+                               pse->vmem_minflt = entry->vmem_minflt_counter
+                                       + (ULONG_MAX - pse->vmem_minflt_counter);
+                       }
+                       else
+                       {
+                               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
+                                       + (ULONG_MAX - pse->vmem_majflt_counter);
+                       }
+                       else
+                       {
+                               pse->vmem_majflt = entry->vmem_majflt_counter - pse->vmem_majflt_counter;
+                       }
+                       pse->vmem_majflt_counter = entry->vmem_majflt_counter;
                }
-               pse->vmem_majflt_counter = entry->vmem_majflt_counter;
-       }
-
-       ps->vmem_minflt_counter += pse->vmem_minflt;
-       ps->vmem_majflt_counter += pse->vmem_majflt;
 
-       if ((entry->cpu_user_counter == 0)
-                       && (entry->cpu_system_counter == 0))
-       {
-               pse->cpu_user_counter += entry->cpu_user;
-               pse->cpu_user = entry->cpu_user;
+               ps->vmem_minflt_counter += pse->vmem_minflt;
+               ps->vmem_majflt_counter += pse->vmem_majflt;
 
-               pse->cpu_system_counter += entry->cpu_system;
-               pse->cpu_system = entry->cpu_system;
-       }
-       else
-       {
-               if (entry->cpu_user_counter < pse->cpu_user_counter)
+               if ((entry->cpu_user_counter == 0)
+                               && (entry->cpu_system_counter == 0))
                {
-                       pse->cpu_user = entry->cpu_user_counter
-                               + (ULONG_MAX - pse->cpu_user_counter);
-               }
-               else
-               {
-                       pse->cpu_user = entry->cpu_user_counter - pse->cpu_user_counter;
-               }
-               pse->cpu_user_counter = entry->cpu_user_counter;
+                       pse->cpu_user_counter += entry->cpu_user;
+                       pse->cpu_user = entry->cpu_user;
 
-               if (entry->cpu_system_counter < pse->cpu_system_counter)
-               {
-                       pse->cpu_system = entry->cpu_system_counter
-                               + (ULONG_MAX - pse->cpu_system_counter);
+                       pse->cpu_system_counter += entry->cpu_system;
+                       pse->cpu_system = entry->cpu_system;
                }
                else
                {
-                       pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
+                       if (entry->cpu_user_counter < pse->cpu_user_counter)
+                       {
+                               pse->cpu_user = entry->cpu_user_counter
+                                       + (ULONG_MAX - pse->cpu_user_counter);
+                       }
+                       else
+                       {
+                               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
+                                       + (ULONG_MAX - pse->cpu_system_counter);
+                       }
+                       else
+                       {
+                               pse->cpu_system = entry->cpu_system_counter - pse->cpu_system_counter;
+                       }
+                       pse->cpu_system_counter = entry->cpu_system_counter;
                }
-               pse->cpu_system_counter = entry->cpu_system_counter;
-       }
 
-       ps->cpu_user_counter   += pse->cpu_user;
-       ps->cpu_system_counter += pse->cpu_system;
+               ps->cpu_user_counter   += pse->cpu_user;
+               ps->cpu_system_counter += pse->cpu_system;
+       }
 }
 
+/* remove old entries from instances of processes in list_head_g */
 static void ps_list_reset (void)
 {
        procstat_t *ps;
@@ -347,11 +441,31 @@ static void ps_list_reset (void)
        } /* for (ps = list_head_g; ps != NULL; ps = ps->next) */
 }
 
+/* put all pre-defined 'Process' names from config to list_head_g tree */
 static int ps_config (const char *key, const char *value)
 {
        if (strcasecmp (key, "Process") == 0)
        {
-               ps_list_register (value);
+               ps_list_register (value, NULL);
+       }
+       else if (strcasecmp (key, "ProcessMatch") == 0)
+       {
+               char *new_val;
+               char *fields[3];
+               int fields_num;
+
+               new_val = strdup (value);
+               if (new_val == NULL)
+                       return (1);
+               fields_num = strsplit (new_val, fields,
+                               STATIC_ARRAY_SIZE (fields));
+               if (fields_num != 2)
+               {
+                       sfree (new_val);
+                       return (1);
+               }
+               ps_list_register (fields[0], fields[1]);
+               sfree (new_val);
        }
        else
        {
@@ -399,6 +513,7 @@ static int ps_init (void)
        return (0);
 } /* int ps_init */
 
+/* submit global state (e.g.: qty of zombies, running, etc..) */
 static void ps_submit_state (const char *state, double value)
 {
        value_t values[1];
@@ -412,11 +527,13 @@ static void ps_submit_state (const char *state, double value)
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
        sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
-       strncpy (vl.type_instance, state, sizeof (vl.type_instance));
+       sstrncpy (vl.type, "ps_state", sizeof (vl.type));
+       sstrncpy (vl.type_instance, state, sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("ps_state", &vl);
+       plugin_dispatch_values (&vl);
 }
 
+/* submit info about specific process (e.g.: memory taken, cpu usage, etc..) */
 static void ps_submit_proc_list (procstat_t *ps)
 {
        value_t values[2];
@@ -427,26 +544,30 @@ static void ps_submit_proc_list (procstat_t *ps)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "processes", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
+       sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance));
 
+       sstrncpy (vl.type, "ps_rss", sizeof (vl.type));
        vl.values[0].gauge = ps->vmem_rss;
        vl.values_len = 1;
-       plugin_dispatch_values ("ps_rss", &vl);
+       plugin_dispatch_values (&vl);
 
+       sstrncpy (vl.type, "ps_cputime", sizeof (vl.type));
        vl.values[0].counter = ps->cpu_user_counter;
        vl.values[1].counter = ps->cpu_system_counter;
        vl.values_len = 2;
-       plugin_dispatch_values ("ps_cputime", &vl);
+       plugin_dispatch_values (&vl);
 
+       sstrncpy (vl.type, "ps_count", sizeof (vl.type));
        vl.values[0].gauge = ps->num_proc;
        vl.values[1].gauge = ps->num_lwp;
        vl.values_len = 2;
-       plugin_dispatch_values ("ps_count", &vl);
+       plugin_dispatch_values (&vl);
 
+       sstrncpy (vl.type, "ps_pagefaults", sizeof (vl.type));
        vl.values[0].counter = ps->vmem_minflt_counter;
        vl.values[1].counter = ps->vmem_majflt_counter;
        vl.values_len = 2;
-       plugin_dispatch_values ("ps_pagefaults", &vl);
+       plugin_dispatch_values (&vl);
 
        DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; "
                        "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; "
@@ -456,6 +577,7 @@ static void ps_submit_proc_list (procstat_t *ps)
                        ps->cpu_user_counter, ps->cpu_system_counter);
 } /* void ps_submit_proc_list */
 
+/* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
 #if KERNEL_LINUX
 static int *ps_read_tasks (int pid)
 {
@@ -467,8 +589,7 @@ static int *ps_read_tasks (int pid)
        DIR           *dh;
        struct dirent *ent;
 
-       snprintf (dirname, 64, "/proc/%i/task", pid);
-       dirname[63] = '\0';
+       ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid);
 
        if ((dh = opendir (dirname)) == NULL)
        {
@@ -523,7 +644,6 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
 {
        char  filename[64];
        char  buffer[1024];
-       FILE *fh;
 
        char *fields[64];
        char  fields_len;
@@ -540,19 +660,12 @@ int ps_read_process (int pid, procstat_t *ps, char *state)
 
        memset (ps, 0, sizeof (procstat_t));
 
-       snprintf (filename, 64, "/proc/%i/stat", pid);
-       filename[63] = '\0';
+       ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid);
 
-       if ((fh = fopen (filename, "r")) == NULL)
+       i = read_file_contents (filename, buffer, sizeof(buffer) - 1);
+       if (i <= 0)
                return (-1);
-
-       if (fgets (buffer, 1024, fh) == NULL)
-       {
-               fclose (fh);
-               return (-1);
-       }
-
-       fclose (fh);
+       buffer[i] = 0;
 
        fields_len = strsplit (buffer, fields, 64);
        if (fields_len < 24)
@@ -663,7 +776,9 @@ static int mach_get_task_name (task_t t, int *pid, char *name, size_t name_max_l
        return (0);
 }
 #endif /* HAVE_THREAD_INFO */
+/* ------- end of additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */
 
+/* do actual readings from kernel */
 static int ps_read (void)
 {
 #if HAVE_THREAD_INFO
@@ -731,7 +846,13 @@ static int ps_read (void)
                        if (mach_get_task_name (task_list[task],
                                                &task_pid,
                                                task_name, PROCSTAT_NAME_LEN) == 0)
-                               ps = ps_list_search (task_name);
+                       {
+                               /* search for at least one match */
+                               for (ps = list_head_g; ps != NULL; ps = ps->next)
+                                       /* FIXME: cmdline should be here instead of NULL */
+                                       if (ps_list_match (task_name, NULL, ps) == 1)
+                                               break;
+                       }
 
                        /* Collect more detailed statistics for this process */
                        if (ps != NULL)
@@ -889,7 +1010,8 @@ static int ps_read (void)
                        }
 
                        if (ps != NULL)
-                               ps_list_add (task_name, &pse);
+                               /* FIXME: cmdline should be here instead of NULL */
+                               ps_list_add (task_name, NULL, &pse);
                } /* for (task_list) */
 
                if ((status = vm_deallocate (port_task_self,
@@ -992,7 +1114,8 @@ static int ps_read (void)
                        case 'W': paging++;   break;
                }
 
-               ps_list_add (ps.name, &pse);
+               /* FIXME: cmdline should be here instead of NULL */
+               ps_list_add (ps.name, NULL, &pse);
        }
 
        closedir (proc);
@@ -1006,7 +1129,129 @@ 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 /* KERNEL_LINUX */
+/* #endif KERNEL_LINUX */
+
+#elif HAVE_LIBKVM
+       int running  = 0;
+       int sleeping = 0;
+       int zombies  = 0;
+       int stopped  = 0;
+       int blocked  = 0;
+       int idle     = 0;
+       int wait     = 0;
+
+       kvm_t *kd;
+       char errbuf[1024];
+       char cmdline[ARG_MAX];
+       char *cmdline_ptr;
+       struct kinfo_proc *procs;          /* array of processes */
+       char **argv;
+       int count;                         /* returns number of processes */
+       int i;
+
+       procstat_t *ps_ptr;
+       procstat_entry_t pse;
+
+       ps_list_reset ();
+
+       /* Open the kvm interface, get a descriptor */
+       kd = kvm_open (NULL, NULL, NULL, 0, errbuf);
+       if (kd == NULL)
+       {
+               ERROR ("processes plugin: Cannot open kvm interface: %s",
+                               errbuf);
+               return (0);
+       }
+
+       /* Get the list of processes. */
+       procs = kvm_getprocs(kd, KERN_PROC_ALL, 0, &count);
+       if (procs == NULL)
+       {
+               kvm_close (kd);
+               ERROR ("processes plugin: Cannot get kvm processes list: %s",
+                               kvm_geterr(kd));
+               return (0);
+       }
+
+       /* Iterate through the processes in kinfo_proc */
+       for (i = 0; i < count; i++)
+       {
+               /* retrieve the arguments */
+               cmdline[0] = 0;
+               cmdline_ptr = NULL;
+
+               argv = kvm_getargv (kd, (const struct kinfo_proc *) &(procs[i]), 0);
+               if (argv != NULL)
+               {
+                       int status;
+                       int argc;
+
+                       argc = 0;
+                       while (argv[argc] != NULL)
+                               argc++;
+
+                       status = strjoin (cmdline, sizeof (cmdline),
+                                       argv, argc, " ");
+
+                       if (status < 0)
+                       {
+                               WARNING ("processes plugin: Command line did "
+                                               "not fit into buffer.");
+                       }
+                       else
+                       {
+                               cmdline_ptr = &cmdline[0];
+                       }
+               }
+
+               pse.id       = procs[i].ki_pid;
+               pse.age      = 0;
+
+               pse.num_proc = 1;
+               pse.num_lwp  = procs[i].ki_numthreads;
+
+               pse.vmem_rss = procs[i].ki_rssize * getpagesize();
+               pse.vmem_minflt = 0;
+               pse.vmem_minflt_counter = procs[i].ki_rusage.ru_minflt;
+               pse.vmem_majflt = 0;
+               pse.vmem_majflt_counter = procs[i].ki_rusage.ru_majflt;
+
+               pse.cpu_user = 0;
+               pse.cpu_user_counter = procs[i].ki_rusage.ru_utime.tv_sec
+                       * 1000
+                       + procs[i].ki_rusage.ru_utime.tv_usec;
+               pse.cpu_system = 0;
+               pse.cpu_system_counter = procs[i].ki_rusage.ru_stime.tv_sec
+                       * 1000
+                       + procs[i].ki_rusage.ru_stime.tv_usec;
+
+               switch (procs[i].ki_stat)
+               {
+                       case SSTOP:     stopped++;      break;
+                       case SSLEEP:    sleeping++;     break;
+                       case SRUN:      running++;      break;
+                       case SIDL:      idle++;         break;
+                       case SWAIT:     wait++;         break;
+                       case SLOCK:     blocked++;      break;
+                       case SZOMB:     zombies++;      break;
+               }
+
+               ps_list_add (procs[i].ki_comm, cmdline_ptr, &pse);
+       }
+
+       kvm_close(kd);
+
+       ps_submit_state ("running",  running);
+       ps_submit_state ("sleeping", sleeping);
+       ps_submit_state ("zombies",  zombies);
+       ps_submit_state ("stopped",  stopped);
+       ps_submit_state ("blocked",  blocked);
+       ps_submit_state ("idle",     idle);
+       ps_submit_state ("wait",     wait);
+
+       for (ps_ptr = list_head_g; ps_ptr != NULL; ps_ptr = ps_ptr->next)
+               ps_submit_proc_list (ps_ptr);
+#endif /* HAVE_LIBKVM */
 
        return (0);
 } /* int ps_read */
diff --git a/src/qmail.c b/src/qmail.c
new file mode 100644 (file)
index 0000000..063eee9
--- /dev/null
@@ -0,0 +1,263 @@
+/**
+ * collectd - src/qmail.c
+ * Copyright (C) 2008  Alessandro Iurlano
+ *
+ * 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:
+ *   Alessandro Iurlano <alessandro.iurlano at gmail.com>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"       
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dirent.h>
+
+#define DEFAULT_BASE_DIR "/var/qmail"
+
+static char *qmail_base_dir;
+
+static const char *config_keys[] =
+{
+  "QmailDir"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+static void qmail_submit (const char *plugin_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);
+  vl.time = time (NULL);
+  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+  sstrncpy (vl.type, "gauge", sizeof (vl.type));
+  sstrncpy (vl.plugin, "qmail", sizeof (vl.plugin));
+  sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+
+  plugin_dispatch_values (&vl);
+} /* void qmail_submit */
+
+static int count_files_in_subtree (const char *path, int depth)
+{
+#if NAME_MAX < 1024
+# define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + 1024 + 1)
+#else
+# define DIRENT_BUFFER_SIZE (sizeof (struct dirent) + NAME_MAX + 1)
+#endif
+
+  DIR *dh;
+  struct dirent *de;
+  char dirent_buffer[DIRENT_BUFFER_SIZE];
+  int status;
+
+  char **subdirs;
+  size_t subdirs_num;
+
+  int count;
+  int i;
+
+  dh = opendir (path);
+  if (dh == NULL)
+  {
+    ERROR ("qmail plugin: opendir (%s) failed.", path);
+    return (-1);
+  }
+
+  subdirs = NULL;
+  subdirs_num = 0;
+
+  count = 0;
+  while (42)
+  {
+    char abs_path[PATH_MAX];
+    struct stat statbuf;
+
+    de = NULL;
+    status = readdir_r (dh, (struct dirent *) dirent_buffer, &de);
+    if (status != 0)
+    {
+      char errbuf[4096];
+      ERROR ("qmail plugin: readdir_r failed: %s",
+          sstrerror (errno, errbuf, sizeof (errbuf)));
+      closedir (dh);
+      return (-1);
+    }
+    else if (de == NULL)
+    {
+      /* end of directory */
+      break;
+    }
+
+    if (de->d_name[0] == '.')
+      continue;
+
+    ssnprintf (abs_path, sizeof (abs_path), "%s/%s", path, de->d_name);
+
+    status = lstat (abs_path, &statbuf);
+    if (status != 0)
+    {
+      ERROR ("qmail plugin: stat (%s) failed.", abs_path);
+      continue;
+    }
+
+    if (S_ISREG (statbuf.st_mode))
+    {
+      count++;
+    }
+    else if (S_ISDIR (statbuf.st_mode))
+    {
+      char **temp;
+
+      temp = (char **) realloc (subdirs, sizeof (char *) * (subdirs_num + 1));
+      if (temp == NULL)
+      {
+        ERROR ("qmail plugin: realloc failed.");
+        continue;
+      }
+      subdirs = temp;
+
+      subdirs[subdirs_num] = strdup (abs_path);
+      if (subdirs[subdirs_num] == NULL)
+      {
+        ERROR ("qmail plugin: strdup failed.");
+        continue;
+      }
+      subdirs_num++;
+    }
+  }
+
+  closedir (dh);
+  dh = NULL;
+
+  if (depth > 0)
+  {
+    for (i = 0; i < subdirs_num; i++)
+    {
+      status = count_files_in_subtree (subdirs[i], depth - 1);
+      if (status > 0)
+        count += status;
+    }
+  }
+
+  for (i = 0; i < subdirs_num; i++)
+  {
+    sfree (subdirs[i]);
+  }
+  sfree (subdirs);
+
+  return (count);
+} /* int count_files_in_subtree */
+
+static int read_queue_length (const char *queue_name, const char *path)
+{
+  int64_t num_files;
+
+  num_files = count_files_in_subtree (path, /* depth = */ 1);
+  if (num_files < 0)
+  {
+    ERROR ("qmail plugin: Counting files in `%s' failed.", path);
+    return (-1);
+  }
+
+  qmail_submit (queue_name, (gauge_t) num_files);
+  return (0);
+} /* int read_queue_length */
+
+static int queue_len_read (void)
+{
+  char path[4096];
+  int success;
+  int status;
+
+  success = 0;
+  
+  ssnprintf (path, sizeof (path), "%s/queue/mess",
+      (qmail_base_dir != NULL)
+      ? qmail_base_dir
+      : DEFAULT_BASE_DIR);
+
+  status = read_queue_length ("messages", path);
+  if (status == 0)
+    success++;
+
+  ssnprintf (path, sizeof (path), "%s/queue/todo",
+      (qmail_base_dir != NULL)
+      ? qmail_base_dir
+      : DEFAULT_BASE_DIR);
+
+  status = read_queue_length ("todo", path);
+  if (status == 0)
+    success++;
+
+  if (success > 0)
+    return 0;
+  return (-1);
+} /* int queue_len_read */
+
+static int qmail_config (const char *key, const char *val)
+{
+  if (strcasecmp ("QmailDir", key) == 0)
+  {
+    size_t qmail_base_dir_len;
+
+    sfree (qmail_base_dir);
+    qmail_base_dir = strdup(val);
+    if (qmail_base_dir == NULL)
+    {
+      ERROR ("qmail plugin: strdup failed.");
+      return (1);
+    }
+
+    qmail_base_dir_len = strlen (qmail_base_dir);
+    while ((qmail_base_dir_len > 0)
+        && (qmail_base_dir[qmail_base_dir_len - 1] == '/'))
+    {
+      qmail_base_dir[qmail_base_dir_len - 1] = 0;
+      qmail_base_dir_len--;
+    }
+
+    if (qmail_base_dir_len == 0)
+    {
+      ERROR ("qmail plugin: QmailDir is invalid.");
+      sfree (qmail_base_dir);
+      qmail_base_dir = NULL;
+      return (1);
+    }
+  }
+  else
+  {
+    return (-1);
+  }
+
+  return (0);
+} /* int qmail_config */
+
+void module_register (void)
+{
+  plugin_register_config ("qmail", qmail_config,
+      config_keys, config_keys_num);
+  plugin_register_read ("qmail", queue_len_read);
+} /* void module_register */
+
+/*
+ * vim: set sw=2 sts=2 et :
+ */
index 3b2c54f..7cf2436 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/rrdtool.c
- * Copyright (C) 2006,2007  Florian octo Forster
+ * Copyright (C) 2006-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
@@ -47,6 +47,13 @@ struct rrd_cache_s
 };
 typedef struct rrd_cache_s rrd_cache_t;
 
+enum rrd_queue_dir_e
+{
+  QUEUE_INSERT_FRONT,
+  QUEUE_INSERT_BACK
+};
+typedef enum rrd_queue_dir_e rrd_queue_dir_t;
+
 struct rrd_queue_s
 {
        char *filename;
@@ -211,7 +218,7 @@ static int rra_get (char ***ret, const value_list_t *vl)
                        if (rra_num >= rra_max)
                                break;
 
-                       if (snprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
+                       if (ssnprintf (buffer, sizeof (buffer), "RRA:%s:%3.1f:%u:%u",
                                                rra_types[j], xff,
                                                cdp_len, cdp_num) >= sizeof (buffer))
                        {
@@ -288,22 +295,16 @@ static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
                        sstrncpy (min, "U", sizeof (min));
                }
                else
-               {
-                       snprintf (min, sizeof (min), "%lf", d->min);
-                       min[sizeof (min) - 1] = '\0';
-               }
+                       ssnprintf (min, sizeof (min), "%lf", d->min);
 
                if (isnan (d->max))
                {
                        sstrncpy (max, "U", sizeof (max));
                }
                else
-               {
-                       snprintf (max, sizeof (max), "%lf", d->max);
-                       max[sizeof (max) - 1] = '\0';
-               }
+                       ssnprintf (max, sizeof (max), "%lf", d->max);
 
-               status = snprintf (buffer, sizeof (buffer),
+               status = ssnprintf (buffer, sizeof (buffer),
                                "DS:%s:%s:%i:%s:%s",
                                d->name, type,
                                (heartbeat > 0) ? heartbeat : (2 * vl->interval),
@@ -335,7 +336,7 @@ static int ds_get (char ***ret, const data_set_t *ds, const value_list_t *vl)
 
 #if HAVE_THREADSAFE_LIBRRD
 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
-               int argc, char **argv)
+               int argc, const char **argv)
 {
        int status;
 
@@ -353,7 +354,8 @@ static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
        return (status);
 } /* int srrd_create */
 
-static int srrd_update (char *filename, char *template, int argc, char **argv)
+static int srrd_update (char *filename, char *template,
+               int argc, const char **argv)
 {
        int status;
 
@@ -374,7 +376,7 @@ static int srrd_update (char *filename, char *template, int argc, char **argv)
 
 #else /* !HAVE_THREADSAFE_LIBRRD */
 static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
-               int argc, char **argv)
+               int argc, const char **argv)
 {
        int status;
 
@@ -395,10 +397,8 @@ static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
        if (last_up == 0)
                last_up = time (NULL) - 10;
 
-       snprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
-       pdp_step_str[sizeof (pdp_step_str) - 1] = '\0';
-       snprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
-       last_up_str[sizeof (last_up_str) - 1] = '\0';
+       ssnprintf (pdp_step_str, sizeof (pdp_step_str), "%lu", pdp_step);
+       ssnprintf (last_up_str, sizeof (last_up_str), "%u", (unsigned int) last_up);
 
        new_argv[0] = "create";
        new_argv[1] = filename;
@@ -428,7 +428,8 @@ static int srrd_create (char *filename, unsigned long pdp_step, time_t last_up,
        return (status);
 } /* int srrd_create */
 
-static int srrd_update (char *filename, char *template, int argc, char **argv)
+static int srrd_update (char *filename, char *template,
+               int argc, const char **argv)
 {
        int status;
 
@@ -513,7 +514,7 @@ static int rrd_create_file (char *filename, const data_set_t *ds, const value_li
        status = srrd_create (filename,
                        (stepsize > 0) ? stepsize : vl->interval,
                        vl->time - 10,
-                       argc, argv);
+                       argc, (const char **)argv);
 
        free (argv);
        ds_free (ds_num, ds_def);
@@ -531,7 +532,7 @@ static int value_list_to_string (char *buffer, int buffer_len,
 
        memset (buffer, '\0', buffer_len);
 
-       status = snprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
+       status = ssnprintf (buffer, buffer_len, "%u", (unsigned int) vl->time);
        if ((status < 1) || (status >= buffer_len))
                return (-1);
        offset = status;
@@ -543,10 +544,10 @@ static int value_list_to_string (char *buffer, int buffer_len,
                        return (-1);
 
                if (ds->ds[i].type == DS_TYPE_COUNTER)
-                       status = snprintf (buffer + offset, buffer_len - offset,
+                       status = ssnprintf (buffer + offset, buffer_len - offset,
                                        ":%llu", vl->values[i].counter);
                else
-                       status = snprintf (buffer + offset, buffer_len - offset,
+                       status = ssnprintf (buffer + offset, buffer_len - offset,
                                        ":%lf", vl->values[i].gauge);
 
                if ((status < 1) || (status >= (buffer_len - offset)))
@@ -566,35 +567,35 @@ static int value_list_to_filename (char *buffer, int buffer_len,
 
        if (datadir != NULL)
        {
-               status = snprintf (buffer + offset, buffer_len - offset,
+               status = ssnprintf (buffer + offset, buffer_len - offset,
                                "%s/", datadir);
                if ((status < 1) || (status >= buffer_len - offset))
                        return (-1);
                offset += status;
        }
 
-       status = snprintf (buffer + offset, buffer_len - offset,
+       status = ssnprintf (buffer + offset, buffer_len - offset,
                        "%s/", vl->host);
        if ((status < 1) || (status >= buffer_len - offset))
                return (-1);
        offset += status;
 
        if (strlen (vl->plugin_instance) > 0)
-               status = snprintf (buffer + offset, buffer_len - offset,
+               status = ssnprintf (buffer + offset, buffer_len - offset,
                                "%s-%s/", vl->plugin, vl->plugin_instance);
        else
-               status = snprintf (buffer + offset, buffer_len - offset,
+               status = ssnprintf (buffer + offset, buffer_len - offset,
                                "%s/", vl->plugin);
        if ((status < 1) || (status >= buffer_len - offset))
                return (-1);
        offset += status;
 
        if (strlen (vl->type_instance) > 0)
-               status = snprintf (buffer + offset, buffer_len - offset,
-                               "%s-%s.rrd", ds->type, vl->type_instance);
+               status = ssnprintf (buffer + offset, buffer_len - offset,
+                               "%s-%s.rrd", vl->type, vl->type_instance);
        else
-               status = snprintf (buffer + offset, buffer_len - offset,
-                               "%s.rrd", ds->type);
+               status = ssnprintf (buffer + offset, buffer_len - offset,
+                               "%s.rrd", vl->type);
        if ((status < 1) || (status >= buffer_len - offset))
                return (-1);
        offset += status;
@@ -653,7 +654,8 @@ static void *rrd_queue_thread (void *data)
                pthread_mutex_unlock (&cache_lock);
 
                /* Write the values to the RRD-file */
-               srrd_update (queue_entry->filename, NULL, values_num, values);
+               srrd_update (queue_entry->filename, NULL,
+                               values_num, (const char **)values);
                DEBUG ("rrdtool plugin: queue thread: Wrote %i values to %s",
                                values_num, queue_entry->filename);
 
@@ -675,37 +677,74 @@ static void *rrd_queue_thread (void *data)
        return ((void *) 0);
 } /* void *rrd_queue_thread */
 
-static int rrd_queue_cache_entry (const char *filename)
+static int rrd_queue_cache_entry (const char *filename, rrd_queue_dir_t dir)
 {
-       rrd_queue_t *queue_entry;
-
-       queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
-       if (queue_entry == NULL)
-               return (-1);
-
-       queue_entry->filename = strdup (filename);
-       if (queue_entry->filename == NULL)
-       {
-               free (queue_entry);
-               return (-1);
-       }
-
-       queue_entry->next = NULL;
-
-       pthread_mutex_lock (&queue_lock);
-       if (queue_tail == NULL)
-               queue_head = queue_entry;
-       else
-               queue_tail->next = queue_entry;
-       queue_tail = queue_entry;
-       pthread_cond_signal (&queue_cond);
-       pthread_mutex_unlock (&queue_lock);
-
-       DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
-
-       return (0);
+  rrd_queue_t *queue_entry;
+
+  queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t));
+  if (queue_entry == NULL)
+    return (-1);
+
+  queue_entry->filename = strdup (filename);
+  if (queue_entry->filename == NULL)
+  {
+    free (queue_entry);
+    return (-1);
+  }
+
+  queue_entry->next = NULL;
+
+  pthread_mutex_lock (&queue_lock);
+  if (dir == QUEUE_INSERT_FRONT)
+  {
+    queue_entry->next = queue_head;
+    queue_head = queue_entry;
+    if (queue_tail == NULL)
+      queue_tail = queue_head;
+  }
+  else /* (dir == QUEUE_INSERT_BACK) */
+  {
+    if (queue_tail == NULL)
+      queue_head = queue_entry;
+    else
+      queue_tail->next = queue_entry;
+    queue_tail = queue_entry;
+  }
+  pthread_cond_signal (&queue_cond);
+  pthread_mutex_unlock (&queue_lock);
+
+  DEBUG ("rrdtool plugin: Put `%s' into the update queue", filename);
+
+  return (0);
 } /* int rrd_queue_cache_entry */
 
+static int rrd_queue_move_to_front (const char *filename)
+{
+  rrd_queue_t *this;
+  rrd_queue_t *prev;
+
+  this = NULL;
+  prev = NULL;
+  pthread_mutex_lock (&queue_lock);
+  for (this = queue_head; this != NULL; this = this->next)
+  {
+    if (strcmp (this->filename, filename) == 0)
+      break;
+    prev = this;
+  }
+
+  /* Check if we found the entry and if it is NOT the first entry. */
+  if ((this != NULL) && (prev != NULL))
+  {
+    prev->next = this->next;
+    this->next = queue_head;
+    queue_head = this;
+  }
+  pthread_mutex_unlock (&queue_lock);
+
+  return (0);
+} /* int rrd_queue_move_to_front */
+
 static void rrd_cache_flush (int timeout)
 {
        rrd_cache_t *rc;
@@ -732,7 +771,7 @@ static void rrd_cache_flush (int timeout)
                        continue;
                else if (rc->values_num > 0)
                {
-                       if (rrd_queue_cache_entry (key) == 0)
+                       if (rrd_queue_cache_entry (key, QUEUE_INSERT_BACK) == 0)
                                rc->flags = FLAG_QUEUED;
                }
                else /* ancient and no values -> waste of memory */
@@ -778,6 +817,52 @@ static void rrd_cache_flush (int timeout)
        cache_flush_last = now;
 } /* void rrd_cache_flush */
 
+static int rrd_cache_flush_identifier (int timeout, const char *identifier)
+{
+  rrd_cache_t *rc;
+  time_t now;
+  int status;
+  char key[2048];
+
+  if (identifier == NULL)
+  {
+    rrd_cache_flush (timeout);
+    return (0);
+  }
+
+  now = time (NULL);
+
+  if (datadir == NULL)
+         snprintf (key, sizeof (key), "%s.rrd",
+                         identifier);
+  else
+         snprintf (key, sizeof (key), "%s/%s.rrd",
+                         datadir, identifier);
+  key[sizeof (key) - 1] = 0;
+
+  status = c_avl_get (cache, key, (void *) &rc);
+  if (status != 0)
+  {
+    WARNING ("rrdtool plugin: rrd_cache_flush_identifier: "
+       "c_avl_get (%s) failed. Does that file really exist?",
+       key);
+    return (status);
+  }
+
+  if (rc->flags == FLAG_QUEUED)
+    status = rrd_queue_move_to_front (key);
+  else if ((now - rc->first_value) < timeout)
+    status = 0;
+  else if (rc->values_num > 0)
+  {
+    status = rrd_queue_cache_entry (key, QUEUE_INSERT_FRONT);
+    if (status == 0)
+      rc->flags = FLAG_QUEUED;
+  }
+
+  return (status);
+} /* int rrd_cache_flush_identifier */
+
 static int rrd_cache_insert (const char *filename,
                const char *value, time_t value_time)
 {
@@ -864,9 +949,9 @@ static int rrd_cache_insert (const char *filename,
        }
 
        DEBUG ("rrdtool plugin: rrd_cache_insert: file = %s; "
-                       "values_num = %i; age = %u;",
+                       "values_num = %i; age = %lu;",
                        filename, rc->values_num,
-                       rc->last_value - rc->first_value);
+                       (unsigned long)(rc->last_value - rc->first_value));
 
        if ((rc->last_value - rc->first_value) >= cache_timeout)
        {
@@ -874,7 +959,7 @@ static int rrd_cache_insert (const char *filename,
                 * the same time, ALWAYS lock `cache_lock' first! */
                if (rc->flags != FLAG_QUEUED)
                {
-                       if (rrd_queue_cache_entry (filename) == 0)
+                       if (rrd_queue_cache_entry (filename, QUEUE_INSERT_BACK) == 0)
                                rc->flags = FLAG_QUEUED;
                }
                else
@@ -913,6 +998,11 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl)
        char         values[512];
        int          status;
 
+       if (0 != strcmp (ds->type, vl->type)) {
+               ERROR ("rrdtool plugin: DS type does not match value list type");
+               return -1;
+       }
+
        if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
                return (-1);
 
@@ -947,7 +1037,7 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl)
        return (status);
 } /* int rrd_write */
 
-static int rrd_flush (const int timeout)
+static int rrd_flush (int timeout, const char *identifier)
 {
        pthread_mutex_lock (&cache_lock);
 
@@ -956,7 +1046,8 @@ static int rrd_flush (const int timeout)
                return (0);
        }
 
-       rrd_cache_flush (timeout);
+       rrd_cache_flush_identifier (timeout, identifier);
+
        pthread_mutex_unlock (&cache_lock);
        return (0);
 } /* int rrd_flush */
index 0968e31..5ed82a8 100644 (file)
@@ -182,7 +182,7 @@ static int sensors_snprintf_chip_name (char *buf, size_t buf_size,
 
        if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
        {
-               status = snprintf (buf, buf_size,
+               status = ssnprintf (buf, buf_size,
                                "%s-isa-%04x",
                                chip->prefix,
                                chip->addr);
@@ -480,11 +480,10 @@ static void sensors_submit (const char *plugin_instance,
        value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
 
-       status = snprintf (match_key, sizeof (match_key), "%s/%s-%s",
+       status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s",
                        plugin_instance, type, type_instance);
        if (status < 1)
                return;
-       match_key[sizeof (match_key) - 1] = '\0';
 
        if (sensor_list != NULL)
        {
@@ -499,17 +498,14 @@ static void sensors_submit (const char *plugin_instance,
        vl.values_len = 1;
        vl.time = time (NULL);
 
-       strncpy (vl.host, hostname_g, sizeof (vl.host));
-       vl.host[sizeof (vl.host) - 1] = '\0';
-       strncpy (vl.plugin, "sensors", sizeof (vl.plugin));
-       vl.plugin[sizeof (vl.plugin) - 1] = '\0';
-       strncpy (vl.plugin_instance, plugin_instance,
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, plugin_instance,
                        sizeof (vl.plugin_instance));
-       vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-       vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void sensors_submit */
 
 static int sensors_read (void)
@@ -536,11 +532,9 @@ static int sensors_read (void)
                                sizeof (plugin_instance), fl->chip);
                if (status < 0)
                        continue;
-               plugin_instance[sizeof (plugin_instance) - 1] = '\0';
 
-               strncpy (type_instance, fl->data->name,
+               sstrncpy (type_instance, fl->data->name,
                                sizeof (type_instance));
-               type_instance[sizeof (type_instance) - 1] = '\0';
 
                sensors_submit (plugin_instance,
                                sensor_type_name_map[fl->type],
@@ -567,11 +561,9 @@ static int sensors_read (void)
                                sizeof (plugin_instance), fl->chip);
                if (status < 0)
                        continue;
-               plugin_instance[sizeof (plugin_instance) - 1] = '\0';
 
-               strncpy (type_instance, fl->feature->name,
+               sstrncpy (type_instance, fl->feature->name,
                                sizeof (type_instance));
-               type_instance[sizeof (type_instance) - 1] = '\0';
 
                if (fl->feature->type == SENSORS_FEATURE_IN)
                        type = "voltage";
index 4f08a34..1c874e5 100644 (file)
@@ -43,10 +43,11 @@ static void serial_submit (const char *type_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "serial", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance,
+       sstrncpy (vl.type, "serial_octets", sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance,
                        sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("serial_octets", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int serial_read (void)
index 6788ea9..d19493d 100644 (file)
@@ -198,7 +198,8 @@ static int csnmp_config_add_data_instance (data_definition_t *dd, oconfig_item_t
   else
   {
     /* Instance is a simple string */
-    strncpy (dd->instance.string, ci->values[0].value.string, DATA_MAX_NAME_LEN - 1);
+    sstrncpy (dd->instance.string, ci->values[0].value.string,
+       sizeof (dd->instance.string));
   }
 
   return (0);
@@ -779,7 +780,7 @@ static int csnmp_check_res_left_subtree (const host_definition_t *host,
     if (vb == NULL)
     {
       ERROR ("snmp plugin: host %s: Expected one more variable for "
-         "the instance..");
+         "the instance..", host->name);
       return (-1);
     }
 
@@ -833,11 +834,10 @@ static int csnmp_instance_list_add (csnmp_list_instances_t **head,
     if (instance_len > vb->val_len)
       instance_len = vb->val_len;
 
-    strncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
+    sstrncpy (il->instance, (char *) ((vb->type == ASN_OCTET_STR)
          ? vb->val.string
          : vb->val.bitstring),
        instance_len);
-    il->instance[instance_len] = '\0';
 
     for (ptr = il->instance; *ptr != '\0'; ptr++)
     {
@@ -851,10 +851,9 @@ static int csnmp_instance_list_add (csnmp_list_instances_t **head,
   else
   {
     value_t val = csnmp_value_list_to_value (vb, DS_TYPE_COUNTER, 1.0, 0.0);
-    snprintf (il->instance, sizeof (il->instance),
+    ssnprintf (il->instance, sizeof (il->instance),
        "%llu", val.counter);
   }
-  il->instance[sizeof (il->instance) - 1] = '\0';
 
   /* TODO: Debugging output */
 
@@ -907,8 +906,7 @@ static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *dat
     return (-1);
   }
 
-  strncpy (vl.host, host->name, sizeof (vl.host));
-  vl.host[sizeof (vl.host) - 1] = '\0';
+  sstrncpy (vl.host, host->name, sizeof (vl.host));
   sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
 
   vl.interval = host->interval;
@@ -972,30 +970,28 @@ static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *dat
        || (instance_list_ptr->subid == value_table_ptr[0]->subid));
 #endif
 
+    sstrncpy (vl.type, data->type, sizeof (vl.type));
+
     {
       char temp[DATA_MAX_NAME_LEN];
 
       if (instance_list_ptr == NULL)
-       snprintf (temp, sizeof (temp), "%u",
-           (uint32_t) subid);
+       ssnprintf (temp, sizeof (temp), "%u", (uint32_t) subid);
       else
-       strncpy (temp, instance_list_ptr->instance,
-           sizeof (temp));
-      temp[sizeof (temp) - 1] = '\0';
+       sstrncpy (temp, instance_list_ptr->instance, sizeof (temp));
 
       if (data->instance_prefix == NULL)
-       strncpy (vl.type_instance, temp, sizeof (vl.type_instance));
+       sstrncpy (vl.type_instance, temp, sizeof (vl.type_instance));
       else
-       snprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
+       ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%s%s",
            data->instance_prefix, temp);
-      vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
     }
 
     for (i = 0; i < data->values_len; i++)
       vl.values[i] = value_table_ptr[i]->value;
 
     /* If we get here `vl.type_instance' and all `vl.values' have been set */
-    plugin_dispatch_values (data->type, &vl);
+    plugin_dispatch_values (&vl);
 
     subid++;
   } /* while (have_more != 0) */
@@ -1299,11 +1295,10 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
       vl.values[i].gauge = NAN;
   }
 
-  strncpy (vl.host, host->name, sizeof (vl.host));
-  vl.host[sizeof (vl.host) - 1] = '\0';
+  sstrncpy (vl.host, host->name, sizeof (vl.host));
   sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin));
-  strncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
-  vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+  sstrncpy (vl.type, data->type, sizeof (vl.type));
+  sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance));
 
   vl.interval = host->interval;
 
@@ -1361,8 +1356,8 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data)
     snmp_free_pdu (res);
   res = NULL;
 
-  DEBUG ("snmp plugin: -> plugin_dispatch_values (%s, &vl);", data->type);
-  plugin_dispatch_values (data->type, &vl);
+  DEBUG ("snmp plugin: -> plugin_dispatch_values (&vl);");
+  plugin_dispatch_values (&vl);
   sfree (vl.values);
 
   return (0);
@@ -1400,8 +1395,8 @@ static int csnmp_read_host (host_definition_t *host)
   if ((time_end - time_start) > host->interval)
   {
     WARNING ("snmp plugin: Host `%s' should be queried every %i seconds, "
-       "but reading all values takes %i seconds.",
-       host->name, host->interval, time_end - time_start);
+       "but reading all values takes %lu seconds.",
+       host->name, host->interval, (unsigned long)(time_end - time_start));
   }
 
   return (0);
index 4d1f24d..f9debc1 100644 (file)
@@ -126,9 +126,10 @@ static void swap_submit (const char *type_instance, double value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.type, "swap", sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("swap", &vl);
+       plugin_dispatch_values (&vl);
 } /* void swap_submit */
 
 static int swap_read (void)
index 5b5cdb4..caca537 100644 (file)
@@ -71,10 +71,11 @@ static void tape_submit (const char *plugin_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "tape", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, plugin_instance,
+       sstrncpy (vl.plugin_instance, plugin_instance,
                        sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void tape_submit */
 
 static int tape_read (void)
index 5a8f461..655c53e 100644 (file)
@@ -137,39 +137,36 @@ static void conn_submit_port_entry (port_entry_t *pe)
   vl.time = time (NULL);
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
+  sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
 
   if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
       || (pe->flags & PORT_COLLECT_LOCAL))
   {
-    snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
+    ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
        "%hu-local", pe->port);
-    vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
 
     for (i = 1; i <= TCP_STATE_MAX; i++)
     {
       vl.values[0].gauge = pe->count_local[i];
 
-      strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
-      vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+      sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
 
-      plugin_dispatch_values ("tcp_connections", &vl);
+      plugin_dispatch_values (&vl);
     }
   }
 
   if (pe->flags & PORT_COLLECT_REMOTE)
   {
-    snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
+    ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
        "%hu-remote", pe->port);
-    vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
 
     for (i = 1; i <= TCP_STATE_MAX; i++)
     {
       vl.values[0].gauge = pe->count_remote[i];
 
-      strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
-      vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+      sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
 
-      plugin_dispatch_values ("tcp_connections", &vl);
+      plugin_dispatch_values (&vl);
     }
   }
 } /* void conn_submit */
index 52a1007..5cd427a 100644 (file)
@@ -136,12 +136,14 @@ static void tss2_submit_gauge (const char *plugin_instance,
        if (plugin_instance != NULL)
                sstrncpy (vl.plugin_instance, plugin_instance,
                                sizeof (vl.plugin_instance));
-       
+
+       sstrncpy (vl.type, type, sizeof (vl.type));
+
        if (type_instance != NULL)
                sstrncpy (vl.type_instance, type_instance,
                                sizeof (vl.type_instance));
        
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void tss2_submit_gauge */
 
 static void tss2_submit_io (const char *plugin_instance, const char *type,
@@ -165,8 +167,10 @@ static void tss2_submit_io (const char *plugin_instance, const char *type,
        if (plugin_instance != NULL)
                sstrncpy (vl.plugin_instance, plugin_instance,
                                sizeof (vl.plugin_instance));
-       
-       plugin_dispatch_values (type, &vl);
+
+       sstrncpy (vl.type, type, sizeof (vl.type));
+
+       plugin_dispatch_values (&vl);
 } /* void tss2_submit_gauge */
 
 static void tss2_close_socket (void)
@@ -372,8 +376,7 @@ static int tss2_select_vserver (FILE *read_fh, FILE *write_fh, vserver_list_t *v
        int status;
 
        /* Send request */
-       snprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
-       command[sizeof (command) - 1] = 0;
+       ssnprintf (command, sizeof (command), "sel %i\r\n", vserver->port);
 
        status = tss2_send_request (write_fh, command);
        if (status != 0)
@@ -527,9 +530,8 @@ static int tss2_read_vserver (vserver_list_t *vserver)
        else
        {
                /* Request server information */
-               snprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
+               ssnprintf (plugin_instance, sizeof (plugin_instance), "vserver%i",
                                vserver->port);
-               plugin_instance[sizeof (plugin_instance) - 1] = 0;
 
                /* Select the server */
                status = tss2_select_vserver (read_fh, write_fh, vserver);
diff --git a/src/thermal.c b/src/thermal.c
new file mode 100644 (file)
index 0000000..9378132
--- /dev/null
@@ -0,0 +1,268 @@
+/**
+ * collectd - src/thermal.c
+ * Copyright (C) 2008  MichaÅ‚ MirosÅ‚aw
+ *
+ * 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:
+ *   MichaÅ‚ MirosÅ‚aw <mirq-linux at rere.qmqm.pl>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+#include "configfile.h"
+#include "utils_ignorelist.h"
+
+#if !KERNEL_LINUX
+# error "This module is for Linux only."
+#endif
+
+const char *const dirname_sysfs = "/sys/class/thermal";
+const char *const dirname_procfs = "/proc/acpi/thermal_zone";
+
+static char force_procfs = 0;
+static ignorelist_t *device_list;
+static value_list_t vl_temp_template = VALUE_LIST_STATIC;
+static value_list_t vl_state_template = VALUE_LIST_STATIC;
+
+enum dev_type {
+       TEMP = 0,
+       COOLING_DEV
+};
+
+static void thermal_submit (const char *plugin_instance, enum dev_type dt,
+               gauge_t value)
+{
+       value_list_t vl = (dt == TEMP) ? vl_temp_template : vl_state_template;
+       value_t vt;
+
+       vt.gauge = value;
+
+       vl.values = &vt;
+       vl.time = time (NULL);
+       sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
+       sstrncpy (vl.plugin_instance, plugin_instance,
+                       sizeof(vl.plugin_instance));
+       sstrncpy (vl.type, (dt == TEMP) ? "temperature" : "gauge",
+                       sizeof (vl.type));
+
+       plugin_dispatch_values (&vl);
+}
+
+static int thermal_sysfs_device_read (const char *name)
+{
+       char filename[256];
+       char data[1024];
+       int len;
+       int ok = 0;
+
+       if (device_list && ignorelist_match (device_list, name))
+               return -1;
+
+       len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
+       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
+               return -1;
+
+       len = read_file_contents (filename, data, sizeof(data));
+       if (len > 1 && data[--len] == '\n') {
+               char *endptr = NULL;
+               double temp;
+
+               data[len] = 0;
+               errno = 0;
+               temp = strtod (data, &endptr) / 1000.0;
+
+               if (endptr == data + len && errno == 0) {
+                       thermal_submit(name, TEMP, temp);
+                       ++ok;
+               }
+       }
+
+       len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
+       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
+               return -1;
+
+       len = read_file_contents (filename, data, sizeof(data));
+       if (len > 1 && data[--len] == '\n') {
+               char *endptr = NULL;
+               double state;
+
+               data[len] = 0;
+               errno = 0;
+               state = strtod (data, &endptr);
+
+               if (endptr == data + len && errno == 0) {
+                       thermal_submit(name, COOLING_DEV, state);
+                       ++ok;
+               }
+       }
+
+       return ok ? 0 : -1;
+}
+
+static int thermal_procfs_device_read (const char *name)
+{
+       const char str_temp[] = "temperature:";
+       char filename[256];
+       char data[1024];
+       int len;
+
+       if (device_list && ignorelist_match (device_list, name))
+               return -1;
+
+       /**
+        * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
+        * temperature:             55 C
+        */
+       
+       len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
+       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
+               return -1;
+
+       len = read_file_contents (filename, data, sizeof(data));
+       if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) {
+               char *endptr = NULL;
+               double temp;
+               double celsius, add;
+               
+               if (data[--len] == 'C') {
+                       add = 0;
+                       celsius = 1;
+               } else if (data[len] == 'F') {
+                       add = -32;
+                       celsius = 5/9;
+               } else if (data[len] == 'K') {
+                       add = -273.15;
+                       celsius = 1;
+               } else
+                       return -1;
+
+               while (len > 0 && data[--len] == ' ')
+                       ;
+               data[len + 1] = 0;
+
+               while (len > 0 && data[--len] != ' ')
+                       ;
+               ++len;
+
+               errno = 0;
+               temp = (strtod (data + len, &endptr) + add) * celsius;
+
+               if (endptr != data + len && errno == 0) {
+                       thermal_submit(name, TEMP, temp);
+                       return 0;
+               }
+       }
+
+       return -1;
+}
+
+static const char *config_keys[] = {
+       "Device",
+       "IgnoreSelected",
+       "ForceUseProcfs"
+};
+
+static int thermal_config (const char *key, const char *value)
+{
+       if (device_list == NULL)
+               device_list = ignorelist_create (1);
+
+       if (strcasecmp (key, "Device") == 0)
+       {
+               if (ignorelist_add (device_list, value))
+               {
+                       ERROR ("thermal plugin: "
+                                       "Cannot add value to ignorelist.");
+                       return 1;
+               }
+       }
+       else if (strcasecmp (key, "IgnoreSelected") == 0)
+       {
+               ignorelist_set_invert (device_list, 1);
+               if ((strcasecmp (value, "True") == 0)
+                               || (strcasecmp (value, "Yes") == 0)
+                               || (strcasecmp (value, "On") == 0))
+                       ignorelist_set_invert (device_list, 0);
+       }
+       else if (strcasecmp (key, "ForceUseProcfs") == 0)
+       {
+               force_procfs = 0;
+               if ((strcasecmp (value, "True") == 0)
+                               || (strcasecmp (value, "Yes") == 0)
+                               || (strcasecmp (value, "On") == 0))
+                       force_procfs = 1;
+       }
+       else
+       {
+               return -1;
+       }
+
+       return 0;
+}
+
+static int thermal_sysfs_read (void)
+{
+       return walk_directory (dirname_sysfs, thermal_sysfs_device_read);
+}
+
+static int thermal_procfs_read (void)
+{
+       return walk_directory (dirname_procfs, thermal_procfs_device_read);
+}
+
+static int thermal_init (void)
+{
+       int ret = -1;
+
+       if (!force_procfs && access (dirname_sysfs, R_OK | X_OK) == 0) {
+               ret = plugin_register_read ("thermal", thermal_sysfs_read);
+       } else if (access (dirname_procfs, R_OK | X_OK) == 0) {
+               ret = plugin_register_read ("thermal", thermal_procfs_read);
+       }
+
+       if (!ret) {
+               vl_temp_template.values_len = 1;
+               vl_temp_template.interval = interval_g;
+               sstrncpy (vl_temp_template.host, hostname_g,
+                       sizeof(vl_temp_template.host));
+               sstrncpy (vl_temp_template.plugin, "thermal",
+                       sizeof(vl_temp_template.plugin));
+               sstrncpy (vl_temp_template.type_instance, "temperature",
+                       sizeof(vl_temp_template.type_instance));
+
+               vl_state_template = vl_temp_template;
+               sstrncpy (vl_state_template.type_instance, "cooling_state",
+                       sizeof(vl_state_template.type_instance));
+       }
+
+       return ret;
+}
+
+static int thermal_shutdown (void)
+{
+       ignorelist_free (device_list);
+
+       return 0;
+}
+
+void module_register (void)
+{
+       plugin_register_config ("thermal", thermal_config,
+                       config_keys, STATIC_ARRAY_SIZE(config_keys));
+       plugin_register_init ("thermal", thermal_init);
+       plugin_register_shutdown ("thermal", thermal_shutdown);
+}
+
index 97c170b..fabae12 100644 (file)
@@ -4,7 +4,7 @@ apache_requests         count:COUNTER:0:134217728
 apache_scoreboard      count:GAUGE:0:65535
 bitrate                        value:GAUGE:0:4294967295
 cache_result           value:COUNTER:0:4294967295
-cache_size              value:GAUGE:0:4294967295
+cache_size             value:GAUGE:0:4294967295
 charge                 value:GAUGE:0:U
 connections            value:COUNTER:0:U
 counter                        value:COUNTER:U:U
@@ -63,6 +63,12 @@ nfs_procedure                value:COUNTER:0:4294967295
 nginx_connections      value:GAUGE:0:U
 nginx_requests         value:COUNTER:0:134217728
 percent                        percent:GAUGE:0:100.1
+pg_blks                        value:COUNTER:0:U
+pg_n_tup_c             value:COUNTER:0:U
+pg_n_tup_g             value:GAUGE:0:U
+pg_numbackends         value:GAUGE:0:U
+pg_scan                        value:COUNTER:0:U
+pg_xact                        value:COUNTER:0:U
 ping                   ping:GAUGE:0:65535
 players                        value:GAUGE:0:1000000
 power                  value:GAUGE:0:U
index 3be792d..7600c34 100644 (file)
@@ -34,7 +34,7 @@ static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
 
   if (buf_len < 11)
   {
-    ERROR ("parse_ds: (buf_len = %u) < 11", buf_len);
+    ERROR ("parse_ds: (buf_len = %zu) < 11", buf_len);
     return (-1);
   }
 
@@ -62,8 +62,7 @@ static int parse_ds (data_source_t *dsrc, char *buf, size_t buf_len)
     return (-1);
   }
 
-  strncpy (dsrc->name, fields[0], sizeof (dsrc->name));
-  dsrc->name[sizeof (dsrc->name) - 1] = '\0';
+  sstrncpy (dsrc->name, fields[0], sizeof (dsrc->name));
 
   if (strcasecmp (fields[1], "GAUGE") == 0)
     dsrc->type = DS_TYPE_GAUGE;
@@ -105,8 +104,7 @@ static void parse_line (char *buf)
 
   memset (ds, '\0', sizeof (data_set_t));
 
-  strncpy (ds->type, fields[0], sizeof (ds->type));
-  ds->type[sizeof (ds->type) - 1] = '\0';
+  sstrncpy (ds->type, fields[0], sizeof (ds->type));
 
   ds->ds_num = fields_num - 1;
   ds->ds = (data_source_t *) calloc (ds->ds_num, sizeof (data_source_t));
index d80091b..d34b91f 100644 (file)
@@ -86,8 +86,8 @@ static int us_open_socket (void)
 
        memset (&sa, '\0', sizeof (sa));
        sa.sun_family = AF_UNIX;
-       strncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
-                       sizeof (sa.sun_path) - 1);
+       sstrncpy (sa.sun_path, (sock_file != NULL) ? sock_file : US_DEFAULT_PATH,
+                       sizeof (sa.sun_path));
        /* unlink (sa.sun_path); */
 
        DEBUG ("unixsock plugin: socket path = %s", sa.sun_path);
index d0bd63c..1231075 100644 (file)
@@ -51,8 +51,9 @@ static void users_submit (gauge_t value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "users", sizeof (vl.plugin));
+       sstrncpy (vl.type, "users", sizeof (vl.plugin));
 
-       plugin_dispatch_values ("users", &vl);
+       plugin_dispatch_values (&vl);
 } /* void users_submit */
 
 static int users_read (void)
index 9f7e3b6..d0e77ef 100644 (file)
@@ -156,13 +156,12 @@ static int uc_send_notification (const char *name)
     return (-1);
   }
 
-  snprintf (n.message, sizeof (n.message),
+  ssnprintf (n.message, sizeof (n.message),
       "%s has not been updated for %i seconds.", name,
       (int) (n.time - ce->last_update));
 
   pthread_mutex_unlock (&cache_lock);
 
-  n.message[sizeof (n.message) - 1] = '\0';
   plugin_dispatch_notification (&n);
 
   return (0);
@@ -325,7 +324,7 @@ int uc_check_timeout (void)
     }
     else
     {
-      WARNING ("uc_check_timeout: ut_check_interesting (%s) returned ",
+      WARNING ("uc_check_timeout: ut_check_interesting (%s) returned "
          "invalid status %i.",
          keys[i], status);
     }
@@ -450,10 +449,9 @@ int uc_update (const data_set_t *ds, const value_list_t *vl)
   n.severity = NOTIF_OKAY;
   n.time = vl->time;
 
-  snprintf (n.message, sizeof (n.message),
+  ssnprintf (n.message, sizeof (n.message),
       "Received a value for %s. It was missing for %u seconds.",
       name, (unsigned int) update_delay);
-  n.message[sizeof (n.message) - 1] = '\0';
 
   plugin_dispatch_notification (&n);
 
@@ -524,7 +522,7 @@ gauge_t *uc_get_rate (const data_set_t *ds, const value_list_t *vl)
   if (ret_num != ds->ds_num)
   {
     ERROR ("utils_cache: uc_get_rate: ds[%s] has %i values, "
-       "but uc_get_rate_by_name returned %i.",
+       "but uc_get_rate_by_name returned %zu.",
        ds->type, ds->ds_num, ret_num);
     sfree (ret);
     return (NULL);
index 6fa8b7b..7feaac2 100644 (file)
                return -1; \
        }
 
+static int add_to_array (char ***array, int *array_num, char *value)
+{
+       char **temp;
+
+       temp = (char **) realloc (*array, sizeof (char *) * (*array_num + 1));
+       if (temp == NULL)
+               return (-1);
+
+       *array = temp;
+       (*array)[*array_num] = value;
+       (*array_num)++;
+
+       return (0);
+} /* int add_to_array */
+
 int handle_flush (FILE *fh, char **fields, int fields_num)
 {
        int success = 0;
        int error   = 0;
 
        int timeout = -1;
+       char **plugins = NULL;
+       int plugins_num = 0;
+       char **identifiers = NULL;
+       int identifiers_num = 0;
 
        int i;
 
@@ -49,12 +68,17 @@ int handle_flush (FILE *fh, char **fields, int fields_num)
 
                if (strncasecmp ("plugin=", option, strlen ("plugin=")) == 0)
                {
-                       char *plugin = option + strlen ("plugin=");
+                       char *plugin;
+                       
+                       plugin = option + strlen ("plugin=");
+                       add_to_array (&plugins, &plugins_num, plugin);
+               }
+               else if (strncasecmp ("identifier=", option, strlen ("identifier=")) == 0)
+               {
+                       char *identifier;
 
-                       if (0 == plugin_flush_one (timeout, plugin))
-                               ++success;
-                       else
-                               ++error;
+                       identifier = option + strlen ("identifier=");
+                       add_to_array (&identifiers, &identifiers_num, identifier);
                }
                else if (strncasecmp ("timeout=", option, strlen ("timeout=")) == 0)
                {
@@ -79,6 +103,35 @@ int handle_flush (FILE *fh, char **fields, int fields_num)
                }
        }
 
+       /* Add NULL entries for `any plugin' and/or `any value' if nothing was
+        * specified. */
+       if (plugins_num == 0)
+               add_to_array (&plugins, &plugins_num, NULL);
+
+       if (identifiers_num == 0)
+               add_to_array (&identifiers, &identifiers_num, NULL);
+
+       for (i = 0; i < plugins_num; i++)
+       {
+               char *plugin;
+               int j;
+
+               plugin = plugins[i];
+
+               for (j = 0; j < identifiers_num; j++)
+               {
+                       char *identifier;
+                       int status;
+
+                       identifier = identifiers[j];
+                       status = plugin_flush (plugin, timeout, identifier);
+                       if (status == 0)
+                               success++;
+                       else
+                               error++;
+               }
+       }
+
        if ((success + error) > 0)
        {
                print_to_socket (fh, "0 Done: %i successful, %i errors\n",
index 16a09b1..9cbba12 100644 (file)
@@ -32,7 +32,6 @@
        }
 
 static int parse_value (const data_set_t *ds, value_list_t *vl,
-               const char *type,
                FILE *fh, char *buffer)
 {
        char *dummy;
@@ -90,7 +89,7 @@ static int parse_value (const data_set_t *ds, value_list_t *vl,
                return (-1);
        }
 
-       plugin_dispatch_values (type, vl);
+       plugin_dispatch_values (vl);
        return (0);
 } /* int parse_value */
 
@@ -173,6 +172,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
 
        sstrncpy (vl.host, hostname, sizeof (vl.host));
        sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (plugin_instance != NULL)
                sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
        if (type_instance != NULL)
@@ -201,7 +201,7 @@ int handle_putval (FILE *fh, char **fields, int fields_num)
                        /* It's parse_value's job to write an error to `fh'.
                         * This is not the case with `parse_option below.
                         * Neither will write an success message. */
-                       if (parse_value (ds, &vl, type, fh, fields[i]) != 0)
+                       if (parse_value (ds, &vl, fh, fields[i]) != 0)
                                break;
                }
                else if (strchr (fields[i], '=') != NULL)
diff --git a/src/utils_complain.c b/src/utils_complain.c
new file mode 100644 (file)
index 0000000..118245c
--- /dev/null
@@ -0,0 +1,101 @@
+/**
+ * collectd - src/utils_complain.c
+ * Copyright (C) 2006-2007  Florian octo Forster
+ * Copyright (C) 2008  Sebastian tokkee 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
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ *   Sebastian tokkee Harl <sh at tokkee.org>
+ **/
+
+#include "utils_complain.h"
+#include "plugin.h"
+
+/* vcomplain returns 0 if it did not report, 1 else */
+static int vcomplain (int level, c_complain_t *c,
+               const char *format, va_list ap)
+{
+       time_t now;
+       char   message[512];
+
+       now = time (NULL);
+
+       if (c->last + c->interval > now)
+               return 0;
+
+       c->last = now;
+
+       if (c->interval < interval_g)
+               c->interval = interval_g;
+       else
+               c->interval *= 2;
+
+       if (c->interval > 86400)
+               c->interval = 86400;
+
+       vsnprintf (message, sizeof (message), format, ap);
+       message[sizeof (message) - 1] = '\0';
+
+       plugin_log (level, message);
+       return 1;
+} /* vcomplain */
+
+void c_complain (int level, c_complain_t *c, const char *format, ...)
+{
+       va_list ap;
+
+       /* reset the old interval */
+       if (c->interval < 0)
+               c->interval *= -1;
+
+       va_start (ap, format);
+       vcomplain (level, c, format, ap);
+       va_end (ap);
+} /* c_complain */
+
+void c_complain_once (int level, c_complain_t *c, const char *format, ...)
+{
+       va_list ap;
+
+       if (c->interval < 0)
+               return;
+
+       va_start (ap, format);
+       if (vcomplain (level, c, format, ap))
+               c->interval *= -1;
+       va_end (ap);
+} /* c_complain_once */
+
+void c_do_release (int level, c_complain_t *c, const char *format, ...)
+{
+       char message[512];
+       va_list ap;
+
+       if (c->interval == 0)
+               return;
+
+       c->interval = 0;
+
+       va_start (ap, format);
+       vsnprintf (message, sizeof (message), format, ap);
+       message[sizeof (message) - 1] = '\0';
+       va_end (ap);
+
+       plugin_log (level, message);
+} /* c_release */
+
+/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
+
diff --git a/src/utils_complain.h b/src/utils_complain.h
new file mode 100644 (file)
index 0000000..e93d823
--- /dev/null
@@ -0,0 +1,96 @@
+/**
+ * collectd - src/utils_complain.h
+ * Copyright (C) 2006-2007  Florian octo Forster
+ * Copyright (C) 2008  Sebastian tokkee 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
+ *
+ * Authors:
+ *   Florian octo Forster <octo at verplant.org>
+ *   Sebastian tokkee Harl <sh at tokkee.org>
+ **/
+
+#ifndef UTILS_COMPLAIN_H
+#define UTILS_COMPLAIN_H 1
+
+#include <time.h>
+
+typedef struct
+{
+       /* time of the last report */
+       time_t last;
+
+       /* how long to wait until reporting again
+        *   0 indicates that the complaint is no longer valid
+        * < 0 indicates that the complaint has been reported once
+        *     => c_complain_once will not report again
+        *     => c_complain uses the absolute value to reset the old value */
+       int interval;
+} c_complain_t;
+
+#define C_COMPLAIN_INIT { 0, 0 }
+
+/*
+ * NAME
+ *   c_complain
+ *
+ * DESCRIPTION
+ *   Complain about something. This function will report a message (usually
+ *   indicating some error condition) using the collectd logging mechanism.
+ *   When this function is called again, reporting the message again will be
+ *   deferred by an increasing interval (up to one day) to prevent flooding
+ *   the logs. A call to `c_release' resets the counter.
+ *
+ * PARAMETERS
+ *   `level'  The log level passed to `plugin_log'.
+ *   `c'      Identifier for the complaint.
+ *   `format' Message format - see the documentation of printf(3).
+ */
+void c_complain (int level, c_complain_t *c, const char *format, ...);
+
+/*
+ * NAME
+ *   c_complain_once
+ *
+ * DESCRIPTION
+ *   Complain about something once. This function will not report anything
+ *   again, unless `c_release' has been called in between. If used after some
+ *   calls to `c_complain', it will report again on the next interval and stop
+ *   after that.
+ *
+ *   See `c_complain' for further details and a description of the parameters.
+ */
+void c_complain_once (int level, c_complain_t *c, const char *format, ...);
+
+/*
+ * NAME
+ *   c_release
+ *
+ * DESCRIPTION
+ *   Release a complaint. This will report a message once, marking the
+ *   complaint as released.
+ *
+ *   See `c_complain' for a description of the parameters.
+ */
+void c_do_release (int level, c_complain_t *c, const char *format, ...);
+#define c_release(level, c, ...) \
+       do { \
+               if ((c)->interval != 0) \
+                       c_do_release(level, c, __VA_ARGS__); \
+       } while (0)
+
+#endif /* UTILS_COMPLAIN_H */
+
+/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
+
index 9b00151..f2ab337 100644 (file)
@@ -34,6 +34,7 @@
  */
 
 #include "collectd.h"
+#include "common.h"
 
 #if HAVE_NETINET_IN_SYSTM_H
 # include <netinet/in_systm.h>
@@ -384,7 +385,7 @@ handle_dns(const char *buf, int len,
     if (0 != x)
        return 0;
     if ('\0' == qh.qname[0])
-       strncpy (qh.qname, ".", sizeof (qh.qname));
+       sstrncpy (qh.qname, ".", sizeof (qh.qname));
     while ((t = strchr(qh.qname, '\n')))
        *t = ' ';
     while ((t = strchr(qh.qname, '\r')))
@@ -815,8 +816,7 @@ const char *qtype_str(int t)
            case T_ANY:         return ("ANY"); /* ... 255 */
 #endif /* __BIND >= 19950621 */
            default:
-                   snprintf (buf, 32, "#%i", t);
-                   buf[31] = '\0';
+                   ssnprintf (buf, sizeof (buf), "#%i", t);
                    return (buf);
     }; /* switch (t) */
     /* NOTREACHED */
@@ -843,7 +843,7 @@ const char *opcode_str (int o)
        return "Update";
        break;
     default:
-       snprintf(buf, 30, "Opcode%d", o);
+       ssnprintf(buf, sizeof (buf), "Opcode%d", o);
        return buf;
     }
     /* NOTREACHED */
@@ -887,8 +887,7 @@ const char *rcode_str (int rcode)
 #endif  /* RFC2136 rcodes */
 #endif /* __BIND >= 19950621 */
                default:
-                       snprintf (buf, 32, "RCode%i", rcode);
-                       buf[31] = '\0';
+                       ssnprintf (buf, sizeof (buf), "RCode%i", rcode);
                        return (buf);
        }
        /* Never reached */
index 1d9467f..518715b 100644 (file)
@@ -310,7 +310,7 @@ int ignorelist_add (ignorelist_t *il, const char *entry)
                /* We need to copy `entry' since it's const */
                entry_copy = smalloc (entry_len);
                memset (entry_copy, '\0', entry_len);
-               strncpy (entry_copy, entry + 1, entry_len - 2);
+               sstrncpy (entry_copy, entry + 1, entry_len - 2);
 
                DEBUG("I'm about to add regex entry: %s", entry_copy);
                ret = ignorelist_append_regex(il, entry_copy);
index 44ad7ea..c53431f 100644 (file)
@@ -260,7 +260,7 @@ uuidcache_init(void)
                        * (This is useful, if the cdrom on /dev/hdc must not
                        * be accessed.)
                        */
-                               snprintf(device, sizeof(device), "%s/%s",
+                               ssnprintf(device, sizeof(device), "%s/%s",
                                        DEVLABELDIR, ptname);
                                if(!get_label_uuid(device, &label, uuid)) {
                                        uuidcache_addentry(sstrdup(device),
@@ -769,8 +769,6 @@ cu_mount_getoptionvalue(char *line, char *keyword)
        return r;
 } /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
 
-
-
 int
 cu_mount_type(const char *type)
 {
@@ -782,5 +780,3 @@ cu_mount_type(const char *type)
        return CUMT_UNKNOWN;
 } /* int cu_mount_type(const char *type) */
 
-
-
index 34fe2dc..f518b1c 100644 (file)
@@ -85,10 +85,11 @@ static int simple_submit_match (cu_match_t *match, void *user_data)
   sstrncpy (vl.plugin, data->plugin, sizeof (vl.plugin));
   sstrncpy (vl.plugin_instance, data->plugin_instance,
       sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, data->type, sizeof (vl.type));
   sstrncpy (vl.type_instance, data->type_instance,
       sizeof (vl.type_instance));
 
-  plugin_dispatch_values (data->type, &vl);
+  plugin_dispatch_values (&vl);
 
   if (match_value->ds_type & UTILS_MATCH_DS_TYPE_GAUGE)
   {
index 4da8943..bea5bfb 100644 (file)
@@ -184,9 +184,8 @@ static int ut_config_type_instance (threshold_t *th, oconfig_item_t *ci)
     return (-1);
   }
 
-  strncpy (th->type_instance, ci->values[0].value.string,
+  sstrncpy (th->type_instance, ci->values[0].value.string,
       sizeof (th->type_instance));
-  th->type_instance[sizeof (th->type_instance) - 1] = '\0';
 
   return (0);
 } /* int ut_config_type_instance */
@@ -284,8 +283,7 @@ static int ut_config_type (const threshold_t *th_orig, oconfig_item_t *ci)
   }
 
   memcpy (&th, th_orig, sizeof (th));
-  strncpy (th.type, ci->values[0].value.string, sizeof (th.type));
-  th.type[sizeof (th.type) - 1] = '\0';
+  sstrncpy (th.type, ci->values[0].value.string, sizeof (th.type));
 
   th.warning_min = NAN;
   th.warning_max = NAN;
@@ -340,9 +338,8 @@ static int ut_config_plugin_instance (threshold_t *th, oconfig_item_t *ci)
     return (-1);
   }
 
-  strncpy (th->plugin_instance, ci->values[0].value.string,
+  sstrncpy (th->plugin_instance, ci->values[0].value.string,
       sizeof (th->plugin_instance));
-  th->plugin_instance[sizeof (th->plugin_instance) - 1] = '\0';
 
   return (0);
 } /* int ut_config_plugin_instance */
@@ -369,8 +366,7 @@ static int ut_config_plugin (const threshold_t *th_orig, oconfig_item_t *ci)
   }
 
   memcpy (&th, th_orig, sizeof (th));
-  strncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
-  th.plugin[sizeof (th.plugin) - 1] = '\0';
+  sstrncpy (th.plugin, ci->values[0].value.string, sizeof (th.plugin));
 
   for (i = 0; i < ci->children_num; i++)
   {
@@ -417,8 +413,7 @@ static int ut_config_host (const threshold_t *th_orig, oconfig_item_t *ci)
   }
 
   memcpy (&th, th_orig, sizeof (th));
-  strncpy (th.host, ci->values[0].value.string, sizeof (th.host));
-  th.host[sizeof (th.host) - 1] = '\0';
+  sstrncpy (th.host, ci->values[0].value.string, sizeof (th.host));
 
   for (i = 0; i < ci->children_num; i++)
   {
@@ -501,46 +496,45 @@ int ut_config (const oconfig_item_t *ci)
  */
 /* }}} */
 
-static threshold_t *threshold_search (const data_set_t *ds,
-    const value_list_t *vl)
+static threshold_t *threshold_search (const value_list_t *vl)
 {
   threshold_t *th;
 
   if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
-         ds->type, vl->type_instance)) != NULL)
+         vl->type, vl->type_instance)) != NULL)
     return (th);
   else if ((th = threshold_get (vl->host, vl->plugin, vl->plugin_instance,
-         ds->type, NULL)) != NULL)
+         vl->type, NULL)) != NULL)
     return (th);
   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
-         ds->type, vl->type_instance)) != NULL)
+         vl->type, vl->type_instance)) != NULL)
     return (th);
   else if ((th = threshold_get (vl->host, vl->plugin, NULL,
-         ds->type, NULL)) != NULL)
+         vl->type, NULL)) != NULL)
     return (th);
   else if ((th = threshold_get (vl->host, "", NULL,
-         ds->type, vl->type_instance)) != NULL)
+         vl->type, vl->type_instance)) != NULL)
     return (th);
   else if ((th = threshold_get (vl->host, "", NULL,
-         ds->type, NULL)) != NULL)
+         vl->type, NULL)) != NULL)
     return (th);
   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
-         ds->type, vl->type_instance)) != NULL)
+         vl->type, vl->type_instance)) != NULL)
     return (th);
   else if ((th = threshold_get ("", vl->plugin, vl->plugin_instance,
-         ds->type, NULL)) != NULL)
+         vl->type, NULL)) != NULL)
     return (th);
   else if ((th = threshold_get ("", vl->plugin, NULL,
-         ds->type, vl->type_instance)) != NULL)
+         vl->type, vl->type_instance)) != NULL)
     return (th);
   else if ((th = threshold_get ("", vl->plugin, NULL,
-         ds->type, NULL)) != NULL)
+         vl->type, NULL)) != NULL)
     return (th);
   else if ((th = threshold_get ("", "", NULL,
-         ds->type, vl->type_instance)) != NULL)
+         vl->type, vl->type_instance)) != NULL)
     return (th);
   else if ((th = threshold_get ("", "", NULL,
-         ds->type, NULL)) != NULL)
+         vl->type, NULL)) != NULL)
     return (th);
 
   return (NULL);
@@ -597,35 +591,43 @@ static int ut_report_state (const data_set_t *ds,
 
   n.time = vl->time;
 
-  status = snprintf (buf, bufsize, "Host %s, plugin %s",
+  status = ssnprintf (buf, bufsize, "Host %s, plugin %s",
       vl->host, vl->plugin);
   buf += status;
   bufsize -= status;
 
   if (vl->plugin_instance[0] != '\0')
   {
-    status = snprintf (buf, bufsize, " (instance %s)",
+    status = ssnprintf (buf, bufsize, " (instance %s)",
        vl->plugin_instance);
     buf += status;
     bufsize -= status;
   }
 
-  status = snprintf (buf, bufsize, " type %s", ds->type);
+  status = ssnprintf (buf, bufsize, " type %s", vl->type);
   buf += status;
   bufsize -= status;
 
   if (vl->type_instance[0] != '\0')
   {
-    status = snprintf (buf, bufsize, " (instance %s)",
+    status = ssnprintf (buf, bufsize, " (instance %s)",
        vl->type_instance);
     buf += status;
     bufsize -= status;
   }
 
+  plugin_notification_meta_add_string (&n, "DataSource",
+      ds->ds[ds_index].name);
+  plugin_notification_meta_add_double (&n, "CurrentValue", values[ds_index]);
+  plugin_notification_meta_add_double (&n, "WarningMin", th->warning_min);
+  plugin_notification_meta_add_double (&n, "WarningMax", th->warning_max);
+  plugin_notification_meta_add_double (&n, "FailureMin", th->failure_min);
+  plugin_notification_meta_add_double (&n, "FailureMax", th->failure_max);
+
   /* Send an okay notification */
   if (state == STATE_OKAY)
   {
-    status = snprintf (buf, bufsize, ": All data sources are within range again.");
+    status = ssnprintf (buf, bufsize, ": All data sources are within range again.");
     buf += status;
     bufsize -= status;
   }
@@ -641,7 +643,7 @@ static int ut_report_state (const data_set_t *ds,
     {
       if (!isnan (min) && !isnan (max))
       {
-       status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
+       status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
            "%f. That is within the %s region of %f and %f.",
            ds->ds[ds_index].name, values[ds_index],
            (state == STATE_ERROR) ? "failure" : "warning",
@@ -649,7 +651,7 @@ static int ut_report_state (const data_set_t *ds,
       }
       else
       {
-       status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
+       status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
            "%f. That is %s the %s threshold of %f.",
            ds->ds[ds_index].name, values[ds_index],
            isnan (min) ? "below" : "above",
@@ -659,7 +661,7 @@ static int ut_report_state (const data_set_t *ds,
     }
     else /* is not inverted */
     {
-      status = snprintf (buf, bufsize, ": Data source \"%s\" is currently "
+      status = ssnprintf (buf, bufsize, ": Data source \"%s\" is currently "
          "%f. That is %s the %s threshold of %f.",
          ds->ds[ds_index].name, values[ds_index],
          (values[ds_index] < min) ? "below" : "above",
@@ -672,6 +674,7 @@ static int ut_report_state (const data_set_t *ds,
 
   plugin_dispatch_notification (&n);
 
+  plugin_notification_meta_free (&n);
   return (0);
 } /* }}} int ut_report_state */
 
@@ -783,7 +786,7 @@ int ut_check_threshold (const data_set_t *ds, const value_list_t *vl)
   /* Is this lock really necessary? So far, thresholds are only inserted at
    * startup. -octo */
   pthread_mutex_lock (&threshold_lock);
-  th = threshold_search (ds, vl);
+  th = threshold_search (vl);
   pthread_mutex_unlock (&threshold_lock);
   if (th == NULL)
     return (0);
@@ -865,27 +868,19 @@ int ut_check_interesting (const char *name)
   memset (&ds, '\0', sizeof (ds));
   memset (&vl, '\0', sizeof (vl));
 
-  strncpy (vl.host, host, sizeof (vl.host));
-  vl.host[sizeof (vl.host) - 1] = '\0';
-  strncpy (vl.plugin, plugin, sizeof (vl.plugin));
-  vl.plugin[sizeof (vl.plugin) - 1] = '\0';
+  sstrncpy (vl.host, host, sizeof (vl.host));
+  sstrncpy (vl.plugin, plugin, sizeof (vl.plugin));
   if (plugin_instance != NULL)
-  {
-    strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
-    vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
-  }
-  strncpy (ds.type, type, sizeof (ds.type));
-  ds.type[sizeof (ds.type) - 1] = '\0';
+    sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+  sstrncpy (ds.type, type, sizeof (ds.type));
+  sstrncpy (vl.type, type, sizeof (vl.type));
   if (type_instance != NULL)
-  {
-    strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
-    vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-  }
+    sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
   sfree (name_copy);
   host = plugin = plugin_instance = type = type_instance = NULL;
 
-  th = threshold_search (&ds, &vl);
+  th = threshold_search (&vl);
   if (th == NULL)
     return (0);
   if ((th->flags & UT_FLAG_PERSIST) == 0)
index d54301a..e0de0d9 100644 (file)
@@ -257,8 +257,7 @@ uuid_init (void)
     char *uuid = uuid_get_local ();
 
     if (uuid) {
-        strncpy (hostname_g, uuid, DATA_MAX_NAME_LEN);
-        hostname_g[DATA_MAX_NAME_LEN-1] = '\0';
+        sstrncpy (hostname_g, uuid, DATA_MAX_NAME_LEN);
         sfree (uuid);
         return 0;
     }
index 5341e15..25f7c70 100644 (file)
@@ -50,10 +50,11 @@ static void submit (const char *plugin_instance, const char *type,
   sstrncpy (vl.plugin, "vmem", sizeof (vl.plugin));
   if (plugin_instance != NULL)
     sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, type, sizeof (vl.type));
   if (type_instance != NULL)
     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-  plugin_dispatch_values (type, &vl);
+  plugin_dispatch_values (&vl);
 } /* void vmem_submit */
 
 static void submit_two (const char *plugin_instance, const char *type,
index 5484344..413674b 100644 (file)
@@ -59,10 +59,11 @@ static void traffic_submit (const char *plugin_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, "if_octets", sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values ("if_octets", &vl);
+       plugin_dispatch_values (&vl);
 } /* void traffic_submit */
 
 static void load_submit (const char *plugin_instance,
@@ -80,9 +81,10 @@ static void load_submit (const char *plugin_instance,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+       sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, "load", sizeof (vl.type));
 
-       plugin_dispatch_values ("load", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static void submit_gauge (const char *plugin_instance, const char *type,
@@ -99,10 +101,11 @@ static void submit_gauge (const char *plugin_instance, const char *type,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "vserver", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
-       strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
+       sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void submit_gauge */
 
 static inline long long __get_sock_bytes(const char *s)
@@ -147,8 +150,9 @@ static int vserver_read (void)
                        continue;
 
                /* socket message accounting */
-               len = snprintf (file, BUFSIZE, PROCDIR "/%s/cacct", dent->d_name);
-               if ((len < 0) || (len >= BUFSIZE))
+               len = ssnprintf (file, sizeof (file),
+                               PROCDIR "/%s/cacct", dent->d_name);
+               if ((len < 0) || (len >= sizeof (file)))
                        continue;
 
                if (NULL == (fh = fopen (file, "r")))
@@ -194,8 +198,9 @@ static int vserver_read (void)
                }
 
                /* thread information and load */
-               len = snprintf (file, BUFSIZE, PROCDIR "/%s/cvirt", dent->d_name);
-               if ((len < 0) || (len >= BUFSIZE))
+               len = ssnprintf (file, sizeof (file),
+                               PROCDIR "/%s/cvirt", dent->d_name);
+               if ((len < 0) || (len >= sizeof (file)))
                        continue;
 
                if (NULL == (fh = fopen (file, "r")))
@@ -246,8 +251,9 @@ static int vserver_read (void)
                }
 
                /* processes and memory usage */
-               len = snprintf (file, BUFSIZE, PROCDIR "/%s/limit", dent->d_name);
-               if ((len < 0) || (len >= BUFSIZE))
+               len = ssnprintf (file, sizeof (file),
+                               PROCDIR "/%s/limit", dent->d_name);
+               if ((len < 0) || (len >= sizeof (file)))
                        continue;
 
                if (NULL == (fh = fopen (file, "r")))
index 0edaf45..21bbcb4 100644 (file)
@@ -58,10 +58,11 @@ static void wireless_submit (const char *plugin_instance, const char *type,
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "wireless", sizeof (vl.plugin));
-       strncpy (vl.plugin_instance, plugin_instance,
+       sstrncpy (vl.plugin_instance, plugin_instance,
                        sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void wireless_submit */
 
 #define POWER_MIN -90.0
index 7f3edf0..09786fc 100644 (file)
@@ -39,8 +39,9 @@ static void cxmms_submit (const char *type, gauge_t value)
        vl.time = time (NULL);
        sstrncpy (vl.host, hostname_g, sizeof (vl.host));
        sstrncpy (vl.plugin, "xmms", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof (vl.type));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void cxmms_submit */
 
 int cxmms_read (void)