服务检测脚本
检测服务端口是否正常,不正常直接从lvs撤下,正常自动添加
#!/bin/bash
srv_ip="192.168.1.191";
srv_port="30001";
ip_list="192.168.1.117 192.168.1.118";
port_list="30001";
Sleep=3;
declare -A map=(['test']=1);
while true
do
for ip in $ip_list
do
for port in $port_list
do
srv_exists=`nc -w 10 -z $ip $port | wc -l`;
last_srv_exists=${map[${ip}:${port}]};
if [ -z "$last_srv_exists" ]; then
echo '初始化状态.....'$ip:$port;
last_srv_exists=$srv_exists;
map[${ip}:${port}]=$last_srv_exists;
fi
if [ $srv_exists != '1' ]; then
if [ $last_srv_exists != $srv_exists ]; then
cmd="ipvsadm -d -t $srv_ip:$srv_port -r $ip:$port ;";
echo $cmd ------------`date '+%Y-%m-%d %H:%M:%S'`;
ipvsadm -d -t $srv_ip:$srv_port -r $ip:$port ;
map[${ip}:${port}]=$srv_exists;
fi
else
if [ $last_srv_exists != "$srv_exists" ]; then
cmd="ipvsadm -a -t $srv_ip:$srv_port -r $ip:$port -g;";
echo $cmd -------------`date '+%Y-%m-%d %H:%M:%S'`;
ipvsadm -a -t $srv_ip:$srv_port -r $ip:$port -g;
map[${ip}:${port}]=$srv_exists;
fi
fi
done;
done;
sleep $Sleep;
done;