Merge pull request #3339 from jkohen/patch-1
[collectd.git] / contrib / examples / MyPlugin.pm
index 1b98d5b..b1a8a6d 100644 (file)
@@ -17,25 +17,34 @@ package Collectd::Plugin::MyPlugin;
 use strict;
 use warnings;
 
+use Collectd qw( :all );
+
 # data set definition:
 # see section "DATA TYPES" in collectd-perl(5) for details
+#
+# NOTE: If you're defining a custom data-set, you have to make that known to
+# any servers as well. Else, the server is not able to store values using the
+# type defined by that data-set.
+# It is strongly recommended to use one of the types and data-sets pre-defined
+# in the types.db file.
 my $dataset =
 [
        {
                name => 'my_ds',
-               type => Collectd::DS_TYPE_GAUGE,
+               type => DS_TYPE_GAUGE,
                min  => 0,
                max  => 65535,
        },
 ];
 
 # This code is executed after loading the plugin to register it with collectd.
-Collectd::plugin_register (Collectd::TYPE_LOG, 'myplugin', \&my_log);
-Collectd::plugin_register (Collectd::TYPE_DATASET, 'myplugin', $dataset);
-Collectd::plugin_register (Collectd::TYPE_INIT, 'myplugin', \&my_init);
-Collectd::plugin_register (Collectd::TYPE_READ, 'myplugin', \&my_read);
-Collectd::plugin_register (Collectd::TYPE_WRITE, 'myplugin', \&my_write);
-Collectd::plugin_register (Collectd::TYPE_SHUTDOWN, 'myplugin', \&my_shutdown);
+plugin_register (TYPE_LOG, 'myplugin', 'my_log');
+plugin_register (TYPE_NOTIF, 'myplugin', 'my_notify');
+plugin_register (TYPE_DATASET, 'mytype', $dataset);
+plugin_register (TYPE_INIT, 'myplugin', 'my_init');
+plugin_register (TYPE_READ, 'myplugin', 'my_read');
+plugin_register (TYPE_WRITE, 'myplugin', 'my_write');
+plugin_register (TYPE_SHUTDOWN, 'myplugin', 'my_shutdown');
 
 # For each of the functions below see collectd-perl(5) for details about
 # arguments and the like.
@@ -59,15 +68,17 @@ sub my_read
 
        # do the magic to read the data:
        # the number of values has to match the number of data sources defined in
-       # the registered data set
+       # the registered data set. The type used here (in this example:
+       # "mytype") must be defined in the types.db, see types.db(5) for
+       # details, or registered as "TYPE_DATASET".
        $vl->{'values'} = [ rand(65535) ];
        $vl->{'plugin'} = 'myplugin';
+       $vl->{'type'}   = 'mytype';
        # any other elements are optional
 
        # dispatch the values to collectd which passes them on to all registered
-       # write functions - the first argument is used to lookup the data set
-       # definition
-       Collectd::plugin_dispatch_values ('myplugin', $vl);
+       # write functions
+       plugin_dispatch_values ($vl);
 
        # A false return value indicates an error and the plugin will be skipped
        # for an increasing amount of time.
@@ -82,8 +93,7 @@ sub my_write
        my $vl   = shift;
 
        if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
-               Collectd::plugin_log (Collectd::LOG_WARNING,
-                       "DS number does not match values length");
+               plugin_log (LOG_WARNING, "DS number does not match values length");
                return;
        }
 
@@ -123,3 +133,38 @@ sub my_log
        return 1;
 } # my_log ()
 
+# This function is called when plugin_dispatch_notification () has been used
+sub my_notify
+{
+       my $notif = shift;
+
+       my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($notif->{'time'});
+
+       printf "NOTIF (%04d-%02d-%02d %02d:%02d:%02d): %d - ",
+                       $year + 1900, $mon + 1, $mday, $hour, $min, $sec,
+                       $notif->{'severity'};
+
+       if (defined $notif->{'host'}) {
+               print "$notif->{'host'}: ";
+       }
+
+       if (defined $notif->{'plugin'}) {
+               print "$notif->{'plugin'}: ";
+       }
+
+       if (defined $notif->{'plugin_instance'}) {
+               print "$notif->{'plugin_instance'}: ";
+       }
+
+       if (defined $notif->{'type'}) {
+               print "$notif->{'type'}: ";
+       }
+
+       if (defined $notif->{'type_instance'}) {
+               print "$notif->{'type_instance'}: ";
+       }
+
+       print "$notif->{'message'}\n";
+       return 1;
+} # my_notify ()
+