安裝Nagios來監控遠程Windows主機和Linux主機

安裝Nagios來監控遠程Windows主機和Linux主機
Nagios程序簡介

Nagios一般由一個主程序(Nagios)、一個插件程序(Nagios-plugins)和四個可選的ADDON(NRPE、NSCA、NSClient++和NDOUtils)組成。Nagios的監控工做都是經過插件實現的,所以,Nagios和Nagios-plugins是服務器端工做所必須的組件。而四個ADDON中,NRPE用來在監控的遠程Linux/Unix主機上執行腳本插件以實現對這些主機資源的監控;NSCA用來讓被監控的遠程Linux/Unix主機主動將監控信息發送給Nagios服務器(這在冗餘監控模式中特別要用到);NSClient++是用來監控Windows主機時安裝在Windows主機上的組件;而NDOUtils則用來將Nagios的配置信息和各event產生的數據存入數據庫,以實現這些數據的快速檢索和處理。這四個ADDON(附件)中,NRPE和NSClient++工做於客戶端,NDOUtils工做於服務器端,而NSCA則須要同時安裝在服務器端和客戶端。

目前,Nagios只能安裝在Linux系統主機上,其編譯須要用到gcc。同時,若是打算使用web界面的管理工具的話,還須要有apache服務器和GD圖形庫的支持。


安裝配置Nagios

安裝前的準備工做

(1)解決安裝Nagios的依賴關係:

Nagios基本組件的運行依賴於httpd、gcc和gd。能夠經過如下命令來檢查nagios所依賴的rpm包是否已經徹底安裝:
# yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server

說明:以上軟件包您也能夠經過編譯源代碼的方式安裝,只是後面許多要用到的相關文件的路徑等須要按照您的源代碼安裝時的配置逐一修改。此外,您還得按需啓動必要
的服務,如httpd等。

(2)添加nagios運行所須要的用戶和組:
# groupadd nagcmd
# useradd -G nagcmd nagios
# passwd nagios

把apache加入到nagcmd組,以便於在經過web Interface操做nagios時可以具備足夠的權限:
# usermod -a -G nagcmd apache
若是源碼編譯安裝的Apache配置文件中的用戶可能不是apache,只把相應的用戶加入nagcmd便可。

(3)若是您的系統開啓了selinux服務,則默認爲拒絕nagios web cgi程序的運行。您能夠經過下面的命令來檢查您的系統是否開啓了selinux:
#getenforce

若是上面命令的結果顯示開啓了selinux服務,您能夠經過下面的命令暫時性的將其關閉:
#setenforce 0

若是您想在之後徹底關閉selinux,能夠經過編輯/etc/sysconfig/selinux文件,將其中的selinux後面的值「force」修改成「disable」便可。

固然,您也能夠經過如下方式將nagios的CGI程序運行於SELinux/targeted模式而不用關閉selinux:
# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin
# chcon -R -t httpd_sys_content_t /usr/local/nagios/share

下面就能夠安裝nagios軟件了。所須要的軟件有:
nagios-3.3.1.tar.gz
nagios-plugins-1.4.15.tar.gz
這裏用的都是nagios的最新版本。

安裝nagios:
# tar zxf nagios-3.3.1.tar.gz
# cd nagios-3.3.1
# ./configure --with-command-group=nagcmd --enable-event-broker
# make all
# make install
# make install-init
# make install-commandmode
# make install-config

爲email指定您想用來接收nagios警告信息的郵件地址,默認是本機的nagios用戶:
# vi /usr/local/nagios/etc/objects/contacts.cfg
email nagios@localhost #這個是默認設置

在httpd的配置文件目錄(conf.d)中建立Nagios的Web程序配置文件:
# make install-webconf

建立一個登陸nagios web程序的用戶,這個用戶賬號在之後經過web登陸nagios認證時所用:
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

以上過程配置結束之後須要從新啓動httpd:
# service httpd restart

編譯、安裝nagios-plugins

nagios的全部監控工做都是經過插件完成的,所以,在啓動nagios以前還須要爲其安裝官方提供的插件。

# tar zxf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make
# make install

配置並啓動Nagios

(1)把nagios添加爲系統服務並將之加入到自動啓動服務隊列:
# chkconfig --add nagios
# chkconfig nagios on


(2)檢查其主配置文件的語法是否正確:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

(3)若是上面的語法檢查沒有問題,接下來就能夠正式啓動nagios服務了:
# service nagios start

經過web界面查看nagios:
http://your_nagios_IP/nagios 登陸時須要指定前面設定的web認證賬號和密碼。如圖所示是監控成功所顯示的結果:


從圖上能夠看出全部的服務都是正常。
下面來用這個Nagios來監控遠程主機。先來用此軟件來監控|Windows主機

基於NSClinet++監控Windows主機

要想監控Windows服務器要先在Windows主機上安裝上NSClinet++這個軟件,在安裝能夠定義好讓這個Nagios服務器來監控Windows主機。安裝好完成後在Windows主機可查看12489端口,若是啓動說明安裝成功。
下面在配置Nagios來監控Windows主機。
在配置以前能夠在命令行中來測試一下是否能監控Windows主機。方法是:
在nagios端使用以下命令測試:
check_nt -H <client ip> -p <port> -v <command> ...
# check_nt -H 192.168.35.20 -p 12489 -v CPULOAD -w 80 -c 90 -l 5,80,90
若是可以測試成功,下面就能夠配置了。若是不能成功,在Windows上安裝的軟件沒有安裝好。

下面來配置Nagios的文件來監控Windows主機。

一、定義命令:
# vim /etc/nagios/object/command.cfg
  在這個配置文件中定義一個主機,對於Windows主機已經在配置文件中有一個定義了。能夠直接運用
