【简单介绍】
这里介绍如何编写nagios脚本支持nagios图形化界面。重点在脚本的返回值的格式上。
【简单实例】
#!/bin/bash
# Determine memory usage percentage on Linux servers.
# Original write for RHEL3 for PC1 Project - jlightner 05-Jul-2005
#
# Modified for RHEL5 on mailservers.
# -Some of the escapes previously required for RHEL3's ksh not needed on RHEL5.
# -Changed comparisons to allow for decimal rather than integer values.
# jlightner 23-Jan-2009
#
# Usage: check_mem.sh WARNING CRITICAL
# Where WARNING and CRITICAL are the integer only portions of the
# percentage for the level desired.
# (i.e. 85% Warning & 95% Critical should be input only as "85 95".)
# Define Levels based on input
#
WARN=$1
CRIT=$2
# Setup standard Nagios/NRPE return codes
#
# Give full paths to commands - Nagios can't determine location otherwise
#
BC=/usr/bin/bc
GREP=/bin/grep
AWK=/bin/awk
FREE=/usr/bin/free
TAIL=/usr/bin/tail
HEAD=/usr/bin/head
# Get memory information from the "free" command - output of top two lines
# looks like:
# total used free shared buffers cached
# Mem: 8248768 6944444 1304324 0 246164 5647524
# The set command will get everything from the second line and put it into
# posiional variables $1 through $7.
#
set `$FREE |$HEAD -2 |$TAIL -1`
# Now give variable names to the positional variables we set above
#
MEMTOTAL=$2
MEMUSED=$3
MEMFREE=$4
MEMBUFFERS=$6
MEMCACHED=$7
# Do calculations based on what we got from free using the variables defined
#
REALMEMUSED=`echo $MEMUSED - $MEMBUFFERS - $MEMCACHED | $BC`
USEPCT=`echo "scale=3; $REALMEMUSED / $MEMTOTAL * 100" |$BC -l`
REALMEMUSEDmb=`echo "($REALMEMUSED)/1024" | $BC`
# Compare the Used percentage to the Warning and Critical levels input at
# command line. Issue message and set return code as appropriate for each
# level. Nagios web page will use these to determine alarm level and message.
#
#if [ `echo "5.0 > 5" |bc` -eq 1 ]
#then echo it is greater
#else echo it is not greater
#fi
if [ "`echo "$USEPCT > $CRIT" |bc`" == 1 ]
then echo "MEM CRITICAL - Memory usage = ${USEPCT}%, RealUsed=${REALMEMUSEDmb}MB |Used=${USEPCT}%;REALMEMUSED=${REALMEMUSEDmb}MB"
exit 2
elif [ "`echo "$USEPCT > $WARN" |bc`" == 1 ]
then echo "MEM WARNING - Memory usage = ${USEPCT}%, RealUsed=${REALMEMUSEDmb}MB |Used=${USEPCT}%;REALMEMUSED=${REALMEMUSEDmb}MB"
exit 1
elif [ "`echo "$USEPCT < $WARN" |bc`" == 1 ]
then echo "MEM OK - Memory usage = ${USEPCT}%, RealUsed=${REALMEMUSEDmb}MB|Used=${USEPCT}%;REALMEMUSED=${REALMEMUSEDmb}MB"
exit 0
else echo "MEM ERROR - Unable to determine memory usage"
exit 3
fi
echo "Unable to determine memory usage."
exit 3
正常情况下nagios脚本返回的值就是一个字符串变量,显示在“Status Information”列上。
为了支持pnp获取nagios的返回值,脚本返回中"|"号后的内容作为性能数据。
格式:
引用
'label'=value[UOM];[warn];[crit];[min];[max]
注意:
1. 多个性能数据之间用空格分割
2. label 可以包含任何字符
3. 如果label中包含空格、等号、或者单引号,则label需要用单引号来括起来
4. warn/crit/min/max可以为null值
value, min and max只能为负号“-” “0到9” 和小数点“.” 并且单位必须统一
例如:cpu_user=0.5%;99.9;-9;
5. 如果UOM单位是%,则min和max不需要再指定
6. UOM单位可以是如下: 默认空,表示数量(用于用户数、处理器数等)
s 表示秒(也可以用us,ms)
% 表示百分比
B 表示字节(也可以用KB,MB,TB,GB)
c 一个连续的计数(如:接口传输的字节数)
如内存监控的数据:
# /usr/local/nagios/libexec//check_mem 90 95
MEM OK - Memory usage = 18.700%, RealUsed=3003MB|Used=18.700%;REALMEMUSED=3003MB
图片显示:
【参考引用】
http://blog.chinaunix.net/uid-25266990-id-3437195.html

- 大小: 90 KB
分享到:
相关推荐
### Nagios与PnP绘图相关知识点 #### 1. Nagios简介与安装 - **Nagios** 是一个开源的系统监视工具,主要用于监控网络中的服务器、工作站、路由器等设备的状态,支持对硬件(如磁盘利用率)、软件(如进程、端口...
4. **自动配置**:PNP4Nagios支持自动配置,当Nagios添加新的监控对象时,PNP4Nagios会自动创建相应的RRD文件,无需手动干预。 5. **性能阈值**:PNP4Nagios还能帮助用户设置性能阈值,一旦超过设定值,系统会触发...
9. 绘图功能相关的库:如pnp、cairo、pango、libart_lgpl等,用于生成性能数据图表。 10. 邮件服务和DNS服务:在安装CentOS时需要定制安装,用于发送报警邮件。 11. Fetion库:实现飞信报警功能。 12. libACE和MSN库...
本章节将详细介绍如何在RHEL6系统上搭建Icinga监控服务器,并结合PnP4Nagios插件进行性能数据的收集与展示。 ##### 2.1 环境准备 - **操作系统**: RHEL6 - **软件包**: - freetype - libjpeg - libpng - ...
此外,脚本还考虑了Nagios的报警格式和性能数据输出,这使得它能够无缝集成到Nagios监控系统中,支持pnp绘图,便于直观展示命中率变化趋势。为了方便使用,脚本中包含了一些通用函数,可以直接复制粘贴到其他脚本中...