天天看點

nagios 監控在centos中安裝與使用

 1.系統:CentOS 5.2

2.安裝前準備:

1).安裝好apache,GD庫(我的GD庫安裝在/usr/local/libgd)。

2).下載下傳Nagios3.0.5,Nagios-plugins1.4.11,nrpe2.12,nsclient++

# http://www.nagios.org/download/

1.     wget http://osdn.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.5.tar.gz

2.     wget http://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz

複制代碼

# Nagios3.0.5,Nagios-plugins1.4.11,nrpe2.12安裝在監控伺服器上。

# 去http://www.nagios.org/download/addons/下載下傳nrpe和nsclient++插件。

# nrpe2.12安裝在Linux/Unix被監控端。

# nsclient++安裝在Windows被監控端。

3).建立帳号及組

# 建立帳号

1.     /usr/sbin/useradd -m nagios

2.     passwd nagios

複制代碼

# 建立組

1.     /usr/sbin/groupadd nagcmd

2.     /usr/sbin/usermod -a -G nagcmd nagios

3.     /usr/sbin/usermod -a -G nagcmd daemon

複制代碼

# daemon為運作apache的帳号。

3.安裝nagios

1.     tar xzf nagios-3.0.5.tar.gz

2.     cd nagios-3.0.5

3.     ./configure --with-command-group=nagcmd --with-gd-lib=/usr/local/libgd/lib/ --with-gd-inc=/usr/local/libgd/include/

4.     make all

5.     # 使用make install來安裝主程式,CGI和HTML檔案

6.     make install

7.     # 使用make install-init在/etc/rc.d/init.d安裝啟動腳本

8.     make install-init

9.     # 使用make install-cofig來安裝示例配置檔案,安裝的路徑是/usr/local/nagios/etc.

10. make install-config

11. # 使用make install-commandmode來配置目錄權限

12. make install-commandmode

複制代碼

nagios目錄功能的簡要說明:

bin    Nagios執行程式所在目錄,nagios檔案即為主程式

etc    Nagios配置檔案位置

sbin    Nagios Cgi檔案所在目錄,也就是執行外部指令所需檔案所在的目錄

Share    Nagios網頁檔案所在的目錄

var    Nagios日志檔案、spid 等檔案所在的目錄

var/archives    日志歸檔目錄

var/rw    用來存放外部指令檔案

配置apache

将現面行加入apache配置檔案的alias子產品<IfModule alias_module>

----------------------------------------

1.     ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

2.      

3.     <Directory "/usr/local/nagios/sbin">

4.     #  SSLRequireSSL

5.        Options ExecCGI

6.        AllowOverride None

7.        Order allow,deny

8.        Allow from all

9.     #  Order deny,allow

10. #  Deny from all

11. #  Allow from 127.0.0.1

12.    AuthName "Nagios Access"

13.    AuthType Basic

14.    AuthUserFile /usr/local/nagios/etc/htpasswd.users

15.    Require valid-user

16. </Directory>

17.  

18. Alias /nagios "/usr/local/nagios/share"

19.  

20. <Directory "/usr/local/nagios/share">

21. #  SSLRequireSSL

22.    Options None

23.    AllowOverride None

24.    Order allow,deny

25.    Allow from all

26. #  Order deny,allow

27. #  Deny from all

28. #  Allow from 127.0.0.1

29.    AuthName "Nagios Access"

30.    AuthType Basic

31.    AuthUserFile /usr/local/nagios/etc/htpasswd.users

32.    Require valid-user

33. </Directory>

複制代碼

----------------------------------------

# 建立apache目錄驗證檔案

1.     /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

2.     New password: (輸入密碼)

3.     Re-type new password: (再輸入一次密碼)

4.     Adding password for user nagiosadmin

複制代碼

# 重新開機apache:/usr/local/apache/bin/apachectl -k restart

# 配置nagios配置檔案

vi /usr/local/nagios/etc/objects/contacts.cfg

# 将裡面的email位址改為自己的email位址。

4.安裝Nagios插件

1.     tar xzf nagios-plugins-1.4.11.tar.gz

2.     cd nagios-plugins-1.4.11

3.     ./configure --with-nagios-user=nagios --with-nagios-group=nagios

4.     make

5.     make install

複制代碼

5.啟動Nagios

# 配置機器啟動時自動啟動Nagios

1.     chkconfig --add nagios

2.     chkconfig nagios on

複制代碼

# 檢查Nagios配置檔案

1.     /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

複制代碼

