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>
31 if (! $Config{'useithreads'}) {
32 die "Perl does not support ithreads!";
38 our @ISA = qw( Exporter );
44 plugin_dispatch_values
45 plugin_dispatch_notification
86 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
87 foreach keys %EXPORT_TAGS;
94 Exporter::export_ok_tags ('all');
96 my @plugins : shared = ();
102 TYPE_SHUTDOWN, "shutdown",
107 foreach my $type (keys %types) {
108 $plugins[$type] = &share ({});
116 if ("Collectd" eq $caller) {
119 return plugin_log ($lvl, $msg);
122 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
123 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
124 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
125 sub INFO { _log (scalar caller, LOG_INFO, shift); }
126 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
128 sub plugin_call_all {
131 our $cb_name = undef;
133 if (! defined $type) {
137 if (TYPE_LOG != $type) {
138 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
141 if (! defined $plugins[$type]) {
142 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
147 foreach my $plugin (keys %{$plugins[$type]}) {
148 my $p = $plugins[$type]->{$plugin};
152 if ($p->{'wait_left'} > 0) {
153 $p->{'wait_left'} -= $interval_g;
156 next if ($p->{'wait_left'} > 0);
158 $cb_name = $p->{'cb_name'};
159 $status = call_by_name (@_);
168 $err = "callback returned false";
171 if (TYPE_LOG != $type) {
172 ERROR ("Execution of callback \"$cb_name\" failed: $err");
179 $p->{'wait_left'} = 0;
180 $p->{'wait_time'} = $interval_g;
182 elsif (TYPE_READ == $type) {
183 if ($p->{'wait_time'} < $interval_g) {
184 $p->{'wait_time'} = $interval_g;
187 $p->{'wait_left'} = $p->{'wait_time'};
188 $p->{'wait_time'} *= 2;
190 if ($p->{'wait_time'} > 86400) {
191 $p->{'wait_time'} = 86400;
194 WARNING ("${plugin}->read() failed with status $status. "
195 . "Will suspend it for $p->{'wait_left'} seconds.");
197 elsif (TYPE_INIT == $type) {
198 ERROR ("${plugin}->init() failed with status $status. "
199 . "Plugin will be disabled.");
201 foreach my $type (keys %types) {
202 plugin_unregister ($type, $plugin);
205 elsif (TYPE_LOG != $type) {
206 WARNING ("${plugin}->$types{$type}() failed with status $status.");
212 # Collectd::plugin_register (type, name, data).
215 # init, read, write, shutdown, data set
221 # reference to the plugin's subroutine that does the work or the data set
223 sub plugin_register {
228 DEBUG ("Collectd::plugin_register: "
229 . "type = \"$type\", name = \"$name\", data = \"$data\"");
231 if (! ((defined $type) && (defined $name) && (defined $data))) {
232 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
236 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
237 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
241 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
242 return plugin_register_data_set ($name, $data);
244 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
245 my $pkg = scalar caller;
249 if ($data !~ m/^$pkg\:\:/) {
250 $data = $pkg . "::" . $data;
254 wait_time => $interval_g,
260 $plugins[$type]->{$name} = \%p;
263 ERROR ("Collectd::plugin_register: Invalid data.");
269 sub plugin_unregister {
273 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
275 if (! ((defined $type) && (defined $name))) {
276 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
280 if (TYPE_DATASET == $type) {
281 return plugin_unregister_data_set ($name);
283 elsif (defined $plugins[$type]) {
285 delete $plugins[$type]->{$name};
288 ERROR ("Collectd::plugin_unregister: Invalid type.");
295 # vim: set sw=4 ts=4 tw=78 noexpandtab :