#!/bin/awk -f
#function: nagios插件,針對(duì)每個(gè)網(wǎng)卡單獨(dú)設(shè)定監(jiān)控閥值,腳本傳遞參數(shù)閥值時(shí)單位為Mbps。
#example:
# NAT服務(wù)器(WAN:em1, LAN:em2)帶寬:下行100Mbps上行約33.3Mbps(假設(shè)上行為下行的三分之一),報(bào)警閥值按90%計(jì)算,nagios每分鐘監(jiān)控一次,如下:
#接收(上行):100/3*0.9約等于30Mbps
#發(fā)送(下行):100*0.9*等于90Mbps
#監(jiān)控所有網(wǎng)卡,nrpe配置如下:
#command[check_netcount]=/usr/local/nagios/libexec/check_netcount.awk em1 30 90 em2 90 30
#注:如果是普通單臺(tái)機(jī)器,只指定一個(gè)網(wǎng)卡監(jiān)控即可:網(wǎng)卡名 接收閥值 發(fā)送閥值
#
#Date:20150129 by yangjy
BEGIN {
#存放上一次數(shù)據(jù)的臨時(shí)文件
last_f="/var/tmp/nagios_net.tmp"
run_f="/proc/net/dev"
#獲取當(dāng)前所有網(wǎng)卡:設(shè)備名、接收數(shù)據(jù)、發(fā)送數(shù)據(jù)
while (getline runline last_f
for (i in rundata_r) {
print i,rundata_r[i],rundata_t[i] >>last_f
}
}
#未找到臨時(shí)文件,使用本次獲取的各網(wǎng)卡接收和發(fā)送數(shù)據(jù)初始化臨時(shí)文件(一般在第一次運(yùn)行時(shí)執(zhí)行)
else {
if (system("touch "last_f) == 0) {
for (j in rundata_r) {
print j,rundata_r[j],rundata_t[j] >>last_f
lastdata_r[j]=rundata_r[j]
lastdata_t[j]=rundata_t[j]
}
}
else {
print "CRITICAL - cat not create file:",last_f"."
exit 2
}
}
#獲取各網(wǎng)卡及其報(bào)警閥值
for (k=1; k= monitor_r[m] ) {
critical_r[m]
}
if (generate_t[m] >= monitor_t[m] ) {
critical_t[m]
}
#原則,只對(duì)監(jiān)控的網(wǎng)卡產(chǎn)生信息和性能數(shù)據(jù)繪圖
status=status"__"m"("monitor_r[m]","monitor_t[m]"):R="generate_r[m]"Mbps,T="generate_t[m]"Mbps"
sub(/^__/,"",status)
performance=performance""m"_r="generate_r[m]";"monitor_r[m]"; "m"_t="generate_t[m]";"monitor_t[m]"; "
}
else {
print "CRITICAL - wrong dev name."
exit 2
}
}
#如果狀態(tài)數(shù)組都為0,則正常
if (length(critical_r)==0 && length(critical_t)==0 ) {
print " OK - "status" | "performance
exit 0
}
else {
#快速確認(rèn)哪個(gè)網(wǎng)卡哪個(gè)方向傳輸報(bào)警,收集狀態(tài)數(shù)組信息
for (n in critical_r) {
problem_r=problem_r" "n
sub(/^ /,"",problem_r)
}
for (o in critical_t) {
problem_t=problem_t" "o
sub(/^ /,"",problem_t)
}
print " CRITICAL - Problem(R:"problem_r", T:"problem_t") - "status" | "performance
exit 2
}
}
本文題目:awk:Nagios流量監(jiān)控插件
本文來(lái)源:
http://weahome.cn/article/igpohh.html