# 啟Nnagios

1.     service nagios start

複制代碼

6.SELinux及防火牆設定

1).關閉SELinux

vi /etc/selinux/config

# 将下面行設為disabled

SELINUX=disabled

# 重新開機系統

2).如果開啟防火牆,應該允許通路apache(一般為80端口)并允許nagios去抓取被監控機資訊(一般nrpe為5666端口)。

7.通路Nagios伺服器

http://localhost/nagios/

輸入使用者名及密碼登入。

8.安裝nrpe插件,用來監控Linux機器

1.     tar xzvf nrpe-2.12.tar.gz

2.     cd nrpe-2.12

3.     ./configure

4.     make all

5.     # 在Nagios伺服器端隻要安裝nrpe監控插件就行

6.     make install-plugin

複制代碼

在/usr/local/nagios/etc/objects/commands.cfg中定義check_nrpe指令

vi /usr/local/nagios/etc/objects/commands.cfg

1.     ########################################################################

2.     #

3.     # 2008.11.18 add by Stone

4.     # NRPE COMMAND

5.     #

6.     ########################################################################

7.     # 'check_nrpe ' command definition

8.     define command{

9.             command_name check_nrpe

10.         command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

11.         }

複制代碼

9.在被監控伺服器(Linux/unix)上安裝Nagios-plugins和nrpe

1.     useradd nagios

2.     tar xzvf nagios-plugins-1.4.13.tar.gz

3.     cd nagios-plugins-1.4.13

4.     # Nagios-plugins預設安裝到/usr/local/nagios

5.     ./configure

6.     make

7.     make install

8.     chown nagios.nagios /usr/local/nagios/

9.     chown -R nagios.nagios /usr/local/nagios/libexec/

複制代碼

1.     tar xzvf nrpe-2.12.tar.gz

2.     cd nrpe-2.12

3.     ./configure

4.     make all

5.     # 安裝nrpe插件,本監控端可以不裝

6.     make install-plugin

7.     # 安裝nrpe守護程序

8.     make install-daemon

9.     # 安張nrpe配置檔案

10. make install-daemon-config

複制代碼

# 修改nrpe配置檔案,允許Nagios監控伺服器(192.168.0.19)監控

vi nrpe.cfg

# 多台機器用逗号隔開

allowed_hosts=127.0.0.1,192.168.0.19

# 以獨立守護程序啟動nrpe,也可以使用xinetd啟動nrpe,具體清檢視nrpe官方文檔。

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

# 開機自動啟動nrpe

vi /etc/rc.d/rc.local

# 加入下面行

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

# 檢查nrpe是否安裝正常

[r[email protected] etc]# /usr/local/nagios/libexec/check_nrpe -H localhost

NRPE v2.12

# 傳回nrpe版本說明安裝沒問題。

# 檢視啟動端口

[[email protected] ~]# netstat -tunlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  

tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      27387/nrpe

如果有防火牆應該開放5666端口:

iptables -I eth0 -p tcp -m tcp ?dport 5666 -j ACCEPT

***********************************************************

注意:我們需要在/usr/local/nagios/etc/nrpe.cfg中定義我們用到的監控本地資源的指令。

下面的指令是預設定義的:

1.     command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10

2.     command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

3.     command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1

4.     command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z

5.     command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

複制代碼

下面的指令是自己定義的:

# 監控交換分區的使用情況,使用超過20%時為警告狀态,超過10%時為嚴重狀态

command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

# 監控根分區磁盤使用情況

command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /

***********************************************************

10.Nagios如何監控Linux機器

NRPE總共由兩部分組成:

(1).check_nrpe插件,運作在監控主機上。

(2).NRPE daemon,運作在遠端的linux主機上(通常就是被監控機)

按照上圖,整個的監控過程如下:

當Nagios需要監控某個遠端linux主機的服務或者資源情況時:

1).nagios會運作check_nrpe插件,我們要在nagios配置檔案中告訴它要檢查什麼.

2).check_nrpe插件會通過SSL連接配接到遠端的NRPE daemon.

3).NRPE daemon會運作相應的nagios插件來執行檢查本地資源或服務.

4).NRPE daemon将檢查的結果傳回給check_nrpe插件,插件将其遞交給nagios做處理.

注意:NRPE daemon需要nagios插件安裝在遠端被監控linux主機上,否則,daemon不能做任何的監控.

11.Nagios的配置檔案

# 控制cgi通路的配置檔案

cgi.cfg

# Nagios主配置檔案

