MRTG交换机各端口当前流量排序scripts
时间:2005-01-31 来源:atom
前言:
脚本在FreeBsd4.10上测试完成。于原文上的相关脚本(用于LINUX)做了相应修改。
参考:
http://www.net130.com/2004/6-22/224959.html
――使用MRTG监控网络信息流量
目 录
排序脚本(加到/etc/crontab中) - 2 -
附件:通过建立bash脚本自动化配置mrtg.cfg文件的步骤__ - 5 -
1.建立/etc/mrtg/mrtgip(权限为600)__ - 5 -
2.建立文件/etc/mrgt/mrtglocate(权限为600)__ - 5 -
3.建立bash脚本文件__ - 6 -
4.建立/usr/local/www/data/mrtg/index.html文件__ - 7 -
5.为Web服务器建立密码__ - 8 -
排序脚本(加到/etc/crontab中)
#!/bin/sh
# MRTGSORT V1.01
#Last Edition by Thomas 2005-2-4
#In /etc/crontab, Just add to the end of MRTG sentence in /etc/crontab, example:
#*/5 * * * * root /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.cfg;/usr/local/etc/mrtg/mrtgsort.sh
#
#START....
#
>/tmp/tmp1
>/tmp/tmp2
>/tmp/tmp3
mrtgdir=/usr/local/www/data/mrtg
ls $mrtgdir/1/192*html>/tmp/tmp1
ls $mrtgdir/2/192*html>>/tmp/tmp1
for i in `cat /tmp/tmp1`
do
more $i|grep TITLE>>/tmp/tmp2
more $i|grep %
|cut -f4 -f5 -d" "
|awk 'NR==3{print}NR==6{print}'
|cut -f3 -d">" >>/tmp/tmp2
done
more /tmp/tmp2 | awk 'NR%3==1{printf("<tr><td> %s </td>",$0)}
NR%3==2{printf("<td> %s </td>",$0)}
NR%3==0{printf("<td> %s </td></tr> ",$0)}'
|sed -e 's/<TITLE>//g' -e 's:</TITLE>::g' >/tmp/tmp3
echo -n '<HTML>
<HEAD>
<META content="TEXT/html; charset=gb2312" http-equiv=Content-Type>
<META http-equiv=refresh content=300>
</HEAD>
<BODY>
<p align=center><b>Sorted by current Input. (Unit KB) </b></p>
<table cellSpacing=0 cellPadding=1 width=600 align=center border=1>
<tr height=30 bgcolor=#FF9900>
<td >
<p>SwitchName&Port</p>
</td>
<td >
<p>InputSize</p>
</td>
<td >
<p>OutputSize</p>
</td>
</tr>
' >/usr/local/www/data/mrtg/1.html
echo -n '<HTML>
<HEAD>
<META content="TEXT/html; charset=gb2312" http-equiv=Content-Type>
<META http-equiv=refresh content=300>
</HEAD>
<BODY>
<p align=center><b>Sorted by current Output. (Unit KB) </b></p>
<table cellSpacing=0 cellPadding=1 width=600 align=center border=1>
<tr height=30 bgcolor=#00FFFF>
<td >
<p>SwitchName&Port</p>
</td>
<td >
<p>InputSize</p>
</td>
<td >
<p>OutputSize</p>
</td>
</tr>
' >/usr/local/www/data/mrtg/2.html
#Sort by Input bytes
more /tmp/tmp3 | awk '{if ($7~/Mb.*/) print}'|sort -t " " -rn +5 >>/usr/local/www/data/mrtg/1.html
more /tmp/tmp3 | awk '{if ($7~/kb.*/) print}'|sort -t " " -rn +5 >>/usr/local/www/data/mrtg/1.html
more /tmp/tmp3 | awk '{if ($7!~/Mb.*/ && $7!~/kb.*/) print}'|sort -t " " -rn +5>>/usr/local/www/data/mrtg/1.html
#Sort by Output bytes
more /tmp/tmp3 | awk '{if ($10~/Mb.*/) print}'|sort -t " " -rn +8 >>/usr/local/www/data/mrtg/2.html
more /tmp/tmp3 | awk '{if ($10~/kb.*/) print}'|sort -t " " -rn +8 >>/usr/local/www/data/mrtg/2.html
more /tmp/tmp3 | awk '{if ($10!~/Mb.*/ && $10!~/kb.*/) print}'|sort -t " " -rn +8 >>/usr/local/www/data/mrtg/2.html
echo -n " </table>
</BODY>
</HTML>
">>/usr/local/www/data/mrtg/1.html
echo -n " </table>
</BODY>
</HTML>
">>/usr/local/www/data/mrtg/2.html
####The End of the scripts#####
附件:通过建立bash脚本自动化配置mrtg.cfg文件的步骤
1.建立/etc/mrtg/mrtgip(权限为600)
文件/etc/mrgt/mrtgip的格式如下:
建立HTML的目录|监测交换机的IP地址|Community串|交换机的安装地点与说明|
示例如下:
1|192.168.27.101|gamabjoff48|switch101|
2|192.168.27.103|gamabjoff48|switch103|
2.建立文件/etc/mrgt/mrtglocate(权限为600)
文件/etc/mrgt/mrtglocate的格式如下:
监测交换机的IP地址|端口号|该端口所连接的计算机设备的安装地点与说明|
示例如下:
#交换机(192.168.27.101)
...
192.168.27.101|1|switch101|
192.168.27.101|2|switch101|
。。。。。。。
192.168.27.101|47|switch103|
192.168.27.101|48|switch103|...
#交换机(192.168.27.103)
。。。。。。。。。
192.168.27.103|47|switch103|
192.168.27.103|48|switch103|...
开头为#的行为注释行,如果一些端口没有联接计算机设备,可以注释,也可适当加入一些空行,使用 | 作为字段的分隔符。在加入文字说明的同时也可以加入符号,如F5P01-V2,含义如下:F5表示办公楼5楼,P01表示顺序号,V2表示该端口的VLAN为2。还可以按照自己的意愿加入一些易于理解的文字说明。
3.建立bash脚本文件
#!/bin/sh
# crmrtg.sh for FreeBsd4.10
# an script of create mrtg.cfg & index.html file
#
htmldir=/usr/local/www/data/mrtg
conf=/usr/local/etc/mrtg
tmp1=/tmp/mrtg1.tmp
tmp2=/tmp/mrtg2.tmp
tmpmrtg=/tmp/mrtg.cfg
rm -f $conf/mrtg.cfg $tmp1 $tmp2 $tmpmrtg
for i in `cat $conf/mrtgip`
do
dir=`echo $i | cut -f1 -d"|"`
ip=`echo $i | cut -f2 -d"|"`
community=`echo $i|cut -f3 -d"|"`
locate=`echo $i | cut -f4 -d"|"`
echo "create $ip at $locate "
mkdir -p $htmldir/$dir
cfgmaker --global "WorkDir: $htmldir/$dir"
--global "icondir: /mrtg"
--global "language: chinese"
--show-op-down --ifref=nr --ifdesc=descr
--global "Options[_]: growright,bits"
--output=$htmldir/$dir/mrtg$dir $community@$ip 2>/dev/null
cfgmaker --global "WorkDir: $htmldir"
--global "Options[_]: growright,bits"
--global "directory[_]: $dir"
--global "icondir: /mrtg"
--global "language: chinese"
--show-op-down --ifref=nr --ifdesc=descr
--output=$tmpmrtg $community@$ip 2>/dev/null
# create ports & describe file for modify mrtg.cfg configuration
grep "^$ip|" $conf/mrtglocate >| $tmp1
for j in `cat $tmp1`
do
port=`echo $j | cut -f2 -d"|"`
describe=`echo $j | cut -f3 -d"|"`
#PageTop[192.168.1.6_2]: <H1>BayStack 450-12T - 2 -- computer</H1>
echo "s|^PageTop[$ip\_$port]: <H1>|&$describe |" >> $tmp2
done
sed -f $tmp2 $htmldir/$dir/mrtg$dir >| $htmldir/$dir/mrtg$dir.cfg
sed -f $tmp2 $tmpmrtg >> $conf/mrtg.cfg
indexmaker --title="$locate $ip" --output=$htmldir/$dir/index.html
$htmldir/$dir/mrtg$dir.cfg
rm -f $htmldir/$dir/mrtg$dir $tmp1 $tmp2 $tmpmrtg
done
#The End.
4.建立/usr/local/www/data/mrtg/index.html文件
HTML代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>Switch Traffic Information </TITLE>
</HEAD>
<!-- Background white, links blue .unvisited., navy .visited., red .active. -->
<BODY BGCOLOR="#FFFFFF">
<P ALIGN="center">
Switch Traffic Information
</P>
<P ALIGN="cent