| 1 | package Monitoring::Sneck::Checks::conntrack; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 32 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 32 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 32 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 32 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Monitoring::Sneck::Checks::conntrack - checks how full the connection tracking table is | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 32 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Where pfctl is on FreeBSD. Set by tests. | ||||||||||||||||||||||||||||
| 21 | 32 | our $PFCTL = '/sbin/pfctl'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | conntrack|perl:conntrack | ||||||||||||||||||||||||||||
| 26 | pf_states|perl:conntrack -w 70 -c 85 | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Checks the percent of the connection tracking table in use. Once it is | ||||||||||||||||||||||||||||
| 31 | full, new connections are dropped, with nothing but a kernel log line | ||||||||||||||||||||||||||||
| 32 | saying so. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | On Linux, it is nf_conntrack_count against nf_conntrack_max, under | ||||||||||||||||||||||||||||
| 35 | /proc/sys/net/netfilter. If conntrack is not loaded there is no table, | ||||||||||||||||||||||||||||
| 36 | which is OK. On FreeBSD, it is pf's states, from pfctl -si and the | ||||||||||||||||||||||||||||
| 37 | states limit from pfctl -sm, which needs root. pf being loaded but | ||||||||||||||||||||||||||||
| 38 | disabled is OK. pfctl failing, such as pf not being in the kernel, is | ||||||||||||||||||||||||||||
| 39 | UNKNOWN, as are other OSes. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | - -w, --warning :: Range of percent used. | ||||||||||||||||||||||||||||
| 44 | Default :: 80 | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | - -c, --critical :: Range of percent used. | ||||||||||||||||||||||||||||
| 47 | Default :: 90 | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 129 | ▶ | sub option_spec { return ( 'warning|w=s', 'critical|c=s' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 129 | ▶ | sub defaults { return { warning => '80', critical => '90' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 129 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 METHODS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 check | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Does the check. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 129 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 129 | my ($self) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 129 | my ( $used, $max, $what ) = $self->table; | |||||||||||||||||||||||||||
| 69 | 124 | ▶ | return $self->finish( $self->OK, $what ) if !defined($used); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 75 | ▶ | die("the limit is 0\n") if !$max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 75 | my $percent = sprintf( '%.1f', $used / $max * 100 ); | |||||||||||||||||||||||||||
| 73 | 75 | $self->perfdata( | |||||||||||||||||||||||||||
| 74 | label => 'used', | ||||||||||||||||||||||||||||
| 75 | value => $percent, | ||||||||||||||||||||||||||||
| 76 | uom => '%', | ||||||||||||||||||||||||||||
| 77 | warning => $self->opt('warning'), | ||||||||||||||||||||||||||||
| 78 | critical => $self->opt('critical'), | ||||||||||||||||||||||||||||
| 79 | min => 0, | ||||||||||||||||||||||||||||
| 80 | max => 100 | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | 75 | $self->perfdata( label => 'entries', value => $used, min => 0, max => $max ); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 75 | return $self->finish( $self->threshold_status( $percent, $self->opt('warning'), $self->opt('critical') ), | |||||||||||||||||||||||||||
| 85 | $what . ' ' . $percent . '% used, ' . $used . ' of ' . $max ); | ||||||||||||||||||||||||||||
| 86 | } ## end sub check | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 table | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Returns the entries in use, the limit, and what the table is, or undef | ||||||||||||||||||||||||||||
| 91 | for the entries and why there is no table. Dies if it can not be found. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 129 | ▶ | sub table { | ||||||||||||||||||||||||||
|
Subroutine
table: called CC=10 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 129 | my ($self) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 129 | ▶ | if ( $self->os eq 'linux' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 54 | my $count = eval { $self->proc_file('sys/net/netfilter/nf_conntrack_count') }; | |||||||||||||||||||||||||||
| 100 | 54 | ▶ | return ( undef, undef, 'conntrack is not loaded' ) if !defined($count); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 27 | my $max = $self->proc_file('sys/net/netfilter/nf_conntrack_max'); | |||||||||||||||||||||||||||
| 102 | 27 | return ( $count =~ s/\s+//gr, $max =~ s/\s+//gr, 'conntrack' ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 75 | ▶ | if ( $self->os eq 'freebsd' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 75 | my ( $exit, $info ) = $self->run_command( $PFCTL, '-si' ); | |||||||||||||||||||||||||||
| 106 | 72 | ▶ | die( 'pfctl -si exited ' . $exit . ', ' . ( ( split( /\n/, $info ) )[0] // '' ) . "\n" ) if $exit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
(split(/\n/u, $info, 0))[0] // ""
MC/DC0%0/1
(split(/\n/u, $info, 0))[0] // ""
(split(/\n/u, $info, 0))[0]
Truth table0%0/2
A
A= (split(/\n/u, $info, 0))[0]
| |||||||||||||||||||||||||||||
| 107 | 72 | ▶ | return ( undef, undef, 'pf is not enabled' ) if $info =~ /^Status: Disabled/m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 50 | my ($entries) = $info =~ /^\s*current entries\s+([0-9]+)/m; | |||||||||||||||||||||||||||
| 109 | 50 | my ( $limit_exit, $limits ) = $self->run_command( $PFCTL, '-sm' ); | |||||||||||||||||||||||||||
| 110 | 48 | ▶ | die( 'pfctl -sm exited ' . $limit_exit . "\n" ) if $limit_exit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 48 | my ($max) = $limits =~ /^states\s+hard limit\s+([0-9]+)/m; | |||||||||||||||||||||||||||
| 112 | 48 | ▶ | die("can not make sense of pfctl\n") if !defined($entries) || !defined($max); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $entries and defined $max
MC/DC0%0/2
defined $entries and defined $max
defined $entries
defined $max
Truth table33%1/3
A and B
A= defined $entriesB= defined $max
| |||||||||||||||||||||||||||||
| 113 | 48 | return ( $entries, $max, 'pf states' ); | |||||||||||||||||||||||||||
| 114 | } ## end if ( $self->os eq 'freebsd' ) | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | die( 'not supported on ' . $self->os . "\n" ); | |||||||||||||||||||||||||||
| 117 | } ## end sub table | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 32 | 1; | |||||||||||||||||||||||||||