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 {
138 our $cb_name = undef;
140 if (! defined $type) {
144 if (TYPE_LOG != $type) {
145 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
148 if (! defined $plugins[$type]) {
149 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
154 lock %{$plugins[$type]};
155 %plugins = %{$plugins[$type]};
158 foreach my $plugin (keys %plugins) {
159 my $p = $plugins{$plugin};
163 if ($p->{'wait_left'} > 0) {
164 $p->{'wait_left'} -= $interval_g;
167 next if ($p->{'wait_left'} > 0);
169 $cb_name = $p->{'cb_name'};
170 $status = call_by_name (@_);
179 $err = "callback returned false";
182 if (TYPE_LOG != $type) {
183 ERROR ("Execution of callback \"$cb_name\" failed: $err");
190 $p->{'wait_left'} = 0;
191 $p->{'wait_time'} = $interval_g;
193 elsif (TYPE_READ == $type) {
194 if ($p->{'wait_time'} < $interval_g) {
195 $p->{'wait_time'} = $interval_g;
198 $p->{'wait_left'} = $p->{'wait_time'};
199 $p->{'wait_time'} *= 2;
201 if ($p->{'wait_time'} > 86400) {
202 $p->{'wait_time'} = 86400;
205 WARNING ("${plugin}->read() failed with status $status. "
206 . "Will suspend it for $p->{'wait_left'} seconds.");
208 elsif (TYPE_INIT == $type) {
209 ERROR ("${plugin}->init() failed with status $status. "
210 . "Plugin will be disabled.");
212 foreach my $type (keys %types) {
213 plugin_unregister ($type, $plugin);
216 elsif (TYPE_LOG != $type) {
217 WARNING ("${plugin}->$types{$type}() failed with status $status.");
223 # Collectd::plugin_register (type, name, data).
226 # init, read, write, shutdown, data set
232 # reference to the plugin's subroutine that does the work or the data set
234 sub plugin_register {
239 DEBUG ("Collectd::plugin_register: "
240 . "type = \"$type\", name = \"$name\", data = \"$data\"");
242 if (! ((defined $type) && (defined $name) && (defined $data))) {
243 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
247 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
248 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
252 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
253 return plugin_register_data_set ($name, $data);
255 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
256 my $pkg = scalar caller;
260 if ($data !~ m/^$pkg\:\:/) {
261 $data = $pkg . "::" . $data;
265 wait_time => $interval_g,
270 lock %{$plugins[$type]};
271 $plugins[$type]->{$name} = \%p;
274 ERROR ("Collectd::plugin_register: Invalid data.");
280 sub plugin_unregister {
284 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
286 if (! ((defined $type) && (defined $name))) {
287 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
291 if (TYPE_DATASET == $type) {
292 return plugin_unregister_data_set ($name);
294 elsif (defined $plugins[$type]) {
295 lock %{$plugins[$type]};
296 delete $plugins[$type]->{$name};
299 ERROR ("Collectd::plugin_unregister: Invalid type.");
309 DEBUG ("Collectd::plugin_flush:"
310 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
311 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : ""));
313 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
314 $timeout = $args{'timeout'};
317 if (! defined $args{'plugins'}) {
318 plugin_flush_all ($timeout);
321 if ("ARRAY" eq ref ($args{'plugins'})) {
322 foreach my $plugin (@{$args{'plugins'}}) {
323 plugin_flush_one ($timeout, $plugin);
327 plugin_flush_one ($timeout, $args{'plugins'});
334 # vim: set sw=4 ts=4 tw=78 noexpandtab :