nagios安裝部署

Contents

一、nagios服務端安裝... 2

1.nagios安裝... 2

2.安裝nagios插件... 3

3.安裝nrpe插件... 3

二、運行nagios. 3

三、添加監控主機... 4

1. windows主機... 4

2.linux主機... 6

四、使用插件... 10

五、自定義通知報警... 10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

一、nagios服務端安裝

1.nagios安裝

1.      創建用戶 nagios

/usr/sbin/useradd nagios

2.      下載nagios4.0.4

http://jaist.dl.sourceforge.net/project/nagios/nagios-4.x/nagios-4.0.4/nagios-4.0.4.tar.gz

3.      解壓

tar  –zxfnagios-4.0.4.tar.gz

4.      進入nagios-4.0.4目錄進行依次執行。

./configure

make all     #compile the main program and CGIs

make install   #this installs  the main program CGIs, and HTML files

make install-init   #this installs the initscript in  /etc/rc.d/init.d

make install-commandmode   #this installs and configures permissions onthe directory for holding the external command file

make install-config      #this installs  SAMPLE config files in /usr/local/nagios/etc/

you’ll have to modify these sample files before you can usenagios ,

 

 

樣例配置文件默認安裝在這個目錄下/usr/local/nagios/etc,這些樣例文件可

以配置Nagios使之正常運行,只需要做一個簡單的修改...

用你擅長的編輯器軟件來編輯這個

/usr/local/nagios/etc/objects/contacts.cfg配置文件,更改email地址

nagiosadmin的聯繫人定義信息中的EMail信息爲你的EMail信息以接收報警內

容。

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

 

#配置web接口

make install-webconf # this installs  the Apache configfile for the Nagios web interface

創建下面命令創建一個nagiosadmin的用戶,用於Nagios的web接口登錄。

htpasswd -c /usr/local/nagios/etc/htpasswd.usersnagiosadmin 

重啓apache,使服務生效。

service httpd restart

 

make install-exfoliation  #this installs  the exfoliation theme for the Nagios webinterface

make install-classicui  # this installs the classictheme for the Nagios web interface

 

2.安裝nagios插件

編譯並安裝插件

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

make

makeinstall

3.安裝nrpe插件

下面在監控linux主機中有詳細說明。

安裝好nrpe後,需要配置

在objects/command.cfg命令定義文件中添加NRPE命令。
[root@KCentOS5C ~]# vi /usr/local/nagios/etc/commands.cfg
-------------------------------------------------------
# NRPE Command
添加NRPE功能命令。

