-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbird-peer-info.pl
68 lines (59 loc) · 2.09 KB
/
bird-peer-info.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl -w
use strict;
use warnings;
#use Data::Dumper;
my $birdc = "/usr/sbin/birdc";
my $peer2check;
my $output ="";
my $state = "";
my $since = "";
my $reason = "";
$peer2check = $ARGV[0];
if (defined($peer2check)) {
# see the details for the peer provider
my @peer = qx/$birdc show protocols all $peer2check/;
#print Dumper \@peer;
# remove headers from command line
#shift @peer for 1..2;
if (defined($peer[2])) {
#print "DEBUG in the peer if : $peer[2]\n";
# json $output = "{ \"proto\": \"$peer2check\", ";
my $peerfile = "/tmp/$peer2check";
open (my $fh, '>', $peerfile) or die "Не могу открыть $peerfile !";
# NOS_ipv4 BGP --- up 2020-09-22 Established
# PCH1_2001_7f8_a_1__55 BGP --- down 11:21:54.010
if ($peer[2] =~ m/^[\w\d\_\-]+\s+BGP\s+---\s+(\w+)\s+([\d+\-\:]+\ [\d+\-\:]+)(.*)/) {
$state = $1;
$since = $2;
if ($3 ne " ") {
$reason = $3;
} else {
$reason = "down";
}
if ($state eq "up") {
my $string = join( ',', @peer );
#print "STRING: $string\n";
$string =~ m/Routes:\s+(\d+) imported,\s+(\d+) exported,\s+(\d+) preferred/;
# check if i've more than one route...
#if ($1 >= 1) {
#print "DEBUG in the routes if : ROUTES: $1\n";
# json $output .= "\"state\": \"$state\", \"since\": \"$since\", \"routes\": \"$1\", \"exported\": \"$2\", \"preferred\": \"$3\", \"reason\": \"$reason\"";
print $fh "\"routes\": \"$1\", \"exported\": \"$2\", \"preferred\": \"$3\"";
#} else {
#print "WARNING, To few routes for this provider: $1\n";
#}
} else {
# json $output .= "\"state\": \"$state\", \"since\": \"$since\", \"routes\": \"0\", \"exported\": \"0\", \"preferred\": \"0\", \"reason\": \"$reason\"";
print $fh "\"routes\": \"0\", \"exported\": \"0\", \"preferred\": \"0\"";
}
# json print "$output}, ";
close $fh;
} else {
print "CRITICAL, Peer protocol session name state doesn't match: $peer[2]\n";
}
} else {
print "CRITICAL, Peer protocol session name doesn't exists: $peer2check\n";
}
} else {
print "CRITICAL, Peer name empty\n";
}