diff options
| author | Paul Buetow <paul@buetow.org> | 2015-01-02 13:27:02 +0100 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2015-01-02 13:27:02 +0100 |
| commit | 336c6c8a415603c772f97ccd63912d05209f3795 (patch) | |
| tree | 1a0febb81031d77fa8bec857333cce0a6d87436e /lib/PINGDOMFETCH/TLS.pm | |
initial1.0.0
Diffstat (limited to 'lib/PINGDOMFETCH/TLS.pm')
| -rw-r--r-- | lib/PINGDOMFETCH/TLS.pm | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/lib/PINGDOMFETCH/TLS.pm b/lib/PINGDOMFETCH/TLS.pm new file mode 100644 index 0000000..e5f1325 --- /dev/null +++ b/lib/PINGDOMFETCH/TLS.pm @@ -0,0 +1,166 @@ +package PINGDOMFETCH::TLS; + +use strict; +use warnings; + +use PINGDOMFETCH::Config; +use PINGDOMFETCH::Display; +use PINGDOMFETCH::Result; +use PINGDOMFETCH::Utils; + +our @ISA = ('PINGDOMFETCH::Display'); + +sub new { + my ( $class, %vals ) = @_; + + my $self = bless \%vals, $class; + $self->{is_critical} = 0; + + return $self; +} + +sub acc { + my ($self) = @_; + + my $config = $self->{config}; + my $is_in_future = $config->bool('interval_is_in_future'); + + my $count = 0; + my $tls_result = PINGDOMFETCH::Result->new( + config => $config, + totaldown => 0, + totalup => 0, + totalunknown => 0, + avgresponse => 0, + remaining => 0, + ); + $tls_result->{remaining} = 0 if $is_in_future; + + my $acc = sub { + my ( $service, $result ) = @_; + + $count++; + my $weight = + exists $service->{opts}{weight} + ? $service->{opts}{weight} + : 1; + + $tls_result->{$_} += $result->{$_} * $weight + for qw(totaldown totalup totalunknown); + + $tls_result->{$_} += $result->{$_} for qw(avgresponse); + + $tls_result->{remaining} += $result->{remaining} * $weight + if $is_in_future; + }; + + if ( exists $self->{services} ) { + $self->{services}{$_}->acc($acc) for keys %{ $self->{services} }; + } + + if ( $count > 0 ) { + $tls_result->{avgresponse} /= $count; + $tls_result->compute(); + $self->{result} = $tls_result; + } + + $self->{is_critical} = 1 + if $self->{result}{avail_perc} < + $config->get('critical.if.avail.is.less'); + + return undef; +} + +sub print { + my ($self) = @_; + + my $config = $self->{config}; + my $is_in_future = $config->bool('interval_is_in_future'); + my $notify = $config->{notify}; + + my $str = do { + if ($is_in_future) { + sprintf( +"TLS: %03.3f%%; %s (Best: %03.3f%%; Worst: %03.3f%%; Avgresponse: %dms)", + $self->{result}{avail_perc}, + $self->{name}, + $self->{result}{possible_avail_perc_best}, + $self->{result}{possible_avail_perc_worst}, + $self->{result}{avgresponse} + ); + } + else { + sprintf( + "TLS: %03.3f%%; %s (Avgresponse: %dms)", + $self->{result}{avail_perc}, + $self->{name}, $self->{result}{avgresponse} + ); + } + }; + + if ( $self->{result}{avail_perc} < + $config->get('critical.if.avail.is.less') ) + { + $self->critical( $str, $notify ); + + } + elsif ( + $self->{result}{avail_perc} < $config->get('warning.if.avail.is.less') ) + { + $self->warning( $str, $notify ); + + } + else { + $self->info( $str, $notify ); + } + + if ( exists $self->{services} ) { + $self->inc(); + + my @sorted_data = + sort { $b->{result}{avail_perc} <=> $a->{result}{avail_perc} } + values %{ $self->{services} }; + @sorted_data = reverse @sorted_data + if $config->bool('arg.sort-reverse'); + + $_->print() for @sorted_data; + $self->dec(); + } + + return undef; +} + +sub print_full { + my ($self) = @_; + + my $config = $self->{config}; + + $self->info("TLS $self->{name}"); + $self->inc(); + + if ( exists $self->{result} ) { + $self->{result}->print_full(); + } + + $self->info("$_: $self->{$_}") + for sort grep { not ref $self->{$_} and $_ ne 'name' } keys %$self; + + if ( exists $self->{services} ) { + $self->inc(); + while ( my ( $k, $v ) = each %{ $self->{services} } ) { + $v->print_full(); + } + $self->dec(); + + } + else { + $self->warning("No services for this TLS"); + } + + $self->dec(); + + return undef; +} + +1; + |
