Merge branch 'collectd-4.4'
[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 #
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.
30 my $dataset =
31 [
32         {
33                 name => 'my_ds',
34                 type => DS_TYPE_GAUGE,
35                 min  => 0,
36                 max  => 65535,
37         },
38 ];
39
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');
48
49 # For each of the functions below see collectd-perl(5) for details about
50 # arguments and the like.
51
52 # This function is called once upon startup to initialize the plugin.
53 sub my_init
54 {
55         # open sockets, initialize data structures, ...
56
57         # A false return value indicates an error and causes the plugin to be
58         # disabled.
59         return 1;
60 } # my_init ()
61
62 # This function is called in regular intervals to collectd the data.
63 sub my_read
64 {
65         # value list to dispatch to collectd:
66         # see section "DATA TYPES" in collectd-perl(5) for details
67         my $vl = {};
68
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
75
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
79         # types.db file)
80         plugin_dispatch_values ('myplugin', $vl);
81
82         # A false return value indicates an error and the plugin will be skipped
83         # for an increasing amount of time.
84         return 1;
85 } # my_read ()
86
87 # This function is called after values have been dispatched to collectd.
88 sub my_write
89 {
90         my $type = shift;
91         my $ds   = shift;
92         my $vl   = shift;
93
94         if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
95                 plugin_log (LOG_WARNING, "DS number does not match values length");
96                 return;
97         }
98
99         for (my $i = 0; $i < scalar (@$ds); ++$i) {
100                 # do the magic to output the data
101                 print "$vl->{'host'}: $vl->{'plugin'}: ";
102
103                 if (defined $vl->{'plugin_instance'}) {
104                         print "$vl->{'plugin_instance'}: ";
105                 }
106
107                 print "$type: ";
108
109                 if (defined $vl->{'type_instance'}) {
110                         print "$vl->{'type_instance'}: ";
111                 }
112
113                 print "$vl->{'values'}->[$i]\n";
114         }
115         return 1;
116 } # my_write()
117
118 # This function is called before shutting down collectd.
119 sub my_shutdown
120 {
121         # close sockets, ...
122         return 1;
123 } # my_shutdown ()
124
125 # This function is called when plugin_log () has been used.
126 sub my_log
127 {
128         my $level = shift;
129         my $msg   = shift;
130
131         print "LOG: $level - $msg\n";
132         return 1;
133 } # my_log ()
134
135 # This function is called when plugin_dispatch_notification () has been used
136 sub my_notify
137 {
138         my $notif = shift;
139
140         my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($notif->{'time'});
141
142         printf "NOTIF (%04d-%02d-%02d %02d:%02d:%02d): %d - ",
143                         $year + 1900, $mon + 1, $mday, $hour, $min, $sec,
144                         $notif->{'severity'};
145
146         if (defined $notif->{'host'}) {
147                 print "$notif->{'host'}: ";
148         }
149
150         if (defined $notif->{'plugin'}) {
151                 print "$notif->{'plugin'}: ";
152         }
153
154         if (defined $notif->{'plugin_instance'}) {
155                 print "$notif->{'plugin_instance'}: ";
156         }
157
158         if (defined $notif->{'type'}) {
159                 print "$notif->{'type'}: ";
160         }
161
162         if (defined $notif->{'type_instance'}) {
163                 print "$notif->{'type_instance'}: ";
164         }
165
166         print "$notif->{'message'}\n";
167         return 1;
168 } # my_notify ()
169