1 # collectd - Collectd.pm
2 # Copyright (C) 2007 Sebastian Harl
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; only version 2 of the License is applicable.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 # Sebastian Harl <sh at tokkee.org>
27 # make all symbols available at compile time
28 BEGIN { our $VERSION = '4.1.2'; bootstrap Collectd $VERSION; }
30 our @ISA = qw( Exporter );
36 plugin_dispatch_values
67 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
68 foreach keys %EXPORT_TAGS;
71 Exporter::export_ok_tags ('all');
80 TYPE_SHUTDOWN, "shutdown",
84 foreach my $type (keys %types) {
93 if ("Collectd" eq $caller) {
96 return plugin_log ($lvl, $msg);
99 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
100 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
101 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
102 sub INFO { _log (scalar caller, LOG_INFO, shift); }
103 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
105 sub plugin_call_all {
108 if (! defined $type) {
112 if (TYPE_LOG != $type) {
113 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
116 if (! defined $plugins[$type]) {
117 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
121 foreach my $plugin (keys %{$plugins[$type]}) {
122 my $p = $plugins[$type]->{$plugin};
124 if ($p->{'wait_left'} > 0) {
125 # TODO: use interval_g
126 $p->{'wait_left'} -= 10;
129 next if ($p->{'wait_left'} > 0);
131 if (my $status = $p->{'code'}->(@_)) {
132 $p->{'wait_left'} = 0;
133 $p->{'wait_time'} = 10;
135 elsif (TYPE_READ == $type) {
136 $p->{'wait_left'} = $p->{'wait_time'};
137 $p->{'wait_time'} *= 2;
139 if ($p->{'wait_time'} > 86400) {
140 $p->{'wait_time'} = 86400;
143 WARNING ("${plugin}->read() failed with status $status. "
144 . "Will suspend it for $p->{'wait_left'} seconds.");
146 elsif (TYPE_INIT == $type) {
147 foreach my $type (keys %types) {
148 plugin_unregister ($type, $plugin);
151 ERROR ("${plugin}->init() failed with status $status. "
152 . "Plugin will be disabled.");
154 elsif (TYPE_LOG != $type) {
155 WARNING ("${plugin}->$types{$type}() failed with status $status.");
161 # Collectd::plugin_register (type, name, data).
164 # init, read, write, shutdown, data set
170 # reference to the plugin's subroutine that does the work or the data set
172 sub plugin_register {
177 DEBUG ("Collectd::plugin_register: "
178 . "type = \"$type\", name = \"$name\", data = \"$data\"");
180 if (! ((defined $type) && (defined $name) && (defined $data))) {
181 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
185 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
186 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
190 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
191 return plugin_register_data_set ($name, $data);
193 elsif ("CODE" eq ref $data) {
194 # TODO: make interval_g available at configuration time
195 $plugins[$type]->{$name} = {
202 ERROR ("Collectd::plugin_register: Invalid data.");
208 sub plugin_unregister {
212 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
214 if (! ((defined $type) && (defined $name))) {
215 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
219 if (TYPE_DATASET == $type) {
220 return plugin_unregister_data_set ($name);
222 elsif (defined $plugins[$type]) {
223 delete $plugins[$type]->{$name};
226 ERROR ("Collectd::plugin_unregister: Invalid type.");
233 # vim: set sw=4 ts=4 tw=78 noexpandtab :