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
87 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
88 foreach keys %EXPORT_TAGS;
95 Exporter::export_ok_tags ('all');
97 my @plugins : shared = ();
103 TYPE_SHUTDOWN, "shutdown",
105 TYPE_NOTIF, "notify",
109 foreach my $type (keys %types) {
110 $plugins[$type] = &share ({});
118 if ("Collectd" eq $caller) {
121 return plugin_log ($lvl, $msg);
124 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
125 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
126 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
127 sub INFO { _log (scalar caller, LOG_INFO, shift); }
128 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
130 sub plugin_call_all {
133 our $cb_name = undef;
135 if (! defined $type) {
139 if (TYPE_LOG != $type) {
140 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
143 if (! defined $plugins[$type]) {
144 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
149 foreach my $plugin (keys %{$plugins[$type]}) {
150 my $p = $plugins[$type]->{$plugin};
154 if ($p->{'wait_left'} > 0) {
155 $p->{'wait_left'} -= $interval_g;
158 next if ($p->{'wait_left'} > 0);
160 $cb_name = $p->{'cb_name'};
161 $status = call_by_name (@_);
170 $err = "callback returned false";
173 if (TYPE_LOG != $type) {
174 ERROR ("Execution of callback \"$cb_name\" failed: $err");
181 $p->{'wait_left'} = 0;
182 $p->{'wait_time'} = $interval_g;
184 elsif (TYPE_READ == $type) {
185 if ($p->{'wait_time'} < $interval_g) {
186 $p->{'wait_time'} = $interval_g;
189 $p->{'wait_left'} = $p->{'wait_time'};
190 $p->{'wait_time'} *= 2;
192 if ($p->{'wait_time'} > 86400) {
193 $p->{'wait_time'} = 86400;
196 WARNING ("${plugin}->read() failed with status $status. "
197 . "Will suspend it for $p->{'wait_left'} seconds.");
199 elsif (TYPE_INIT == $type) {
200 ERROR ("${plugin}->init() failed with status $status. "
201 . "Plugin will be disabled.");
203 foreach my $type (keys %types) {
204 plugin_unregister ($type, $plugin);
207 elsif (TYPE_LOG != $type) {
208 WARNING ("${plugin}->$types{$type}() failed with status $status.");
214 # Collectd::plugin_register (type, name, data).
217 # init, read, write, shutdown, data set
223 # reference to the plugin's subroutine that does the work or the data set
225 sub plugin_register {
230 DEBUG ("Collectd::plugin_register: "
231 . "type = \"$type\", name = \"$name\", data = \"$data\"");
233 if (! ((defined $type) && (defined $name) && (defined $data))) {
234 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
238 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
239 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
243 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
244 return plugin_register_data_set ($name, $data);
246 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
247 my $pkg = scalar caller;
251 if ($data !~ m/^$pkg/) {
252 $data = $pkg . "::" . $data;
256 wait_time => $interval_g,
262 $plugins[$type]->{$name} = \%p;
265 ERROR ("Collectd::plugin_register: Invalid data.");
271 sub plugin_unregister {
275 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
277 if (! ((defined $type) && (defined $name))) {
278 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
282 if (TYPE_DATASET == $type) {
283 return plugin_unregister_data_set ($name);
285 elsif (defined $plugins[$type]) {
287 delete $plugins[$type]->{$name};
290 ERROR ("Collectd::plugin_unregister: Invalid type.");
297 # vim: set sw=4 ts=4 tw=78 noexpandtab :