1 # collectd - Collectd.pm
2 # Copyright (C) 2007, 2008 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
48 plugin_dispatch_notification
90 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
91 foreach keys %EXPORT_TAGS;
98 Exporter::export_ok_tags ('all');
100 my @plugins : shared = ();
106 TYPE_SHUTDOWN, "shutdown",
108 TYPE_NOTIF, "notify",
112 foreach my $type (keys %types) {
113 $plugins[$type] = &share ({});
121 if ("Collectd" eq $caller) {
124 return plugin_log ($lvl, $msg);
127 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
128 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
129 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
130 sub INFO { _log (scalar caller, LOG_INFO, shift); }
131 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
133 sub plugin_call_all {
136 our $cb_name = undef;
138 if (! defined $type) {
142 if (TYPE_LOG != $type) {
143 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
146 if (! defined $plugins[$type]) {
147 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
152 foreach my $plugin (keys %{$plugins[$type]}) {
153 my $p = $plugins[$type]->{$plugin};
157 if ($p->{'wait_left'} > 0) {
158 $p->{'wait_left'} -= $interval_g;
161 next if ($p->{'wait_left'} > 0);
163 $cb_name = $p->{'cb_name'};
164 $status = call_by_name (@_);
173 $err = "callback returned false";
176 if (TYPE_LOG != $type) {
177 ERROR ("Execution of callback \"$cb_name\" failed: $err");
184 $p->{'wait_left'} = 0;
185 $p->{'wait_time'} = $interval_g;
187 elsif (TYPE_READ == $type) {
188 if ($p->{'wait_time'} < $interval_g) {
189 $p->{'wait_time'} = $interval_g;
192 $p->{'wait_left'} = $p->{'wait_time'};
193 $p->{'wait_time'} *= 2;
195 if ($p->{'wait_time'} > 86400) {
196 $p->{'wait_time'} = 86400;
199 WARNING ("${plugin}->read() failed with status $status. "
200 . "Will suspend it for $p->{'wait_left'} seconds.");
202 elsif (TYPE_INIT == $type) {
203 ERROR ("${plugin}->init() failed with status $status. "
204 . "Plugin will be disabled.");
206 foreach my $type (keys %types) {
207 plugin_unregister ($type, $plugin);
210 elsif (TYPE_LOG != $type) {
211 WARNING ("${plugin}->$types{$type}() failed with status $status.");
217 # Collectd::plugin_register (type, name, data).
220 # init, read, write, shutdown, data set
226 # reference to the plugin's subroutine that does the work or the data set
228 sub plugin_register {
233 DEBUG ("Collectd::plugin_register: "
234 . "type = \"$type\", name = \"$name\", data = \"$data\"");
236 if (! ((defined $type) && (defined $name) && (defined $data))) {
237 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
241 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
242 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
246 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
247 return plugin_register_data_set ($name, $data);
249 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
250 my $pkg = scalar caller;
254 if ($data !~ m/^$pkg\:\:/) {
255 $data = $pkg . "::" . $data;
259 wait_time => $interval_g,
265 $plugins[$type]->{$name} = \%p;
268 ERROR ("Collectd::plugin_register: Invalid data.");
274 sub plugin_unregister {
278 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
280 if (! ((defined $type) && (defined $name))) {
281 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
285 if (TYPE_DATASET == $type) {
286 return plugin_unregister_data_set ($name);
288 elsif (defined $plugins[$type]) {
290 delete $plugins[$type]->{$name};
293 ERROR ("Collectd::plugin_unregister: Invalid type.");
303 DEBUG ("Collectd::plugin_flush:"
304 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
305 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : ""));
307 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
308 $timeout = $args{'timeout'};
311 if (! defined $args{'plugins'}) {
312 plugin_flush_all ($timeout);
315 if ("ARRAY" eq ref ($args{'plugins'})) {
316 foreach my $plugin (@{$args{'plugins'}}) {
317 plugin_flush_one ($timeout, $plugin);
321 plugin_flush_one ($timeout, $args{'plugins'});
328 # vim: set sw=4 ts=4 tw=78 noexpandtab :