nagios.cfg

# resource.cfg定義了一些變量,以便被其他檔案引用,如$USER1$

resource.cfg

# objects是一個目錄,用于定義Nagios對象

objects

# servers是自己建立的一個目錄,Nagios可以加載一個目錄下面的所有配置檔案(需要在nagios.cfg中配置)

servers

./objects:

# 指令定義配置檔案,裡面定義的指令可以被其他檔案引用

commands.cfg

# 聯系人和聯系人組配置檔案

contacts.cfg

# 監控本地機器的配置檔案

localhost.cfg

# 監控列印機的一個事例配置檔案(預設未啟用)

printer.cfg

# 監控路由器的一個事例配置檔案(預設未啟用)

switch.cfg

# 模闆配置檔案,在此可以定義模闆,在其他檔案中引用

templates.cfg

# 定義監控時間段的配置檔案

timeperiods.cfg

# 監控Windows的一個事例配置檔案(預設未啟用)

windows.cfg

./servers:

# 自己建立的主機群組配置檔案

hostgroup.cfg

# 自己建立的監控遠端Linux主機的配置檔案

wiki-l-11.cfg

配置檔案是怎樣引用的?

用nagios主要是監控一台主機的各種資訊,包括本機資源以及對外的服務等等.這些在nagios裡面都是被定義為一個個的項目(nagios稱之為服務,為了與主機提供的服務相差別,我這裡用項目這個詞),而實作每個監控項目,則需要通過commands.cfg檔案中定義的指令。

為了不必重複定義一些項目,Nagios引入了一個模闆配置檔案(templates.cfg),将一些共性的屬性定義成模闆,以便于多次引用。

我們現在有一個監控項目是監控一台機器的web服務是否正常, 我們需要哪些元素呢?最重要的有下面三點:首先是監控哪台機器,然後是這個監控要用什麼指令實作,最後就是出了問題的時候要通知哪個聯系人。

我們首先應該在commands.cfg中定義監控遠端服務和資源的指令,以及如何發送郵件的指令。大部分監控遠端服務和資源的指令的指令通過/usr/local/nagios/libexec下的腳本實作,如ping指令為check_ping。

/usr/local/nagios/libexec下的腳本指令的使用發法可以通過-h參數檢視,如:

-----------------------------------------------------------------------------------------

[[email protected] ~]# /usr/local/nagios/libexec/check_ping -h

check_ping v1991 (nagios-plugins 1.4.13)

Copyright (c) 1999 Ethan Galstad <[email protected]>

Copyright (c) 2000-2007 Nagios Plugin Development Team

    <[email protected]>

Use ping to check connection statistics for a remote host.

Usage:check_ping -H <host_address> -w <wrta>,<wpl>% -c <crta>,<cpl>%

[-p packets] [-t timeout] [-4|-6]

Options:

-h, --help

    Print detailed help screen

-V, --version

    Print version information

-4, --use-ipv4

    Use IPv4 connection

-6, --use-ipv6

    Use IPv6 connection

-H, --hostname=HOST

    host to ping

-w, --warning=THRESHOLD

    warning threshold pair

-c, --critical=THRESHOLD

    critical threshold pair

-p, --packets=INTEGER

    number of ICMP ECHO packets to send (Default: 5)

-L, --link

    show HTML in the plugin output (obsoleted by urlize)

-t, --timeout=INTEGER

    Seconds before connection times out (default: 10)

-----------------------------------------------------------------------------------------

然後我們在contacts.cfg檔案中定義聯系人和聯系人組,在timeperiods.cfg中定義監控時間段。最後我們在伺服器監控配置檔案中引用前面定義的元素來監控伺服器狀态。

===============================================================================

下面引用配置檔案中部配置設定置做說明:

vi /usr/local/nagios/etc/resource.cfg

# 定義$USER1$變量,設定插件路徑

$USER1$=/usr/local/nagios/libexec

vi /usr/local/nagios/etc/objects/commands.cfg

# 定義check-host-alive指令

define command{

        command_name    check-host-alive  # 指令名稱

        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5

        }

# 上面的$USER1$和$HOSTADDRESS$引用自已定義的配置檔案。變量不需現定義才能被引用。

########################################################################

#

# 2008.11.18 add by Stone

# NRPE COMMAND

# 自己定義check_nrpe指令,此指令後接必需接一個參數,用于告訴遠端伺服器上的NRPE daemon需要監控的内容,如check_swap參數為監控遠端機器的交換分區。

