




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】nagios監(jiān)控網(wǎng)卡流量腳本
#!/bin/bash
#
#Time
:
2014-06-23
#Author
:
ftlynx
#Function
:
use
NET-SNMP
get
NIC
traffic
on
nagios.
Usage(){
echo
"Usage:
check_traffic.sh
[options]"
echo
"
-H
Host
IP."
echo
"
-P
net-snmp
community
string."
echo
"
-N
NIC
desc."
echo
"
-W
nagios
warning
value.
Format:
200,300.
200
is
in
traffic.
300
is
out
traffic.
unit:Kb.
Default:
5000,5000"
echo
"
-C
nagios
crit
value.
Reference
-W.
Default:
10000,10000"
echo
"
-V
net-snmp
version.
Default
2c."
exit
2
}
DefaultValue(){
if
[
-z
"$IP"
-o
-z
"$nicdesc"
-o
-z
"$community"
];then
echo
-e
"Error:Parameter
not
enough.\n"
Usage
fi
if
[
-z
"$warn"
];then
warn="5000,5000"
fi
if
[
-z
"$crit"
];then
crit="10000,10000"
fi
if
[
-z
"$version"
];then
version=2c
fi
}
GetResult(){
while
[
1
]
do
index=`snmpwalk
-v
$version
-c
$community
$IP
IF-MIB::ifDescr
|
grep
"${nicdesc}$"
|awk
-F
'.'
$2}'
|awk
$1}'`
if
[
$?
-ne
0
];then
echo
"Error:
snmpwalk
wrong."
exit
2
fi
if
[
-z
"$index"
];then
continue
else
break
fi
done
tempfile="/tmp/traffic.${IP}-$index"
while
[
1
]
do
if
[
-f
"$tempfile"
];then
value=`cat
$tempfile`
last_time=`echo
"$value"
|
awk
$1}'`
last_in_traffic=`echo
"$value"
|awk
$2}'`
last_out_traffic=`echo
"$value"
|awk
$3}'`
now_time=`date
+%s`
now_in_traffic=`snmpwalk
-v
$version
-c
$community
$IP
IF-MIB::ifInOctets.${index}
|awk
$NF}'`
now_out_traffic=`snmpwalk
-v
$version
-c
$community
$IP
IF-MIB::ifOutOctets.${index}
|awk
$NF}'`
if
[
-z
"$now_in_traffic"
-o
-z
"$now_out_traffic"
];then
sleep
10
continue
fi
in_traffic=$(($now_in_traffic
-
$last_in_traffic))
out_traffic=$(($now_out_traffic
-
$last_out_traffic))
second=$(($now_time
-
$last_time))
else
now_time=`date
+%s`
now_in_traffic=`snmpwalk
-v
$version
-c
$community
$IP
IF-MIB::ifInOctets.${index}
|awk
$NF}'`
now_out_traffic=`snmpwalk
-v
$version
-c
$community
$IP
IF-MIB::ifOutOctets.${index}
|awk
$NF}'`
if
[
-z
"$now_in_traffic"
-o
-z
"$now_out_traffic"
];then
sleep
10
continue
fi
in_traffic=0
out_traffic=0
fi
echo
"$now_time
$now_in_traffic
$now_out_traffic"
>
$tempfile
if
[
$?
-ne
0
];then
echo
"$tempfile
write
fail."
exit
2
fi
if
[
$in_traffic
-le
0
-o
$out_traffic
-le
0
];then
sleep
10
continue
else
in_result=$(($in_traffic
/
$second
/
1024
*
8))
out_result=$(($out_traffic
/
$second
/
1024
*
8))
break
fi
done
#warn
vaule
in_warn=`echo
$warn
|awk
-F
','
$1}'`
out_warn=`echo
$warn
|awk
-F
','
$2}'`
#crit
value
in_crit=`echo
$crit
|
awk
-F
','
$1}'`
out_crit=`echo
$crit
|
awk
-F
','
$2}'`
echo
"IN:
${in_result}Kbps[${in_warn}Kbps][${in_crit}Kbps]
OUT:
${out_result}Kbps[${out_warn}Kbps][${out_crit}Kbps]
|
IN=${in_result}Kb;
OUT=${out_result}Kb;"
if
[
$in_result
-ge
$in_crit
-o
$out_result
-ge
$out_crit
];then
exit
2
fi
if
[
$in_result
-ge
$in_warn
-o
$out_result
-ge
$out_warn
];then
exit
1
fi
exit
0
}
while
getopts
H:P:N:W:C:V:
args
do
case
$args
in
H)
IP="$OPTARG"
;;
P)
community="$OPTARG"
;;
W)
warn="$OPTARG"
;;
C)
crit="$OPTARG"
;;
V)
version="$OPTARG"
;;
N)
nicdesc="$OPTARG"
;;
?)
Usage
esac
done
DefaultValue
GetResult需要注意的地方:使用IF-MIB::ifDescr,使用網(wǎng)卡名稱來獲取網(wǎng)卡對應(yīng)的索引值,然后使用IF-MIB::ifInOctets.{index}和IF-MIB::ifOutOctets.{index}來獲取網(wǎng)卡的進(jìn)出流量。使用snmpwalk-v2c-cpublic
IF-MIB::ifDescr來查看有哪些接口,找到自己對應(yīng)的。要保證使用snmpwalk能抓取到數(shù)據(jù)。所以腳本中使用死循環(huán)來判斷。由于使用的snmp的32位計(jì)數(shù)器,所以當(dāng)達(dá)到最大值時(shí),計(jì)數(shù)器會從頭開始。這樣取兩次間隔時(shí)會出現(xiàn)負(fù)數(shù),所以腳本中有判斷兩次間隔的值一定要大于0。同時(shí)使用sleep10來延遲10秒再取值,同時(shí)這個(gè)間隔時(shí)間最好不要小于10s,因?yàn)槭褂胹nmpwalk抓取數(shù)據(jù)時(shí),間隔太小會導(dǎo)致抓取到的值是一樣的。臨時(shí)文件的權(quán)限,如果先手動運(yùn)行就會就root用戶,導(dǎo)致放在naigos中的時(shí)候不能寫臨時(shí)文件。被監(jiān)控機(jī)要安裝snmp服務(wù)。本腳本實(shí)用于linux和windows。同時(shí)滿足pnp4nagios的繪圖。當(dāng)然要自己定義模板(見下面)
本人使用:定義nagiso命令:define
command{
command_name
check_traffic
command_line
$USER1$/check_traffic.sh
-H
$HOSTADDRESS$
-P
$ARG1$
-W
$ARG2$
-C
$ARG3$
-N
$ARG4$
#ARG1
is
snmp-community;
ARG2
is
warn;
ARG3
is
crit;
ARG4
is
NIC
Name
}定義nagios服務(wù):windows的define
service{
use
XXX
host_name
XXX
service_description
wan_traffic
check_command
check_traffic!public!5000,5000!9000,9000!"Net
Device
PV
Driver
#2"
}linux的define
service{
use
XXX
host_name
XXX
service_description
wan_traffic
check_command
check_traffic!public!10000,12000!14000,14000!eth2
}pnp4nagios的模板[root@nagios
pnp4nagios]#
cat
share/templates/check_traffic.php
<?php
#
##
Copyright
(c)
2006-2010
Joerg
Linge
()
##
Plugin:
check_iftraffic.pl
(COUNTER)
#
$opt[1]
=
"--vertical-label
\"Traffic\"
-b
1024
--title
\"Interface
Traffic
for
$hostname
/
$servicedesc\"
";
$def[1]
=
"DEF:var1=$RRDFILE[1]:$DS[1]:AVERAGE
"
;
$def[1]
.=
"DEF:var2=$RRDFILE[2]:$DS[2]:AVERAGE
"
;
$def[1]
.=
"LINE1:var1#003300:\"in\"
"
;
$def[1]
.=
"GPRINT:var1:LAST:\"%7.2lf
%SKb/s
last\"
"
;
$def[1]
.=
"GPRINT:var1:AVERAGE:\"%7.2lf
%SKb/s
avg\"
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 成都工業(yè)學(xué)院《數(shù)字電視節(jié)目編導(dǎo)與制作》2023-2024學(xué)年第二學(xué)期期末試卷
- 西寧城市職業(yè)技術(shù)學(xué)院《城市傳播》2023-2024學(xué)年第二學(xué)期期末試卷
- 上海交通大學(xué)《單片機(jī)原理及其應(yīng)用》2023-2024學(xué)年第二學(xué)期期末試卷
- 內(nèi)蒙古大學(xué)《材料化學(xué)與物理》2023-2024學(xué)年第二學(xué)期期末試卷
- 西安海棠職業(yè)學(xué)院《風(fēng)景園林制圖》2023-2024學(xué)年第二學(xué)期期末試卷
- 襄陽職業(yè)技術(shù)學(xué)院《設(shè)計(jì)基礎(chǔ)(1)》2023-2024學(xué)年第二學(xué)期期末試卷
- 河南藝術(shù)職業(yè)學(xué)院《形體基訓(xùn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 隴南師范高等??茖W(xué)?!渡锇踩c實(shí)驗(yàn)室安全》2023-2024學(xué)年第二學(xué)期期末試卷
- 新鄉(xiāng)職業(yè)技術(shù)學(xué)院《醫(yī)用生物材料A》2023-2024學(xué)年第二學(xué)期期末試卷
- 2021電力工程樓梯及欄桿施工作業(yè)指導(dǎo)書
- QSB快速反應(yīng)看板
- 初中信息技術(shù)備課組工作計(jì)劃8篇
- 售后維修服務(wù)單模板
- (中職)電子技術(shù)基礎(chǔ)與技能(電子信息類)教案
- 汪小蘭有機(jī)化學(xué)課件(第四版)3
- 減少電力監(jiān)控系統(tǒng)告警信息上傳方法的研究(QC成果)
- 交易商協(xié)會非金融企業(yè)債務(wù)融資工具發(fā)行注冊工作介紹
- 《人與環(huán)境》課程教學(xué)大綱
- 班組長管理能力提升培訓(xùn)(PPT96張)課件
- 深圳市城市用地分類表
- 內(nèi)蒙古自治區(qū)小額貸款公司試點(diǎn)管理實(shí)施細(xì)則
評論
0/150
提交評論