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
91 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
92 foreach keys %EXPORT_TAGS;
99 Exporter::export_ok_tags ('all');
101 my @plugins : shared = ();
102 my %cf_callbacks : shared = ();
108 TYPE_SHUTDOWN, "shutdown",
110 TYPE_NOTIF, "notify",
114 foreach my $type (keys %types) {
115 $plugins[$type] = &share ({});
123 if ("Collectd" eq $caller) {
126 return plugin_log ($lvl, $msg);
129 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
130 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
131 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
132 sub INFO { _log (scalar caller, LOG_INFO, shift); }
133 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
135 sub plugin_call_all {
140 our $cb_name = undef;
142 if (! defined $type) {
146 if (TYPE_LOG != $type) {
147 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
150 if (! defined $plugins[$type]) {
151 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
156 lock %{$plugins[$type]};
157 %plugins = %{$plugins[$type]};
160 foreach my $plugin (keys %plugins) {
161 my $p = $plugins{$plugin};
165 if ($p->{'wait_left'} > 0) {
166 $p->{'wait_left'} -= $interval_g;
169 next if ($p->{'wait_left'} > 0);
171 $cb_name = $p->{'cb_name'};
172 $status = call_by_name (@_);
181 $err = "callback returned false";
184 if (TYPE_LOG != $type) {
185 ERROR ("Execution of callback \"$cb_name\" failed: $err");
192 $p->{'wait_left'} = 0;
193 $p->{'wait_time'} = $interval_g;
195 elsif (TYPE_READ == $type) {
196 if ($p->{'wait_time'} < $interval_g) {
197 $p->{'wait_time'} = $interval_g;
200 $p->{'wait_left'} = $p->{'wait_time'};
201 $p->{'wait_time'} *= 2;
203 if ($p->{'wait_time'} > 86400) {
204 $p->{'wait_time'} = 86400;
207 WARNING ("${plugin}->read() failed with status $status. "
208 . "Will suspend it for $p->{'wait_left'} seconds.");
210 elsif (TYPE_INIT == $type) {
211 ERROR ("${plugin}->init() failed with status $status. "
212 . "Plugin will be disabled.");
214 foreach my $type (keys %types) {
215 plugin_unregister ($type, $plugin);
218 elsif (TYPE_LOG != $type) {
219 WARNING ("${plugin}->$types{$type}() failed with status $status.");
225 # Collectd::plugin_register (type, name, data).
228 # init, read, write, shutdown, data set
234 # reference to the plugin's subroutine that does the work or the data set
236 sub plugin_register {
241 DEBUG ("Collectd::plugin_register: "
242 . "type = \"$type\", name = \"$name\", data = \"$data\"");
244 if (! ((defined $type) && (defined $name) && (defined $data))) {
245 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
249 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
250 && (TYPE_CONFIG != $type)) {
251 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
255 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
256 return plugin_register_data_set ($name, $data);
258 elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
259 my $pkg = scalar caller;
261 if ($data !~ m/^$pkg\:\:/) {
262 $data = $pkg . "::" . $data;
266 $cf_callbacks{$name} = $data;
268 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
269 my $pkg = scalar caller;
273 if ($data !~ m/^$pkg\:\:/) {
274 $data = $pkg . "::" . $data;
278 wait_time => $interval_g,
283 lock %{$plugins[$type]};
284 $plugins[$type]->{$name} = \%p;
287 ERROR ("Collectd::plugin_register: Invalid data.");
293 sub plugin_unregister {
297 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
299 if (! ((defined $type) && (defined $name))) {
300 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
304 if (TYPE_DATASET == $type) {
305 return plugin_unregister_data_set ($name);
307 elsif (TYPE_CONFIG == $type) {
309 delete $cf_callbacks{$name};
311 elsif (defined $plugins[$type]) {
312 lock %{$plugins[$type]};
313 delete $plugins[$type]->{$name};
316 ERROR ("Collectd::plugin_unregister: Invalid type.");
328 DEBUG ("Collectd::plugin_flush:"
329 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
330 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
331 . (defined ($args{'identifiers'})
332 ? " identifiers = $args{'identifiers'}" : ""));
334 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
335 $timeout = $args{'timeout'};
338 if (defined ($args{'plugins'})) {
339 if ("ARRAY" eq ref ($args{'plugins'})) {
340 @plugins = @{$args{'plugins'}};
343 @plugins = ($args{'plugins'});
350 if (defined ($args{'identifiers'})) {
351 if ("ARRAY" eq ref ($args{'identifiers'})) {
352 @ids = @{$args{'identifiers'}};
355 @ids = ($args{'identifiers'});
362 foreach my $plugin (@plugins) {
363 foreach my $id (@ids) {
364 _plugin_flush($plugin, $timeout, $id);
369 sub plugin_flush_one {
373 WARNING ("Collectd::plugin_flush_one is deprecated - "
374 . "use Collectd::plugin_flush instead.");
376 if (! (defined ($timeout) && defined ($name))) {
377 ERROR ("Usage: Collectd::plugin_flush_one(timeout, name)");
381 plugin_flush (plugins => $name, timeout => $timeout);
384 sub plugin_flush_all {
387 WARNING ("Collectd::plugin_flush_all is deprecated - "
388 . "use Collectd::plugin_flush instead.");
390 if (! defined ($timeout)) {
391 ERROR ("Usage: Collectd::plugin_flush_all(timeout)");
395 plugin_flush (timeout => $timeout);
398 sub _plugin_dispatch_config {
402 our $cb_name = undef;
404 if (! (defined ($plugin) && defined ($config))) {
408 if (! defined $cf_callbacks{$plugin}) {
409 WARNING ("Found a configuration for the \"$plugin\" plugin, but "
410 . "the plugin isn't loaded or didn't register "
411 . "a configuration callback.");
417 $cb_name = $cf_callbacks{$plugin};
419 call_by_name ($config);
424 # vim: set sw=4 ts=4 tw=78 noexpandtab :