1 # collectd - Collectd.pm
2 # Copyright (C) 2007-2009 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
50 plugin_dispatch_notification
80 'filter_chain' => [ qw(
105 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
106 foreach keys %EXPORT_TAGS;
113 Exporter::export_ok_tags ('all');
115 my @plugins : shared = ();
116 my @fc_plugins : shared = ();
117 my %cf_callbacks : shared = ();
120 TYPE_CONFIG, "config",
124 TYPE_SHUTDOWN, "shutdown",
126 TYPE_NOTIF, "notify",
135 my %fc_exec_names = (
142 FC_CB_CREATE, "create",
143 FC_CB_DESTROY, "destroy"
146 foreach my $type (keys %types) {
147 $plugins[$type] = &share ({});
150 foreach my $type (keys %fc_types) {
151 $fc_plugins[$type] = &share ({});
159 if ("Collectd" eq $caller) {
162 return plugin_log ($lvl, $msg);
165 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
166 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
167 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
168 sub INFO { _log (scalar caller, LOG_INFO, shift); }
169 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
171 sub plugin_call_all {
176 our $cb_name = undef;
178 if (! defined $type) {
182 if (TYPE_LOG != $type) {
183 DEBUG ("Collectd::plugin_call: type = \"$type\" ("
184 . $types{$type} . "), args=\""
185 . join(', ', map { defined($_) ? $_ : '<undef>' } @_) . "\"");
188 if (! defined $plugins[$type]) {
189 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
194 lock %{$plugins[$type]};
195 %plugins = %{$plugins[$type]};
198 foreach my $plugin (keys %plugins) {
199 my $p = $plugins{$plugin};
203 if ($p->{'wait_left'} > 0) {
204 $p->{'wait_left'} -= $interval_g;
207 next if ($p->{'wait_left'} > 0);
209 $cb_name = $p->{'cb_name'};
210 $status = call_by_name (@_);
219 $err = "callback returned false";
222 if (TYPE_LOG != $type) {
223 ERROR ("Execution of callback \"$cb_name\" failed: $err");
230 $p->{'wait_left'} = 0;
231 $p->{'wait_time'} = $interval_g;
233 elsif (TYPE_READ == $type) {
234 if ($p->{'wait_time'} < $interval_g) {
235 $p->{'wait_time'} = $interval_g;
238 $p->{'wait_left'} = $p->{'wait_time'};
239 $p->{'wait_time'} *= 2;
241 if ($p->{'wait_time'} > 86400) {
242 $p->{'wait_time'} = 86400;
245 WARNING ("${plugin}->read() failed with status $status. "
246 . "Will suspend it for $p->{'wait_left'} seconds.");
248 elsif (TYPE_INIT == $type) {
249 ERROR ("${plugin}->init() failed with status $status. "
250 . "Plugin will be disabled.");
252 foreach my $type (keys %types) {
253 plugin_unregister ($type, $plugin);
256 elsif (TYPE_LOG != $type) {
257 WARNING ("${plugin}->$types{$type}() failed with status $status.");
263 # Collectd::plugin_register (type, name, data).
266 # init, read, write, shutdown, data set
272 # reference to the plugin's subroutine that does the work or the data set
274 sub plugin_register {
279 DEBUG ("Collectd::plugin_register: "
280 . "type = \"$type\" (" . $types{$type}
281 . "), name = \"$name\", data = \"$data\"");
283 if (! ((defined $type) && (defined $name) && (defined $data))) {
284 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
288 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
289 && (TYPE_CONFIG != $type)) {
290 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
294 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
295 return plugin_register_data_set ($name, $data);
297 elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
298 my $pkg = scalar caller;
300 if ($data !~ m/^$pkg\:\:/) {
301 $data = $pkg . "::" . $data;
305 $cf_callbacks{$name} = $data;
307 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
308 my $pkg = scalar caller;
312 if ($data !~ m/^$pkg\:\:/) {
313 $data = $pkg . "::" . $data;
317 wait_time => $interval_g,
322 lock %{$plugins[$type]};
323 $plugins[$type]->{$name} = \%p;
326 ERROR ("Collectd::plugin_register: Invalid data.");
332 sub plugin_unregister {
336 DEBUG ("Collectd::plugin_unregister: type = \"$type\" ("
337 . $types{$type} . "), name = \"$name\"");
339 if (! ((defined $type) && (defined $name))) {
340 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
344 if (TYPE_DATASET == $type) {
345 return plugin_unregister_data_set ($name);
347 elsif (TYPE_CONFIG == $type) {
349 delete $cf_callbacks{$name};
351 elsif (defined $plugins[$type]) {
352 lock %{$plugins[$type]};
353 delete $plugins[$type]->{$name};
356 ERROR ("Collectd::plugin_unregister: Invalid type.");
368 if (! defined $args{'valuelists'}) {
369 ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
373 DEBUG ("Collectd::plugin_write:"
374 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
375 . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
376 . " valueslists = $args{'valuelists'}");
378 if (defined ($args{'plugins'})) {
379 if ("ARRAY" eq ref ($args{'plugins'})) {
380 @plugins = @{$args{'plugins'}};
383 @plugins = ($args{'plugins'});
390 if ("ARRAY" eq ref ($args{'valuelists'})) {
391 @valuelists = @{$args{'valuelists'}};
394 @valuelists = ($args{'valuelists'});
397 if (defined ($args{'datasets'})) {
398 if ("ARRAY" eq ref ($args{'datasets'})) {
399 @datasets = @{$args{'datasets'}};
402 @datasets = ($args{'datasets'});
406 @datasets = (undef) x scalar (@valuelists);
409 if ($#datasets != $#valuelists) {
410 ERROR ("Collectd::plugin_write: Invalid number of datasets.");
414 foreach my $plugin (@plugins) {
415 for (my $i = 0; $i < scalar (@valuelists); ++$i) {
416 _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
428 DEBUG ("Collectd::plugin_flush:"
429 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
430 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
431 . (defined ($args{'identifiers'})
432 ? " identifiers = $args{'identifiers'}" : ""));
434 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
435 $timeout = $args{'timeout'};
438 if (defined ($args{'plugins'})) {
439 if ("ARRAY" eq ref ($args{'plugins'})) {
440 @plugins = @{$args{'plugins'}};
443 @plugins = ($args{'plugins'});
450 if (defined ($args{'identifiers'})) {
451 if ("ARRAY" eq ref ($args{'identifiers'})) {
452 @ids = @{$args{'identifiers'}};
455 @ids = ($args{'identifiers'});
462 foreach my $plugin (@plugins) {
463 foreach my $id (@ids) {
464 _plugin_flush($plugin, $timeout, $id);
476 our $cb_name = undef;
479 if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
480 ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
484 if (! defined $fc_plugins[$type]) {
485 ERROR ("Collectd::fc_call: Invalid type \"$type\"");
489 if (! defined $fc_plugins[$type]->{$name}) {
490 ERROR ("Collectd::fc_call: Unknown "
491 . ($type == FC_MATCH ? "match" : "target")
496 DEBUG ("Collectd::fc_call: "
497 . "type = \"$type\" (" . $fc_types{$type}
498 . "), name = \"$name\", cb_type = \"$cb_type\" ("
499 . $fc_cb_types{$cb_type} . ")");
502 lock %{$fc_plugins[$type]};
503 %proc = %{$fc_plugins[$type]->{$name}};
506 if (FC_CB_EXEC == $cb_type) {
507 $cb_name = $proc{$fc_exec_names{$type}};
509 elsif (FC_CB_CREATE == $cb_type) {
510 if (defined $proc{'create'}) {
511 $cb_name = $proc{'create'};
517 elsif (FC_CB_DESTROY == $cb_type) {
518 if (defined $proc{'destroy'}) {
519 $cb_name = $proc{'destroy'};
526 $status = call_by_name (@_);
535 $err = "callback returned false";
538 ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
551 DEBUG ("Collectd::fc_register: "
552 . "type = \"$type\" (" . $fc_types{$type}
553 . "), name = \"$name\", proc = \"$proc\"");
555 if (! ((defined $type) && (defined $name) && (defined $proc))) {
556 ERROR ("Usage: Collectd::fc_register(type, name, proc)");
560 if (! defined $fc_plugins[$type]) {
561 ERROR ("Collectd::fc_register: Invalid type \"$type\"");
565 if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
566 || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
567 ERROR ("Collectd::fc_register: Invalid proc.");
571 for my $p (qw( create destroy )) {
572 if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
573 ERROR ("Collectd::fc_register: Invalid proc.");
580 foreach my $p (keys %fc) {
581 my $pkg = scalar caller;
583 if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
587 if ($fc{$p} !~ m/^$pkg\:\:/) {
588 $fc{$p} = $pkg . "::" . $fc{$p};
592 lock %{$fc_plugins[$type]};
593 if (defined $fc_plugins[$type]->{$name}) {
594 WARNING ("Collectd::fc_register: Overwriting previous "
595 . "definition of match \"$name\".");
598 if (! _fc_register ($type, $name)) {
599 ERROR ("Collectd::fc_register: Failed to register \"$name\".");
603 $fc_plugins[$type]->{$name} = \%fc;
607 sub _plugin_dispatch_config {
611 our $cb_name = undef;
613 if (! (defined ($plugin) && defined ($config))) {
617 if (! defined $cf_callbacks{$plugin}) {
618 WARNING ("Found a configuration for the \"$plugin\" plugin, but "
619 . "the plugin isn't loaded or didn't register "
620 . "a configuration callback.");
626 $cb_name = $cf_callbacks{$plugin};
628 call_by_name ($config);
633 # vim: set sw=4 ts=4 tw=78 noexpandtab :