/usr/share/doc/collectd-dev/examples/MyPlugin.pm is in collectd-dev 5.7.2-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | # /usr/share/doc/collectd/examples/MyPlugin.pm
#
# A Perl plugin template for collectd.
#
# Written by Sebastian Harl <sh@tokkee.org>
#
# This is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; only version 2 of the License is applicable.
# Notes:
# - each of the functions below (and the corresponding plugin_register call)
# is optional
package Collectd::Plugin::MyPlugin;
use strict;
use warnings;
use Collectd qw( :all );
# data set definition:
# see section "DATA TYPES" in collectd-perl(5) for details
#
# NOTE: If you're defining a custom data-set, you have to make that known to
# any servers as well. Else, the server is not able to store values using the
# type defined by that data-set.
# It is strongly recommended to use one of the types and data-sets pre-defined
# in the types.db file.
my $dataset =
[
{
name => 'my_ds',
type => DS_TYPE_GAUGE,
min => 0,
max => 65535,
},
];
# This code is executed after loading the plugin to register it with collectd.
plugin_register (TYPE_LOG, 'myplugin', 'my_log');
plugin_register (TYPE_NOTIF, 'myplugin', 'my_notify');
plugin_register (TYPE_DATASET, 'mytype', $dataset);
plugin_register (TYPE_INIT, 'myplugin', 'my_init');
plugin_register (TYPE_READ, 'myplugin', 'my_read');
plugin_register (TYPE_WRITE, 'myplugin', 'my_write');
plugin_register (TYPE_SHUTDOWN, 'myplugin', 'my_shutdown');
# For each of the functions below see collectd-perl(5) for details about
# arguments and the like.
# This function is called once upon startup to initialize the plugin.
sub my_init
{
# open sockets, initialize data structures, ...
# A false return value indicates an error and causes the plugin to be
# disabled.
return 1;
} # my_init ()
# This function is called in regular intervals to collectd the data.
sub my_read
{
# value list to dispatch to collectd:
# see section "DATA TYPES" in collectd-perl(5) for details
my $vl = {};
# do the magic to read the data:
# the number of values has to match the number of data sources defined in
# the registered data set. The type used here (in this example:
# "mytype") must be defined in the types.db, see types.db(5) for
# details, or registered as "TYPE_DATASET".
$vl->{'values'} = [ rand(65535) ];
$vl->{'plugin'} = 'myplugin';
$vl->{'type'} = 'mytype';
# any other elements are optional
# dispatch the values to collectd which passes them on to all registered
# write functions
plugin_dispatch_values ($vl);
# A false return value indicates an error and the plugin will be skipped
# for an increasing amount of time.
return 1;
} # my_read ()
# This function is called after values have been dispatched to collectd.
sub my_write
{
my $type = shift;
my $ds = shift;
my $vl = shift;
if (scalar (@$ds) != scalar (@{$vl->{'values'}})) {
plugin_log (LOG_WARNING, "DS number does not match values length");
return;
}
for (my $i = 0; $i < scalar (@$ds); ++$i) {
# do the magic to output the data
print "$vl->{'host'}: $vl->{'plugin'}: ";
if (defined $vl->{'plugin_instance'}) {
print "$vl->{'plugin_instance'}: ";
}
print "$type: ";
if (defined $vl->{'type_instance'}) {
print "$vl->{'type_instance'}: ";
}
print "$vl->{'values'}->[$i]\n";
}
return 1;
} # my_write()
# This function is called before shutting down collectd.
sub my_shutdown
{
# close sockets, ...
return 1;
} # my_shutdown ()
# This function is called when plugin_log () has been used.
sub my_log
{
my $level = shift;
my $msg = shift;
print "LOG: $level - $msg\n";
return 1;
} # my_log ()
# This function is called when plugin_dispatch_notification () has been used
sub my_notify
{
my $notif = shift;
my ($sec, $min, $hour, $mday, $mon, $year) = localtime ($notif->{'time'});
printf "NOTIF (%04d-%02d-%02d %02d:%02d:%02d): %d - ",
$year + 1900, $mon + 1, $mday, $hour, $min, $sec,
$notif->{'severity'};
if (defined $notif->{'host'}) {
print "$notif->{'host'}: ";
}
if (defined $notif->{'plugin'}) {
print "$notif->{'plugin'}: ";
}
if (defined $notif->{'plugin_instance'}) {
print "$notif->{'plugin_instance'}: ";
}
if (defined $notif->{'type'}) {
print "$notif->{'type'}: ";
}
if (defined $notif->{'type_instance'}) {
print "$notif->{'type_instance'}: ";
}
print "$notif->{'message'}\n";
return 1;
} # my_notify ()
|