1 # /usr/share/doc/collectd/examples/MyPlugin.pm
3 # A Perl plugin template for collectd.
5 # Written by Sebastian Harl <sh@tokkee.org>
7 # This is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free
9 # Software Foundation; only version 2 of the License is applicable.
12 # - each of the functions below (and the corresponding plugin_register call)
15 package Collectd::Plugin::MyPlugin;
20 use Collectd qw( :all );
22 # data set definition:
23 # see section "DATA TYPES" in collectd-perl(5) for details
24 # (take a look at the types.db file for a large list of predefined data-sets)
29 type => DS_TYPE_GAUGE,
35 # This code is executed after loading the plugin to register it with collectd.
36 plugin_register (TYPE_LOG, 'myplugin', 'my_log');
37 plugin_register (TYPE_NOTIF, 'myplugin', 'my_notify');
38 plugin_register (TYPE_DATASET, 'myplugin', $dataset);
39 plugin_register (TYPE_INIT, 'myplugin', 'my_init');
40 plugin_register (TYPE_READ, 'myplugin', 'my_read');
41 plugin_register (TYPE_WRITE, 'myplugin', 'my_write');
42 plugin_register (TYPE_SHUTDOWN, 'myplugin', 'my_shutdown');
44 # For each of the functions below see collectd-perl(5) for details about
45 # arguments and the like.
47 # This function is called once upon startup to initialize the plugin.
50 # open sockets, initialize data structures, ...
52 # A false return value indicates an error and causes the plugin to be
57 # This function is called in regular intervals to collectd the data.
60 # value list to dispatch to collectd:
61 # see section "DATA TYPES" in collectd-perl(5) for details
64 # do the magic to read the data:
65 # the number of values has to match the number of data sources defined in
66 # the registered data set
67 $vl->{'values'} = [ rand(65535) ];
68 $vl->{'plugin'} = 'myplugin';
69 # any other elements are optional
71 # dispatch the values to collectd which passes them on to all registered
72 # write functions - the first argument is used to lookup the data set
74 plugin_dispatch_values ('myplugin', $vl);
76 # A false return value indicates an error and the plugin will be skipped
77 # for an increasing amount of time.
81 # This function is called after values have been dispatched to collectd.
88 if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
89 plugin_log (LOG_WARNING, "DS number does not match values length");
93 for (my $i = 0; $i < scalar (@$ds); ++$i) {
94 # do the magic to output the data
95 print "$vl->{'host'}: $vl->{'plugin'}: ";
97 if (defined $vl->{'plugin_instance'}) {
98 print "$vl->{'plugin_instance'}: ";
103 if (defined $vl->{'type_instance'}) {
104 print "$vl->{'type_instance'}: ";
107 print "$vl->{'values'}->[$i]\n";
112 # This function is called before shutting down collectd.
119 # This function is called when plugin_log () has been used.
125 print "LOG: $level - $msg\n";
129 # This function is called when plugin_dispatch_notification () has been used
134 my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($notif->{'time'});
136 printf "NOTIF (%04d-%02d-%02d %02d:%02d:%02d): %d - ",
137 $year + 1900, $mon + 1, $mday, $hour, $min, $sec,
138 $notif->{'severity'};
140 if (defined $notif->{'host'}) {
141 print "$notif->{'host'}: ";
144 if (defined $notif->{'plugin'}) {
145 print "$notif->{'plugin'}: ";
148 if (defined $notif->{'plugin_instance'}) {
149 print "$notif->{'plugin_instance'}: ";
152 if (defined $notif->{'type'}) {
153 print "$notif->{'type'}: ";
156 if (defined $notif->{'type_instance'}) {
157 print "$notif->{'type_instance'}: ";
160 print "$notif->{'message'}\n";