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
25 # NOTE: If you're defining a custom data-set, you have to make that known to
26 # any servers as well. Else, the server is not able to store values using the
27 # type defined by that data-set.
28 # It is strongly recommended to use one of the types and data-sets pre-defined
29 # in the types.db file.
34 type => DS_TYPE_GAUGE,
40 # This code is executed after loading the plugin to register it with collectd.
41 plugin_register (TYPE_LOG, 'myplugin', 'my_log');
42 plugin_register (TYPE_NOTIF, 'myplugin', 'my_notify');
43 plugin_register (TYPE_DATASET, 'myplugin', $dataset);
44 plugin_register (TYPE_INIT, 'myplugin', 'my_init');
45 plugin_register (TYPE_READ, 'myplugin', 'my_read');
46 plugin_register (TYPE_WRITE, 'myplugin', 'my_write');
47 plugin_register (TYPE_SHUTDOWN, 'myplugin', 'my_shutdown');
49 # For each of the functions below see collectd-perl(5) for details about
50 # arguments and the like.
52 # This function is called once upon startup to initialize the plugin.
55 # open sockets, initialize data structures, ...
57 # A false return value indicates an error and causes the plugin to be
62 # This function is called in regular intervals to collectd the data.
65 # value list to dispatch to collectd:
66 # see section "DATA TYPES" in collectd-perl(5) for details
69 # do the magic to read the data:
70 # the number of values has to match the number of data sources defined in
71 # the registered data set
72 $vl->{'values'} = [ rand(65535) ];
73 $vl->{'plugin'} = 'myplugin';
74 # any other elements are optional
76 # dispatch the values to collectd which passes them on to all registered
77 # write functions - the first argument is used to lookup the data set
78 # definition (it is strongly recommended to use a type defined in the
80 plugin_dispatch_values ('myplugin', $vl);
82 # A false return value indicates an error and the plugin will be skipped
83 # for an increasing amount of time.
87 # This function is called after values have been dispatched to collectd.
94 if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
95 plugin_log (LOG_WARNING, "DS number does not match values length");
99 for (my $i = 0; $i < scalar (@$ds); ++$i) {
100 # do the magic to output the data
101 print "$vl->{'host'}: $vl->{'plugin'}: ";
103 if (defined $vl->{'plugin_instance'}) {
104 print "$vl->{'plugin_instance'}: ";
109 if (defined $vl->{'type_instance'}) {
110 print "$vl->{'type_instance'}: ";
113 print "$vl->{'values'}->[$i]\n";
118 # This function is called before shutting down collectd.
125 # This function is called when plugin_log () has been used.
131 print "LOG: $level - $msg\n";
135 # This function is called when plugin_dispatch_notification () has been used
140 my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($notif->{'time'});
142 printf "NOTIF (%04d-%02d-%02d %02d:%02d:%02d): %d - ",
143 $year + 1900, $mon + 1, $mday, $hour, $min, $sec,
144 $notif->{'severity'};
146 if (defined $notif->{'host'}) {
147 print "$notif->{'host'}: ";
150 if (defined $notif->{'plugin'}) {
151 print "$notif->{'plugin'}: ";
154 if (defined $notif->{'plugin_instance'}) {
155 print "$notif->{'plugin_instance'}: ";
158 if (defined $notif->{'type'}) {
159 print "$notif->{'type'}: ";
162 if (defined $notif->{'type_instance'}) {
163 print "$notif->{'type_instance'}: ";
166 print "$notif->{'message'}\n";