jenkins groovy+bash脚本实现linux 机器磁盘监控,并飞书发送告警

一、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的博客-CSDN博客_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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值