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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
package Nagios::MiniPlugin;
use strict;
use Getopt::Long qw(:config no_ignore_case bundling);
our @STATUS_CODES = qw(OK WARNING CRITICAL UNKNOWN DEPENDENT);
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = (@STATUS_CODES, qw(nagios_exit nagios_die check_messages));
our @EXPORT_OK = qw(%ERRORS);
use constant OK => 0;
use constant WARNING => 1;
use constant CRITICAL => 2;
use constant UNKNOWN => 3;
use constant DEPENDENT => 4;
our %ERRORS = (
'OK' => OK,
'WARNING' => WARNING,
'CRITICAL' => CRITICAL,
'UNKNOWN' => UNKNOWN,
'DEPENDENT' => DEPENDENT,
);
our %STATUS_TEXT = reverse %ERRORS;
sub new {
my $class = shift;
my %params = @_;
my $self = {
perfdata => [],
messages => {
ok => [],
warning => [],
critical => [],
unknown => [],
},
args => [],
opts => Nagios::MiniPlugin::Getopt->new(%params),
};
foreach (qw(shortname usage version url plugin blurb extra
license timeout)) {
$self->{$_} = $params{$_};
}
bless $self, $class;
}
sub add_arg {
my $self = shift;
$self->{opts}->add_arg(@_);
}
sub getopts {
my $self = shift;
$self->{opts}->getopts();
}
sub opts {
my $self = shift;
return $self->{opts};
}
sub add_message {
my $self = shift;
my ($code, @messages) = @_;
$code = (qw(ok warning critical unknown))[$code] if $code =~ /^\d+$/;
$code = lc $code;
push @{$self->{messages}->{$code}}, @messages;
}
sub remove_message {
my $self = shift;
my ($code, @messages) = @_;
$code = (qw(ok warning critical unknown))[$code] if $code =~ /^\d+$/;
$code = lc $code;
pop @{$self->{messages}->{$code}};
}
sub add_perfdata {
my ($self, %args) = @_;
my $str = $args{label}.'='.$args{value};
if ($args{uom}) {
$str .= $args{uom};
}
if ($args{warning}) {
$str .= ';'.$args{warning};
}
if ($args{critical}) {
$str .= ';'.$args{critical};
}
push @{$self->{perfdata}}, $str;
}
sub check_messages {
my $self = shift;
my %args = @_;
# Add object messages to any passed in as args
for my $code (qw(critical warning unknown ok)) {
my $messages = $self->{messages}->{$code} || [];
if ($args{$code}) {
unless (ref $args{$code} eq 'ARRAY') {
if ($code eq 'ok') {
$args{$code} = [ $args{$code} ];
}
}
push @{$args{$code}}, @$messages;
} else {
$args{$code} = $messages;
}
}
my %arg = %args;
$arg{join} = ' ' unless defined $arg{join};
# Decide $code
my $code = OK;
$code ||= CRITICAL if @{$arg{critical}};
$code ||= WARNING if @{$arg{warning}};
$code ||= UNKNOWN if @{$arg{unknown}};
return $code unless wantarray;
# Compose message
my $message = '';
if ($arg{join_all}) {
$message = join( $arg{join_all},
map { @$_ ? join( $arg{'join'}, @$_) : () }
$arg{critical},
$arg{warning},
$arg{unknown},
$arg{ok} ? (ref $arg{ok} ? $arg{ok} : [ $arg{ok} ]) : []
);
}
else {
$message ||= join( $arg{'join'}, @{$arg{critical}} )
if $code == CRITICAL;
$message ||= join( $arg{'join'}, @{$arg{warning}} )
if $code == WARNING;
$message ||= join( $arg{'join'}, @{$arg{unknown}} )
if $code == UNKNOWN;
$message ||= ref $arg{ok} ? join( $arg{'join'}, @{$arg{ok}} ) : $arg{ok}
if $arg{ok};
}
return ($code, $message);
}
sub nagios_exit {
my $self = shift;
my ($code, $message, $arg) = @_;
$code = $ERRORS{$code} if defined $code && exists $ERRORS{$code};
$code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code};
$message = '' unless defined $message;
if (ref $message && ref $message eq 'ARRAY') {
$message = join(' ', map { chomp; $_ } @$message);
} else {
chomp $message;
}
my $output = "$STATUS_TEXT{$code}";
$output .= " - $message" if defined $message && $message ne '';
if (scalar (@{$self->{perfdata}})) {
$output .= " | ".join(" ", @{$self->{perfdata}});
}
$output .= "\n";
print $output;
exit $code;
}
package Nagios::MiniPlugin::Getopt;
use strict;
use File::Basename;
use Data::Dumper;
use Getopt::Long qw(:config no_ignore_case bundling);
# Standard defaults
my %DEFAULT = (
timeout => 60,
verbose => 0,
license =>
"This nagios plugin is free software, and comes with ABSOLUTELY NO WARRANTY.
It may be used, redistributed and/or modified under the terms of the GNU
General Public Licence (see http://www.fsf.org/licensing/licenses/gpl.txt).",
);
# Standard arguments
my @ARGS = ({
spec => 'usage|?',
help => "-?, --usage\n Print usage information",
}, {
spec => 'help|h',
help => "-h, --help\n Print detailed help screen",
}, {
spec => 'version|V',
help => "-V, --version\n Print version information",
}, {
#spec => 'extra-opts:s@',
#help => "--extra-opts=[<section>[@<config_file>]]\n Section and/or config_file from which to load extra options (may repeat)",
}, {
spec => 'timeout|t=i',
help => "-t, --timeout=INTEGER\n Seconds before plugin times out (default: %s)",
default => $DEFAULT{timeout},
}, {
spec => 'verbose|v+',
help => "-v, --verbose\n Show details for command-line debugging (can repeat up to 3 times)",
default => $DEFAULT{verbose},
},
);
# Standard arguments we traditionally display last in the help output
my %DEFER_ARGS = map { $_ => 1 } qw(timeout verbose);
sub _init
{
my $self = shift;
my %params = @_;
# Check params
my $plugin = basename($ENV{NAGIOS_PLUGIN} || $0);
#my %attr = validate( @_, {
my %attr = (
usage => 1,
version => 0,
url => 0,
plugin => { default => $plugin },
blurb => 0,
extra => 0,
'extra-opts' => 0,
license => { default => $DEFAULT{license} },
timeout => { default => $DEFAULT{timeout} },
);
# Add attr to private _attr hash (except timeout)
$self->{timeout} = delete $attr{timeout};
$self->{_attr} = { %attr };
foreach (keys %{$self->{_attr}}) {
if (exists $params{$_}) {
$self->{_attr}->{$_} = $params{$_};
} else {
$self->{_attr}->{$_} = $self->{_attr}->{$_}->{default}
if ref ($self->{_attr}->{$_}) eq 'HASH' &&
exists $self->{_attr}->{$_}->{default};
}
}
# Chomp _attr values
chomp foreach values %{$self->{_attr}};
# Setup initial args list
$self->{_args} = [ grep { exists $_->{spec} } @ARGS ];
$self
}
sub new
{
my $class = shift;
my $self = bless {}, $class;
$self->_init(@_);
}
sub add_arg {
my $self = shift;
my %arg = @_;
push (@{$self->{_args}}, \%arg);
}
sub getopts {
my $self = shift;
my %commandline = ();
my @params = map { $_->{spec} } @{$self->{_args}};
if (! GetOptions(\%commandline, @params)) {
$self->print_help();
exit 0;
} else {
no strict 'refs';
do { $self->print_help(); exit 0; } if $commandline{help};
do { $self->print_version(); exit 0 } if $commandline{version};
do { $self->print_usage(); exit 0 } if $commandline{usage};
foreach (map { $_->{spec} =~ /^([\w\-]+)/; $1; } @{$self->{_args}}) {
my $field = $_;
*{"$field"} = sub {
return $self->{opts}->{$field};
};
}
foreach (grep { exists $_->{default} } @{$self->{_args}}) {
$_->{spec} =~ /^([\w\-]+)/;
my $spec = $1;
$self->{opts}->{$spec} = $_->{default};
}
foreach (keys %commandline) {
$self->{opts}->{$_} = $commandline{$_};
}
}
}
sub get {
my $self = shift;
my $opt = shift;
return $self->{opts}->{$opt};
}
sub print_help {
my $self = shift;
$self->print_version();
printf "\n%s\n", $self->{_attr}->{license};
printf "\n%s\n\n", $self->{_attr}->{blurb};
$self->print_usage();
foreach (@{$self->{_args}}) {
printf " %s\n", $_->{help};
}
exit 0;
}
sub print_usage {
my $self = shift;
printf $self->{_attr}->{usage}, $self->{_attr}->{plugin};
print "\n";
}
sub print_version {
my $self = shift;
printf "%s %s", $self->{_attr}->{plugin}, $self->{_attr}->{version};
printf " [%s]", $self->{_attr}->{url} if $self->{_attr}->{url};
print "\n";
}
sub print_license {
my $self = shift;
printf "%s\n", $self->{_attr}->{license};
print "\n";
}
1;
|