我的博客已迁移到xdoujiang.com请去那边和我交流
需求:获取交换机当前使用带宽 如果超过阀值就发短消息报警 
主要利用公司的短信接口和snmpwalk命令来实现

一、基础环境
1、版本
cat /etc/debian_version 
7.8

2、内核
uname -r
3.2.0-4-amd64

3、ip(eth0)
10.1.10.185

二、具体脚本
#!/bin/bash
#--------------------------------------------------
#Author:jimmygong
#Email:jimmygong@taomee.com
#FileName:shcnc.sh
#Function:获取交换机带宽超过阀值就发短消息 
#Version:1.0 
#Created:2015-11-12
#--------------------------------------------------
#apt-get -y install snmp curl links or yum -y install curl links net-snmp-utils
function get ()
{
    hostip='111.111.111.111'
    snmpc="222222222222"
    hostport="1111"
    timed="15"
    in1=$(snmpwalk -Ov -v 2c -c $snmpc $hostip IF-MIB::ifHCInOctets.$hostport)
    in1=${in1#*: }
    out1=$(snmpwalk -Ov -v 2c -c $snmpc $hostip IF-MIB::ifHCOutOctets.$hostport)
    out1=${out1#*: }
    sleep $timed
    in2=$(snmpwalk -Ov -v 2c -c $snmpc $hostip IF-MIB::ifHCInOctets.$hostport)
    in2=${in2#*: }
    out2=$(snmpwalk -Ov -v 2c -c $snmpc $hostip IF-MIB::ifHCOutOctets.$hostport)
    out2=${out2#*: }
    in=$((($in2-$in1)*8/1000000/$timed))
    out=$((($out2-$out1)*8/1000000/$timed))
    echo $in:$out
}
function send () 
{
    g=$(get)
    i=${g%:*}
    e=${g#*:}
    c=95
    telgroup=(11111111111)
    sign="222222222222222"
    if [[ $i -gt $c ]]
    then
        msg="shcnc%20ingress%20rate:%20${i}mbps%20>%20$c"
        for tt in ${telgroup[@]}
        do
            /usr/bin/links -dump "http://111.111.111.111/aaa/back.send?mobile=$tt&msg=$msg&sms_sign=$sign"
        done
    fi
    if [[ $e -gt $c ]]
    then
        msg="shcnc%20egress%20rate:%20${e}mbps%20>%20$c"
        for tt in ${telgroup[@]}
        do
            /usr/bin/curl "http://111.111.111.111/aaa/back.send?mobile=$tt&msg=$msg&sms_sign=$sign"
        done
    fi
}
send
exit 0