這幾天學習了Nagios的plugin寫法,先寫了個簡單的例子: 相似主機netstat命令的監控,能夠監控連接的數量.
ios
學習筆記,以備查閱git
@Author duangrshell
@Website http://my.oschina.net/duangr/blog/185080app
nagios的插件都部署在 ${nagios_home}/libexec 路徑下面,能夠發現下面有不少的可執行腳本
ide
也能夠開發本身的腳本文件,只要返回碼符合以下要求便可:學習
# declare return code RTN_OK=0 RTN_WARNING=1 RTN_CRITICAL=2 RTN_UNKNOWN=3
因爲我對shell還算是瞭解一些,所以準備用shell來編寫netstat的擴展插件.ui
http://git.oschina.net/duangr/duangr-nagios-plugin
this
代碼放在 Git@OSC 上面了,以前對git不太瞭解,又先學習了半天.
spa
簡要思路:.net
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