shell监控源站状态码

function ding(){
        content=$1
        curl 'https://oapi.dingtalk.com/robot/send?access_token=' -H 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content": "'"${content}"'"}}'
	`echo ${content}"\n" >> /data/jobs/error.log`
}
function monitor(){

        url=$1
        ip=$2
        title=$3
        data=`curl -I -m 10 -o /dev/null -s -w "%{time_connect}@%{time_starttransfer}@%{time_total}@%{http_code} \n" ${url} -x ${ip} --user-agent 'yanxm Monitor'`
        # data=`curl -I -m 10 -o /dev/null -s -w "%{time_connect}@%{time_starttransfer}@%{time_total}@%{http_code} \n" ${url} --user-agent 'yanxm Monitor'`
	echo ${data}
        time_connect=`echo ${data} |awk -F '@' '{print $1}' `
        time_starttransfer=`echo ${data} |awk -F '@' '{print $2}' `
        time_total=`echo ${data} |awk -F '@' '{print $3}' `
        code=`echo ${data} |awk -F '@' '{print $4}'`
        str="time_connect:"${time_connect}"\ntime_starttransfer:"${time_starttransfer}"\ntime_total:"${time_total}
        if (( ${code} != "200" ))
        then
                ding "${title} 源站状态码错误\nURL:"${url}"\n状态码:"${code}"\n"${str}
		if [ ! -f /data/jobs/lock/${title}.lock ]; 
		then
			sendsms "${title}--源站状态码错误;状态码:"${code}";time_total:"${time_total}";URL:"${url}
			touch /data/jobs/lock/${title}.lock
			date +%s > /data/jobs/lock/${title}.lock
		fi
	else
		if [ -f /data/jobs/lock/${title}.lock ];
		then
			starttime=`cat /data/jobs/lock/${title}.lock`
			endtime=`date +%s`
			s=$[${endtime}-${starttime}]
			ding "${title} 源站状态码错误恢复\nURL:"${url}"\n状态码:"${code}"\n"${str}"\n持续$[${s}/3600]小时$[${s}%3600/60]分钟$[${s}%60]秒"	
			rm -f /data/jobs/lock/${title}.lock
		fi
        fi
        bigtime=$4
        timebool=`echo ${time_total}|awk '{if($0 > '"${bigtime}"') print "true"; else print "false"}'`
        if [ "${timebool}" = 'true' ]
        then
#                ding "${title} 响应时间超时\nURL:"${url}"\n状态码:"${code}"\n"${str}
	echo 1
        fi
}

monitor 'http://test.local.com/' '127.0.0.1:80' '测试' '0.5'
# 地址 IP:端口 站名 超出报警时间(秒)

赤狐博客 - 对于互联网行业的胡诌八扯
请先登录后发表评论
  • 最新评论
  • 总共0条评论