Outils pour utilisateurs

Outils du site


scripting:superviser

BASH : Supervision d'hotes et services réseaux

  • Selon un intervalle de temps de temps définit,
    le script va vérifier que la liaison réseau (ping) avec les hosts définis
    ainsi que les ports des services (netcat) voulus.

#!/bin/bash
 
# COLOURS VARIABLES
RED="\033[1;31m"
GREEN="\033[1;32m"
END="\033[0m"
GATEWAY="10.32.255.253"		                        # IP GATEWAY
HOST="10.32.101.4"					# IP HOST
DNS="10.35.0.1"		   				# IP DNS
REPOS="10.0.0.9"					# IP REPOS 
LOG="/var/log/syslog"				        # LOG file
 
# FONCTION DE CHECK HOST
check_host ()
{
		ping -c1 $1 2>&1 > /dev/null ;
	if [[ $? != 0 ]];
		then
		case $1 in
                		$HOST)
				count1=$( expr $count1 + 1 )
				echo -e "`date +%H:%m:%S` HOST $1 : "$RED"KO"$END" ($count1)";
                        	;;
                		$GATEWAY)
                        	count2=$( expr $count2 + 1 )
				echo -e "`date +%H:%m:%S` HOST $1 : "$RED"KO"$END" ($count2)";
				;;
                		$DNS)
                        	count3=$( expr $count3 + 1 )
				echo -e "`date +%H:%m:%S` HOST $1 : "$RED"KO"$END" ($count3)";
				;;
                		$REPOS)
                        	count4=$( expr $count4 + 1 )
				echo -e "`date +%H:%m:%S` HOST $1 : "$RED"KO"$END" ($count4)";
				;;
		esac	
	else
		echo -e "`date +%H:%m:%S` HOST "$1" : "$GREEN"OK"$END"";
	fi
	if [[ $count1 = 3 || $count2 = 3 || $count3 = 3 || $count4 = 3 ]];
		then
		log "$1"
	fi
}	
 
# FONCTION DE CHECK SERVICE
check_service ()
{
	netcat -n $1 $2 -z > /dev/null;
		if [[ $? != 0 ]];
			then
			case $1 in
                                $DNS)
                                count3=$( expr $count3 + 1 )
                                echo -e "`date +%H:%m:%S` PORT "$2" de "$1" : "$RED"KO"$END" ("$count3")";
                                ;;
                                $REPOS)
                                count4=$( expr $count4 + 1 )
                                echo -e "`date +%H:%m:%S` PORT "$2" de "$1" : "$RED"KO"$END" ("$count4")";
                                ;;
                	esac
	       else
                	echo -e "`date +%H:%m:%S` PORT "$2" de "$1" : "$GREEN"OK"$END"";
        	fi
        if [[ $count3 = 3 || $count4 = 3 ]];
                then
                log "$1"
        fi
}
 
# FONCTION DE LOG + ACTION
log ()
{
echo -e ""$RED"[`date +%Y-%m-%d+%H:%M]` Liaison avec "$1" - redémarrage réseau"$END"" >> $LOG && /etc/init.d/networking restart ;
count1=0; count2=0; count3=0; count4=0;
}
 
# SCRIPT
count1=0
count2=0
count3=0
count4=0
i=0
while :
	do
		check_host $HOST;
		[[ `expr $i % 5` == 0 ]] && check_host $GATEWAY;
		[[ `expr $i % 10` == 0 ]] && check_host $DNS && check_service $DNS 53;
		[[ `expr $i % 155555` == 0 ]] && check_host $REPOS && check_service $REPOS 80;
		sleep 1s
		i=$( expr $i + 1 )
	done

scripting/superviser.txt · Dernière modification: 15/04/2016 à 21:15 (modification externe)

Outils de la page