Nagios監控HP-UX

本文轉自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服務器端的監控配置,前面的文章已經說過很多,這裏就不再重複。

 監控效果(點擊圖片放大查看):

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章