« blib/lib/Monitoring/Sneck/Checks/time_sync.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/tls_protocols.pm »
| 1 | package Monitoring::Sneck::Checks::fail2ban; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 24 | ▶ | use 5.014; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 24 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 24 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 24 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Monitoring::Sneck::Checks::fail2ban - checks fail2ban's jails are up, and how many are banned | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | Version 0.0.1 | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 24 | our $VERSION = '0.0.1'; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Where fail2ban-client is. Set by tests. Undef means the first of | ||||||||||||||||||||
| 21 | # FAIL2BAN_CLIENT_PATHS that is there. | ||||||||||||||||||||
| 22 | 24 | our $FAIL2BAN_CLIENT; | |||||||||||||||||||
| 23 | 24 | our @FAIL2BAN_CLIENT_PATHS = ( '/usr/bin/fail2ban-client', '/usr/local/bin/fail2ban-client' ); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | fail2ban|perl:fail2ban | ||||||||||||||||||||
| 28 | fail2ban_mail|perl:fail2ban -j sshd -j postfix -j dovecot | ||||||||||||||||||||
| 29 | fail2ban_attack|perl:fail2ban -j sshd -w 500 -c 2000 | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | Checks fail2ban is answering, the jails it has, and how many addresses | ||||||||||||||||||||
| 34 | each has banned now, through fail2ban-client status, and status for | ||||||||||||||||||||
| 35 | each jail. | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | - CRITICAL :: fail2ban not answering. A jail given with --jail not | ||||||||||||||||||||
| 38 | running. The banned over --critical. | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | - WARNING :: No jails running. The banned over --warning. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | - UNKNOWN :: fail2ban-client not being there, or not being allowed to | ||||||||||||||||||||
| 43 | talk to fail2ban, as when not run as root. | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | The status line has the banned now for each jail. --warning and | ||||||||||||||||||||
| 46 | --critical are for those added up, as a lot banned at once can be a | ||||||||||||||||||||
| 47 | attack. The total failed and banned since fail2ban started are in the | ||||||||||||||||||||
| 48 | perfdata as well. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head1 OPTIONS | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | - -j, --jail :: A jail that must be running. May be given more than | ||||||||||||||||||||
| 53 | once. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | - -w, --warning :: Range for the banned now, of all jails. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | - -c, --critical :: Range for the banned now, of all jails. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 106 | ▶ | sub option_spec { return ( 'jail|j=s@', 'warning|w=s', 'critical|c=s' ) } | ||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 106 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | ||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head1 METHODS | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | =head2 check | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | Does the check. | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 106 | ▶ | sub check { | ||||||||||||||||||
|
Subroutine
check: called CC=18 SCAR=29.4
Pod
documented
| |||||||||||||||||||||
| 74 | 106 | my ($self) = @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | my $client = $FAIL2BAN_CLIENT // ( grep { -x $_ } @FAIL2BAN_CLIENT_PATHS )[0]; | ||||||||||||||||||
|
Condition33%1/3
$Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENT // (grep {-x $_;} @Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENT_PATHS)[0]
MC/DC0%0/2
$Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENT // (grep {-x $_;} @Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENT_PATHS)[0]
$Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENT
(grep {-x $_;} @Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENT_PATHS)[0]
Truth table33%1/3
A // B
A= $Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENTB= (grep {-x $_;} @Monitoring::Sneck::Checks::fail2ban::FAIL2BAN_CLIENT_PATHS)[0]
| |||||||||||||||||||||
| 77 | 106 | ▶ | return $self->finish( $self->UNKNOWN, 'fail2ban-client is not there' ) if !defined($client); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | 106 | ▶ | return $self->finish( $self->UNKNOWN, $client . ' is not there' ) if !-x $client; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # such as "`- Jail list: sshd, postfix", empty with no jails | ||||||||||||||||||||
| 81 | 105 | my $status = $self->client_status( $client, 'status' ); | |||||||||||||||||||
| 82 | 95 | ▶ | return $status->{code} if defined( $status->{code} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 88 | my ($jail_list) = $status->{output} =~ /Jail list:[ \t]*(.*?)\s*$/m; | |||||||||||||||||||
| 84 | 88 | ▶ | return $self->finish( $self->UNKNOWN, 'fail2ban-client status did not list jails' ) if !defined($jail_list); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 88 | my @jails = grep { $_ ne '' } split( /\s*,\s*/, $jail_list ); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 88 | my @codes = ( $self->OK ); | |||||||||||||||||||
| 88 | 88 | my @parts; | |||||||||||||||||||
| 89 | 88 | my %running = map { $_ => 1 } @jails; | |||||||||||||||||||
| 90 | 88 | ▶ | foreach my $wanted ( @{ $self->opt('jail') // [] } ) { | ||||||||||||||||||
|
Condition100%2/2
$self->opt("jail") // []
MC/DC100%1/1
$self->opt("jail") // []
$self->opt("jail")
Truth table100%2/2
A
A= $self->opt("jail")
| |||||||||||||||||||||
| 91 | 71 | ▶ | next if $running{$wanted}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 23 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||
| 93 | 23 | push( @parts, $wanted . ' not running' ); | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | 88 | ▶ | if ( !defined( $jails[0] ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 11 | push( @codes, $self->WARNING ); | |||||||||||||||||||
| 97 | 11 | push( @parts, 'no jails running' ); | |||||||||||||||||||
| 98 | 11 | return $self->finish( $self->worst(@codes), join( ', ', @parts ) ); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 77 | my $banned_total = 0; | |||||||||||||||||||
| 102 | 77 | my @banned_parts; | |||||||||||||||||||
| 103 | 77 | foreach my $jail (@jails) { | |||||||||||||||||||
| 104 | 147 | my $jail_status = $self->client_status( $client, 'status', $jail ); | |||||||||||||||||||
| 105 | 136 | ▶ | return $jail_status->{code} if defined( $jail_status->{code} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # lines such as "| |- Currently failed: 3" and " |- Total banned: 15" | ||||||||||||||||||||
| 108 | 136 | my %counts; | |||||||||||||||||||
| 109 | 136 | foreach my $count ( 'Currently failed', 'Total failed', 'Currently banned', 'Total banned' ) { | |||||||||||||||||||
| 110 | 544 | ( $counts{$count} ) = $jail_status->{output} =~ /\Q$count\E:\s*([0-9]+)/; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | return $self->finish( $self->UNKNOWN, 'fail2ban-client status ' . $jail . ' did not say how many are banned' ) | ||||||||||||||||||||
| 113 | 136 | ▶ | if !defined( $counts{'Currently banned'} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 135 | $banned_total = $banned_total + $counts{'Currently banned'}; | |||||||||||||||||||
| 116 | 135 | push( @banned_parts, $jail . ' ' . $counts{'Currently banned'} ); | |||||||||||||||||||
| 117 | 135 | $self->perfdata( label => $jail . '_banned', value => $counts{'Currently banned'}, min => 0 ); | |||||||||||||||||||
| 118 | 135 | ▶ | $self->perfdata( label => $jail . '_failed', value => $counts{'Currently failed'} // 0, min => 0 ); | ||||||||||||||||||
|
Condition50%1/2
$counts{"Currently failed"} // 0
MC/DC0%0/1
$counts{"Currently failed"} // 0
$counts{"Currently failed"}
Truth table50%1/2
A
A= $counts{"Currently failed"}
| |||||||||||||||||||||
| 119 | $self->perfdata( | ||||||||||||||||||||
| 120 | label => $jail . '_total_banned', | ||||||||||||||||||||
| 121 | 135 | ▶ | value => $counts{'Total banned'} // 0, | ||||||||||||||||||
|
Condition50%1/2
$counts{"Total banned"} // 0
MC/DC0%0/1
$counts{"Total banned"} // 0
$counts{"Total banned"}
Truth table50%1/2
A
A= $counts{"Total banned"}
| |||||||||||||||||||||
| 122 | uom => 'c', | ||||||||||||||||||||
| 123 | min => 0 | ||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||
| 125 | $self->perfdata( | ||||||||||||||||||||
| 126 | label => $jail . '_total_failed', | ||||||||||||||||||||
| 127 | 135 | ▶ | value => $counts{'Total failed'} // 0, | ||||||||||||||||||
|
Condition50%1/2
$counts{"Total failed"} // 0
MC/DC0%0/1
$counts{"Total failed"} // 0
$counts{"Total failed"}
Truth table50%1/2
A
A= $counts{"Total failed"}
| |||||||||||||||||||||
| 128 | uom => 'c', | ||||||||||||||||||||
| 129 | min => 0 | ||||||||||||||||||||
| 130 | ); | ||||||||||||||||||||
| 131 | } ## end foreach my $jail (@jails) | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 65 | push( @codes, $self->threshold_status( $banned_total, $self->opt('warning'), $self->opt('critical') ) ); | |||||||||||||||||||
| 134 | 65 | $self->perfdata( | |||||||||||||||||||
| 135 | label => 'banned', | ||||||||||||||||||||
| 136 | value => $banned_total, | ||||||||||||||||||||
| 137 | warning => $self->opt('warning'), | ||||||||||||||||||||
| 138 | critical => $self->opt('critical'), | ||||||||||||||||||||
| 139 | min => 0, | ||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||
| 141 | # the banned for each jail goes last, so jails not running are not read as part of it | ||||||||||||||||||||
| 142 | 65 | ▶ | unshift( @parts, scalar(@jails) . ' jail' . ( @jails == 1 ? '' : 's' ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | 65 | push( @parts, $banned_total . ' banned now: ' . join( ', ', @banned_parts ) ); | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 65 | return $self->finish( $self->worst(@codes), join( ', ', @parts ) ); | |||||||||||||||||||
| 146 | } ## end sub check | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head2 client_status | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Runs fail2ban-client with the args given, finishing the check if it | ||||||||||||||||||||
| 151 | fails. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | - client :: The path to fail2ban-client. | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | - args :: The args, such as 'status' or 'status', 'sshd'. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | Returns a hash ref of output, what it printed, if it worked, or of | ||||||||||||||||||||
| 158 | code, what finish returned, if it did not. Not being allowed to talk | ||||||||||||||||||||
| 159 | to fail2ban is UNKNOWN, as it is a problem with how the check is run, | ||||||||||||||||||||
| 160 | and anything else is CRITICAL, as fail2ban is not answering. | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | my $status = $check->client_status( '/usr/bin/fail2ban-client', 'status' ); | ||||||||||||||||||||
| 163 | return $status->{code} if defined( $status->{code} ); | ||||||||||||||||||||
| 164 | # $status->{output} is "Status\n|- Number of jail:\t2\n`- Jail list:\tsshd, postfix\n" | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 252 | ▶ | sub client_status { | ||||||||||||||||||
|
Subroutine
client_status: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 169 | 252 | my ( $self, $client, @args ) = @_; | |||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 252 | my ( $exit, $output ) = $self->run_command( $client, @args ); | |||||||||||||||||||
| 172 | 231 | ▶ | return { output => $output } if !$exit; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 7 | ▶ | my $first = ( grep { /\S/ } split( /\n/, $output ) )[0] // 'saying nothing'; | ||||||||||||||||||
|
Condition50%1/2
(grep {/\S/u;} split(/\n/u, $output, 0))[0] // "saying nothing"
MC/DC0%0/1
(grep {/\S/u;} split(/\n/u, $output, 0))[0] // "saying nothing"
(grep {/\S/u;} split(/\n/u, $output, 0))[0]
Truth table50%1/2
A
A= (grep {/\S/u;} split(/\n/u, $output, 0))[0]
| |||||||||||||||||||||
| 175 | 7 | $first =~ s/\AERROR\s+//; | |||||||||||||||||||
| 176 | 7 | ▶ | my $code = $output =~ /Permission denied/ ? $self->UNKNOWN : $self->CRITICAL; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | 7 | return { code => | |||||||||||||||||||
| 178 | $self->finish( $code, 'fail2ban-client ' . join( ' ', @args ) . ' exited ' . $exit . ', ' . $first ) }; | ||||||||||||||||||||
| 179 | } ## end sub client_status | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 24 | 1; | |||||||||||||||||||