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>
28 if (! $Config{'useithreads'}) {
29 die "Perl does not support ithreads!";
35 our @ISA = qw( Exporter );
41 plugin_dispatch_values
72 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
73 foreach keys %EXPORT_TAGS;
76 Exporter::export_ok_tags ('all');
84 TYPE_SHUTDOWN, "shutdown",
88 foreach my $type (keys %types) {
97 if ("Collectd" eq $caller) {
100 return plugin_log ($lvl, $msg);
103 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
104 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
105 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
106 sub INFO { _log (scalar caller, LOG_INFO, shift); }
107 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
109 sub plugin_call_all {
112 if (! defined $type) {
116 if (TYPE_LOG != $type) {
117 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
120 if (! defined $plugins[$type]) {
121 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
125 foreach my $plugin (keys %{$plugins[$type]}) {
126 my $p = $plugins[$type]->{$plugin};
128 if ($p->{'wait_left'} > 0) {
129 # TODO: use interval_g
130 $p->{'wait_left'} -= 10;
133 next if ($p->{'wait_left'} > 0);
135 if (my $status = $p->{'code'}->(@_)) {
136 $p->{'wait_left'} = 0;
137 $p->{'wait_time'} = 10;
139 elsif (TYPE_READ == $type) {
140 $p->{'wait_left'} = $p->{'wait_time'};
141 $p->{'wait_time'} *= 2;
143 if ($p->{'wait_time'} > 86400) {
144 $p->{'wait_time'} = 86400;
147 WARNING ("${plugin}->read() failed with status $status. "
148 . "Will suspend it for $p->{'wait_left'} seconds.");
150 elsif (TYPE_INIT == $type) {
151 foreach my $type (keys %types) {
152 plugin_unregister ($type, $plugin);
155 ERROR ("${plugin}->init() failed with status $status. "
156 . "Plugin will be disabled.");
158 elsif (TYPE_LOG != $type) {
159 WARNING ("${plugin}->$types{$type}() failed with status $status.");
165 # Collectd::plugin_register (type, name, data).
168 # init, read, write, shutdown, data set
174 # reference to the plugin's subroutine that does the work or the data set
176 sub plugin_register {
181 DEBUG ("Collectd::plugin_register: "
182 . "type = \"$type\", name = \"$name\", data = \"$data\"");
184 if (! ((defined $type) && (defined $name) && (defined $data))) {
185 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
189 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
190 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
194 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
195 return plugin_register_data_set ($name, $data);
197 elsif ("CODE" eq ref $data) {
198 # TODO: make interval_g available at configuration time
199 $plugins[$type]->{$name} = {
206 ERROR ("Collectd::plugin_register: Invalid data.");
212 sub plugin_unregister {
216 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
218 if (! ((defined $type) && (defined $name))) {
219 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
223 if (TYPE_DATASET == $type) {
224 return plugin_unregister_data_set ($name);
226 elsif (defined $plugins[$type]) {
227 delete $plugins[$type]->{$name};
230 ERROR ("Collectd::plugin_unregister: Invalid type.");
237 # vim: set sw=4 ts=4 tw=78 noexpandtab :