查看pod是否正常打印日志,并发送webhook到企微

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
#!/bin/sh
# 获取当前UTC时间
utc_now=`date -u`
# 将时间转换为timestamp
timestamp_now=`date -d "$utc_now" +%s`
PODNAME=
NAMASPACE=
function restart_pod() {
for i in `kubectl get pod -n iot|grep $PODNAME|awk '{print $1}'`;do
for time in `kubectl logs --tail=1 --timestamps $i -n $NAMASPACE | awk '{print $1}'`;do
timestamp_pod=`date -d "$time" +%s`
delay=$(($timestamp_now-$timestamp_pod))
echo $i:$delay
if [ "$delay" -ge "30" ];then
echo "Pod $i 30S内没有最新日志产生,重启pod!"
echo $i
kubectl delete pod $i -n $NAMASPACE --force --grace-period=0
curlwxFunc "Pod $i 30S内没有最新日志产生,重启pod!(生产环境)"
fi
done
done
}
function curlwxFunc() {
JSON='{
"msgtype": "text",
"text": {
"content": "'$1'"
}
}'
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx' \
-H 'Content-Type:application/json' \
-d "${JSON}"
}
restart_pod