| 1 | #vim: softtabstop-2 sw=2 : | ||
| 2 | package Nagios::Passive::Base; | ||
| 3 | |||
| 4 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 5 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 5 | ▶ | use Fcntl qw/:DEFAULT :flock/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 5 | ▶ | use Nagios::Plugin::Threshold; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Nagios::Plugin::Performance; | ||
| 9 | Nagios::Plugin::Functions::_use_die(1); | ||
| 10 | use overload '""' => 'to_string'; | ||
| 11 | use Any::Moose; | ||
| 12 | |||
| 13 | my %RETURN_CODES = ( | ||
| 14 | 0 => 'OK', | ||
| 15 | 1 => 'WARNING', | ||
| 16 | 2 => 'CRITICAL', | ||
| 17 | 3 => 'UNKNOWN', | ||
| 18 | ); | ||
| 19 | |||
| 20 | has 'check_name' => ( is => 'rw', isa => 'Str', required => 1); | ||
| 21 | has 'host_name' => ( is => 'rw', isa => 'Str', required => 1); | ||
| 22 | has 'service_description' => ( is => 'rw', isa => 'Str'); | ||
| 23 | has 'time' => ( is => 'rw', isa => 'Int', default => sub { time }); | ||
| 24 | has 'return_code' => ( is => 'rw', isa => 'Int', default => 0); | ||
| 25 | has 'output' => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => 'Str', | ||
| 28 | traits => ['String'], | ||
| 29 | default => '', | ||
| 30 | handles => { | ||
| 31 | add_output => 'append', | ||
| 32 | }, | ||
| 33 | ); | ||
| 34 | has 'threshold' => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'Nagios::Plugin::Threshold', | ||
| 37 | handles => [qw/set_thresholds/], | ||
| 38 | lazy => 1, | ||
| 39 | predicate => 'has_threshold', | ||
| 40 | default => sub { Nagios::Plugin::Threshold->new }, | ||
| 41 | ); | ||
| 42 | has 'performance' => ( | ||
| 43 | traits => ['Array'], | ||
| 44 | is => 'ro', | ||
| 45 | isa => 'ArrayRef[Nagios::Plugin::Performance]', | ||
| 46 | default => sub { [] }, | ||
| 47 | lazy => 1, | ||
| 48 | predicate => 'has_performance', | ||
| 49 | handles => { | ||
| 50 | _performance_add => 'push', | ||
| 51 | } | ||
| 52 | ); | ||
| 53 | |||
| 54 | sub to_string { | ||
| 55 | croak("override this"); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub submit { | ||
| 59 | croak("override this"); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub add_perf { | ||
| 63 | my $self = shift; | ||
| 64 | my $perf = Nagios::Plugin::Performance->new(@_); | ||
| 65 | $self->_performance_add($perf); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub set_status { | ||
| 69 | my $self = shift; | ||
| 70 | my $value = shift; | ||
| 71 | unless($self->has_threshold) { | ||
| 72 | croak("you have to call set_thresholds before calling set_status"); | ||
| 73 | } | ||
| 74 | $self->return_code($self->threshold->get_status($value)) | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _status_code { | ||
| 78 | my $self = shift; | ||
| 79 | my $r = $RETURN_CODES{$self->return_code}; | ||
| 80 | return defined($r) ? $r : 'UNKNOWN'; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _quoted_output { | ||
| 84 | my $self = shift; | ||
| 85 | my $output = $self->output; | ||
| 86 | # remove trailing newlines and quote the remaining ones | ||
| 87 | $output =~ s/[\r\n]*$//o; | ||
| 88 | $output =~ s/\n/\\n/go; | ||
| 89 | if($self->has_performance) { | ||
| 90 | return $output . " | ".$self->_perf_string; | ||
| 91 | } | ||
| 92 | return $output; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub _perf_string { | ||
| 96 | my $self = shift; | ||
| 97 | return "" unless($self->has_performance); | ||
| 98 | return join (" ", map { $_->perfoutput } @{ $self->performance }); | ||
| 99 | } | ||
| 100 | |||
| 101 | no Any::Moose; | ||
| 102 | __PACKAGE__->meta->make_immutable; | ||
| 103 | 1; | ||
| 104 | __END__ | ||
| 105 | |||
| 106 | =head1 NAME | ||
| 107 | |||
| 108 | Nagios::Passive::Base - Base class for Nagios::Passive backends. | ||
| 109 | |||
| 110 | =head1 SYNOPSIS | ||
| 111 | |||
| 112 | This is an abstract class. | ||
| 113 | |||
| 114 | =head1 DESCRIPTION | ||
| 115 | |||
| 116 | This is just a base class for Nagios::Passive backends. Currently | ||
| 117 | known implementations: | ||
| 118 | |||
| 119 | =over 4 | ||
| 120 | |||
| 121 | =item * Nagios::Passive::CommandFile | ||
| 122 | |||
| 123 | =item * Nagios::Passive::ResultPath | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | =cut | ||