Subversion Repositories ALCASAR

Rev

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

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