| 1 | package Monitoring::Sneck::Checks::wireguard; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | Monitoring::Sneck::Checks::wireguard - checks WireGuard peers have handshaken recently | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 12 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Where wg is. Set by tests. Undef means the first of WG_PATHS that is there. | ||||||||||||||||||||||||||||
| 21 | 12 | our $WG; | |||||||||||||||||||||||||||
| 22 | 12 | our @WG_PATHS = ( '/usr/bin/wg', '/usr/sbin/wg', '/usr/local/bin/wg' ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | wireguard|perl:wireguard | ||||||||||||||||||||||||||||
| 27 | wireguard_wg0|perl:wireguard -i wg0 -w 180 -c 600 | ||||||||||||||||||||||||||||
| 28 | wireguard_hub|perl:wireguard -k | ||||||||||||||||||||||||||||
| 29 | wireguard_site|perl:wireguard -p 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg=' | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Checks how long ago each peer last handshook, through wg show all | ||||||||||||||||||||||||||||
| 34 | dump. A peer with traffic, or persistent keepalive, handshakes about | ||||||||||||||||||||||||||||
| 35 | every two minutes, so a old handshake means the tunnel is down. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | A peer without persistent keepalive only handshakes when there is | ||||||||||||||||||||||||||||
| 38 | traffic, so a idle one looks the same as a down one. --keepalive checks | ||||||||||||||||||||||||||||
| 39 | only peers with it, for hubs with peers that come and go. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | - CRITICAL :: A handshake older than --critical, or never made. A | ||||||||||||||||||||||||||||
| 42 | interface or peer given that is not there. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | - WARNING :: A handshake older than --warning. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | - UNKNOWN :: wg not being there, or failing, as it does when not run as | ||||||||||||||||||||||||||||
| 47 | root. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | - OK :: Everything else, including no WireGuard interfaces. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Peers are named by interface and first allowed IP, or the start of | ||||||||||||||||||||||||||||
| 52 | their public key if they have none. A line for each peer follows the | ||||||||||||||||||||||||||||
| 53 | status line, with its endpoint, handshake, and transfer. Private and | ||||||||||||||||||||||||||||
| 54 | preshared keys are never output. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | - -i, --interface :: A interface to check, instead of all of them. May | ||||||||||||||||||||||||||||
| 59 | be given more than once. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | - -p, --peer :: The public key of a peer to check, instead of all of | ||||||||||||||||||||||||||||
| 62 | them. May be given more than once. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | - -k, --keepalive :: Only check peers with persistent keepalive. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | - -w, --warning :: Range for the seconds since the latest handshake. | ||||||||||||||||||||||||||||
| 67 | Default :: 300 | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | - -c, --critical :: Range for the seconds since the latest handshake. | ||||||||||||||||||||||||||||
| 70 | Default :: 900 | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 55 | ▶ | sub option_spec { return ( 'interface|i=s@', 'peer|p=s@', 'keepalive|k', 'warning|w=s', 'critical|c=s' ) } | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 55 | ▶ | sub defaults { return { warning => '300', critical => '900' } } | ||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 55 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 METHODS | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 check | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Does the check. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 55 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=30 SCAR=34.1
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 55 | my ($self) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $wg = $WG // ( grep { -x $_ } @WG_PATHS )[0]; | ||||||||||||||||||||||||||
|
Condition33%1/3
$Monitoring::Sneck::Checks::wireguard::WG // (grep {-x $_;} @Monitoring::Sneck::Checks::wireguard::WG_PATHS)[0]
MC/DC0%0/2
$Monitoring::Sneck::Checks::wireguard::WG // (grep {-x $_;} @Monitoring::Sneck::Checks::wireguard::WG_PATHS)[0]
$Monitoring::Sneck::Checks::wireguard::WG
(grep {-x $_;} @Monitoring::Sneck::Checks::wireguard::WG_PATHS)[0]
Truth table33%1/3
A // B
A= $Monitoring::Sneck::Checks::wireguard::WGB= (grep {-x $_;} @Monitoring::Sneck::Checks::wireguard::WG_PATHS)[0]
| |||||||||||||||||||||||||||||
| 92 | 55 | ▶ | return $self->finish( $self->UNKNOWN, 'wg is not there' ) if !defined($wg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 55 | ▶ | return $self->finish( $self->UNKNOWN, $wg . ' is not there' ) if !-x $wg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 54 | my ( $exit, $output ) = $self->run_command( $wg, 'show', 'all', 'dump' ); | |||||||||||||||||||||||||||
| 96 | 45 | ▶ | if ($exit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | 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]
| |||||||||||||||||||||||||||||
| 98 | 1 | return $self->finish( $self->UNKNOWN, 'wg show exited ' . $exit . ', ' . $first ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # interface lines have 5 fields, the private key among them, peer lines 9 | ||||||||||||||||||||||||||||
| 102 | 44 | my %interfaces; | |||||||||||||||||||||||||||
| 103 | my @peers; | ||||||||||||||||||||||||||||
| 104 | 44 | foreach my $line ( split( /\n/, $output ) ) { | |||||||||||||||||||||||||||
| 105 | 198 | my @fields = split( /\t/, $line ); | |||||||||||||||||||||||||||
| 106 | 198 | ▶ | if ( @fields == 5 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 107 | 72 | $interfaces{ $fields[0] } = 1; | |||||||||||||||||||||||||||
| 108 | } elsif ( @fields == 9 ) { | ||||||||||||||||||||||||||||
| 109 | 126 | push( | |||||||||||||||||||||||||||
| 110 | @peers, | ||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | interface => $fields[0], | ||||||||||||||||||||||||||||
| 113 | key => $fields[1], | ||||||||||||||||||||||||||||
| 114 | endpoint => $fields[3], | ||||||||||||||||||||||||||||
| 115 | allowed => ( split( /,/, $fields[4] ) )[0], | ||||||||||||||||||||||||||||
| 116 | handshake => $fields[5], | ||||||||||||||||||||||||||||
| 117 | received => $fields[6], | ||||||||||||||||||||||||||||
| 118 | sent => $fields[7], | ||||||||||||||||||||||||||||
| 119 | keepalive => $fields[8], | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||
| 122 | } ## end elsif ( @fields == 9 ) | ||||||||||||||||||||||||||||
| 123 | } ## end foreach my $line ( split( /\n/, $output ) ) | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 44 | my @codes = ( $self->OK ); | |||||||||||||||||||||||||||
| 126 | 44 | my @parts; | |||||||||||||||||||||||||||
| 127 | 44 | ▶ | my @interfaces_wanted = @{ $self->opt('interface') // [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("interface") // []
MC/DC100%1/1
$self->opt("interface") // []
$self->opt("interface")
Truth table100%2/2
A
A= $self->opt("interface")
| |||||||||||||||||||||||||||||
| 128 | 44 | foreach my $interface ( grep { !$interfaces{$_} } @interfaces_wanted ) { | |||||||||||||||||||||||||||
| 129 | 8 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 130 | 8 | push( @parts, $interface . ' is not there' ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 44 | my %interface_wanted = map { $_ => 1 } @interfaces_wanted; | |||||||||||||||||||||||||||
| 133 | 44 | ▶ | @peers = grep { $interface_wanted{ $_->{interface} } } @peers if defined( $interfaces_wanted[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 44 | ▶ | my @keys_wanted = @{ $self->opt('peer') // [] }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("peer") // []
MC/DC100%1/1
$self->opt("peer") // []
$self->opt("peer")
Truth table100%2/2
A
A= $self->opt("peer")
| |||||||||||||||||||||||||||||
| 136 | 44 | foreach my $key (@keys_wanted) { | |||||||||||||||||||||||||||
| 137 | 17 | ▶ | next if grep { $_->{key} eq $key } @peers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 7 | push( @codes, $self->CRITICAL ); | |||||||||||||||||||||||||||
| 139 | 7 | push( @parts, 'peer ' . substr( $key, 0, 8 ) . ' is not there' ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 44 | my %key_wanted = map { $_ => 1 } @keys_wanted; | |||||||||||||||||||||||||||
| 142 | 44 | ▶ | @peers = grep { $key_wanted{ $_->{key} } } @peers if defined( $keys_wanted[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 44 | ▶ | @peers = grep { $_->{keepalive} ne 'off' && $_->{keepalive} } @peers if $self->opt('keepalive'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_->{'keepalive'} ne 'off' && $_->{'keepalive'}
MC/DC50%1/2
$_->{'keepalive'} ne 'off' && $_->{'keepalive'}
$_->{'keepalive'} ne 'off'
$_->{'keepalive'}
Truth table66%2/3
A && B
A= $_->{'keepalive'} ne 'off'B= $_->{'keepalive'}
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 44 | ▶ | if ( !defined( $peers[0] ) && !defined( $parts[0] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $peers[0] or defined $parts[0]
MC/DC50%1/2
defined $peers[0] or defined $parts[0]
defined $peers[0]
defined $parts[0]
Truth table66%2/3
A or B
A= defined $peers[0]B= defined $parts[0]
| |||||||||||||||||||||||||||||
| 146 | 5 | ▶ | return $self->finish( $self->OK, %interfaces ? 'no peers to check' : 'no WireGuard interfaces' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 39 | my $now = time; | |||||||||||||||||||||||||||
| 150 | 39 | my $stale = 0; | |||||||||||||||||||||||||||
| 151 | 39 | my @details; | |||||||||||||||||||||||||||
| 152 | 39 | foreach my $peer (@peers) { | |||||||||||||||||||||||||||
| 153 | my $name | ||||||||||||||||||||||||||||
| 154 | = $peer->{interface} . ' ' | ||||||||||||||||||||||||||||
| 155 | . ( | ||||||||||||||||||||||||||||
| 156 | ( defined( $peer->{allowed} ) && $peer->{allowed} ne '(none)' ) | ||||||||||||||||||||||||||||
| 157 | ? $peer->{allowed} | ||||||||||||||||||||||||||||
| 158 | 79 | ▶ | : substr( $peer->{key}, 0, 8 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $peer->{"allowed"} and $peer->{"allowed"} ne "(none)"
MC/DC50%1/2
defined $peer->{"allowed"} and $peer->{"allowed"} ne "(none)"
defined $peer->{"allowed"}
$peer->{"allowed"} ne "(none)"
Truth table66%2/3
A and B
A= defined $peer->{"allowed"}B= $peer->{"allowed"} ne "(none)"
| |||||||||||||||||||||||||||||
| 159 | ); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # a handshake of 0 is never, which is as old as can be | ||||||||||||||||||||||||||||
| 162 | 79 | ▶ | my $age = $peer->{handshake} ? $now - $peer->{handshake} : 9**9**9; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 79 | ▶ | my $handshake = $peer->{handshake} ? 'handshake ' . $age . ' seconds ago' : 'never handshaken'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 79 | my $code = $self->threshold_status( $age, $self->opt('warning'), $self->opt('critical') ); | |||||||||||||||||||||||||||
| 165 | 79 | push( @codes, $code ); | |||||||||||||||||||||||||||
| 166 | 79 | ▶ | if ( $code != $self->OK ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 26 | $stale++; | |||||||||||||||||||||||||||
| 168 | 26 | push( @parts, $name . ' ' . $handshake ); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | push( @details, | ||||||||||||||||||||||||||||
| 171 | $name | ||||||||||||||||||||||||||||
| 172 | . ', endpoint ' | ||||||||||||||||||||||||||||
| 173 | . $peer->{endpoint} . ', ' | ||||||||||||||||||||||||||||
| 174 | . $handshake | ||||||||||||||||||||||||||||
| 175 | . ', received ' | ||||||||||||||||||||||||||||
| 176 | . $peer->{received} | ||||||||||||||||||||||||||||
| 177 | . ' B, sent ' | ||||||||||||||||||||||||||||
| 178 | . $peer->{sent} | ||||||||||||||||||||||||||||
| 179 | 79 | . ' B' ); | |||||||||||||||||||||||||||
| 180 | } ## end foreach my $peer (@peers) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 39 | my $fine = scalar(@peers) - $stale; | |||||||||||||||||||||||||||
| 183 | 39 | ▶ | if ($fine) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 35 | ▶ | my $noun = defined( $parts[0] ) ? 'other' : 'peer'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 35 | ▶ | $noun = $noun . 's' if $fine > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 35 | push( @parts, $fine . ' ' . $noun . ' ok' ); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 39 | $self->perfdata( label => 'peers', value => scalar(@peers), min => 0 ); | |||||||||||||||||||||||||||
| 189 | 39 | $self->perfdata( label => 'stale', value => $stale, min => 0 ); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 39 | return $self->finish( $self->worst(@codes), join( ', ', @parts ), @details ); | |||||||||||||||||||||||||||
| 192 | } ## end sub check | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 12 | 1; | |||||||||||||||||||||||||||