二、定義遠程主機的服務
# vim /etc/nagios/object/name.cfg
在這裏是使用的winhost.cfg
在這個配置文件中要定義的內容以下:
定義主機和服務:
define host{
use windows-server
host_name winhost
alias My Windows Host
address 192.168.35.20
}
define service{
use generic-service
host_name winhost
service_description NSClient++ Version
check_command check_nt!CLIENTVERSION
}
define service{
use generic-service
host_name winhost
service_description Uptime
check_command check_nt!UPTIME
}
define service{
use generic-service
host_name winhost
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90
}
define service{
use generic-service
host_name winhost
service_description Memory Usage
check_command check_nt!MEMUSE!-w 80 -c 90
}
define service{
use generic-service
host_name winhost
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}

define service{
use generic-service
host_name winhost
service_description W3SVC
check_command check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
}

define service{
use generic-service
host_name winhost
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}

這樣一個主機和服務就定義完成了。
下面要在nagios.cfg中去加入這個遠程主機。
# vim /etc/nagios/nagios.cfg
在相應的位置加入:
像以下的行:
cfg_file=/etc/nagios/objects/windows.cfg

作完相應的配置後就能夠檢測一下所作的是否有錯誤:
# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg
 若是沒有什麼錯誤 就能夠在服務端對這個遠程主機進行監控了。在瀏覽器打開網頁就看到如圖所示:


到此就把一個遠程的Windows服務器監控起來了。也能夠定義不一樣的服務。

下面來定義如何來監控遠程的Linux主機。

基於NRPE監控遠程Linux主機

NRPE簡介

Nagios監控遠程主機的方法有多種,其方式包括SNMP、NRPE、SSH和NCSA等。這裏介紹其經過NRPE監控遠程Linux主機的方式。

NRPE(Nagios Remote Plugin Executor)是用於在遠端服務器上運行檢測命令的守護進程,它用於讓Nagios監控端基於安裝的方式觸發遠端主機上的檢測命令,並將檢測結果輸出至監控端。而其執行的開銷遠低於基於SSH的檢測方式,並且檢測過程並不須要遠程主機上的系統賬號等信息,其安全性也高於SSH的檢測方式。

安裝配置被監控端

1)先添加nagios用戶
# useradd -s /sbin/nologin nagios

2)NRPE依賴於nagios-plugins,所以,須要先安裝之

# tar zxf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make instal

3)安裝NRPE

# tar -zxvf nrpe-2.12.tar.gz
# cd nrpe-2.12.tar.gz
# ./configure --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args \
--enable-ssl
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config

4)配置NRPE

# vim /usr/local/nagios/etc/nrpe.conf

log_facility=daemon
pid_file=/var/run/nrpe.pid
server_address=172.16.100.11
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=192.168.35.11
command_timeout=60
connection_timeout=300
debug=0

上述配置指令能夠作到見名知義,所以,配置過程當中根據實際須要進行修改便可。其中,須要特定說明的是allowed_hosts指令用於定義本機所容許的監控端的IP地址。

5)啓動NRPE

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

爲了便於NRPE服務的啓動,能夠將以下內容定義爲/etc/init.d/nrped腳本:

#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON

NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg

case "$1" in
start)
echo -n "Starting NRPE daemon..."
$NRPE -c $NRPECONF -d
echo " done."
;;
stop)
echo -n "Stopping NRPE daemon..."
pkill -u nagios nrpe
echo " done."
;;
restart)
$0 stop
sleep 2
$0 start
;;
*)
echo "Usage: $0 start|stop|restart"
;;
esac
exit 0

在監控端上的配置:

1)安裝NRPE

# tar -zxvf nrpe-2.12.tar.gz
# cd nrpe-2.12.tar.gz
# ./configure --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args \
--enable-ssl
# make all
# make install-plugin

2)定義如何監控遠程主機及服務:

若是但願上面的command定義更具備通用性,那麼上面的定義也能夠修改成以下:
# vim /etc/nagios/objects/commands.cfg

定義監控遠程Linux主機的命令:
define command
{
command_name check_nrpe
command_line $USER1$/check_nrpe –H "$HOSTADDRESS$" -c $ARG1$
}

定義遠程Linux主機的swap資源:
define service
{
use generic-service
host_name linuxserver1,linuxserver2
hostgroup_name linux-servers
service_description SWAP
check_command check_nrpe!check_swap
normal_check_interval 30
}

# vim /etc/nagios/objects/linhost.cfg
添加以下內容:
define host{
use linux-server ; Inherit default values from a template
host_name linhost
alias My Linux Host ; A longer name associated with the host
address 192.168.35.12 ; IP address of the host
}


define service{
use generic-service
host_name linhost
service_description CHECK USERS
check_command check_nrpe!check_users
}


define service{
use generic-service
host_name linhost
service_description Load
check_command check_nrpe!check_load
}

define service{
use generic-service
host_name linhost
service_description SDA1
check_command check_nrpe!check_sda1
  }

define service{
use generic-service
host_name linhost
service_description Zombie
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service
host_name linhost
service_description Total procs
check_command check_nrpe!check_total_procs
}

這樣一個Linux主機就定義完成了。下面要把這個Linux加入到nagios.cfg中。
# vim /etc/nagios/nagios.cfg
在相應的位置上添加以下行便可
cfg_file=/etc/nagios/objects/linhost.cfg

作完以後從新啓動nagios服務就能夠監控一個遠程的Linux主機了。
在瀏覽器中以下圖所示:

這個主機剛剛接入,等待幾分鐘後就可心查看各服務的狀況了。

好了,Nagiosr的監控就先寫到這裏了,在下一篇中要用到聯繫人了。所要用到的是飛信機器人這個工具。想一想這個飛信機器人仍是蠻好玩的。
相關文章
相關標籤/搜索