#!/bin/awk -f
#function: nagios插件,针对每个网卡单独设定监控阀值,脚本传递参数阀值时单位为Mbps。
#example:
# NAT服务器(WAN:em1, LAN:em2)带宽:下行100Mbps上行约33.3Mbps(假设上行为下行的三分之一),报警阀值按90%计算,nagios每分钟监控一次,如下:
#接收(上行):100/3*0.9约等于30Mbps
#发送(下行):100*0.9*等于90Mbps
#监控所有网卡,nrpe配置如下:
#command[check_netcount]=/usr/local/nagios/libexec/check_netcount.awk em1 30 90 em2 90 30
#注:如果是普通单台机器,只指定一个网卡监控即可:网卡名 接收阀值 发送阀值
#
#Date:20150129 by yangjy
BEGIN {
#存放上一次数据的临时文件
last_f="/var/tmp/nagios_net.tmp"
run_f="/proc/net/dev"
#获取当前所有网卡:设备名、接收数据、发送数据
while (getline runline last_f
for (i in rundata_r) {
print i,rundata_r[i],rundata_t[i] >>last_f
}
}
#未找到临时文件,使用本次获取的各网卡接收和发送数据初始化临时文件(一般在第一次运行时执行)
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
}
}
#获取各网卡及其报警阀值
for (k=1; k= monitor_r[m] ) {
critical_r[m]
}
if (generate_t[m] >= monitor_t[m] ) {
critical_t[m]
}
#原则,只对监控的网卡产生信息和性能数据绘图
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
}
}
#如果状态数组都为0,则正常
if (length(critical_r)==0 && length(critical_t)==0 ) {
print " OK - "status" | "performance
exit 0
}
else {
#快速确认哪个网卡哪个方向传输报警,收集状态数组信息
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流量监控插件
URL链接:
http://cdweb.net/article/igpohh.html