########################################################################

# 'check_nrpe ' command definition

define command{

        command_name check_nrpe

        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

        }

vi /usr/local/nagios/etc/objects/contacts.cfg

# 定義聯系人

define contact{

        contact_name                    nagiosadmin             ; Short name of user

        use                             generic-contact         ; Inherit default values from generic-contact template (defined above)

        alias                           Nagios Admin            ; Full name of user

        email                           [email protected]         ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

        }

# 上面的generic-contact在templates.cfg中定義。

# 定義聯系人組

define contactgroup{

        contactgroup_name       admins

        alias                   Nagios Administrators

        members                 nagiosadmin   #在此可以加入多個聯系人,中間用逗号隔開

        }

vi /usr/local/nagios/etc/objects/timeperiods.cfg

# 定義監控的時間段

define timeperiod{

        timeperiod_name 24x7   #監控所有時間段(7*24小時)

        alias           24 Hours A Day, 7 Days A Week

        sunday          00:00-24:00

        monday          00:00-24:00

        tuesday         00:00-24:00

        wednesday       00:00-24:00

        thursday        00:00-24:00

        friday          00:00-24:00

        saturday        00:00-24:00

        }

vi /usr/local/nagios/etc/objects/templates.cfg

# 定義generic-contact聯系人模闆,并非真正的聯系人,真正的聯系人在contacts.cfg中定義

define contact{

        name                            generic-contact         ; The name of this contact template

        service_notification_period     24x7                    ; service notifications can be sent anytime

        host_notification_period        24x7                    ; host notifications can be sent anytime

        service_notification_options    w,u,c,r,f,s             ; send notifications for all service states, flapping events, and scheduled downtime events

        host_notification_options       d,u,r,f,s               ; send notifications for all host states, flapping events, and scheduled downtime events

        service_notification_commands   notify-service-by-email ; send service notifications via email

        host_notification_commands      notify-host-by-email    ; send host notifications via email

        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

        }

------------------------------------------------------------------------------------------------------------------

service_notification_period     24x7

服務出了狀況通知的時間段,這個時間段就是上面在timeperiods.cfg中定義的.

host_notification_period        24x7

主機出了狀況通知的時間段, 這個時間段就是上面在timeperiods.cfg中定義的

service_notification_options    w,u,c,r

當服務出現w?報警(warning),u?未知(unkown),c?嚴重(critical),或者r?從異常情況恢複正常,在這四種情況下通知聯系人.

host_notification_options       d,u,r

當主機出現d­­­­?當機(down),u?傳回不可達(unreachable),r?從異常情況恢複正常,在這3種情況下通知聯系人

service_notification_commands   notify-service-by-email

服務出問題通知采用的指令notify-service-by-email,這個指令是在commands.cfg中定義的,作用是給聯系人發郵件.

host_notification_commands      notify-host-by-email

同上,主機出問題時采用的也是發郵件的方式通知聯系人

------------------------------------------------------------------------------------------------------------------

# 定義generic-host主機模闆

define host{

        name                            generic-host    ; The name of this host template

        notifications_enabled           1               ; Host notifications are enabled

        event_handler_enabled           1               ; Host event handler is enabled

        flap_detection_enabled          1               ; Flap detection is enabled

        failure_prediction_enabled      1               ; Failure prediction is enabled

        process_perf_data               1               ; Process performance data

        retain_status_information       1               ; Retain status information across program restarts

        retain_nonstatus_information    1               ; Retain non-status information across program restarts

        notification_period             24x7            ; Send host notifications at any time

        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!

        }

# 定義Linux主機模闆

define host{

        name                            linux-server    ; The name of this host template

        use                             generic-host    ; This template inherits other values from the generic-host template

        check_period                    24x7            ; By default, Linux hosts are checked round the clock

        check_interval                  5               ; Actively check the host every 5 minutes

        retry_interval                  1               ; Schedule host check retries at 1 minute intervals

        max_check_attempts              10              ; Check each Linux host 10 times (max)

        check_command                   check-host-alive ; Default command to check Linux hosts

        notification_period             workhours       ; Linux admins hate to be woken up, so we only notify during the day

                                                        ; Note that the notification_period variable is being overridden from

                                                        ; the value that is inherited from the generic-host template!

        notification_interval           120             ; Resend notifications every 2 hours

        notification_options            d,u,r           ; Only send notifications for specific host states

        contact_groups                  admins          ; Notifications get sent to the admins by default

        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!

        }

