#!/bin/bash ##服务名称 SERVICE_NAME="nginx1 nginx2 ningx3 nginx4" ip=1.1.1.1 #本机ip #检测服务是否正常 for service in $SERVICE_NAME do #echo ----------------------- 无视分割线内内容 ----------------------------- ##检测服务是否存在 service_quantity=`docker ps | grep $service | wc -l` if [ $service_quantity -lt 1 ];then curl ‘https://oapi.dingtalk.com/robot/send?access_token=5e64853712e0e40b4ab069c70b71743d4347c55b3e2f9ab9891d082744db54b5‘ -H ‘Content-Type: application/json‘ -d "{\"msgtype\": \"text\", \"text\": { \"content\": \"Details $ip $service 服务不存在 请检查服务\" } }" break ##判断服务是否正常 service_logs=`docker inspect --format ‘{{.State.Running}}‘ ${service}` if [ ${service_logs} != true ];then curl ‘https://oapi.dingtalk.com/robot/send?access_token=5e64853712e0e40b4ab069c70b71743d4347c55b3e2f9ab9891d082744db54b5‘ -H ‘Content-Type: application/json‘ -d "{\"msgtype\": \"text\", \"text\": { \"content\": \"Details $ip $service 服务没有正常启动 请检查服务\" } }" break fi fi done #检查端口 server_ports=(80 81 82 83) for server_port in ${server_ports[@]}; do while ((count<5));do nc -w 1 -z $ip $server_port > /dev/null 2>&1 if [[ $? -ne 0 ]];then curl ‘https://oapi.dingtalk.com/robot/send?access_token=5e64853712e0e40b4ab069c70b71743d4347c55b3e2f9ab9891d082744db54b5‘ -H ‘Content-Type: application/json‘ -d "{\"msgtype\": \"text\", \"text\": { \"content\": \"Details $ip:$server_port 端口异常 请检查端口\" } }" break else echo > /dev/null 2>&1 break fi done done