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 # data set definition:
21 # see section "DATA TYPES" in collectd-perl(5) for details
26 type => Collectd::DS_TYPE_GAUGE,
32 # This code is executed after loading the plugin to register it with collectd.
33 Collectd::plugin_register (Collectd::TYPE_LOG, 'myplugin', \&my_log);
34 Collectd::plugin_register (Collectd::TYPE_DATASET, 'myplugin', $dataset);
35 Collectd::plugin_register (Collectd::TYPE_INIT, 'myplugin', \&my_init);
36 Collectd::plugin_register (Collectd::TYPE_READ, 'myplugin', \&my_read);
37 Collectd::plugin_register (Collectd::TYPE_WRITE, 'myplugin', \&my_write);
38 Collectd::plugin_register (Collectd::TYPE_SHUTDOWN, 'myplugin', \&my_shutdown);
40 # For each of the functions below see collectd-perl(5) for details about
41 # arguments and the like.
43 # This function is called once upon startup to initialize the plugin.
46 # open sockets, initialize data structures, ...
48 # A false return value indicates an error and causes the plugin to be
53 # This function is called in regular intervals to collectd the data.
56 # value list to dispatch to collectd:
57 # see section "DATA TYPES" in collectd-perl(5) for details
60 # do the magic to read the data:
61 # the number of values has to match the number of data sources defined in
62 # the registered data set
63 $vl->{'values'} = [ rand(65535) ];
64 $vl->{'plugin'} = 'myplugin';
65 # any other elements are optional
67 # dispatch the values to collectd which passes them on to all registered
68 # write functions - the first argument is used to lookup the data set
70 Collectd::plugin_dispatch_values ('myplugin', $vl);
72 # A false return value indicates an error and the plugin will be skipped
73 # for an increasing amount of time.
77 # This function is called after values have been dispatched to collectd.
84 if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
85 Collectd::plugin_log (Collectd::LOG_WARNING,
86 "DS number does not match values length");
90 for (my $i = 0; $i < scalar (@$ds); ++$i) {
91 # do the magic to output the data
92 print "$vl->{'host'}: $vl->{'plugin'}: ";
94 if (defined $vl->{'plugin_instance'}) {
95 print "$vl->{'plugin_instance'}: ";
100 if (defined $vl->{'type_instance'}) {
101 print "$vl->{'type_instance'}: ";
104 print "$vl->{'values'}->[$i]\n";
109 # This function is called before shutting down collectd.
116 # This function is called when plugin_log () has been used.
122 print "LOG: $level - $msg\n";