summaryrefslogtreecommitdiffstats
path: root/lib/Nagios/Plugin/Base.pm
blob: d1678a5639b689de988a2a63022b979e564010e7 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
# This module holds all exported variables
# and base functions
package Nagios::Plugin::Base;

use strict;
use warnings;

use Nagios::Plugin;
our ($VERSION) = $Nagios::Plugin::VERSION;

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;

    if (  exists $args->{return_code} 
	  && exists $STATUS_TEXT{$args->{return_code}}  
	) {
	$return_code = $args->{return_code};
    }
    else {
	$return_code = $ERRORS{UNKNOWN};
    }
    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