Linux之监控微服务shell脚本

监控微服务shell脚本内容(包含邮件告警):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

### check port
check_port() {

netstat -tlpn | grep "\b$1\b"
}

### check mkdir
check_mkdir(){

if [ ! -d "/home/youcong/project/monitor/$1" ]; then
mkdir /home/youcong/project/monitor/$1
fi

}

### server check

monitor_server_register(){

if check_port $1 #端口

then

DATE_N=`date "+%Y-%m%d"`

DATE_N_F=`date "+%Y-%m%d %H:%M:%S"`

echo "server $1 online date:${DATE_N}" >> /home/youcong/project/monitor/$1/server_"${DATE_N}."log

exit 1
else


DATE_N=`date "+%Y-%m%d"`

DATE_N_F=`date "+%Y-%m%d %H:%M:%S"`


echo "server $1 offline date:${DATE_N_F}" >> /home/youcong/project/monitor/$1/server_${DATE_N}.log

echo "服务 $1 宕机 宕机日期为:${DATE_N_F} 可进入/home/youcong/project/log查看宕机时间或进入/home/youcong/project/log查看错误详情 " |mail -s "邮件告警-服务为$1 的端口宕机了" test@163.com
fi
}

#服务端口(定义一个端口数组遍历监控,可写多个,记得以空格进行分隔)
arrayIndex=(8080 8081)

for var in ${arrayIndex[@]}
do
echo $var

#检查目录是否存在
check_mkdir $var &

#检测微服务状态
monitor_server_register $var &

done

文章目录