Pour vérifier l'état des sessions BGP sur les routeurs Linux tournant [[http://www.quagga.net/|Quagga]] on utilise un plugin local pour ''check_mk'' qui va donc régulièrement interroger Quagga pour connaitre les sessions en cours, et leur état. Un fichier de configuration permet de définir, en spécifiant les numéros d'AS, les sessions à ignorer, ou celles qui sont critiques (un client, un transitaire, par exemple). Par défaut une session qui n'est pas active, et pour laquelle ce n'est pas volontaire (''shutdown'') donne un retour WARNING.
* ''DISABLEPWD'' est le mot de passe Quagga pour pouvoir se connecter
* ''ENABLEPWD'' est le mot de passe Quagga pour pouvoir passer ''//enable//''
#!/bin/bash
## Neuronnexion - www.neuronnexion.coop - tech@nnx.com
## Released under the terms of GNU/GPL
conf="/etc/check_mk/bgp.conf"
script_expect="/usr/local/bin/bgp_summary localhost bgpd DISABLEPWD ENABLEPWD"
# Var NAGIOS
STATE_OK="0"
STATE_WARNING="1"
STATE_CRITICAL="2"
STATE_UNKNOWN="3"
STATE_DEPENDENT="4"
# Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
(
$script_expect |grep -A 9999999 "^Neighbor"|grep -B 999999 "^Total"|egrep "^[0-9]" | sed -e 's/\r//' | \
while read Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ UpDown StatePfxRcd sup ; do
# Status ItemName PerformanceData CheckOutput
if [[ $(grep -i "^exclude:as$AS:" "$conf") ]] ; then
continue # skip when exclude
fi
infos="$StatePfxRcd"
[ "x" != "x$sup" ] && infos="$infos $sup" # Gestion des cas ou l'info est en deux mots
count=0
val=$STATE_UNKNOWN
if [[ $StatePfxRcd =~ [0-9]+ ]] ; then
val=$STATE_OK
count=$StatePfxRcd
else
# Erreur, donc warning
val=$STATE_WARNING
# Si CRITIQUE, alors on hurle
if [[ $(grep -i "^critical:as$AS:" "$conf") ]] ; then
val=$STATE_CRITICAL
fi
# Si c'est volontaire, shut
if [[ "$infos" =~ \(Admin\) ]] ; then
val=$STATE_OK
fi
fi
echo "$val BGP_AS"$AS"_$Neighbor count=$count Duration: $UpDown - Status: $infos"
done
)|sort -k 2
Exemple de fichier de configuration.
# Keywords : critical or exclude
# AS like ASXXXX
# Description is not an option ;)
# this one is a transit
critical:AS29075:Ielo
# this one keeps flapping
exclude:AS666:BadPeer
(en vrai, on a pas de session avec l'AS 666 ;-)
Le script communiquant avec Quagga utilise expect.
#!/usr/bin/expect -f
# Utilisé par le check_mk_agent pour gérer la vue des sessions BGP
# Viva NNX
# Note: this script is NOT good example of writting code in Expect at all.
if { [llength $argv] != 4 } {
puts "Expect 'show ip bgp summary' script for Quagga services."
puts "Copyright (C) 2007 Matous Jan Fialka."
puts "Released under the terms of GNU/GPL.\n"
puts "Usage: hostname { service | port } disable_password enable_password\n"
puts "Example: qshru.exp localhost zebra secret password\n"
puts "Use with extreme caution! You have been warned..."
exit 1
}
set hostname [lindex $argv 0]
set service [lindex $argv 1]
set disable_password [lindex $argv 2]
set enable_password [lindex $argv 3]
set disable_prompt "^*>"
set enable_prompt "^*#"
set disable_password_prompt "Password:"
set enable_password_prompt "Password:"
set terminal_length_command "terminal length 0"
set enable_command "enable"
set disable_command "disable"
set quit_command "quit"
set show_running_config_command "show running-config"
set show_ip_bgp_summary "show ip bgp summary"
set timeout 10
set telnet /usr/bin/telnet
spawn "$telnet" "$hostname" "$service"
expect "$disable_password_prompt"
send "$disable_password\r"
expect "$disable_prompt"
send "$terminal_length_command\r"
expect "$disable_prompt"
send "$enable_command\r"
expect "$enable_password_prompt"
send "$enable_password\r"
expect "$enable_prompt"
send "$show_ip_bgp_summary\r"
#send "$show_running_config_command\r"
expect "$enable_prompt"
send "$disable_command\r"
expect "$disable_prompt"
send "$quit_command\r"
expect eof
puts "\nBye, bye..."
exit 0