#!/bin/bash
scripts_dir=$(cd $(dirname $0) && pwd)
connsvr_proxy_ip="100.119.153.55;100.119.203.55"
log_file="/tmp/tncagent_check.log"
tncagent_confdir="/data/sniper/tnc_agent/conf/"
cmd_dir="/data/sniper/tnc_agent/bin/"
write_log() {
content=$1
echo "`date +%Y%m%d_%H:%M:%S` $content" >>${log_file}
}
[ -f ${log_file} ] && echo>${log_file}
write_log "check to ${connsvr_proxy_ip} port [9090] establish status"
flag=0
for ip in `echo $connsvr_proxy_ip|tr ';' '\n'`;do
echo "a"|telnet -e "a" $ip 9090 >/dev/null
if [ $? -eq 0 ];then
echo -e "\033[34m telnet $ip [9090] is ok\033[0m"
#let flag+=1
flag=$(($flag+1))
else
echo -e "\033[31m telnet $ip [9090] is NOT ok\033[0m"
continue
fi
done
[ $flag -lt 1 ] && {
echo -e "\033[31m pls,check port[9090] estab!!!\033[0m";
exit 1
}
write_log "check tncagent configuration[tnc3_agent.ini]"
[ -f ${tncagent_confdir}tnc3_agent.ini ] || {
write_log "check tncagent install";
exit 1
}
cd ${tncagent_confdir}
current_connsvr_ip=`grep ^ConnSvrIPs tnc3_agent.ini|awk -F"=" '{print $NF}'`
echo -e "\033[34mcurrent connsvr_ip[${current_connsvr_ip}]\033[0m"
cp -a tnc3_agent.ini tnc3_agent.ini.bak$(date +%Y%m%d)
write_log "change configuration[tnc3_agent.ini]"
sed -i "/^ConnSvrIPs/s/${current_connsvr_ip}/${current_connsvr_ip};${connsvr_proxy_ip}/" tnc3_agent.ini
if [ `grep -w ${connsvr_proxy_ip} tnc3_agent.ini|wc -l` -eq 1 ];then
write_log "change configuration[tnc3_agent.ini] success"
else
write_log "change configuration[tnc3_agent.ini] failed"
exit 1
fi
new_connsvr_ip=`grep ^ConnSvrIPs tnc3_agent.ini|awk -F"=" '{print $NF}'`
echo -e "\033[34m new connsvr_ip[${new_connsvr_ip}]\033[0m"
write_log "restart tncagent service"
[ -d ${cmd_dir} ] || exit 2
sh ${cmd_dir}restart.sh
cnt=`ps -ef|grep -w tnc_agent|grep -v grep|wc -l`
echo -e "\033[34m new established status\033[0m"
est_ip=`netstat -antp|grep 9090|grep -w ${connsvr_proxy_ip}|awk -F"[: ]*" '{print $6}'|uniq`
netstat -antp|grep -w 9090|grep -i ESTAB
if [[ $cnt -eq 1 ]] && [[ ${est_ip}=="${connsvr_proxy_ip}" ]];then
write_log "restart tncagent success and esb_status is ok"
else
write_log "restart tncagent failed or esb_status is not ok"
echo -e "\033[31m restart tncagent failed or esb_status is not ok\033[0m"
fi