# 在nagios.cfg配置檔案中開啟對/usr/local/nagios/etc/servers/中配置檔案的引用。

cfg_dir=/usr/local/nagios/etc/servers

# 遠端Linux主機監控檔案,如果監控多台主機隻需簡單複制修改即可。

#我們應該牢記wiki-l-11.cfg用到的指令在commands.cfg中定義,在commands.cfg中定義的指令用到/usr/local/nagios/libexec下的插件(指令)。

vi /usr/local/nagios/etc/servers/wiki-l-11.cfg

# 定義主機

define host{

        use                     linux-server            ; Name of host template to use

                                                        ; This host definition will inherit all variables that are defined

                                                        ; in (or inherited by) the linux-server host template definition.

        host_name               wiki

        alias                   Docs

        address                 192.168.0.11

        }

# 定義Ping遠端Linux主機

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       wiki

        service_description             PING

        check_command                   check_ping!100.0,20%!500.0,60%   ;check_ping指令在commands.cfg中定義,後跟兩個參數,指令及參數間用!分割。

        }

#檢查遠端Linux主機根分區使用情況,check_nrpe指令必須在/usr/local/nagios/etc/objects/commands.cfg中定義(預設未定義)

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       wiki

        service_description             Root Partition

        check_command                   check_nrpe!check_disk_root

        }

# 檢查遠端Linux主機的登入人數

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       wiki

        service_description             Current Users

        check_command                   check_nrpe!check_users

        }

# 檢查遠端Linux的主機的負載

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       wiki

        service_description             Current Load

        check_command                   check_nrpe!check_load

        }

# 檢查遠端Linux主機swap分區使用情況

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       wiki

        service_description             Swap Usage

        check_command                   check_nrpe!check_swap

        }

# 檢查遠端Linux主機的SSH服務

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       wiki

        service_description             SSH

        check_command                   check_ssh

        notifications_enabled           0

        }

# 檢查遠端Linux主機的HTTP服務

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       wiki

        service_description             HTTP

        check_command                   check_http

        notifications_enabled           0

        }

vi /usr/local/nagios/etc/servers/hostgroup.cfg

# 定義主機組(localhost.cfg中有類似的主機組設定,我已将其注釋掉,否則可能會有沖突)

define hostgroup{

        hostgroup_name  linux-servers ; The name of the hostgroup

        alias           Linux Servers ; Long name of the group

        members         localhost,wiki     ; Comma separated list of hosts that belong to this group

        }

#define hostgroup{

#        hostgroup_name  windows-servers ; The name of the hostgroup

#        alias           Windows Servers ; Long name of the group

#        members         print     ; Comma separated list of hosts that belong to this group

#        }

===============================================================================

# 完成監控主機配置檔案的配置後使用下面指令檢查配置檔案的正确性:

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# 确定無誤後重新開機Nagios:

service nagios restart

使用Nagios監控Windows伺服器

12.Nagios使用NSClient++監控遠端Windows主機

下載下傳NSClient++-Win32-0.3.5.msi并安裝。

到安裝目錄打開NSC.ini檔案進行修改:

在[modules]子產品,将除CheckWMI.dll和RemoteConfiguration.dll外的所有dll檔案明前的注釋(;)去掉。

在[Settings]子產品可以設定一個連接配接密碼password=PWD,為了簡單,在此不設密碼。設定 allowed_hosts=127.0.0.1/32,192.168.0.19,可以連接配接的監控伺服器的位址,如果寫成192.168.0.0/24 則表示該子網内的所有機器都可以通路;如果這個地方是空白則表示所有的主機都可以連接配接上來(注意在[NSClient]有allowed_hosts的同樣設定,不要設定錯了),最後不要忘記去掉前面的注釋符(;)。

運作nsclient++

NSClient++ /install

NSClient++ SysTray install

NSClient++ /start

如果有防火牆,請開放相應端口。

建立監控配置檔案,使用check_nt指令監控windows系統資訊(此指令預設已定義)。

Windows監控示例配置檔案:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

[[email protected] etc]# cat /usr/local/nagios/etc/servers/print-w-80.cfg

###############################################################################

# WINDOWS.CFG - SAMPLE CONFIG FILE FOR MONITORING A WINDOWS MACHINE

#

# Last Modified: 06-13-2007

#

# NOTES: This config file assumes that you are using the sample configuration

#     files that get installed with the Nagios quickstart guide.

#

###############################################################################

###############################################################################

###############################################################################

#

# HOST DEFINITIONS

