前言
这几天学习了Nagios的plugin写法,先写了个简单的例子: 类似主机netstat命令的监控,可以监控链接的数量.
学习笔记,以备查阅
@Author duangr
@Website http://my.oschina.net/duangr/blog/185080
1. plugin机制
nagios的插件都部署在 ${nagios_home}/libexec 路径下面,可以发现下面有很多的可执行脚本
也可以开发自己的脚本文件,只要返回码符合如下要求即可:
# declare return code
RTN_OK=0
RTN_WARNING=1
RTN_CRITICAL=2
RTN_UNKNOWN=3
由于我对shell还算是了解一些,因此准备用shell来编写netstat的扩展插件.
2. 代码获取
http://git.oschina.net/duangr/duangr-nagios-plugin
代码放在 Git@OSC 上面了,之前对git不太了解,又先学习了半天.
3. 功能介绍
check_netstat
简要思路:
netstat 命令可以监控主机的链接情况,因此对netstat的结果列表 增加 端口、状态 的过滤, 并将过滤后的数量统计出来.
命令中对结果数量可以定制WARNING和CRITICAL的报警阈值,并且参考了check_procs的范围阈值定义的思路.
check_netstat 1.0 @duangr
Checks all network connects and generates WARNING or CRITICAL states if the specified
metric is outside the required threshold ranges. The metric defaults to number
of connects. Search filters can be applied to limit the connects to check.
Usage: check_netstat -w limit -c limit [-p Port] [-s State]
Options:
-h, --help
Print detailed help screen
-V, --version
Print version information
-w
Generate warning state if metric is outside this range
-c
Generate critical state if metric is outside this range
-p
Port Num for filtering
-s
State for filtering (e.g. ESTABLISHED LISTEN TIME_WAIT)
Examples:
check_netstat -w 2 -c 4 -p 22 -s ESTABLISHED
Critical if > 4 ESTABLISHED connects with port 22
Warning if > 2 ESTABLISHED connects with port 22
check_netstat -w 2:10 -c :20 -p 80 -s ESTABLISHED
Critical if > 20 ESTABLISHED connects with port 80
Warning if < 2 or > 10 ESTABLISHED connects with port 80
check_netstat -c 1:1 -p 80 -s LISTEN
Critical if not one LISTEN connects with port 80
4.相关链接
来源:oschina
链接:https://my.oschina.net/u/1189023/blog/185080