欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

一个ping检测告警函数代码

程序员文章站 2023-11-02 12:50:28
复制代码 代码如下:#!/bin/bash ping_monitor()    {    host_=...
复制代码 代码如下:

#!/bin/bash
 ping_monitor()
    {
    host_=(203 204 205 207 208 209 210 212 213 214 215 216 217 218 219 220 221)
    ping_count=3
    called=(13000000001 13000000002 13000000003)
    calling=13000000000
    callop=1310000000
    #main------------------------------------------------------------
    echo "`date "+%y%m%d %h:%m:%s"`----->脚本开始执行......">>ping_log
    for host_1 in ${host_[*]};do
       host=192.168.1.$host_1
       echo "-------->开始检测$host服务器通迅是否正常,ping次数$count"
       ping_result=`ping $host -c $ping_count |tail -2 |head -1`
       #sleep 1
       echo "-------->服务器$host检测已完成"
       #取成功条数
       succ_ping=`echo ${ping_result:23:1}`
       #取失败百分比
       loss_ping=`echo ${ping_result:46:4}`
          if [ $succ_ping -eq $ping_count ];then
             echo "本次检测结果---->$host服务器ping检测正常"
             echo "`date "+%y%m%d %h:%m:%s"`--->$host服务器ping检测正常" >>ping_log
          else
             echo "本次检测结果---->$host服务器有丢包现象,丢包率为:$loss_ping"
             echo "`date "+%y%m%d %h:%m:%s"`--->$host服务器有丢包现象,丢包率为:$loss_ping" >>ping_err_log
             #插入短信表—smssend00
             for called in ${telno[*]};do
    mysql -h$dbhost -u$dbuser -p$dbpass -d$dbbase <<getrecode
        insert into $dbtable (servercode,smsservercode,groupid,priority,calling,called,payer,orderdatetime,senddatetime,effectstarttime,
    effectendtime,effectstarttime2,effectendtime2,invaliddatetime,feevalue,iffee,message,hideflag,msgtype,smsfeetype,ifsmsreport,service
    type,morelatetomtflag,reserve,optempid,optuserid,clientprogramtype,optcode,optip,remark)
      values
    (2,2,1,0,'$calling','$called','$callop',now(),now(),'00:00:00','23:59:59',null,null,null,0,1,'服务器告警!!$host服务器检测到有丢包现像,丢包率:$loss_ping',0,1,2,0,'2_2',null,null,1,668,1,3001,'192.168.1.182',null)
    getrecode
              done
           fi
    done
       echo "`date "+%y%m%d %h:%m:%s"`----->脚本运行完毕......">>ping_log
    }