Created
June 25, 2019 01:58
-
-
Save oiooj/860955b21b4311ec0e16ff85a78008f8 to your computer and use it in GitHub Desktop.
switch.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
BASE_PATH='/data/htdocs/net' | |
RRD_PATH='/data/htdocs/net/rrd' | |
FLOW_LOG="/data/htdocs/net/script/switch-ip.log" | |
community="secret" | |
function checkip { | |
dot=`echo $1 | awk -F '.' '{print NF-1}'` | |
if [ $dot -ne 3 ]; then | |
return 1 | |
fi | |
count=0 | |
for var in `echo $1 | awk -F. '{print $1, $2, $3, $4}'` | |
do | |
echo $var | grep "^[0-9]*$" >/dev/null | |
if [ $? -ne 0 ]; then | |
return 1 | |
fi | |
if [ $var -ge 0 -a $var -le 255 ] ; then | |
((count=count+1)) | |
continue | |
else | |
return 1 | |
fi | |
done | |
if [ $count -eq 4 ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function create_rrd_flow_graph() { | |
/usr/local/rrdtool/bin/rrdtool create ${RRD_PATH}/${line}/eth0.rrd --step 120 DS:traffic_in:COUNTER:600:0:1000000000 DS:traffic_out:COUNTER:600:0:1000000000 RRA:AVERAGE:0.5:1:500 RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797 RRA:MAX:0.5:1:500 RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:288:797 | |
/usr/local/rrdtool/bin/rrdtool create ${RRD_PATH}/${line}/eth1.rrd --step 120 DS:traffic_in:COUNTER:600:0:1000000000 DS:traffic_out:COUNTER:600:0:1000000000 RRA:AVERAGE:0.5:1:500 RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797 RRA:MAX:0.5:1:500 RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:288:797 | |
} | |
function create_rrd_hw_flow_graph() { | |
/usr/local/rrdtool/bin/rrdtool create ${RRD_PATH}/${line}/eth0-hw.rrd --step 120 DS:traffic_in:COUNTER:600:0:1000000000 DS:traffic_out:COUNTER:600:0:1000000000 RRA:AVERAGE:0.5:1:500 RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797 RRA:MAX:0.5:1:500 RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:288:797 RRA:HWPREDICT:3600:0.1:0.0035:720 | |
/usr/local/rrdtool/bin/rrdtool create ${RRD_PATH}/${line}/eth1-hw.rrd --step 120 DS:traffic_in:COUNTER:600:0:1000000000 DS:traffic_out:COUNTER:600:0:1000000000 RRA:AVERAGE:0.5:1:500 RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:700 RRA:AVERAGE:0.5:24:775 RRA:AVERAGE:0.5:288:797 RRA:MAX:0.5:1:500 RRA:MAX:0.5:1:600 RRA:MAX:0.5:6:700 RRA:MAX:0.5:24:775 RRA:MAX:0.5:288:797 RRA:HWPREDICT:3600:0.1:0.0035:720 | |
} | |
update_rrd_flow_graph() { | |
if [ ! -f ${RRD_PATH}/${line}/eth0.rrd ]; then | |
mkdir -p ${RRD_PATH}/${line}/ | |
create_rrd_flow_graph | |
echo "create rrd file" | |
fi | |
if [ ! -f ${RRD_PATH}/${line}/eth0-hw.rrd ]; then | |
mkdir -p ${RRD_PATH}/${line}/ | |
create_rrd_hw_flow_graph | |
echo "create rrdhw file" | |
fi | |
n=`date "+"%s""` | |
#index=$(snmpwalk -v2c -c ${community} ${line} RFC1213-MIB::ifDescr |grep eth0|cut -d '=' -f 1|cut -d '.' -f 2) | |
eth0_in=$(snmpget -v2c -c ${community} ${line} ifHCInOctets.580 | cut -d ':' -f 4 | tr -d '[:blank:]') | |
eth0_out=$(snmpget -v2c -c ${community} ${line} ifHCOutOctets.580 | cut -d ':' -f 4 | tr -d '[:blank:]') | |
/usr/local/rrdtool/bin/rrdtool updatev ${RRD_PATH}/${line}/eth0.rrd $n:${eth0_in}:${eth0_out} > /tmp/rrd.log | |
/usr/local/rrdtool/bin/rrdtool updatev ${RRD_PATH}/${line}/eth0-hw.rrd $n:${eth0_in}:${eth0_out} > /tmp/rrdhw.log | |
} | |
function create_graph() { | |
rm -f ../graph/${line}-eth0.png | |
rm -f ../graph/${line}-eth1.png | |
/usr/local/rrdtool/bin/rrdtool graph ${BASE_PATH}/graph/${line}-eth0.png \ | |
--imgformat=PNG \ | |
--start=-1200 \ | |
--base=1000 \ | |
--title=" XX网 - ${line} - eth0" \ | |
--height=135 \ | |
--width=500 \ | |
--alt-autoscale-max \ | |
--lower-limit=0 \ | |
--vertical-label='位/秒' \ | |
--slope-mode \ | |
--font TITLE:10:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--font AXIS:8:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--font LEGEND:8:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--font UNIT:8:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--watermark="XX网" \ | |
DEF:a="${RRD_PATH}/${line}/eth0.rrd":traffic_in:AVERAGE \ | |
DEF:b="${RRD_PATH}/${line}/eth0.rrd":traffic_out:AVERAGE \ | |
CDEF:cdefa=a,8,* \ | |
CDEF:cdefe=b,8,* \ | |
CDEF:cdefi=a,UN,INF,UNKN,IF \ | |
AREA:cdefa#00BF47FF:"流入" \ | |
GPRINT:cdefa:LAST:"当前\:%8.2lf %s" \ | |
GPRINT:cdefa:AVERAGE:"平均\:%8.2lf %s" \ | |
GPRINT:cdefa:MAX:"最大\:%8.2lf %s\n" \ | |
LINE1:cdefe#0D006AFF:"流出" \ | |
GPRINT:cdefe:LAST:"当前\:%8.2lf %s" \ | |
GPRINT:cdefe:AVERAGE:"平均\:%8.2lf %s" \ | |
GPRINT:cdefe:MAX:"最大\:%8.2lf %s\n" \ | |
AREA:cdefi#8F9286FF:"" > /tmp/rrd.log | |
/usr/local/rrdtool/bin/rrdtool graph ${BASE_PATH}/graph/${line}-eth1.png \ | |
--imgformat=PNG \ | |
--start=-1200 \ | |
--base=1000 \ | |
--title=" XX网 - ${line} - eth1" \ | |
--height=135 \ | |
--width=500 \ | |
--alt-autoscale-max \ | |
--lower-limit=0 \ | |
--vertical-label='位/秒' \ | |
--slope-mode \ | |
--font TITLE:10:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--font AXIS:8:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--font LEGEND:8:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--font UNIT:8:"${BASE_PATH}/font/msyhbd.ttf" \ | |
--watermark="XX网" \ | |
DEF:a="${RRD_PATH}/${line}/eth1.rrd":traffic_in:AVERAGE \ | |
DEF:b="${RRD_PATH}/${line}/eth1.rrd":traffic_out:AVERAGE \ | |
CDEF:cdefa=a,8,* \ | |
CDEF:cdefe=b,8,* \ | |
CDEF:cdefi=a,UN,INF,UNKN,IF \ | |
AREA:cdefa#00BF47FF:"流入" \ | |
GPRINT:cdefa:LAST:"当前\:%8.2lf %s" \ | |
GPRINT:cdefa:AVERAGE:"平均\:%8.2lf %s" \ | |
GPRINT:cdefa:MAX:"最大\:%8.2lf %s\n" \ | |
LINE1:cdefe#0D006AFF:"流出" \ | |
GPRINT:cdefe:LAST:"当前\:%8.2lf %s" \ | |
GPRINT:cdefe:AVERAGE:"平均\:%8.2lf %s" \ | |
GPRINT:cdefe:MAX:"最大\:%8.2lf %s\n" \ | |
AREA:cdefi#8F9286FF:"" > /tmp/rrd.log | |
} | |
while read -r line | |
do | |
echo $line | |
checkip ${line} | |
case $? in | |
1) | |
continue | |
;; | |
esac | |
update_rrd_flow_graph | |
#create_graph | |
done < $FLOW_LOG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment