Nagios监控HP-UX

来源:互联网 发布:网络剧尸语者 编辑:程序博客网 时间:2024/05/01 12:03

本文转自http://www.toxingwang.com/management/1100.html

本文讲解如何在HP-UX下安装nrpe软件并实现Nagios监控。

1、准备NRPE for HP-UX软件和用户:

1.1 准备软件

可以到http://www.mayoxide.com/naghpux/下载

我实际使用中由于存在多台HP-UX,因此都是统一从Nagios服务器拷贝至被监控端的,命令如下:

# scp /var/ftp/nagios/NRPE-2.12.depot HP-UX_IP:/tmp

如果服务器很多,可以将上述命令写成脚本,实现批量推送。

1.2 建立NRPE用户:

groupadd -g 312 nrpe
useradd -g nrpe -u 312 nrpe

##指定GID和UID为312是因为后面的配置脚本中默认是这样指定的,我这里就不做修改

2、在HP-UX上安装NRPE:

2.1 HP-UX的软件包格式为depot,使用swinstall命令安装:

# swinstall -s /tmp/NRPE-2.12.depot

2.2 默认会弹出swinstall命令的使用提示,按任意键继续:

2.3 使用空格键选中NRPE包,并使用tab键切换至菜单“Actions”,然后选择“Mark For Install”

按回车后,NRPE包前方会有Yes标记:

2.4 再次按tab键,切换到菜单“Actions”,然后选择“Install”进行安装:

首先会对安装程序进行分析,通过后选中“OK”,进入正式安装:

安装完成后,选中“done”,后按回车键,然后使用tab键选择“File”菜单的"exit"退出:

检查安装情况,默认nrpe会被安装到/opt/nrpe:

root@rzcs:/#ls /opt/nrpe
bin etc libexec

3、配置nrpe:

3.1 配置nrpe主配置文件/opt/nrpe/etc/nrpe.conf:

vi /opt/nrpe/etc/nrpe.cfg ##修改如下两行
server_address=127.0.0.1 Nagios_Server_IP
allowed_hosts=127.0.0.1 Nagios_Server_IP
##其他可以暂时保持默认,注意底部有监控命令的配置,如果是自写脚本,则需要配置:

……中间省略……

command[check_users]=/opt/nrpe/libexec/check_users -w 5 -c 10
command[check_load]=/opt/nrpe/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_zombie_procs]=/opt/nrpe/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/opt/nrpe/libexec/check_procs -w 1500 -c 2000
command[check_hpux_disk]=/opt/nrpe/libexec/check_disk -w 20 -c 10
command[check_free_mem]=/opt/nrpe/libexec/check_mem.pl -f -w 10 -c 5   ##自写脚本,后面会贴出脚本内容

3.2 将NRPE配置为inetd管理的进程:

NRPE自带有配置脚本,只需执行下该脚本即可

/opt/nrpe/bin/configure.sh
##该脚本会将nrpe相关配置写入/etc/inetd.conf 和/etc/services,与Linux下nrpe启动配置一样

4、编写内存监控脚本:

NRPE自带有大量监控插件的,此处于linux下需要单独安装插件不同。但自带的插件不能监控内存,因此我借鉴网上别人脚本,再根据实际情况做了些调整:

#!/usr/bin/perl -w
#by barlow修改
#use strict;
use Getopt::Std;

use vars qw($opt_c $opt_f $opt_u $opt_w
$free_memory $used_memory $total_memory
$crit_level $warn_level
%exit_codes @memlist
$percent $fmt_pct
$verb_err $command_line);

# Predefined exit codes for Nagios
%exit_codes = ('UNKNOWN' ,-1,
'OK' , 0,
'WARNING' , 1,
'CRITICAL', 2,);

#
$verb_err = 0;
#注意命令需要全路径,且该命令需要root权限
$command_line = `/usr/sbin/swapinfo | tail -1 | awk '{print \$3,\$4}'`;

chomp $command_line;
@memlist = split(/ /, $command_line);

# Define the calculating scalars
$used_memory = $memlist[0];
$free_memory = $memlist[1];
$total_memory = $used_memory + $free_memory;

