diff options
author | Gavin Carr <gonzai@users.sourceforge.net> | 2006-09-26 01:10:23 +0000 |
---|---|---|
committer | Gavin Carr <gonzai@users.sourceforge.net> | 2006-09-26 01:10:23 +0000 |
commit | e548a22a92b3aa345f4a952fddb39cc693c35a70 (patch) | |
tree | 606e3872d3070a90b5fa9881303eddb59efc8bbd /lib/Nagios/Plugin/Base.pm | |
parent | d8f912e8f5abb1476366cfea6e0fb368a9669ec4 (diff) | |
download | monitoring-plugin-perl-e548a22a92b3aa345f4a952fddb39cc693c35a70.tar.gz |
Rename NP::Base to NP::Functions; add check_messages() to NP::Functions.
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/Nagios-Plugin/trunk@1482 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/Nagios/Plugin/Base.pm')
-rw-r--r-- | lib/Nagios/Plugin/Base.pm | 196 |
1 files changed, 0 insertions, 196 deletions
diff --git a/lib/Nagios/Plugin/Base.pm b/lib/Nagios/Plugin/Base.pm deleted file mode 100644 index 92651ed..0000000 --- a/lib/Nagios/Plugin/Base.pm +++ /dev/null | |||
@@ -1,196 +0,0 @@ | |||
1 | # This module holds all exported variables | ||
2 | # and base functions | ||
3 | package Nagios::Plugin::Base; | ||
4 | |||
5 | use strict; | ||
6 | use warnings; | ||
7 | use File::Basename; | ||
8 | |||
9 | our $VERSION = "0.13"; | ||
10 | |||
11 | our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT); | ||
12 | |||
13 | require Exporter; | ||
14 | our @ISA = qw(Exporter); | ||
15 | our @EXPORT = (@STATUS_CODES, qw(nagios_exit %ERRORS)); | ||
16 | our @EXPORT_OK = qw(nagios_die %STATUS_TEXT); | ||
17 | our %EXPORT_TAGS = ( | ||
18 | all => [ @EXPORT, @EXPORT_OK ], | ||
19 | codes => [ @STATUS_CODES ], | ||
20 | functions => [ qw(nagios_exit nagios_die) ], | ||
21 | ); | ||
22 | |||
23 | use constant OK => 0; | ||
24 | use constant WARNING => 1; | ||
25 | use constant CRITICAL => 2; | ||
26 | use constant UNKNOWN => 3; | ||
27 | use constant DEPENDENT => 4; | ||
28 | |||
29 | our %ERRORS = ( | ||
30 | 'OK' => OK, | ||
31 | 'WARNING' => WARNING, | ||
32 | 'CRITICAL' => CRITICAL, | ||
33 | 'UNKNOWN' => UNKNOWN, | ||
34 | 'DEPENDENT' => DEPENDENT, | ||
35 | ); | ||
36 | |||
37 | our %STATUS_TEXT = reverse %ERRORS; | ||
38 | |||
39 | # _fake_exit flag and accessor/mutator, for testing | ||
40 | my $_fake_exit = 0; | ||
41 | sub _fake_exit { @_ ? $_fake_exit = shift : $_fake_exit }; | ||
42 | |||
43 | sub get_shortname { | ||
44 | my %arg = @_; | ||
45 | |||
46 | return $arg{plugin}->shortname if $arg{plugin}; | ||
47 | |||
48 | my $shortname = uc basename($ENV{NAGIOS_PLUGIN} || $0); | ||
49 | $shortname =~ s/^CHECK_//; | ||
50 | return $shortname; | ||
51 | } | ||
52 | |||
53 | # nagios_exit( $code, $message ) | ||
54 | sub nagios_exit { | ||
55 | my ($code, $message, $arg) = @_; | ||
56 | |||
57 | # Handle named parameters | ||
58 | if (defined $code && ($code eq 'return_code' || $code eq 'message')) { | ||
59 | # Remove last argument if odd no and last is ref | ||
60 | if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { | ||
61 | $arg = pop @_; | ||
62 | } else { | ||
63 | undef $arg; | ||
64 | } | ||
65 | my %arg = @_; | ||
66 | $code = $arg{return_code}; | ||
67 | $message = $arg{message}; | ||
68 | } | ||
69 | $arg ||= {}; | ||
70 | |||
71 | # Handle string codes | ||
72 | $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; | ||
73 | |||
74 | # Set defaults | ||
75 | $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; | ||
76 | $message = '' unless defined $message; | ||
77 | $message = join(' ', @$message) if ref $message eq 'ARRAY'; | ||
78 | |||
79 | # Setup output | ||
80 | my $output = "$STATUS_TEXT{$code}"; | ||
81 | $output .= " - $message" if defined $message && $message ne ''; | ||
82 | my $shortname = get_shortname(plugin => $arg->{plugin}); | ||
83 | $output = "$shortname $output" if $shortname; | ||
84 | if ($arg->{plugin}) { | ||
85 | my $plugin = $arg->{plugin}; | ||
86 | $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata; | ||
87 | } | ||
88 | $output .= "\n"; | ||
89 | |||
90 | # Don't actually exit if _fake_exit set | ||
91 | if ($_fake_exit) { | ||
92 | require Nagios::Plugin::ExitResult; | ||
93 | return Nagios::Plugin::ExitResult->new($code, $output); | ||
94 | } | ||
95 | |||
96 | # Print output and exit | ||
97 | print $output; | ||
98 | exit $code; | ||
99 | } | ||
100 | |||
101 | # nagios_die( $message, [ $code ]) OR nagios_die( $code, $message ) | ||
102 | # Default $code: UNKNOWN | ||
103 | sub nagios_die { | ||
104 | my ($arg1, $arg2, $rest) = @_; | ||
105 | |||
106 | # Named parameters | ||
107 | if (defined $arg1 && ($arg1 eq 'return_code' || $arg1 eq 'message')) { | ||
108 | return nagios_exit(@_); | ||
109 | } | ||
110 | |||
111 | # ($code, $message) | ||
112 | elsif (defined $arg1 && (exists $ERRORS{$arg1} || exists $STATUS_TEXT{$arg1})) { | ||
113 | return nagios_exit(@_); | ||
114 | } | ||
115 | |||
116 | # ($message, $code) | ||
117 | elsif (defined $arg2 && (exists $ERRORS{$arg2} || exists $STATUS_TEXT{$arg2})) { | ||
118 | return nagios_exit($arg2, $arg1, $rest); | ||
119 | } | ||
120 | |||
121 | # Else just assume $arg1 is the message and hope for the best | ||
122 | else { | ||
123 | return nagios_exit( UNKNOWN, $arg1, $rest ); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | # For backwards compatibility | ||
128 | sub die { nagios_die(@_); } | ||
129 | |||
130 | |||
131 | =pod old | ||
132 | |||
133 | my $exit_on_die = 1; | ||
134 | sub exit_on_die { shift; @_ ? $exit_on_die = shift : $exit_on_die }; | ||
135 | my $print_on_die = 1; | ||
136 | sub print_on_die { shift; @_ ? $print_on_die = shift : $print_on_die }; | ||
137 | |||
138 | # Old version - TODO: remove | ||
139 | sub old_die { | ||
140 | my ($class, $args, $plugin) = @_; | ||
141 | my $return_code; | ||
142 | |||
143 | if ( exists $args->{return_code} | ||
144 | && exists $STATUS_TEXT{$args->{return_code}} | ||
145 | ) { | ||
146 | $return_code = $args->{return_code}; | ||
147 | } | ||
148 | else { | ||
149 | $return_code = $ERRORS{UNKNOWN}; | ||
150 | } | ||
151 | my $message = $args->{message} || "Internal error"; | ||
152 | my $output = join(" ", $STATUS_TEXT{$return_code}, $message); | ||
153 | if ($plugin) { | ||
154 | $output = $plugin->shortname." $output" if $plugin->shortname; | ||
155 | $output .= " | ".$plugin->all_perfoutput if $plugin->perfdata; | ||
156 | } | ||
157 | if ($print_on_die) { | ||
158 | print $output, $/; | ||
159 | } | ||
160 | if ($exit_on_die) { | ||
161 | exit $return_code; | ||
162 | } else { | ||
163 | return $output; | ||
164 | } | ||
165 | } | ||
166 | |||
167 | =cut | ||
168 | |||
169 | 1; | ||
170 | |||
171 | # vim:sw=4:sm:et | ||
172 | |||
173 | __END__ | ||
174 | |||
175 | =head1 NAME | ||
176 | |||
177 | Nagios::Plugin::Base - Base functions for Nagios::Plugins | ||
178 | |||
179 | =head1 DESCRIPTION | ||
180 | |||
181 | See Nagios::Plugin for public interfaces. This module is for Nagios::Plugin developers to incorporate | ||
182 | common backend functionality. | ||
183 | |||
184 | =head1 AUTHOR | ||
185 | |||
186 | Ton Voon, E<lt>ton.voon@altinity.comE<gt> | ||
187 | |||
188 | =head1 COPYRIGHT AND LICENSE | ||
189 | |||
190 | Copyright (C) 2006 by Nagios Plugin Development Team | ||
191 | |||
192 | This library is free software; you can redistribute it and/or modify | ||
193 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | ||
194 | at your option, any later version of Perl 5 you may have available. | ||
195 | |||
196 | =cut | ||