1. [root@aliyun-cloud scripts]# cat web_code.sh
    2. #!/bin/bash
    3. #WEB状态码监控
    4. source /etc/profile
    5. url=http://127.0.0.1:9999/user/getNewPublicKey?loginType=1
    6. webhook="https://oapi.dingtalk.com/robot/send?access_token=4d15f6354cdddf9498f90131084c72e32e1bc3e028df8e8ae11201392b672840"
    7. Hostname=${hostname}
    8. #Host_IP=`hostname -i|awk -F " " '{print $1}'`
    9. Host_IP=`/usr/sbin/ip a | grep inet | grep -v inet6 | grep -v '127.0.0.1' | awk '{print $2}' | awk -F / '{print$1}'`
    10. Hostname=`hostname`
    11. Time=`date '+%F %T'`
    12. function SendMsgToDingding() {
    13. curl $webhook -H 'Content-Type: application/json' -d "
    14. {
    15. 'msgtype': 'text',
    16. 'text': {
    17. 'content': '告警主题:${Name}-WEB状态码监控 \n钉钉关键字: 1 \n告警时间: $Time \n告警主机名: $Hostname \n主机IP列表: ${Host_IP} \n当前监控指标: $Status_Code \n日志: 服务器异常,请尽快排查异常...\n'
    18. },
    19. 'at': {
    20. 'isAtAll': false
    21. }
    22. }"
    23. }
    24. Status_Code=`curl -I -m 3 -o /dev/null -s -w %{http_code} $url`
    25. docker_status=`docker ps |grep esl_app_1|wc -l`
    26. if [[ "$Status_Code" -ne 200 ]]
    27. then
    28. SendMsgToDingding
    29. if [[ "$docker_status" -ne 1 ]];then
    30. echo "Restart esl_app_1"
    31. docker restart esl_app_1
    32. else
    33. exit -1
    34. fi
    35. else
    36. echo "正常" >/dev/null 2>&1
    37. fi