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
85 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
86 foreach keys %EXPORT_TAGS;
89 Exporter::export_ok_tags ('all');
91 my @plugins : shared = ();
97 TYPE_SHUTDOWN, "shutdown",
102 foreach my $type (keys %types) {
103 $plugins[$type] = &share ({});
111 if ("Collectd" eq $caller) {
114 return plugin_log ($lvl, $msg);
117 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
118 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
119 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
120 sub INFO { _log (scalar caller, LOG_INFO, shift); }
121 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
123 sub plugin_call_all {
126 our $cb_name = undef;
128 if (! defined $type) {
132 if (TYPE_LOG != $type) {
133 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
136 if (! defined $plugins[$type]) {
137 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
142 foreach my $plugin (keys %{$plugins[$type]}) {
143 my $p = $plugins[$type]->{$plugin};
147 if ($p->{'wait_left'} > 0) {
148 # TODO: use interval_g
149 $p->{'wait_left'} -= 10;
152 next if ($p->{'wait_left'} > 0);
154 $cb_name = $p->{'cb_name'};
155 $status = call_by_name (@_);
164 $err = "callback returned false";
167 if (TYPE_LOG != $type) {
168 ERROR ("Execution of callback \"$cb_name\" failed: $err");
175 $p->{'wait_left'} = 0;
176 $p->{'wait_time'} = 10;
178 elsif (TYPE_READ == $type) {
179 WARNING ("${plugin}->read() failed with status $status. "
180 . "Will suspend it for $p->{'wait_left'} seconds.");
182 $p->{'wait_left'} = $p->{'wait_time'};
183 $p->{'wait_time'} *= 2;
185 if ($p->{'wait_time'} > 86400) {
186 $p->{'wait_time'} = 86400;
189 elsif (TYPE_INIT == $type) {
190 ERROR ("${plugin}->init() failed with status $status. "
191 . "Plugin will be disabled.");
193 foreach my $type (keys %types) {
194 plugin_unregister ($type, $plugin);
197 elsif (TYPE_LOG != $type) {
198 WARNING ("${plugin}->$types{$type}() failed with status $status.");
204 # Collectd::plugin_register (type, name, data).
207 # init, read, write, shutdown, data set
213 # reference to the plugin's subroutine that does the work or the data set
215 sub plugin_register {
220 DEBUG ("Collectd::plugin_register: "
221 . "type = \"$type\", name = \"$name\", data = \"$data\"");
223 if (! ((defined $type) && (defined $name) && (defined $data))) {
224 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
228 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
229 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
233 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
234 return plugin_register_data_set ($name, $data);
236 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
237 my $pkg = scalar caller;
241 if ($data !~ m/^$pkg/) {
242 $data = $pkg . "::" . $data;
245 # TODO: make interval_g available at configuration time
253 $plugins[$type]->{$name} = \%p;
256 ERROR ("Collectd::plugin_register: Invalid data.");
262 sub plugin_unregister {
266 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
268 if (! ((defined $type) && (defined $name))) {
269 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
273 if (TYPE_DATASET == $type) {
274 return plugin_unregister_data_set ($name);
276 elsif (defined $plugins[$type]) {
278 delete $plugins[$type]->{$name};
281 ERROR ("Collectd::plugin_unregister: Invalid type.");
288 # vim: set sw=4 ts=4 tw=78 noexpandtab :