Merge branch 'collectd-4.2' into collectd-4.3
[collectd.git] / contrib / examples / MyPlugin.pm
1 # /usr/share/doc/collectd/examples/MyPlugin.pm
2 #
3 # A Perl plugin template for collectd.
4 #
5 # Written by Sebastian Harl <sh@tokkee.org>
6 #
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.
10
11 # Notes:
12 # - each of the functions below (and the corresponding plugin_register call)
13 #   is optional
14
15 package Collectd::Plugin::MyPlugin;
16
17 use strict;
18 use warnings;
19
20 use Collectd qw( :all );
21
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)
25 my $dataset =
26 [
27         {
28                 name => 'my_ds',
29                 type => DS_TYPE_GAUGE,
30                 min  => 0,
31                 max  => 65535,
32         },
33 ];
34
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');
43
44 # For each of the functions below see collectd-perl(5) for details about
45 # arguments and the like.
46
47 # This function is called once upon startup to initialize the plugin.
48 sub my_init
49 {
50         # open sockets, initialize data structures, ...
51
52         # A false return value indicates an error and causes the plugin to be
53         # disabled.
54         return 1;
55 } # my_init ()
56
57 # This function is called in regular intervals to collectd the data.
58 sub my_read
59 {
60         # value list to dispatch to collectd:
61         # see section "DATA TYPES" in collectd-perl(5) for details
62         my $vl = {};
63
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
70
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
73         # definition
74         plugin_dispatch_values ('myplugin', $vl);
75
76         # A false return value indicates an error and the plugin will be skipped
77         # for an increasing amount of time.
78         return 1;
79 } # my_read ()
80
81 # This function is called after values have been dispatched to collectd.
82 sub my_write
83 {
84         my $type = shift;
85         my $ds   = shift;
86         my $vl   = shift;
87
88         if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
89                 plugin_log (LOG_WARNING, "DS number does not match values length");
90                 return;
91         }
92
93         for (my $i = 0; $i < scalar (@$ds); ++$i) {
94                 # do the magic to output the data
95                 print "$vl->{'host'}: $vl->{'plugin'}: ";
96
97                 if (defined $vl->{'plugin_instance'}) {
98                         print "$vl->{'plugin_instance'}: ";
99                 }
100
101                 print "$type: ";
102
103                 if (defined $vl->{'type_instance'}) {
104                         print "$vl->{'type_instance'}: ";
105                 }
106
107                 print "$vl->{'values'}->[$i]\n";
108         }
109         return 1;
110 } # my_write()
111
112 # This function is called before shutting down collectd.
113 sub my_shutdown
114 {
115         # close sockets, ...
116         return 1;
117 } # my_shutdown ()
118
119 # This function is called when plugin_log () has been used.
120 sub my_log
121 {
122         my $level = shift;
123         my $msg   = shift;
124
125         print "LOG: $level - $msg\n";
126         return 1;
127 } # my_log ()
128
129 # This function is called when plugin_dispatch_notification () has been used
130 sub my_notify
131 {
132         my $notif = shift;
133
134         my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($notif->{'time'});
135
136         printf "NOTIF (%04d-%02d-%02d %02d:%02d:%02d): %d - ",
137                         $year + 1900, $mon + 1, $mday, $hour, $min, $sec,
138                         $notif->{'severity'};
139
140         if (defined $notif->{'host'}) {
141                 print "$notif->{'host'}: ";
142         }
143
144         if (defined $notif->{'plugin'}) {
145                 print "$notif->{'plugin'}: ";
146         }
147
148         if (defined $notif->{'plugin_instance'}) {
149                 print "$notif->{'plugin_instance'}: ";
150         }
151
152         if (defined $notif->{'type'}) {
153                 print "$notif->{'type'}: ";
154         }
155
156         if (defined $notif->{'type_instance'}) {
157                 print "$notif->{'type_instance'}: ";
158         }
159
160         print "$notif->{'message'}\n";
161         return 1;
162 } # my_notify ()
163