#

###############################################################################

###############################################################################

# Define a host for the Windows machine we'll be monitoring

# Change the host_name, alias, and address to fit your situation

define host{

    use        windows-server    ; Inherit default values from a template

    host_name    print80        ; The name we're giving to this host

    alias        Print80        ; A longer name associated with the host

    address        192.168.0.80    ; IP address of the host

    }

###############################################################################

###############################################################################

#

# HOST GROUP DEFINITIONS

# 主機組在/usr/local/nagios/etc/servers/hostgroup.cfg中單獨配置

###############################################################################

###############################################################################

# Define a hostgroup for Windows machines

# All hosts that use the windows-server template will automatically be a member of this group

#define hostgroup{

#    hostgroup_name    windows-servers    ; The name of the hostgroup

#    alias        Windows Servers    ; Long name of the group

#    }

###############################################################################

###############################################################################

#

# SERVICE DEFINITIONS

#

###############################################################################

###############################################################################

# Create a service for monitoring the version of NSCLient++ that is installed

# Change the host_name to match the name of the host you defined above

define service{

    use            generic-service

    host_name        print80

    service_description    NSClient++ Version

    check_command        check_nt!CLIENTVERSION

    }

# Create a service for monitoring the uptime of the server

# Change the host_name to match the name of the host you defined above

define service{

    use            generic-service

    host_name        print80

    service_description    Uptime

    check_command        check_nt!UPTIME

    }

# Create a service for monitoring CPU load

# Change the host_name to match the name of the host you defined above

define service{

    use            generic-service

    host_name        print80

    service_description    CPU Load

    check_command        check_nt!CPULOAD!-l 5,80,90

    }

# Create a service for monitoring

# Change the host_name to match the name of the host you defined above

define service{

    use            generic-service

    host_name        print80

    service_description    Memory Usage

    check_command        check_nt!MEMUSE!-w 80 -c 90

    }

# Create a service for monitoring C:\ disk usage

# Change the host_name to match the name of the host you defined above

define service{

    use            generic-service

    host_name        print80

    service_description    C_Drive_Space

    check_command        check_nt!USEDDISKSPACE!-l c -w 80 -c 90

    }

# Create a service for monitoring the W3SVC service

# Change the host_name to match the name of the host you defined above

define service{

    use            generic-service

    host_name        print80

    service_description    W3SVC

    check_command        check_nt!SERVICESTATE!-d SHOWALL -l W3SVC

    }

# Create a service for monitoring the Explorer.exe process

# Change the host_name to match the name of the host you defined above

define service{

    use            generic-service

    host_name        print80

    service_description    Explorer

    check_command        check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe

    }

#主機組配置檔案

[[email protected] etc]# cat /usr/local/nagios/etc/servers/hostgroup.cfg

define hostgroup{

        hostgroup_name  linux-servers ; The name of the hostgroup

        alias           Linux Servers ; Long name of the group

        members         localhost,wiki     ; Comma separated list of hosts that belong to this group

        }

define hostgroup{

        hostgroup_name  windows-servers ; The name of the hostgroup

        alias           Windows Servers ; Long name of the group

        members         print80     ; Comma separated list of hosts that belong to this group

        }

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

使用NDOUtils将Nagios監控資訊存入資料庫

1.NDOUtils安裝需求:

Nagios正确安裝運作,版本>=2.0安裝目錄:/usr/local/nagios

MySQL正确安裝并運作,我用的是二進制安裝包,安裝目錄:/usr/local/mysql

2.安裝

下載下傳安裝包:http://www.nagios.org/download/

# 确認DBD-mysql包安裝,如沒有安裝,請先安裝。

# 如果DBI包沒安裝DBD-mysql前需要先安裝DBI包

tar xzvf DBI-1.605.tar.gz

cd DBI-1.605

perl Makefile.PL

make

make install

tar xzvf DBD-mysql-3.0008.tar.gz

cd DBD-mysql-3.0008

unset LANG

perl Makefile.PL --libs="-L/usr/local/mysql/lib/mysql -lmysqlclient -L/usr/lib -lz " --cflags="-I/usr/local/mysql/include"  --testhost=127.0.0.1 --mysql_config=/usr/local/mysql/bin/mysql_config --testuser=root --testpassword=baihe.com

make

make install

# 做好MySQL庫檔案lib和include檔案的連接配接(這點很重要)。

