summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin/Base.pm
blob: f3a7f7cb11865bcc7e94adb594fe49be979d20bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# This module holds all exported variables
# and base functions
package Nagios::Plugin::Base;

use strict;
use warnings;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(%ERRORS);

our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);

our %STATUS_TEXT = reverse %ERRORS;

my $exit_on_die = 1;
sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die };
my $print_on_die = 1;
sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die };

sub die {
	my ($class, $args, $plugin) = @_;
	my $return_code = $args->{return_code} || 3;
	my $message = $args->{message} || "Internal error";
	my $output = join(" ", $STATUS_TEXT{$return_code}, $message);
	if ($plugin) {
		$output = $plugin->shortname." $output" if $plugin->shortname;
		$output .= " | ".$plugin->all_perfoutput if $plugin->perfdata;
	}
	if ($print_on_die) {
		print $output, $/;
	}
	if ($exit_on_die) {
		exit $return_code;
	} else {
		return $output;
	}
}

1;
__END__

=head1 NAME

Nagios::Plugin::Base - Base functions for Nagios::Plugins

=head1 DESCRIPTION

See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate
common backend functionality.

=head1 AUTHOR

Ton Voon, E<lt>ton.voon@altinity.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Nagios Plugin Development Team

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.4 or,
at your option, any later version of Perl 5 you may have available.


=cut