需求是我們需要對伺服器上的流量進行監控,網路上有個流傳的check_traffic.sh,它需要被監控機開啟snmp。但是感覺都使用上了nagios還要開snmp。。。有點斧子剪刀一起用的感覺,所以就動手寫了個監控流量的shell:
#!/bin/shusage()
foundw=0;
foundc=0;
foundn=0;
for item in $@ ; do
if [[ $foundn == 1 ]]; then
n=$item;
foundn=2;
continue;
fiif [[ $foundw == 1 ]]; then
w1=$item;
foundw=2;
continue;
fiif [[ $foundw == 2 ]]; then
w2=$item;
foundw=3;
continue;
fiif [[ $foundc == 1 ]]; then
c1=$item;
foundc=2;
continue;
fiif [[ $foundc == 2 ]]; then
c2=$item;
foundc=2;
continue;
fiif [[ "$item" == "-w" ]]; then
foundw=1;
continue;
fiif [[ "$item" == "-c" ]]; then
foundc=1;
continue;
fiif [[ "$item" == "-n" ]]; then
foundn=1;
continue;
fidone
if [ -z "$" ] || [ -z "$" ] || [ -z "$" ] || [ -z "$" ] || [ -z "$" ]; then
usage
fir1=`cat /sys/class/net/$n/statistics/rx_bytes`
t1=`cat /sys/class/net/$n/statistics/tx_bytes`
sleep 1
r2=`cat /sys/class/net/$n/statistics/rx_bytes`
t2=`cat /sys/class/net/$n/statistics/tx_bytes`
tbps=`expr $t2 - $t1`
rbps=`expr $r2 - $r1`
tmbps=`expr $tbps / 1024 / 128`
rmbps=`expr $rbps / 1024 / 128`
if [[ $tmbps -ge $c1 ]] || [[ $rmbps -ge $c2 ]] ; then
echo "critical - current is $, $";
exit 2;
fiif [[ $tmbps -ge $w1 ]] || [[ $rmbps -ge $w2 ]] ; then
echo "warning - current is $, $";
exit 1;
fiecho "ok - current is $, $";
exit 0;
其中的w和c的數值單位都是mb。
nagios被監控端nrpe新增流量監控
wget wget wget 在這裡感謝 石頭編寫的指令碼。2.配置snmp 我這裡是用centos 的yum安裝的 yum y install net snmp 修改 etc snmp snmp.conf 改為 改為 去掉前面的注釋 改完儲存退出 server snmpd restart 2.安裝...
網絡卡流量監控指令碼
sar n dev 1 2 sar命令包含在sysstat工具包中,提供系統的眾多統計資料。命令後面1 2 意思是 每一秒鐘取1次值,取2次。dev顯示網路介面資訊 另外,n引數很有用,他有6個不同的開關 dev edev nfs nfsd sock all 其代表的含義如下 dev顯示網路介面資訊...
實時流量監控指令碼
bin bash ethn 1 while true do rx pre cat proc net dev grep ethn sed s g awk tx pre cat proc net dev grep ethn sed s g awk sleep 1 rx next cat proc net...