| 1 | package Zabbix::Reporter::Web::Plugin::Selftest; | ||||||||||||
| 2 | { | ||||||||||||
| 3 | 1 | $Zabbix::Reporter::Web::Plugin::Selftest::VERSION = '0.07'; | |||||||||||
| 4 | } | ||||||||||||
| 5 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | $Zabbix::Reporter::Web::Plugin::Selftest::AUTHORITY = 'cpan:TEX'; | |||||||||||
| 7 | } | ||||||||||||
| 8 | # ABSTRACT: Zabbix Server Selftest | ||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use 5.010_000; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use mro 'c3'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use feature ':5.10'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 1 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 1 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | # use IO::Handle; | ||||||||||||
| 18 | # use autodie; | ||||||||||||
| 19 | # use MooseX::Params::Validate; | ||||||||||||
| 20 | # use Carp; | ||||||||||||
| 21 | # use English qw( -no_match_vars ); | ||||||||||||
| 22 | # use Try::Tiny; | ||||||||||||
| 23 | 1 | ▶ | use IO::Socket::INET; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | # extends ... | ||||||||||||
| 26 | 1 | extends 'Zabbix::Reporter::Web::Plugin'; | |||||||||||
| 27 | # has ... | ||||||||||||
| 28 | 1 | has 'zabbix_server_address' => ( | |||||||||||
| 29 | 'is' => 'ro', | ||||||||||||
| 30 | 'isa' => 'Str', | ||||||||||||
| 31 | 'lazy' => 1, | ||||||||||||
| 32 | 'builder' => '_init_zabbix_server_address', | ||||||||||||
| 33 | ); | ||||||||||||
| 34 | # with ... | ||||||||||||
| 35 | # initializers ... | ||||||||||||
| 36 | 0 | ▶ | sub _init_fields { return [qw()]; } | ||||||||||
|
Subroutine
_init_fields: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | |||||||||||||
| 38 | 0 | ▶ | sub _init_alias { return 'healthcheck'; } | ||||||||||
|
Subroutine
_init_alias: not called CC=1 SCAR=6.9
| |||||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | sub _init_zabbix_server_address { return 'localhost'; } | ||||||||||
|
Subroutine
_init_zabbix_server_address: not called CC=1 SCAR=6.9
| |||||||||||||
| 41 | |||||||||||||
| 42 | # your code here ... | ||||||||||||
| 43 | 0 | ▶ | sub execute { | ||||||||||
|
Subroutine
execute: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 44 | 0 | my $self = shift; | |||||||||||
| 45 | 0 | my $request = shift; | |||||||||||
| 46 | |||||||||||||
| 47 | 0 | my $body; | |||||||||||
| 48 | 0 | my $status = 200; | |||||||||||
| 49 | |||||||||||||
| 50 | 0 | ▶ | if($self->_check_db_ping()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 0 | $body .= "OK - DB Connection is working\n"; | |||||||||||
| 52 | } else { | ||||||||||||
| 53 | 0 | $body .= "ERROR - DB connection not working!\n"; | |||||||||||
| 54 | 0 | $status = 503; | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | # Make sure there were any event during the last 5 minutes | ||||||||||||
| 58 | 0 | ▶ | if($self->_check_db_count('SELECT COUNT(*) FROM events WHERE clock >= UNIX_TIMESTAMP(NOW())-300')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 0 | $body .= "OK - Some events during the last 5 minutes\n"; | |||||||||||
| 60 | } else { | ||||||||||||
| 61 | 0 | $body .= "ERROR - No events during the last 5 minutes\n"; | |||||||||||
| 62 | 0 | $status = 503; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | # Make sure there was at least on trigger event in the last 24h | ||||||||||||
| 66 | 0 | ▶ | if($self->_check_db_count('SELECT COUNT(*) FROM triggers AS t WHERE t.lastchange > UNIX_TIMESTAMP(NOW() - INTERVAL 1 DAY)')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 0 | $body .= "OK - Some events during the last 5 minutes\n"; | |||||||||||
| 68 | } else { | ||||||||||||
| 69 | 0 | $body .= "ERROR - No events during the last 5 minutes\n"; | |||||||||||
| 70 | 0 | $status = 503; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | |||||||||||||
| 74 | # Make sure the server process is listening on port 10051 | ||||||||||||
| 75 | 0 | ▶ | if($self->_check_open_port(10051)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 0 | $body .= "OK - Server is listening on port 10051\n"; | |||||||||||
| 77 | } else { | ||||||||||||
| 78 | 0 | $body .= "ERROR - Server is not listening on port 10051\n"; | |||||||||||
| 79 | 0 | $status = 503; | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | 0 | return [ $status, [ | |||||||||||
| 83 | 'Content-Type', 'text/plain', | ||||||||||||
| 84 | 'Cache-Control', 'no-store, private', # no caching for the selftest | ||||||||||||
| 85 | ], [$body] ]; | ||||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | ▶ | sub _check_open_port { | ||||||||||
|
Subroutine
_check_open_port: not called CC=3 SCAR=24.8
| |||||||||||||
| 89 | 0 | my $self = shift; | |||||||||||
| 90 | 0 | ▶ | my $port = shift || 10051; | ||||||||||
|
Condition0%0/2
shift() || 10051
MC/DC0%0/1
shift() || 10051
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 91 | |||||||||||||
| 92 | 0 | my $sock = IO::Socket::INET::->new( | |||||||||||
| 93 | Proto => 'tcp', | ||||||||||||
| 94 | PeerAddr => $self->zabbix_server_address(), | ||||||||||||
| 95 | PeerPort => $port, | ||||||||||||
| 96 | Timeout => 10, | ||||||||||||
| 97 | ); | ||||||||||||
| 98 | |||||||||||||
| 99 | 0 | ▶ | if($sock) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | close($sock); | |||||||||||
| 101 | 0 | return 1; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 0 | return; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub _check_db_ping { | ||||||||||
|
Subroutine
_check_db_ping: not called CC=2 SCAR=17.9
| |||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||
| 109 | |||||||||||||
| 110 | 0 | ▶ | if($self->zr()->dbh()->ping()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | 0 | return 1; | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | 0 | return; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 0 | ▶ | sub _check_db_count { | ||||||||||
|
Subroutine
_check_db_count: not called CC=5 SCAR=34.0
| |||||||||||||
| 118 | 0 | my $self = shift; | |||||||||||
| 119 | 0 | my $sql = shift; | |||||||||||
| 120 | |||||||||||||
| 121 | 0 | ▶ | if(!$sql) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 0 | return; | |||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | 0 | my $sth = $self->zr()->dbh()->prepare($sql); | |||||||||||
| 126 | 0 | ▶ | if(!$sth) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | 0 | return; | |||||||||||
| 128 | } | ||||||||||||
| 129 | 0 | ▶ | if(!$sth->execute()) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 0 | return; | |||||||||||
| 131 | } | ||||||||||||
| 132 | 0 | my $count = $sth->fetchrow_array(); | |||||||||||
| 133 | |||||||||||||
| 134 | 0 | ▶ | if($count > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | 0 | return 1; | |||||||||||
| 136 | } | ||||||||||||
| 137 | |||||||||||||
| 138 | 0 | return; | |||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | 1 | ▶ | no Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 142 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 143 | |||||||||||||
| 144 | 1 | 1; | |||||||||||
| 145 | |||||||||||||
| 146 | __END__ | ||||||||||||