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
79 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
80 foreach keys %EXPORT_TAGS;
83 Exporter::export_ok_tags ('all');
85 my @plugins : shared = ();
91 TYPE_SHUTDOWN, "shutdown",
95 foreach my $type (keys %types) {
96 $plugins[$type] = &share ({});
104 if ("Collectd" eq $caller) {
107 return plugin_log ($lvl, $msg);
110 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
111 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
112 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
113 sub INFO { _log (scalar caller, LOG_INFO, shift); }
114 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
116 sub plugin_call_all {
119 our $cb_name = undef;
121 if (! defined $type) {
125 if (TYPE_LOG != $type) {
126 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
129 if (! defined $plugins[$type]) {
130 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
135 foreach my $plugin (keys %{$plugins[$type]}) {
136 my $p = $plugins[$type]->{$plugin};
140 if ($p->{'wait_left'} > 0) {
141 # TODO: use interval_g
142 $p->{'wait_left'} -= 10;
145 next if ($p->{'wait_left'} > 0);
147 $cb_name = $p->{'cb_name'};
148 $status = call_by_name (@_);
157 $err = "callback returned false";
160 if (TYPE_LOG != $type) {
161 ERROR ("Execution of callback \"$cb_name\" failed: $err");
168 $p->{'wait_left'} = 0;
169 $p->{'wait_time'} = 10;
171 elsif (TYPE_READ == $type) {
172 WARNING ("${plugin}->read() failed with status $status. "
173 . "Will suspend it for $p->{'wait_left'} seconds.");
175 $p->{'wait_left'} = $p->{'wait_time'};
176 $p->{'wait_time'} *= 2;
178 if ($p->{'wait_time'} > 86400) {
179 $p->{'wait_time'} = 86400;
182 elsif (TYPE_INIT == $type) {
183 ERROR ("${plugin}->init() failed with status $status. "
184 . "Plugin will be disabled.");
186 foreach my $type (keys %types) {
187 plugin_unregister ($type, $plugin);
190 elsif (TYPE_LOG != $type) {
191 WARNING ("${plugin}->$types{$type}() failed with status $status.");
197 # Collectd::plugin_register (type, name, data).
200 # init, read, write, shutdown, data set
206 # reference to the plugin's subroutine that does the work or the data set
208 sub plugin_register {
213 DEBUG ("Collectd::plugin_register: "
214 . "type = \"$type\", name = \"$name\", data = \"$data\"");
216 if (! ((defined $type) && (defined $name) && (defined $data))) {
217 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
221 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)) {
222 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
226 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
227 return plugin_register_data_set ($name, $data);
229 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
230 my $pkg = scalar caller;
234 if ($data !~ m/^$pkg/) {
235 $data = $pkg . "::" . $data;
238 # TODO: make interval_g available at configuration time
246 $plugins[$type]->{$name} = \%p;
249 ERROR ("Collectd::plugin_register: Invalid data.");
255 sub plugin_unregister {
259 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
261 if (! ((defined $type) && (defined $name))) {
262 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
266 if (TYPE_DATASET == $type) {
267 return plugin_unregister_data_set ($name);
269 elsif (defined $plugins[$type]) {
271 delete $plugins[$type]->{$name};
274 ERROR ("Collectd::plugin_unregister: Invalid type.");
281 # vim: set sw=4 ts=4 tw=78 noexpandtab :