钉钉定时发送值班人员

需求

每天运维人员都需要去做些基础服务,就需要值班人员去轮班解决,现在需要写一个定时发送值班人员的脚本

前提

需要自己在钉钉群,申请个机器人,申请过程这里不赘述了,下面是脚本

脚本

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
60
61
62
63
64
65
66
67
68
69
[root@Ops-script dingding]# mkdir /home/monitor/dingding
[root@Ops-script dingding]# touch groupkey helpkey

[root@Ops-script dingding]# cat send_dingding.sh
#!/bin/bash
groupfiles="/home/monitor/dingding/groupkey"
helpfiles="/home/monitor/dingding/helpkey"
Date=`date +%Y-%m-%d\ %H:%M:%S`
url="https://oapi.dingtalk.com/robot/send?access_token=c2123f81820fccfadfc47bbd629d26e7613ae49f1a053edc6e81f5864c550e30"
group=("a:xx;"
"b:xx;"
"c:xx;")
opshelp=("a:xx;"
"b:xx;"
"c:xx;")
groupkey=`sed -n "1p" $groupfiles`
helpkeys=`awk 'NR==1{print $1}' $helpfiles`
helpkey=`awk 'NR==1{print $2}' $helpfiles`
# 每日值班人
for crew in ${group[@]};do
if echo $crew | grep -q $groupkey ;then
values=`echo $crew | awk -F':' '{print $2}'`
onduty_mess="今日运维值班人: [ $values ]"
fi
done
# 修改缓存文件内的运维值班人员 key , 使得下次人员自动更换
if [ $groupkey == "a" ];then
echo "b" > $groupfiles
elif [ $groupkey == "b" ];then
echo "c" > $groupfiles
elif [ $groupkey == "c" ];then
echo "a" > $groupfiles
fi


# 修改缓存文件内的运维上线人员 key , 使得下次人员自动更换
if [ $helpkeys == 7 ];then
helpsum=1
else
helpsum=$(($helpkeys+1))
fi
if [ $helpsum == 1 ];then
if [ $helpkey == "a" ];then
echo "$helpsum b" > $helpfiles
elif [ $helpkey == "b" ];then
echo "$helpsum c" > $helpfiles
elif [ $helpkey == "c" ];then
echo "$helpsum a" > $helpfiles
fi
else
echo "$helpsum $helpkey" > $helpfiles
fi
# 每周支持上线人
for opsdit in ${opshelp[@]};do
if [ $helpsum == 1 ];then
helpkey=`awk 'NR==1{print $2}' $helpfiles`
fi
if echo $opsdit | grep -q $helpkey ;then
helpvalues=`echo $opsdit | awk -F':' '{print $2}'`
help_mess="本周版本上线运维支持: [ $helpvalues ]"
fi
done
curl -XPOST -s -L -H "Content-Type:application/json" -H "charset:utf-8" $url -d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \"大家好~\n$onduty_mess\n$help_mess\"
}
}"
Donate