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
45 plugin_dispatch_notification
86 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
87 foreach keys %EXPORT_TAGS;
90 Exporter::export_ok_tags ('all');
92 my @plugins : shared = ();
98 TYPE_SHUTDOWN, "shutdown",
103 foreach my $type (keys %types) {
104 $plugins[$type] = &share ({});
112 if ("Collectd" eq $caller) {
115 return plugin_log ($lvl, $msg);
118 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
119 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
120 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
121 sub INFO { _log (scalar caller, LOG_INFO, shift); }
122 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
124 sub plugin_call_all {
127 our $cb_name = undef;
129 if (! defined $type) {
133 if (TYPE_LOG != $type) {
134 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
137 if (! defined $plugins[$type]) {
138 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
143 foreach my $plugin (keys %{$plugins[$type]}) {
144 my $p = $plugins[$type]->{$plugin};
148 if ($p->{'wait_left'} > 0) {
149 # TODO: use interval_g
150 $p->{'wait_left'} -= 10;
153 next if ($p->{'wait_left'} > 0);
155 $cb_name = $p->{'cb_name'};
156 $status = call_by_name (@_);
165 $err = "callback returned false";
168 if (TYPE_LOG != $type) {
169 ERROR ("Execution of callback \"$cb_name\" failed: $err");
176 $p->{'wait_left'} = 0;
177 $p->{'wait_time'} = 10;
179 elsif (TYPE_READ == $type) {
180 WARNING ("${plugin}->read() failed with status $status. "
181 . "Will suspend it for $p->{'wait_left'} seconds.");
183 $p->{'wait_left'} = $p->{'wait_time'};
184 $p->{'wait_time'} *= 2;
186 if ($p->{'wait_time'} > 86400) {
187 $p->{'wait_time'} = 86400;
190 elsif (TYPE_INIT == $type) {
191 ERROR ("${plugin}->init() failed with status $status. "
192 . "Plugin will be disabled.");
194 foreach my $type (keys %types) {
195 plugin_unregister ($type, $plugin);
198 elsif (TYPE_LOG != $type) {
199 WARNING ("${plugin}->$types{$type}() failed with status $status.");
205 # Collectd::plugin_register (type, name, data).
208 # init, read, write, shutdown, data set
214 # reference to the plugin's subroutine that does the work or the data set
216 sub plugin_register {
221 DEBUG ("Collectd::plugin_register: "
222 . "type = \"$type\", name = \"$name\", data = \"$data\"");
224 if (! ((defined $type) && (defined $name) && (defined $data))) {
225 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
229 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
230 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
234 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
235 return plugin_register_data_set ($name, $data);
237 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
238 my $pkg = scalar caller;
242 if ($data !~ m/^$pkg/) {
243 $data = $pkg . "::" . $data;
246 # TODO: make interval_g available at configuration time
254 $plugins[$type]->{$name} = \%p;
257 ERROR ("Collectd::plugin_register: Invalid data.");
263 sub plugin_unregister {
267 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
269 if (! ((defined $type) && (defined $name))) {
270 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
274 if (TYPE_DATASET == $type) {
275 return plugin_unregister_data_set ($name);
277 elsif (defined $plugins[$type]) {
279 delete $plugins[$type]->{$name};
282 ERROR ("Collectd::plugin_unregister: Invalid type.");
289 # vim: set sw=4 ts=4 tw=78 noexpandtab :