define command(
command_name nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

二、運行nagios

把Nagios 加入到服務列表中,以使之在系統啓動時自動啓動。

chkconfig --add nagios

chkconfig nagios on

 

驗證Nagios的配置文件,如果不報錯,則可以啓動Nagios

 

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

 

1. 初始化腳本:最簡單的啓動nagios守護進程的方式是使用初始化腳本,

/etc/rc.d/init.d/nagios  start

2、 手工方式,可以收到啓動Nagios守護進程,用命令參數 -d

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

3. 使用service

service nagios start

三、添加監控主機

1. windows主機

安裝NSClient++

安裝過程中,只需要修改添加nagios服務器地址,密碼不要填寫。

其他一路下一步,

2.編輯方式打開windows.cfg文件。

vi/usr/local/nagios/etc/objects/windows.cfg    (這個是在nagios服務器主機上配置的)

 

可以只是修改windows.cfg文件裏的對象定義。修改host_name、

alias和address域以符合添加的Windows機器。

定義主機

define host{    

use windows-server ; Inherit default valuesfrom a

Windows server  template (make sure you keep this line!)

host_name  winserver

alias My Windows Server

address 192.168.1.2      # windows主機地址。

}

定義服務

define service{

       use                    generic-service

       host_name  192.168.120.152

        service_description     NSClient++ Version

       check_command          check_nt!CLIENTVERSION

       }  

 

 

 

# Create a service for monitoring theuptime of the server

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

 

define service{

       use                    generic-service

       host_name  192.168.120.152

       service_description     Uptime

       check_command          check_nt!UPTIME

       }

 

# Create a service for monitoring CPU load

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

 

define service{

       use                    generic-service

       host_name  192.168.120.152

       service_description     CPU Load

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

       }

 

 

 

# Create a service for monitoring memoryusage

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

 

define service{

       use                    generic-service

       host_name  192.168.120.152

       service_description     MemoryUsage

       check_command          check_nt!MEMUSE!-w 80 -c 90

       }

 

# Create a service for monitoring C:\ diskusage

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

 

define service{

       use                    generic-service

       host_name  192.168.120.152

       service_description     C:\ DriveSpace

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

       }

 

 

 

# Create a service for monitoring the W3SVCservice

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

 

define service{

       use                    generic-service

       host_name  192.168.120.152

       service_description     W3SVC

       check_command           check_nt!SERVICESTATE!-dSHOWALL -l W3SVC

       }

 

 

 

# Create a service for monitoring theExplorer.exe process

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

 

define service{

       use                    generic-service

       host_name              192.168.120.152

       service_description     Explorer

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

}

修改好後,把windows.cfg,加入nagios.cfg 中

cfg_file=/usr/local/nagios/etc/objects/windows.cfg

2.linux主機

在被監控主機上安裝nagios插件和nrpe

1.添加Nagio用戶

[root@KCentOS5A~]# useradd nagios

2.解壓Nagios-plugins壓縮包

[root@KCentOS5A~]# tar -zxvf nagios-plugins-1.4.10.tar.gz 4.進入Nagios-plugins插件包目錄

[root@KCentOS5A~]# cd nagios-plugins-1.4.10

 

5.預配置Nagios-plugins的安裝路徑

[[email protected]]# ./configure --prefix=/usr/local/nagios

 

6.預配置完成後會反饋信息並生成Makefile

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

config.status:creating po/Makefile

            --with-apt-get-command:

              --with-ping6-command: /bin/ping6-n -U -w %d -c %d %s

               --with-ping-command: /bin/ping-n -U -w %d -c %d %s

                       --with-ipv6: yes

                      --with-mysql: no

                    --with-openssl: yes

                     --with-gnutls: no

                       --with-perl:/usr/bin/perl

             --enable-perl-modules: no

                     --with-cgiurl:/nagios/cgi-bin

               --with-trusted-path:/bin:/sbin:/usr/bin:/usr/sbin

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

 

7.編譯Nagios-plugins插件程序

[[email protected]]# make

 

8.安裝Nagios-plugins插件程序

[[email protected]]# make install

 

9.查看Nagios-plugins插件程序的安裝

[[email protected]]# ll /usr/local/nagios/

total 8

drwxr-xr-x2 root root 4096 Oct 7 01:02 libexec

drwxr-xr-x3 root root 4096 Oct 7 01:02 share

 

10.遞歸更改Nagios主路徑的屬主

[root@KCentOS5A~]# chown -R nagios.nagios /usr/local/nagios/

 

11.檢查Nagios主路徑的屬性

[root@KCentOS5A~]# ll /usr/local/|grep nagios

drwxr-xr-x4 nagios nagios 4096 Oct 7 01:02 nagios

 

[root@KCentOS5A~]# ll /usr/local/nagios/

total 8

drwxr-xr-x2 nagios nagios 4096 Oct 7 01:02 libexec

drwxr-xr-x3 nagios nagios 4096 Oct 7 01:02 share

 

 

四.再在被監控主機上安裝NRPE擴展插件程序:

1.解壓NRPE的壓縮包

[root@KCentOS5A~]# tar -zxvf nrpe-2.9.tar.gz

 

2.進入NRPE包目錄

[root@KCentOS5A~]# cd nrpe-2.9

 

3.預配置NRPE安裝程序

[[email protected]]# ./configure

 

4.預配置完畢後會反饋提示信息以及建立Makefile

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

configure:creating ./config.status

config.status:creating Makefile

config.status:creating src/Makefile

config.status:creating subst

config.status:creating include/config.h

 

***Configuration summary for nrpe 2.9 08-13-2007 ***:

GeneralOptions:

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

NRPEport:    5666

NRPEuser:    nagios

NRPEgroup:   nagios

Nagiosuser: nagios

Nagiosgroup: nagios

 

Reviewthe options above for accuracy. If they look okay,

type'make all' to compile the NRPE daemon and client.

最後提示繼續使用“make all”命令來對NRPE守護程序以及客戶端程序進行編譯。

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

 

5.對NRPE程序進行編譯

[[email protected]]# make all

 

6.編譯成功後會反饋提示信息

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

***Compile finished ***

If theNRPE daemon and client compiled without any errors, you

cancontinue with the installation or upgrade process.

Read thePDF documentation (NRPE.pdf) for information on the next

steps youshould take to complete the installation or upgrade.

在NRPE的包目錄中有一份NRPE.pdf的手冊,接下來就可以參考那份手冊進行後續的操作了。

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

安裝NRPE插件程序,守護程序以及模板守護程序配置文件。

Installthe NRPE plugin (for testing), daemon, and sample daemon config file.

 

7.安裝NRPE插件程序

[[email protected]]# make install-plugin

 

8.安裝NRPE守護程序

[[email protected]]# make install-daemon

 

9.安裝NRPE守護程序配置文件

[[email protected]]# make install-daemon-config

 

10.檢查NRPE程序的安裝

[[email protected]]# ll /usr/local/nagios/

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

total 16

drwxrwxr-x2 nagios nagios 4096 Oct 7 01:16 bin

drwxrwxr-x2 nagios nagios 4096 Oct 7 01:16 etc

drwxr-xr-x2 nagios nagios 4096 Oct 7 01:15 libexec

drwxr-xr-x 3nagios nagios 4096 Oct 7 01:02 share

在被監測主機上配置並運行NRPE:

1.配置NRPE的主配置文件

[root@KCentOS5A ~]# vi /usr/local/nagios/etc/nrpe.cfg

這裏主要交待一些關鍵重要的設定。

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

pid_file=/var/run/nrpe.pid

設定NRPE運行的PID文件,這個默認就設定好了,不用更改。

 

server_port=5666

這個是NRPE守護進程佔用的系統端口。

 

#server_address=0.0.0.0

server_address=192.168.1.9 #這是被控端IP地址

設定系統監聽NRPE的網絡接口。一般設定具體的IP地址,如果要多個網卡的話就用逗號“,”來分隔多個IP地址。如果要監聽系統全部的網絡接口的話,可以使用“0.0.0.0”這個表示全部網絡接口的特殊地址,但是不可以用通配符“*”。

 

nrpe_user=nagios

nrpe_group=nagios

設定NRPE的宿主用戶。

 

allowed_hosts=192.168.1.12 #這是充許監控服務器IP地址

這裏是設定允許與本機進行NRPE交互的主機的IP地址,也就是Nagios監測服務器的地址,如果要指定多個Nagios服務器的話,那麼用逗號“,”來分隔多個IP地址。

在nrpe.cfg中添加需要監控的命令:

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

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

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

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

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

 

啓動守護nrpe進程

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

將NRPE運行添加加入系統啓動腳本中

[root@KCentOS5A ~]# echo "/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.d/rc.local

 

在nagios服務端添加主機信息。並把該主機加入nagios.cfg中

cfg_file=/usr/local/nagios/etc/objects/host160.cfg

 

 

四、使用插件

編寫插件,放在目錄/usr/local/nagios/libexec/下,

例如:

1, 把check_cpu.sh 文件放在libexec目錄下。

修改執行權限。

2.在監控的主機中加入如下命令, 我的是在localhost.cfg 加入:

 define service{

      host_name             153

              use              generic-service

                 check_command       check_nrpe!check_cpu_233

              service_description    check_cpu_233

                 notifications_enabled  1

               event_handler_enabled  1

                 max_check_attempts     3

                check_interval         5

                  retry_check_interval   2

                notification_options   w,u,c

 }

 在nrpe.cfg 中添加 :

command[check_cpu_233]=/usr/local/nagios/libexec/check_cpu.sh–w 60 –c 80

check_cpu.sh 文件需要放在每個需要進行檢查的目錄下。

五、自定義通知報警

指定服務,指定聯繫人,指定通知方式。

爲了實現對指定服務,實現指定聯繫人的通知方式,

在 timeperiods.cfg 中加入發送通知時間

define timeperiod{

       timeperiod_name notify_at_8

       alias once_a_day

       sunday 08:00-09:00

       monday 08:00-09:00

       tuesday 08:00-09:00

       wednesday 08:00-09:00

       thursday 09:00-10:00

       friday 08:00-09:00

       saturday 08:00-09:00

}

定義需要通知的聯繫人:

define contact {

       contact_name notifyadmin

                alias wu

                use generic-contact

               service_notification_commands  notify-service-by-qqemail   #通知命令

                email [email protected]

}

我們在指定的主機中加入服務

define service{

       host_name localhost

                use local-service

                service_descriptionnotify_at_800 

                contacts   notifyadmin  # 指定聯繫人

                check_command   notify_at_800  #定義一個服務檢查命令來觸發通知發送。

                check_period    notify_at_8   #定義檢測時間。

                notification_period  notify_at_8 #檢測間隔

  

}

重啓服務就可以實現在指定時間段發送消息。

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