一、jenkins配置
使用多文本参数传递的方式,将需要监控的ip传入脚本中
node机器选择:
二、groovy脚本
脚本式pipeline,并发执行192开头的ip磁盘监控以及172开头的磁盘监控
node(env.Node){
def ips_192 = env.ips_192.split('\n')
def ips_172 = env.ips_172.split('\n')
sh """
cd /root/
rm -rf jenkinsfile
git clone -b zongquan_monitor https://github.mthreads.com/mthreads/jenkinsfile.git
"""
def test_parallel = [:]
test_parallel["192"]={
stage("192_monitor"){
for(int i = 0;i<ips_192.length;i++){
ip = ips_192[i]
sh """
cd /root/jenkinsfile/gfx_piplines/scripts
chmod 755 monitor_disk.sh
bash monitor_disk.sh "${ip}"
"""
}
}
}
test_parallel["172"]={
stage("172_monitor"){
for(int j = 0;j<ips_172.length;j++){
ip = ips_172[j]
sh """
cd /root/jenkinsfile/gfx_piplines/scripts
chmod 755 monitor_disk.sh
bash monitor_disk.sh "${ip}"
"""
}
}
}
parallel test_parallel
}
三、monitor_disk.sh的bash 脚本编写
飞书机器人发送消息请看链接:Jenkins + 飞书发送消息通知_Shafir的博客-优快云博客_jenkins 飞书
注意替换脚本中的api
ip=$1
if [[ $ip =~ 192 ]]
then
SPACE_NAME=$(sshpass -p 123456 ssh root@$ip -o StrictHostKeyChecking=no "df -Ph |grep -v -E 'tmpfs|dev/loop|overlay' | awk '{print (\$1)}' ")
for i in $SPACE_NAME
do
SPACE_SIZE=$(sshpass -p 123456 ssh root@$ip -o StrictHostKeyChecking=no "df -Ph |grep $i | awk '{print int(\$5)}' ")
if [[ $SPACE_SIZE -ge 85 ]]
then
api=https://open.feishu.cn/open-apis/bot/v2/hook/8888********
#content=\"$ip磁盘的\"$i\"卷使用率为$SPACE_NAME%,已超过85%请及时处理\"
curl -X POST \
$api \
-H 'Content-Type: application/json' \
-d "{\"msg_type\": \"post\",\"content\": {\"post\": {\"zh_cn\": {\"title\": \"内存监控告警\",\"content\": [[{\"tag\": \"text\",\"un_escape\": true,\"text\": \"$ip磁盘的$i卷使用率为$SPACE_SIZE%,已超过85%请及时处理\"}],[]]}}}}"
fi
done
elif [[ $ip =~ 172 ]]
then
SPACE_NAME=$(sshpass -p jenkins123 ssh jenkins@$ip -o StrictHostKeyChecking=no "df -Ph |grep -v -E 'tmpfs|dev/loop|overlay' | awk '{print (\$1)}' ")
for i in $SPACE_NAME
do
SPACE_SIZE=$(sshpass -p jenkins123 ssh jenkins@$ip -o StrictHostKeyChecking=no "df -Ph |grep $i | awk '{print int(\$5)}' ")
if [[ $SPACE_SIZE -ge 85 ]]
then
api=https://open.feishu.cn/open-apis/bot/v2/hook/8888********
#content="$ip磁盘的\"$i\"卷使用率为$SPACE_NAME%,已超过85%请及时处理"
curl -X POST \
$api \
-H 'Content-Type: application/json' \
-d "{\"msg_type\": \"post\",\"content\": {\"post\": {\"zh_cn\": {\"title\": \"内存监控告警\",\"content\": [[{\"tag\": \"text\",\"un_escape\": true,\"text\": \"$ip磁盘的$i卷使用率为$SPACE_SIZE%,已超过85%请及时处理\"}],[]]}}}}"
fi
done
else
echo "未匹配到ip"
fi