Subversion Repositories ALCASAR

Rev

Rev 376 | Rev 519 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 376 Rev 518
1
#!/bin/sh
1
#!/bin/sh
2
# $Id: alcasar-watchdog.sh 376 2010-12-19 22:24:58Z franck $
2
# $Id: alcasar-watchdog.sh 518 2011-03-25 16:04:12Z stephane $
3
# by rexy
3
# by rexy
4
# Ce script prévient les usagers de l'indisponibilité de l'accès Internet
4
# Ce script prévient les usagers de l'indisponibilité de l'accès Internet
5
# il déconnecte les usagers dont
5
# il déconnecte les usagers dont
6
# - les équipements réseau ne répondent plus
6
# - les équipements réseau ne répondent plus
7
# - les adresses MAC sont usurpées
7
# - les adresses MAC sont usurpées
8
# This script tells users that Internet access is down
8
# This script tells users that Internet access is down
9
# it logs out users whose 
9
# it logs out users whose 
10
# - PCs are quiet
10
# - PCs are quiet
11
# - MAC address are in used by other systems (usurped)
11
# - MAC address are in used by other systems (usurped)
12
 
-
 
13
EXTIF="eth0"
12
EXTIF="eth0"
14
INTIF="eth1"
13
INTIF="eth1"
15
PRIVATE_IP="192.168.182.1"
14
PRIVATE_IP="192.168.182.1"
16
tmp_file="/tmp/watchdog.txt"
15
tmp_file="/tmp/watchdog.txt"
17
DIR_WEB="/var/www/html"
16
DIR_WEB="/var/www/html"
18
Index_Page="$DIR_WEB/index.php"
17
Index_Page="$DIR_WEB/index.php"
19
IFS=$'\n'
18
IFS=$'\n'
20
 
19
 
21
# Fonction appelée si un Pb de connectivité Internet
20
# Fonction appelée si un Pb de connectivité Internet
22
# On fait pointer les usagers sur une page d'erreur
21
# On fait pointer les usagers sur une page d'erreur
23
function ext_down_alert ()
22
function ext_down_alert ()
24
{
23
{
25
	case $EXT_DOWN in
24
	case $EXT_DOWN in
26
	"1")
25
	"1")
27
		logger "eth0 link down"
26
		logger "eth0 link down"
28
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"eth0 link down\";?g" $Index_Page
27
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"eth0 link down\";?g" $Index_Page
29
		;;
28
		;;
30
	"2")
29
	"2")
31
		logger "can't contact the default router"
30
		logger "can't contact the default router"
32
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
31
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page
33
		;;
32
		;;
34
	"3")
33
	"3")
35
		logger "can't contact the Internet DNS"
34
		logger "can't contact the Internet DNS"
36
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the Internet DNS\";?g" $Index_Page
35
		/bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the Internet DNS\";?g" $Index_Page
37
		;;
36
		;;
38
	esac
37
	esac
39
	net_pb=`cat /etc/dnsmasq.d/alcasar-dnsmasq.conf|grep "address=/#/"|wc -l`
38
	net_pb=`cat /etc/dnsmasq.d/alcasar-dnsmasq.conf|grep "address=/#/"|wc -l`
40
	if [ $net_pb != "1" ]
39
	if [ $net_pb != "1" ]
41
		then
40
		then
42
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
41
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page
43
		/bin/sed -i "s?^conf-dir=.*?address=\/#\/$PRIVATE_IP?g" /etc/dnsmasq.d/alcasar-dnsmasq.conf
42
		/bin/sed -i "s?^conf-dir=.*?address=\/#\/$PRIVATE_IP?g" /etc/dnsmasq.d/alcasar-dnsmasq.conf
44
		/etc/init.d/dnsmasq restart
43
		/etc/init.d/dnsmasq restart
45
	fi
44
	fi
46
}
45
}
47
 
46
 
48
# On teste la connectivité réseau
47
# On teste la connectivité réseau
49
# On teste l'état d'EXTIF
48
# On teste l'état d'EXTIF
50
EXT_DOWN="0"
49
EXT_DOWN="0"
51
if [ "`/usr/sbin/ethtool $EXTIF|grep Link|cut -d' ' -f3`" != "yes" ]
50
if [ "`/usr/sbin/ethtool $EXTIF|grep Link|cut -d' ' -f3`" != "yes" ]
52
	then
51
	then
53
	EXT_DOWN="1"
52
	EXT_DOWN="1"
54
fi
53
fi
55
# si EXTIF ok, on teste la connectivité vers le routeur par défaut (Box FAI)
54
# si EXTIF ok, on teste la connectivité vers le routeur par défaut (Box FAI)
56
if [ $EXT_DOWN -eq "0" ]
55
if [ $EXT_DOWN -eq "0" ]
57
	then
56
	then
58
	IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
57
	IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
59
	arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
58
	arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
60
	if [ $arp_reply -eq "0" ]
59
	if [ $arp_reply -eq "0" ]
61
	       	then
60
	       	then
62
		EXT_DOWN="2"
61
		EXT_DOWN="2"
63
	fi
62
	fi
