-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_quagga_bgp.sh
185 lines (160 loc) · 5.31 KB
/
check_quagga_bgp.sh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash
# SvenVD 2014
# Note this only has been tested on Centos6 with quagga-0.99.15-7
#
# Requirements:
#
# When run as the nagios user add this command alias to sudoers file:-
# Cmnd_Alias NRPE_QUAGGA_BGP_CMNDS = /usr/bin/vtysh -c show ip bgp summary, /usr/bin/vtysh -c show ipv6 bgp summary
# /usr/bin/vtysh , sed, egrep, bc
#
# Initialization
#
VERSION="0.4"
PERCENTPEERS=0
NR_ESTA_PEERS=0
PERCPEERS=0
IPPROT=v6
TW_MINPEERS=100
TC_MINPEERS=100
TW_STATEPPFXRCD=1
TW_STATEPPFXRCD_CRIT=1
EXITCODES=0
#
# Functions
#
# Print help along with usage
print_help()
{
echo "Nagios plugin to check quagga bgp"
echo "Version $VERSION, by SvenVD, [email protected]"
echo -e "\n$USAGE\n"
echo "Parameters description:"
echo " -p|--ipprot <v4/v6> # Monitor ipv6 peers(the default) or ipv4 peers"
echo " -wmp|--warnminpeers <threshold> # Minimum percentage of established peers, default 100%, anything below this thresh is warning"
echo " -cmp|--critminpeers <threshold> # Minimum percentage of established peers, default 100%, anything below this thresh is critical"
echo " -wpr|--warnminprefix <threshold> # Every peer should have at least <warnminprefix> prefixes"
echo " -cpr|--critminprefix <threshold> # Every peer should have at least <critminprefix> prefixes"
echo " -h|--help # Print this message"
}
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit 3
;;
-p|--ipprot)
if [ $2 = "v4" -o $2 = "v6" ]; then
IPPROT=$2
else
print_help
fi
shift
;;
-wmp|--warnminpeers)
TW_MINPEERS=$2
shift
;;
-cmp|--critminpeers)
TC_MINPEERS=$2
shift
;;
-wpr|--warnminprefix)
TW_STATEPPFXRCD=$2
shift
;;
-cpr|--critminprefix)
TW_STATEPPFXRCD_CRIT=$2
shift
;;
*)
echo "Unknown argument: $1"
print_help
exit 3
;;
esac
shift
done
#
# main
#
if [ $IPPROT = v4 ];then
BGPDATA=$(sudo /usr/bin/vtysh -c 'show ip bgp summary' | head -n -2 | sed '1,6d' | egrep "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" )
elif [ $IPPROT = v6 ];then
BGPDATA=$(sudo /usr/bin/vtysh -c 'show ipv6 bgp summary' | head -n -2 | sed '1,6d' | awk '
BEGIN{i=1}{line[i++]=$0}
END{j=1;
while (j<i){
split(line[j],array);
if ( array[2]=="" ) {
print line[j] line[j+1]; j+=2
} else {
print line[j]; j+=1
}
}
}'
)
fi
while read line;do
IP=$(echo $line | cut -d" " -f1)
UPDOWN=$(echo $line | cut -d" " -f9)
STATEPPFXRCD=$(echo $line | cut -d" " -f10)
# Needs to match 37w2d00h or 1d18h50m or 23:39:12 and $STATEPPFXRCD is not a state but a count
if [[ "$UPDOWN" =~ ^[0-9]{1,2}[a-z:][0-9]{1,2}[a-z:][0-9]{1,2}[a-z:]{0,1} && "$STATEPPFXRCD" =~ ^[0-9]+$ ]];then
#This means UPDOWN is in established state
NR_ESTA_PEERS=$(( $NR_ESTA_PEERS +1 ))
PERFDATA="$PERFDATA""$IP=$STATEPPFXRCD;$TW_STATEPPFXRCD;$TW_STATEPPFXRCD_CRIT;; "
# This also means we got some prefixes, compare them with threshold
if [ $STATEPPFXRCD -lt $TW_STATEPPFXRCD_CRIT ];then
PEERSPREFIXERR="$PEERSPREFIXERR""$IP->$TW_STATEPPFXRCD_CRIT"
EXITCODES="$EXITCODES"" 2"
elif [ $STATEPPFXRCD -lt $TW_STATEPPFXRCD ];then
PEERSPREFIXERR="$PEERSPREFIXERR""$IP->$TW_STATEPPFXRCD"
EXITCODES="$EXITCODES"" 1"
else
EXITCODES="$EXITCODES"" 0"
fi
else
PEERS_NOT_ESTA="$PEERS_NOT_ESTA""$IP->$STATEPPFXRCD / "
PERFDATA="$PERFDATA""$IP=0;$TW_STATEPPFXRCD;; "
fi
NRPEERS=$(( $NRPEERS +1 ))
done <<< "$BGPDATA"
# Strip last char
PEERS_NOT_ESTA=$(echo $PEERS_NOT_ESTA | sed 's/\/$//')
PEERSPREFIXERR=$(echo $PEERSPREFIXERR | sed 's/\/$//')
# Check for minimum percentage of established peers
test $NR_ESTA_PEERS -eq 0 || PERCPEERS=$(echo "scale=2; ($NR_ESTA_PEERS/$NRPEERS)*100" | bc -l)
# Alerting logic (since bash can not handle floating point comparisions, we use bc for the purpose)
if [ -n "$PEERSPREFIXERR" ];then
MSGNAGIOS="$MSGNAGIOS""Prefixes for peers $PEERSPREFIXERR below thresholds:: "
fi
if [ $(echo "$PERCPEERS < $TC_MINPEERS" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""Only $PERCPEERS% of peers are in established state (required: $TW_MINPEERS%).$(( $NRPEERS - $NR_ESTA_PEERS )) Erroneous peers: $PEERS_NOT_ESTA :: "
EXITCODES="$EXITCODES"" 2"
elif [ $(echo "$PERCPEERS < $TW_MINPEERS" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""Only $PERCPEERS% of peers are in established state (required: $TW_MINPEERS%).$(( $NRPEERS - $NR_ESTA_PEERS )) Erroneous peers: $PEERS_NOT_ESTA :: "
EXITCODES="$EXITCODES"" 1"
else
# Do not output OK message if we already have a non-OK message
if ! echo "$EXITCODES" | egrep -q "1|2|3";then
MSGNAGIOS="$MSGNAGIOS""$PERCPEERS% of $IPPROT peers are in established state"
fi
EXITCODES="$EXITCODES"" 0"
fi
####Substitute all : from PERFDATA output
PERFDATA=$(echo "$PERFDATA" | sed 's/:/_/g')
####Generate nagiosoutput
if echo "$EXITCODES" | grep -q 2;then
echo "CRITICAL: $MSGNAGIOS|$PERFDATA"
exit 2;
elif echo "$EXITCODES" | grep -q 1;then
echo "WARNING: $MSGNAGIOS|$PERFDATA"
exit 1;
elif echo "$EXITCODES" | grep -q 3;then
echo "UNKNOWN: $MSGNAGIOS|$PERFDATA"
exit 3;
else
echo "OK: $MSGNAGIOS|$PERFDATA"
exit 0
fi