ln -s /usr/local/mysql/include/' | sed 's/[ \t]*$//' | grep '^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

    if (($? == 0 ));then

     if [[ -f $DIR/msg.txt ]];then

            cat /dev/null > msg.txt

        fi

        phone=`echo "$phone" | sed 's/^[ \t]*//' | sed 's/[ \t]*$//'`

        echo "sms $phone $1" >> $DIR/msg.txt

        echo "quit" >> $DIR/msg.txt

        $fetionDir/fetion -u $user -p $pwd -b $DIR/msg.txt

    else

        continue

    fi

done

# phonelist.txt為接收短信号碼清單,必須是發送飛信使用者的好友

# 在此控制短信接收者而不是在nagios聯系人中,好處是修改接收者後不需要重新開機nagios

# 可以使用#号注釋接收者的号碼以屏蔽接收者

vi /usr/local/nagios/fetion/phonelist.txt

# Phone NO. 1

138xxxxxxxx

# Phone NO. 2

158xxxxxxxx

# 修改飛信目錄權限(否則運作Nagios的使用者不能寫入資訊内容到/usr/local/fetion/msg.txt)

chown -R nagios.nagios /usr/local/fetion

chmod +x /usr/local/fetion/sendsms.sh

chmod +x /usr/local/fetion/fetion

4.配置Nagios

vi /usr/local/nagios/etc/objects/commands.cfg

################################################################################

#

# notify-host-by-sendmsg/notify-service-by-sendmsg   //定義發送飛信報警的指令

#

################################################################################

define command {

       command_name      notify-host-by-sendmsg

       command_line      /usr/local/fetion/sendsms.sh "Host $HOSTSTATE$ alert for $HOSTNAME$($HOSTADDRESS$) on $TIME$."

       }

define command {

       command_name     notify-service-by-sendmsg

       command_line     /usr/local/fetion/sendsms.sh ""$TIME$":$SERVICEDESC$($HOSTADDRESS$) is $SERVICESTATE$."

       }

定義聯系人模闆

define contact{

        name                            sendmsg-contact         ; The name of this contact template

        service_notification_period     24x7                    ; service notifications can be sent anytime

        host_notification_period        24x7                    ; host notifications can be sent anytime

        service_notification_options    w,u,c,s             ; send notifications for all service states, flapping events, and scheduled downtime events

        host_notification_options       d,u,s               ; send notifications for all host states, flapping events, and scheduled downtime events

        service_notification_commands   notify-service-by-sendmsg ; 使用定義的飛信報警指令

        host_notification_commands      notify-host-by-sendmsg    ; 使用定義的飛信報警指令

        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

        }

定義聯系人及聯系人組

vi /usr/local/nagios/etc/objects/contacts.cfg

define contact{

        contact_name                    msgreceiver       ; Short name of user

        use                             sendmsg-contact   ; 使用上面定義的聯系人模闆

        alias                           MsgReceiver       ; Full name of user

        email                           [email protected]

        }

define contactgroup{

        contactgroup_name       admins-sendmsg

        alias                   BaiHe Monitor

        members                 msgreceiver

        }

5.測試故障時是否能夠觸發短信報警

Nagios事件處理

我們可以使用事件處理來在任何人收到通知之前由Nagios來做一些前期故障修複。

事件處理會在下面情況觸發:

1).主機或服務處于一個軟态故障狀态時

2).主機或服務初始進入一個硬态故障時

3).主機或服務從軟态或硬态的故障狀态中初始恢複時

通過在主配置檔案(nagios.cfg)中設定enable_event_handlers=1來打開全局事件處理,特定主機的和服務的事件處理可用主機和服務對象裡的event_handler_enabled域來開關。如果全局的enable_event_handlers域是關閉的,那麼特定主機的和服務的事件處理也不會運作。

事件處理指令可以用shell或是perl腳本,腳本中應該處理以下宏:

對服務的:$SERVICESTATE$、$SERVICESTATETYPE$和$SERVICEATTEMPT$;

對主機的:$HOSTSTATE$、$HOSTSTATETYPE$和$HOSTATTEMPT$。

腳本須檢測這些作為指令行參數傳入的值,并采取必要動作來處理這些值。

事件處理指令通常是與運作于本機上的Nagios程式的權限是相同的(下面例子中Nagios服務是以nagios使用者運作的)。這可能會有問題,如果你想寫成一個用于系統服務重新開機的指令,它需要有root權限才能執行一系列指令與任務。你或許會嘗使用sudo指令來實作它。