64
fi
63
fi
65
# si routeur OK, on teste la connectivité vers les DNS externes
64
# si routeur OK, on teste la connectivité vers les DNS externes
66
# + tard (EXT_DOWN=3)
65
# + tard (EXT_DOWN=3)
67
# si Pb réseau, on avertit les usagers
66
# si Pb réseau, on avertit les usagers
68
if [ $EXT_DOWN != "0" ]
67
if [ $EXT_DOWN != "0" ]
69
	then
68
	then
70
		ext_down_alert
69
		ext_down_alert
71
	else
70
	else
72
# sinon, on rebascule en mode normal
71
# sinon, on rebascule en mode normal
73
	net_pb=`cat /etc/dnsmasq.d/alcasar-dnsmasq.conf|grep "address=/#/"|wc -l`
72
	net_pb=`cat /etc/dnsmasq.d/alcasar-dnsmasq.conf|grep "address=/#/"|wc -l`
74
	if [ $net_pb -eq "1" ]
73
	if [ $net_pb -eq "1" ]
75
		then
74
		then
76
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
75
		/bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page
77
		/bin/sed -i "s?^address=\/#\/.*?conf-dir=/usr/local/etc/alcasar-dnsfilter-enabled?g" /etc/dnsmasq.d/alcasar-dnsmasq.conf
76
		/bin/sed -i "s?^address=\/#\/.*?conf-dir=/usr/local/etc/alcasar-dnsfilter-enabled?g" /etc/dnsmasq.d/alcasar-dnsmasq.conf
78
		/etc/init.d/dnsmasq restart
77
		/etc/init.d/dnsmasq restart
79
	fi
78
	fi
80
fi
79
fi
81
# lecture du fichier contenant les adresses IP des stations muettes
80
# lecture du fichier contenant les adresses IP des stations muettes
82
if [ -e $tmp_file ]; then
81
if [ -e $tmp_file ]; then
83
	cat $tmp_file | while read noresponse
82
	cat $tmp_file | while read noresponse
84
	do
83
	do
85
		noresponse_ip=`echo $noresponse | cut -d" " -f1`
84
		noresponse_ip=`echo $noresponse | cut -d" " -f1`
86
		noresponse_mac=`echo $noresponse | cut -d" " -f2`
85
		noresponse_mac=`echo $noresponse | cut -d" " -f2`
87
		arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep response|cut -d" " -f2`
86
		arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep response|cut -d" " -f2`
88
		if [[ $(expr $arp_reply) -eq 0 ]]
87
		if [[ $(expr $arp_reply) -eq 0 ]]
89
	       	then
88
	       	then
90
			logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) reste muette. On déconnecte."
89
			logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) reste muette. On déconnecte."
91
			/usr/sbin/chilli_query logout $noresponse_mac
90
			/usr/sbin/chilli_query logout $noresponse_mac
92
		fi
91
		fi
93
	done
92
	done
94
	rm $tmp_file
93
	rm $tmp_file
95
fi
94
fi
96
# on traite chaque équipements connus de chilli
95
# on traite chaque équipements connus de chilli
97
for system in `/usr/sbin/chilli_query list`
96
for system in `/usr/sbin/chilli_query list`
98
do
97
do
99
	active_ip=`echo $system |cut -d" " -f2`
98
	active_ip=`echo $system |cut -d" " -f2`
100
	active_session=`echo $system |cut -d" " -f5`
99
	active_session=`echo $system |cut -d" " -f5`
101
	active_mac=`echo $system | cut -d" " -f1`
100
	active_mac=`echo $system | cut -d" " -f1`
102
# on ne traite que les équipements exploitées par un usager authentifié (test de 2 réponses en 4 secondes)
101
# on ne traite que les équipements exploitées par un usager authentifié (test de 2 réponses en 4 secondes)
103
if [[ $(expr $active_session) -eq 1 ]]
102
if [[ $(expr $active_session) -eq 1 ]]
104
	then
103
	then
105
	arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep response|cut -d" " -f2`
104
	arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep response|cut -d" " -f2`
106
# on stocke les adresses IP des stations muettes
105
# on stocke les adresses IP des stations muettes
107
	if [[ $(expr $arp_reply) -eq 0 ]]
106
	if [[ $(expr $arp_reply) -eq 0 ]]
108
	       	then
107
	       	then
109
			echo "$active_ip $active_mac" >> $tmp_file
108
			echo "$active_ip $active_mac" >> $tmp_file
110
	fi
109
	fi
111
# on deconnecte l'usager d'une stations usurpée (@MAC)
110
# on deconnecte l'usager d'une stations usurpée (@MAC)
112
	if [[ $(expr $arp_reply) -gt 2 ]]
111
	if [[ $(expr $arp_reply) -gt 2 ]]
113
	       	then
112
	       	then
114
			logger "alcasar-watchdog : $active_ip est usurpée ($active_mac). On déconnecte."
113
			logger "alcasar-watchdog : $active_ip est usurpée ($active_mac). On déconnecte."
115
			/usr/sbin/chilli_query logout $active_mac
114
			/usr/sbin/chilli_query logout $active_mac
116
	fi
115
	fi
117
fi
116
fi
118
done
117
done
119
 
118
 
120
 
119