拨测ip+port 告警 telnet nc
1.telnet
[root@master-node156 mon_port]# cat mycheck_port|egrep -v '^$' #! /bin/sh #企业微信警配置 alert=True #配置True为启动告警,其它为不开启 #企业微信机器人ID userkey="5642127c-woab-shib-9da8-ac2f4adashabic003" echo 'a' >/tmp/problemip.txt cur_datetime=`date +%Y%m%d-%H:%M:%S` homeDir=`dirname $0` function check_port { pkill -9 telnet timeout 2 ssh 127.0.0.1 telnet $1 >/tmp/a.txt myresult=`egrep -i 'Escape' /tmp/a.txt|wc -l` if [[ $myresult -eq 1 ]];then echo "$cur_datetime 端口$port listening,status is ok..." | tee -a $homeDir/port_status.log else echo "$cur_datetime Error 端口$port closed,status is failed..." | tee -a $homeDir/port_status.log if [ "$alert" = "True" ];then echo $1 >/tmp/problemip.txt sed -i 's# #-#' /tmp/problemip.txt hostport=`cat /tmp/problemip.txt` curl -s -X POST -H "'Content-type':'application/json'" -d '{ "msgtype": "text", "text": { "content": "端口未启动 \r 主机端口: '$hostport' \r 时间: '$cur_datetime'", "mentioned_list":["wangqing",""], "mentioned_mobile_list":["",""] } }' https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=$userkey fi sleep 1 fi } for i in `cat /opt/mon_port/8080.txt`;do a=`echo $i|awk -F[:] '{print $1" "$2}'`;check_port "$a"; done
2.nc
[root@pmmprd1-awssec2-web01 pmm-user]# cat mycheck_port.sh #! /bin/sh cur_datetime=`date +%Y%m%d-%H:%M:%S` homeDir=`dirname $0` function check_port { nc -w 3 -z $1 $2 > /dev/null 2>&1 echo $? echo 'after nc' if [ $? = 0 ] ; then echo "$cur_datetime 端口 $1 $2 listening,status is ok..." | tee -a $homeDir/port_status.log sleep 2 else echo "$cur_datetime Error 端口$1 $2 closed,status is failed..." | tee -a $homeDir/port_status.log sleep 2 fi } while read line do echo $line check_port $line done < /home/pmm-user/ip_port.txt echo "================================"
[root@pmmprd1-awssec2-web01 pmm-user]# cat /home/pmm-user/ip_port.txt
10.117.16.102 8030
10.117.16.102 38030
[root@master-node156 mon_port]# cat check_port |grep -v '^$' #! /bin/sh #企业微信警配置 alert=True #配置True为启动告警,其它为不开启 #企业微信机器人ID userkey="5wo2127c-shib-458b-9da8-ac2fshabidc003" while getopts ":h:p:" option do case "$option" in h) host=$OPTARG;; p) port=$OPTARG;; *) echo "Usage: args [-h] [-p]" echo "-h means host" echo "-p means port" exit 1 ;; esac done if [ ! $port ];then echo "请输入端口号,如 $0 -h 192.168.1.10 -p 80" echo "没有-h参数,默认检查本地端口" exit 1 fi cur_datetime=`date +%Y%m%d-%H:%M:%S` homeDir=`dirname $0` localIp=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|head -n 1) if [ ! $host ];then host=$localIp fi #res=`$homeDir/check_tcp -H $host -p $port` #if [[ "$res" =~ "OK" ]];then #nc -w 3 $host $port < /dev/null > /dev/null 2>&1 nc -w 3 -z $host $port > /dev/null 2>&1 if [ $? = 0 ] ; then echo "$cur_datetime 端口$port listening,status is ok..." | tee -a $homeDir/port_status.log exit 0 else echo "$cur_datetime Error 端口$port closed,status is failed..." | tee -a $homeDir/port_status.log if [ "$alert" = "True" ];then curl -s -X POST -H "'Content-type':'application/json'" -d '{ "msgtype": "text", "text": { "content": "端口未启动 \r 端口: '$port' \r 服务器: '$host' \r 时间: '$cur_datetime'", "mentioned_list":["wangqing",""], "mentioned_mobile_list":["",""] } }' https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=$userkey fi exit 1 fi
用一个例子来演示会更加清晰