本例通過Nagios檢測遠端機器上的MySQL服務,當服務出現問題時通過Nagios的事件處理邏輯來重新開機遠端機器上的MySQL服務。

1.配置在Nagios伺服器(192.168.0.200)上無密碼登入遠端機器(MySQL服務運作在上面-192.168.0.210)

[[email protected] ~]# su - nagios

[[email protected] ~]$ ssh-keygen -t rsa

# 下面一直回車,不要設定密碼

Generating public/private rsa key pair.

Enter file in which to save the key (/home/nagios/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/nagios/.ssh/id_rsa.

Your public key has been saved in /home/nagios/.ssh/id_rsa.pub.

The key fingerprint is:

d2:82:61:12:53:f9:53:75:77:8d:32:c0:ca:c8:20:60 [email protected]

# 将生成的密鑰拷貝到要遠端登入的機器上

[n[email protected] ~]$ scp .ssh/id_rsa.pub 192.168.0.210:/home/nagios/

[email protected]'s password:

id_rsa.pub                                                        100%  233     0.2KB/s   00:00

# 在要遠端登入的機器上配置公鑰

[[email protected] ~]$ ssh [email protected]

[email protected]'s password:

Last login: Sat Nov 29 22:30:55 2008 from 192.168.0.200

[[email protected] ~]$ cat id_rsa.pub >> .ssh/authorized_keys

[[email protected] ~]$ chmod 600 .ssh/authorized_keys

[[email protected] ~]$ exit

logout

Connection to 192.168.0.210 closed.

# 測試無密碼登入

[[email protected] ~]$ ssh [email protected]

Last login: Sat Nov 29 22:35:27 2008 from 192.168.0.200

2.在遠端機器上配置sudo

使nagios使用者可以以root身份運作/usr/local/nagios/libexec/eventhandlers/restart-mysql腳本

[[email protected] ~]# visudo

nagios  ALL=(root) NOPASSWD:/usr/local/nagios/libexec/eventhandlers/restart-mysql

3.在遠端機器上編寫MySQL重新開機腳本

[[email protected] ~]# vi /usr/local/nagios/libexec/eventhandlers/restart-mysql

#!/bin/sh

#

# Event handler script for restarting the MySQL server on the remote machine

#

# Note: This script will only restart the MySQL server if the service is

#       retried 2 times (in a "soft" state) or if the web service somehow

#       manages to fall into a "hard" error state.

#

#

# What state is the MySQL service in?

case "$1" in

OK)

        ;;

WARNING)

        ;;

UNKNOWN)

        ;;

CRITICAL)

        # Is this a "soft" or a "hard" state?

        case "$2" in

        SOFT)

                # What check attempt are we on?  We don't want to restart the MySQL server on the first

                # check, because it may just be a fluke!

                case "$3" in

                2)

                        echo -n "Restarting MySQL service..."

                        /sbin/service mysqld restart

                        ;;

                        esac

                ;;

        HARD)

                echo -n "Restarting MySQL service..."

                /sbin/service mysqld restart

                ;;

        esac

        ;;

esac

exit 0

上面的腳本隻會在MySQL處于軟狀态,且第二次檢查出現故障時或者進入硬狀态時重新開機MySQL。

4.配置Nagios伺服器上的配置檔案

[[email protected] ~]# cd /usr/local/nagios/etc/

# 檢查全局事件處理是否打開

[[email protected] etc]# vi nagios.cfg

enable_event_handlers=1

# 在指令配置檔案中定義重新開機MySQL的指令

[[email protected] etc]# vi objects/commands.cfg

# restart the service on a remote server

define command{

        command_name restart-mysql

        command_line /usr/bin/ssh nagios@$HOSTADDRESS$ "sudo /usr/local/nagios/libexec/eventhandlers/restart-mysql $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$"

        }

# 配置主機監控檔案

[[email protected] etc]# vi servers/mysql.cfg

# 省略主機定義和其他服務定義

define service{

        use                             generic-service         ; Name of service template to use

        host_name                       MySQL

        service_description             MySQL

        check_command                   check_nrpe!check_mysql

        notifications_enabled           1

        event_handler_enabled           1

        event_handler                   restart-mysql

        }

這個腳本理論上在服務轉入硬态故障之前可以重新開機MySQL服務以修複故障,這裡包含了首次重新開機沒有成功的情況。須注意的是事件處理将隻是第一次進入硬态緊急狀态時才會被觸發,這将阻止Nagios在服務一直處于硬态故障狀态時反複地重新開機MySQL服務。

繼續閱讀