#!/bin/bash
source /etc/profile
#此脚本是根据访问端口，获得返回值，从未进行服务的健康性检测。
ip1=`ip addr | awk '/inet / && !/127.0.0.1/ {split($2, a, "/"); print a[1]}'`
port1=$1
user1=$2
###########以下根据port判断服务###############
#声明字典
declare -A dic
dic=([8072]="portal-app" [8073]="portal-web" [8074]="portal-instrument" [8075]="portal-open" [8076]="portal-simulation" [8071]="portal-socket" [8015]="soa-blog" [8013]="soa-ms" [8011]="soa-system" [8012]="soa-user" [8014]="soa-job" [8019]="soa-third" [8016]="soa-vehicle-base" [8017]="soa-protocol-control" [8018]="soa-protocol-highfreq" [8020]="soa-protocol-lowfreq" [8021]="soa-data-handler" [9099]="ccclubs-api-gateway" [9091]="ccclubs-api-gateway-listen" [7070]="ccclubs-gwa-api" [8033]="soa-sequence-data")
echo ${#dic[@]}
#打印指定key值
echo ${dic["8073"]}
#打印所有key
echo ${!dic[*]}
#打印所有value
echo ${dic[*]}

################根据port判断服务
server1=`echo ${dic["$port1"]}`
echo $server1
################根据port判断服务结束###############

if [ ! -d /data/monitor/logs ];then
    mkdir -p /data/monitor/logs
else
    echo "dir exist"
fi
while true;
do
#    status=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://localhost:$port1/actuator/health`
#    status=`curl -s http://localhost:$port1/actuator/health  |awk -F ":" '{print $2}' | awk -F "\"" '{print $2}'`
    status2=`curl -s http://localhost:$port1/actuator/health`
    echo "$status2" >/data/monitor/logs/$port1.txt
    status=`cat /data/monitor/logs/$port1.txt |awk -F ":" '{print $2}' | awk -F "\"" '{print $2}'`
    if [ "$status" == "UP" ];then
        date1=`date +%y%m%d-%H:%M:%S`
        echo $port1
        echo "服务健康检查结果$date1端口$port1服务启动正常"
    else
        date1=`date +%y%m%d-%H:%M:%S`
        date2=`date +"%y%m%d"`
        echo "服务健康检查不正常"
        echo "http_code: $STATUS"
        #发生报警需要邮件和企业微信告警，需要补充内容
        errlog="服务器$ip1时间$date1端口$port1服务$server1运行异常！健康检查链接: http://101.132.37.121/server/healthcheck/$date2/$port1/${ip1}/${date1}_${port1}.txt"
        echo "$errlog" >>/data/monitor/logs/port-err.log
        #对接飞书通知
        sh /data/monitor/fs_warn.sh -a $ip1 -b $server1 -c $port1
        cp /data/monitor/logs/$port1.txt /data/monitor/logs/${date1}_${port1}.txt
        mc cp --recursive /data/monitor/logs/${date1}_${port1}.txt   alarm/alarm/server/healthcheck/$date2/$port1/${ip1}/
        rm -rf /data/monitor/logs/${date1}_${port1}.txt
    fi
    sleep 10
done
