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_DATASET, 'myplugin', $dataset);
38 plugin_register (TYPE_INIT, 'myplugin', 'my_init');
39 plugin_register (TYPE_READ, 'myplugin', 'my_read');
40 plugin_register (TYPE_WRITE, 'myplugin', 'my_write');
41 plugin_register (TYPE_SHUTDOWN, 'myplugin', 'my_shutdown');
43 # For each of the functions below see collectd-perl(5) for details about
44 # arguments and the like.
46 # This function is called once upon startup to initialize the plugin.
49 # open sockets, initialize data structures, ...
51 # A false return value indicates an error and causes the plugin to be
56 # This function is called in regular intervals to collectd the data.
59 # value list to dispatch to collectd:
60 # see section "DATA TYPES" in collectd-perl(5) for details
63 # do the magic to read the data:
64 # the number of values has to match the number of data sources defined in
65 # the registered data set
66 $vl->{'values'} = [ rand(65535) ];
67 $vl->{'plugin'} = 'myplugin';
68 # any other elements are optional
70 # dispatch the values to collectd which passes them on to all registered
71 # write functions - the first argument is used to lookup the data set
73 plugin_dispatch_values ('myplugin', $vl);
75 # A false return value indicates an error and the plugin will be skipped
76 # for an increasing amount of time.
80 # This function is called after values have been dispatched to collectd.
87 if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
88 plugin_log (LOG_WARNING,
89 "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";