# Get the options
if ($#ARGV le 0)
{
&usage;
}
else
{
getopts('c:fuw:');
}

# Shortcircuit the switches
if (!$opt_w or $opt_w == 0 or !$opt_c or $opt_c == 0)
{
print "*** You must define WARN and CRITICAL levels!" if ($verb_err);
&usage;
}
elsif (!$opt_f and !$opt_u)
{
print "*** You must select to monitor either USED or FREE memory!" if ($verb_err);
&usage;
}

# Check if levels are sane
if ($opt_w <= $opt_c and $opt_f)
{
print "*** WARN level must not be less than CRITICAL when checking FREE memory!" if ($verb_err);
&usage;
}
elsif ($opt_w >= $opt_c and $opt_u)
{
print "*** WARN level must not be greater than CRITICAL when checking USED memory!" if ($verb_err);
&usage;
}

$warn_level = $opt_w;
$crit_level = $opt_c;

if ($opt_f)
{
$percent = $free_memory / $total_memory * 100;
$fmt_pct = sprintf "%.1f", $percent;
if ($percent <= $crit_level)
{
print "Memory CRITICAL -FREE $fmt_pct% (FREE:$free_memory kB TOTAL:$total_memory kB)\n";
exit $exit_codes{'CRITICAL'};
}
elsif ($percent <= $warn_level)
{
print "Memory WARNING -FREE $fmt_pct% (FREE:$free_memory kB TOTAL:$total_memory kB)\n";
exit $exit_codes{'WARNING'};
}
else
{
print "Memory OK -FREE $fmt_pct% (FREE:$free_memory kB TOTAL:$total_memory kB)\n";
exit $exit_codes{'OK'};
}
}
elsif ($opt_u)
{
$percent = $used_memory / $total_memory * 100;
$fmt_pct = sprintf "%.1f", $percent;
if ($percent >= $crit_level)
{
print "Memory CRITICAL -USED $fmt_pct% (USED:$used_memory kB TOTAL:$total_memory kB)\n";
exit $exit_codes{'CRITICAL'};
}
elsif ($percent >= $warn_level)
{
print "Memory WARNING -USED $fmt_pct% (USED:$used_memory kB TOTAL:$total_memory kB)\n";
exit $exit_codes{'WARNING'};
}
else
{
print "Memory OK -USED $fmt_pct% (USED:$used_memory kB TOTAL:$total_memory kB)\n";
exit $exit_codes{'OK'};
}
}

#打印帮助
sub usage()
{
print "\ncheck_mem.pl - Nagios Plugin\n\n";
print "usage:\n";
print " check_mem.pl -<f|u> -w <warnlevel> -c <critlevel>\n\n";
print "options:\n";
print " -f Check FREE memory\n";
print " -u Check USED memory\n";
print " -w PERCENT Percent free/used when to warn\n";
print " -c PERCENT Percent free/used when critical\n";
exit $exit_codes{'UNKNOWN'};
}

脚本说明:/usr/sbin/swapinfo 取出的信息并不是真实的物理内存使用情况,也不是swap信息,而是HP-UX系统下的页面调度信息,与服务器真实的内存使用情况有一定出入,但基本一致。

另外由于swapinfo命令需要管理员身份执行,因此我直接赋予脚本u+s权限:

chmod 4755 /opt/nrpe/libexec/check_mem.pl  ##相当于chmod u+x

5、启动NRPE:

5.1 方法一:

# inetd -k && inetd   ##重启inetd守护进程以实现nrpe的启动

5.2 方法二:

# su - nrpe
/opt/nrpe/bin/nrpe -c /opt/nrpe/etc/nrpe.cfg -i & ##以inetd服务方式启动
/opt/nrpe/bin/nrpe -c /opt/nrpe/etc/nrpe.cfg -d & ##独立守护进程

 

6、测试NRPE:

6.1 在被监控服务器上查看服务是否正常启动:

# netstat -an|grep -i tcp |grep 5666  ##查看监控端口是否开启

6.2 在Nagios服务器上测试联系NRPE:

/usr/local/nagios/libexec/check_nrpe -H HP-UX_IP

NRPE v2.12
##返回如上信息则正常
##反之则需要检查hp-ux服务器日志:
tail -20 /var/adm/syslog/syslog.log

至于Nagios服务器端的监控配置,前面的文章已经说过很多,这里就不再重复。

 监控效果(点击图片放大查看):

 

0 0
原创粉丝点击