#!/bin/bash
#dbgf=2
sbm_addr="http://jp2.gzlong7.net/"
sbm_url=$sbm_addr"/te_push/index.php"
send_noalert()
{
	domain=$1
	utc_unix=`date --utc +"%s"`
	bj_unix=`expr $utc_unix + 28800 `
	cur_time=`date -d @$bj_unix --utc +"(BJ) %b-%d %H:%M"`
	
	m_fr=`echo -e "[$domain] is back "|./urlencode2`
	m_subject=`echo -e "Time: $cur_time" |./urlencode2`
	m_text=`echo -e "$domain is back at $cur_time" |./urlencode2`	
	echo $m_text
	f_info=`echo -e "[$domain] is back \nTime: $cur_time\n$domain is back at $cur_time"|./urlencode2`
	#	curl -d  "m_subject=$m_subject&m_text=$m_text&m_fr=$m_fr" "http://gzlong7.com/sendalert/index.php"
	curl -d  "channel=@duanzidog&content=$f_info" $sbm_url
#noalert_tpl_fr=
}
send_alert(){

	domain=$1
	df="."$domain
	broketime=`cat $df`
	utc_unix=`date --utc +"%s"`
	bj_unix=`expr $broketime + 28800 `
	cur_time=`date -d @$bj_unix --utc +"(BJ) %b-%d %H:%M"`
	info=`dig $domain +short @114.114.114.114`

	m_fr=`echo -e "server [$domain] is offline" |./urlencode2`
	m_subject=`echo  "Time: $cur_time" |./urlencode2`
	m_text=`echo -e "Domain info: \ndig $domain\n$info" |./urlencode2`
	f_info=`echo -e "server [$domain] is offline\nTime: $cur_time\nDomain info: \ndig $domain\n$info" |./urlencode2`
	echo $m_fr
#	curl -d  "m_subject=$m_subject&m_text=$m_text&m_fr=$m_fr" "http://gzlong7.com/sendalert/index.php"
	curl -d  "channel=@duanzidog&content=$f_info" $sbm_url
}

calc_time()
{
	nowtime=`date +"%s"`
	#echo $nowtime
	#echo $1
	if [ `expr $nowtime - $1 ` -gt 1800 ]
	then
	       #echo "more than 30min"
	       return 0
	else
	       #echo "less than 30min"
       		return 1	       
	fi	       
}

check_status()
{
	nc -w 15 -z $1 22 
	if [ "$?" == "0" ]
	then 
	#	echo `date +"%H:%M"`" - "$1' is [OK]' >> /var/log/server_watchdog.log
		return 0
	else
		wget --spider --quiet --timeout=10 --tries=1 "http://www.baidu.com/img/baidu_jgylogo3.gif"
		if [ "$?" == "0" ]
		then
			#echo "[WARNING]: "$1" is down"
			return 1
		else 
			echo "Networking problem"
			return 0
		fi
	fi
}

execute()
{
	domain=$1
	df="."$domain
	#flast="\."$domain".last"
	echo "checking "$domain
	check_status $domain
	if [ "$?" == "1" ]
	then
		echo `date +"%H:%M"`" - "$1' is [OFFLINE]' >> /var/log/server_watchdog.log
		if [ ! -f $df ]
		then	echo "NOW" > $df
			chmod 666 $df
			#first time
			return
		fi
		last_status=`cat $df`
		if [ $last_status != "NOW" ]
		then 
			calc_time $last_status
			if [ "$?" != 0 ] 
			then return 
			fi
		fi
			echo `date +"%s"` > $df
			echo "$domain offline"
			send_alert $domain
	else 
		if [ -f $df ]
		then 
			last_status=`cat $df`
			if [ $last_status != "NOW" ]
			then
				echo `date +"%H:%M"`" - "$1" is [back]" >> /var/log/server_watchdog.log
				send_noalert $domain
			fi
			rm $df
		fi
		echo `date +"%H:%M"`" - "$1' is [OK]' >> /dev/shm/server_watchdog.log
	fi
	 
}
call_online()
{
	cal_url=$sbm_addr"/pp/s.php"
        curl $cal_url
}
main()
{
	call_online
	pushd "/home/Richard/jiankong"
	for domain  in `cat domainlist.txt|grep -Ev "^$|#"`
	do
		execute $domain
		#send_alert $domain
	done
	popd
	return 0
}

if [ $dbgf == '2' ]
then
	if [ $# == 1 ]
	then 
	main
	else 
	echo "[DEBUG MODE] plz  run as \"$0 run \""
	fi

	exit 0

else main
fi

