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
75 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
76 foreach keys %EXPORT_TAGS;
79 Exporter::export_ok_tags ('all');
81 my @plugins : shared = ();
87 TYPE_SHUTDOWN, "shutdown",
91 foreach my $type (keys %types) {
92 $plugins[$type] = &share ({});
100 if ("Collectd" eq $caller) {
103 return plugin_log ($lvl, $msg);
106 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
107 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
108 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
109 sub INFO { _log (scalar caller, LOG_INFO, shift); }
110 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
112 sub plugin_call_all {
115 our $cb_name = undef;
117 if (! defined $type) {
121 if (TYPE_LOG != $type) {
122 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
125 if (! defined $plugins[$type]) {
126 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
131 foreach my $plugin (keys %{$plugins[$type]}) {
132 my $p = $plugins[$type]->{$plugin};
136 if ($p->{'wait_left'} > 0) {
137 # TODO: use interval_g
138 $p->{'wait_left'} -= 10;
141 next if ($p->{'wait_left'} > 0);
143 $cb_name = $p->{'cb_name'};
144 $status = call_by_name (@_);
146 if (! defined $status) {
147 if (TYPE_LOG != $type) {
148 ERROR ("Could not execute callback \"$cb_name\": $@");
155 $p->{'wait_left'} = 0;
156 $p->{'wait_time'} = 10;
158 elsif (TYPE_READ == $type) {
159 $p->{'wait_left'} = $p->{'wait_time'};
160 $p->{'wait_time'} *= 2;
162 if ($p->{'wait_time'} > 86400) {
163 $p->{'wait_time'} = 86400;
166 WARNING ("${plugin}->read() failed with status $status. "
167 . "Will suspend it for $p->{'wait_left'} seconds.");
169 elsif (TYPE_INIT == $type) {
170 foreach my $type (keys %types) {
171 plugin_unregister ($type, $plugin);
174 ERROR ("${plugin}->init() failed with status $status. "
175 . "Plugin will be disabled.");
177 elsif (TYPE_LOG != $type) {
178 WARNING ("${plugin}->$types{$type}() failed with status $status.");
184 # Collectd::plugin_register (type, name, data).
187 # init, read, write, shutdown, data set
193 # reference to the plugin's subroutine that does the work or the data set
195 sub plugin_register {
200 DEBUG ("Collectd::plugin_register: "
201 . "type = \"$type\", name = \"$name\", data = \"$data\"");
203 if (! ((defined $type) && (defined $name) && (defined $data))) {
204 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
208 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
209 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
213 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
214 return plugin_register_data_set ($name, $data);
216 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
217 my $pkg = scalar caller;
221 if ($data !~ m/^$pkg/) {
222 $data = $pkg . "::" . $data;
225 # TODO: make interval_g available at configuration time
233 $plugins[$type]->{$name} = \%p;
236 ERROR ("Collectd::plugin_register: Invalid data.");
242 sub plugin_unregister {
246 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
248 if (! ((defined $type) && (defined $name))) {
249 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
253 if (TYPE_DATASET == $type) {
254 return plugin_unregister_data_set ($name);
256 elsif (defined $plugins[$type]) {
258 delete $plugins[$type]->{$name};
261 ERROR ("Collectd::plugin_unregister: Invalid type.");
268 # vim: set sw=4 ts=4 tw=78 noexpandtab :