Nagios編譯安裝的詳細

 

原本想簡簡單單用RPM包搞定Nagios的偷懶想法果然是行不通啊= =||,原來想編譯安裝多麻煩啊~用RPM包當然比較好,可是偏偏我RP裝到了安裝Nagios插件包的時候需要fping以及SNMP::NET-Perl兩個功能包的缺失,在網上怎麼找也找不到,尤其是fping,無論是RHEL和CentOS裏都不集成。到人家官網www.fping.com上找RPM包,哦不,源碼也好,結果人家Download裏一部片空白(當時大腦裏也是一片空白...囧),只好放棄。無奈選擇了源碼安裝Nagios,本來不想多走的路結果一條都沒少走。RPM包...我的RP阿...囧rz
 
系統環境:CentOS 5.0操作系統。Naigos版本爲2.9穩定版。
 
一.安裝Apache服務:
[root@KCentOS5C ~]# yum install httpd*
Dependencies Resolved
=============================================================================
 Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
 httpd                   i386       2.2.3-6.el5.centos.1  base              1.1 M
 httpd-devel             i386       2.2.3-6.el5.centos.1  base              145 k
 httpd-manual            i386       2.2.3-6.el5.centos.1  base              830 k
Installing for dependencies:
 apr-devel               i386       1.2.7-11         base              237 k
 apr-util-devel          i386       1.2.7-6          base               54 k
Transaction Summary
=============================================================================
 
將Apache服務設定爲系統啓動服務
[root@KCentOS5C ~]# chkconfig httpd on

添加Nagios系統用戶和同名組:
[root@KCentOS5C ~]# useradd nagios

準備好Nagios的源代碼包:
[root@KCentOS5C ~]# ll
----------------------------------------------------
total 3712
-rw------- 1 root root     928 Oct  3 10:33 anaconda-ks.cfg
-rw-r--r-- 1 root root   22760 Oct  3 10:33 install.log
-rw-r--r-- 1 root root    2888 Oct  3 10:33 install.log.syslog
-rw-r--r-- 1 root root 1738698 Sep 30 22:42 nagios-2.9.tar.gz
這個就是Nagios主程序的源代碼包

-rw-r--r-- 1 root root 1693979 Sep 30 22:51 nagios-plugins-1.4.10.tar.gz
這個是Nagios-plugin插件程序源代碼包
 
-rw-r--r-- 1 root root  305019 Sep 30 22:58 nrpe-2.9.tar.gz
這個是Nagios遠程本地監測附加功能源代碼包
----------------------------------------------------
 
 
二.安裝Nagios主程序:
1.解壓縮Nagios主程序源代碼包
[root@KCentOS5C ~]# tar -zxvf nagios-2.9.tar.gz
 
2.進入Nagios主程序包的解壓目錄
[root@KCentOS5C ~]# cd nagios-2.9
 
3.預配置Nagios主程序的環境以及指定安裝路徑
[root@KCentOS5C nagios-2.9]# ./configure --prefix=/usr/local/nagios
預配置成功後將返回配置環境信息
----------------------------------------------------
*** Configuration summary for nagios 2.9 04-10-2007 ***:
 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagios
            Embedded Perl:  no
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
                Lock file:  ${prefix}/var/nagios.lock
           Init directory:  /etc/rc.d/init.d
                  Host OS:  linux-gnu
 Web Interface Options:
 ------------------------
                 HTML URL: 
http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):  /bin/traceroute

Review the options above for accuracy.  If they look okay,
type 'make all' to compile the main program and CGIs.
請注意看最後的提示,如果對以上反饋信息接受的話,則需要繼續執行“make all”命令來編譯主程序以及CGI。
-----------------------------------------------------

4.編譯Nagios主程序
[root@KCentOS5C nagios-2.9]# make all
Nagios主程序編譯成功後將返回信息
-----------------------------------------------------
*** Compile finished ***
If the main program and CGIs compiled without any errors, you
can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
如果在編譯當中沒有發生錯誤的話,那麼可以照以下這些命令繼續操作。
 
  make install
     - This installs the main program, CGIs, and HTML files
  執行“make install”命令來對主程序,CGI以及HTML網頁進行安裝。
 
  make install-init
     - This installs the init script in /etc/rc.d/init.d
  執行“install-init”命令來在/etc/rc.d/init.d/目錄中安裝啓動腳本。
 
  make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file
  執行“install-commandmode”命令來安裝和配置外部命令對Nagios主路徑操作的權限。(這裏所謂的外部命令主要是指Apache服務通過CGI來對Nagios的進行的操作。用戶將通過Web以執行CGI程序腳本的方式來對Nagios的檢測結果進行讀取和調用。)
 
  make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios.  Read the HTML documentation for more info
       on doing this.  Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
  執行“make install-config”命令將會在/usr/local/etc/這個Nagios編譯安裝的主配置路徑下安裝示例配置模板,這將對於之後的配置制定幫助很大。
*** Support Notes *******************************************
這些關於支持的信息就不再詳細講了
If you have questions about configuring or running Nagios,
please make sure that you:
     - Look at the sample config files
     - Read the HTML documentation
     - Read the FAQs online at
http://www.nagios.org/faqs
before you post a question to one of the mailing lists.
Also make sure to include pertinent information that could
help others help you.  This might include:
     - What version of Nagios you are using
     - What version of the plugins you are using
     - Relevant snippets from your config files
     - Relevant error messages from the Nagios log file
For more information on obtaining support for Nagios, visit:
*************************************************************
Enjoy.
-----------------------------------------------------

5.安裝Nagios主程序、CGI以及網頁
[root@KCentOS5C nagios-2.9]# make install
安裝完Nagios主程序、CGI和網頁的之後將提示安裝成功信息以及剩餘可操作的步驟
-----------------------------------------------------
*** Main program, CGIs and HTML files installed ***
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
  make install-init
     - This installs the init script in /etc/rc.d/init.d
  make install-commandmode
     - This installs and configures permissions on the
       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
       use Nagios.  Read the HTML documentation for more info
       on doing this.  Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
-----------------------------------------------------

6.安裝Nagios的系統啓動腳本
[root@KCentOS5C nagios-2.9]# make install-init
安裝完Nagios的系統啓動腳本之後將提示腳本安裝成功信息以及剩餘可操作的步驟
-----------------------------------------------------
*** Init script installed ***
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
  make install-commandmode
     - This installs and configures permissions on the
       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
       use Nagios.  Read the HTML documentation for more info
       on doing this.  Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
-----------------------------------------------------

7.將Nagios的啓動腳本添加到系統服務當中去
[root@KCentOS5C ~]# chkconfig --add nagios
 
8.將Nagios服務加入到系統啓動服務當中去
[root@KCentOS5C ~]# chkconfig nagios on
 
9.安裝和配置外部命令對Nagios主路徑的操作權限
[root@KCentOS5C nagios-2.9]# make install-commandmode
安裝配置外操作權限後將提示配置成功信息以及剩餘可操作步驟
-----------------------------------------------------
*** External command directory configured ***
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
  make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios.  Read the HTML documentation for more info
       on doing this.  Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
-----------------------------------------------------

10.將Apache服務宿主用戶加到nagios組裏面
這樣做是爲了讓Apache有適當的權限能夠通過CGI腳本程序對Nagios進行調用,否則Apache將沒有權限調用Nagios,用戶通過Web將無法訪問Nagios處理的所有信息。
[root@KCentOS5C nagios-2.9]# usermod -G nagios apache

11.安裝Nagios的配置模板
[root@KCentOS5C nagios-2.9]# make install-config
安裝完畢Nagios的配置模板後將反饋安裝成功的提示信息。
-----------------------------------------------------
*** Sample config file installed ***
Remember, these are *SAMPLE* config files.  You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.
If you have questions about configuring Nagios properly, please:
       - Look at the sample config files
       - Read the HTML documentation
       - Read the FAQs online at
http://www.nagios.org/faqs
*BEFORE* you post a question to one of the mailing lists.
-----------------------------------------------------
 
 
三.安裝Nagios-plugins插件程序:
1.解壓縮Nagios-plugins插件源代碼包
[root@KCentOS5C ~]# tar -zxvf nagios-plugins-1.4.10.tar.gz
 
2.進入Nagios-plugins插件包目錄
[root@KCentOS5C ~]# cd nagios-plugins-1.4.10
 
3.預配置Nagios-plugin插件程序的環境以及指定安裝路徑
[root@KCentOS5C nagios-plugins-1.4.10]# ./configure --prefix=/usr/local/nagios/
預配置完成的話將會建立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
-----------------------------------------------------
 
4.編譯Nagios-plugin插件程序
[root@KCentOS5C nagios-plugins-1.4.10]# make
 
5.安裝Nagios-plugin插件程序 
[root@KCentOS5C nagios-plugins-1.4.10]# make install
 
6.查看安裝情況
[root@KCentOS5C nagios-plugins-1.4.10]# ll /usr/local/nagios/
-----------------------------------------------------
total 28
drwxrwxr-x 2 nagios nagios 4096 Oct  5 13:25 bin
drwxrwxr-x 2 nagios nagios 4096 Oct  5 19:03 etc
drwxr-xr-x 2 root   root   4096 Oct  5 19:07 libexec

drwxrwxr-x 2 nagios nagios 4096 Oct  5 13:25 sbin
drwxrwxr-x 9 nagios nagios 4096 Oct  5 19:07 share
drwxrwxr-x 4 nagios nagios 4096 Oct  5 19:08 var
-----------------------------------------------------
只要Nagios主路徑下出現libexec目錄就說明安裝成功了,這個目錄裏面存放着就是Nagios插件執行程序。但是有個問題,就是剛安裝好會有一些目錄和文件的屬主是root。這裏的libexec就有這個問題。
 
7.遞歸更改Nagios主路徑的屬主
[root@KCentOS5C nagios-plugins-1.4.10]# chown -R nagios.nagios /usr/local/nagios/

 
8.遞歸增加Nagios主路徑同組用戶讀寫屬性
[root@KCentOS5C nagios-plugins-1.4.10]# chmod -R g+wr /usr/local/nagios/

四.配置Apache服務:
1.備份Apache服務的主配置文件
[root@KCentOS5C ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup
 
2.配置Apache服務的主配置文件
[root@KCentOS5C ~]# vi /etc/httpd/conf/httpd.conf
這裏主要改動以下這些參數:
-----------------------------------------------------
ServerName *:80
配置Apache的服務器名,如果有域名的話請填寫正確的服務器名。
 
Include conf.d/*.conf
確認Apache擴展配置文件的存放路徑。
-----------------------------------------------------

3.察看Nagios網頁配置模板文件
[root@KCentOS5C ~]# less nagios-2.9/sample-config/httpd.conf
-----------------------------------------------------
# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
# Last Modified: 11-26-2005
#
# This file contains examples of entries that need
# to be incorporated into your Apache web server
# configuration file.  Customize the paths, etc. as
# needed to fit your system.
 
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
設定了Nagios的CGI執行目錄對應的系統路徑。
<Directory "/usr/local/nagios/sbin">
#  SSLRequireSSL
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
這裏指定了訪問用戶帳戶庫文件。
   Require valid-user
</Directory>
 
Alias /nagios "/usr/local/nagios/share"
設定了Nagios的網頁URL對應的系統路徑。
<Directory "/usr/local/nagios/share">
#  SSLRequireSSL
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
這裏指定了訪問用戶帳戶庫文件
   Require valid-user
</Directory>
-----------------------------------------------------
基本上這個文件可以直接使用。根據這個配置文件中指定的AuthUserFile項,它指定的是通過Apache訪問Nagios的合法用戶的帳戶名單庫文件,需要使用htpasswd命令對這個名單庫進行用戶的添加。而默認安裝環境下在/usr/local/nagios/etc/目錄下是沒有htpasswd.user這個文件的,所以需要手動添加。請特別注意,用root身份建立的htpasswd.user文件的權限問題,應該在建立該文件完畢後立即更改爲nagios用戶nagios組的所有權,並且要增加Nagios同組用戶讀寫權限。

4.整合Nagios網頁配置文件到Apache中
將Nagios網頁配置文件作爲Apache的擴展配置文件複製改名到Apache的擴展配置文件目錄/etc/httpd/conf.d/目錄下
[root@KCentOS5C ~]# cp nagios-2.9/sample-config/httpd.conf /etc/httpd/conf.d/nagios-httpd.conf
 
5.建立Nagios的網頁訪問用戶帳戶文件(這裏必須要與Nagios網頁配置文件當中AuthUserFile指定的路徑文件名一致)
[root@KCentOS5C ~]# touch /usr/local/nagios/etc/htpasswd.user
 
6.更改Nagios網頁訪問用戶帳戶文件的屬主
[root@KCentOS5C ~]# chown nagios.nagios /usr/local/nagios/etc/htpasswd.users
 
7.對Nagios網頁訪問用戶帳戶文件增加同組成員讀寫權限(這主要是賦給apache這個系統用戶)
[root@KCentOS5C ~]# chmod g+wr /usr/local/nagios/etc/htpasswd.users
 
8.察看Nagios網頁訪問用戶帳戶文件的屬性信息
[root@KCentOS5C ~]# ll /usr/local/nagios/etc/htpasswd.users
-rw-rw-r-- 1 nagios nagios 0 Oct  5 14:05 /usr/local/nagios/etc/htpasswd.users
 
9.添加htpasswd.user用戶
我添加了一個kanecruise用戶,口令是123456。
[root@KCentOS5C ~]# htpasswd -m /usr/local/nagios/etc/htpasswd.user kanecruise
------------------------------------------------------------
New password: 123456
Re-type new password: 123456
Adding password for user kanecruise
------------------------------------------------------------

10.察看htpasswd的使用方法,這裏我就解釋一些比較重要和常用的。
[root@KCentOS5C ~]# htpasswd
-------------------------------------------------------------
Usage:
        htpasswd [-cmdpsD] passwordfile username
        htpasswd -b[cmdpsD] passwordfile username password
        htpasswd -n[mdps] username
        htpasswd -nb[mdps] username password
 
 -c  Create a new file.
     建立新的帳戶庫文件。
 
 -n  Don't update file; display results on stdout.
 
 -m  Force MD5 encryption of the password.
     添加一個MD5口令加密的新用戶。
 
 -d  Force CRYPT encryption of the password (default).
 
 -p  Do not encrypt the password (plaintext).
     添加一個口令不加密的新用戶。
 
 -s  Force SHA encryption of the password.
     添加一個SHA口令加密的新用戶。
 
 -b  Use the password from the command line rather than prompting for it.
 
 -D  Delete the specified user.
     刪除一個指定的用戶
 
On Windows, NetWare and TPF systems the '-m' flag is used by default.
在Windows、NetWare以及TPF這些操作系統中“-m”參數(啓用MD5對口令加密)是默認使用的
On all other systems, the '-p' flag will probably not work.
在其他的所有操作系統中“-p”(不對口令啓用加密)參數將可能會引起失敗。
-------------------------------------------------------------

11.檢查htpasswd帳戶文件
[root@KCentOS5C ~]# cat /usr/local/nagios/etc/htpasswd.users
-------------------------------------------------------------
kanecruise:$apr1$Qwk9h/..$HaRakpabADGZL10dwPcrx1
-------------------------------------------------------------
用戶添加正確,並且口令都以MD5的方式加密了。
 
 
五.Nagios的配置文件:
默認下Nagios編譯安裝後都沒有現成的配置文件,它們都以模板配置文件的方式在/usr/local/nagios/etc/這個主配置路徑下,需要手動複製並配置成可用的配置文件。
1.處理Nagios的主配置文件
Nagios的主配置文件爲nagios.cfg。
[root@KCentOS5C ~]# cp /usr/local/nagios/etc/nagios.cfg-sample /usr/local/nagios/etc/nagios.cfg
 
2.配置Nagios的主配置文件
[root@KCentOS5C ~]# vi /usr/local/nagios/etc/nagios.cfg
主要修改和確認以下幾個較重要的配置項,其餘的類似優化涉及到的配置調整這裏暫不交待了。這次不像上回我用RPM包安裝Nagios那次的配置方式,這次我打算將所有Nagios功能定義配置都集中在localhost.cfg這一個文件當中去,而命令定義文件commands.cfg獨立保存一個文件,因此除了這兩個文件不註釋外,其他的Nagios功能定義文件都保持註釋狀態。
-------------------------------------------------------------
log_file=/usr/local/nagios/var/nagios.log
確認Nagios服務的日誌記錄文件,需要確認此項是解注狀態的。默認編譯安裝後沒有該文件,要手動添加並指定適當的讀寫權限。
 
cfg_file=/usr/local/nagios/etc/commands.cfg
在Nagios中將對應功能插件定義成命令的命令定義文件,需要確認此項是解注狀態的。默認編譯安裝後也沒有該文件,需要從模版複製。
 
cfg_file=/usr/local/nagios/etc/localhost.cfg
設定Naigos對象功能定義的配置文件,需要確認此項是解注狀態的。默認編譯安裝後也沒有該文件,需要從模版複製。
 
cfg_file=/usr/local/nagios/etc/resource.cfg
設定Nagios的資源文件,需要確認此項是解注狀態的。默認編譯安裝後也沒有該文件,需要從模版複製。
 
#cfg_file=/usr/local/nagios/etc/contactgroups.cfg
#cfg_file=/usr/local/nagios/etc/contacts.cfg
#cfg_file=/usr/local/nagios/etc/dependencies.cfg
#cfg_file=/usr/local/nagios/etc/escalations.cfg
#cfg_file=/usr/local/nagios/etc/hostgroups.cfg
#cfg_file=/usr/local/nagios/etc/hosts.cfg
#cfg_file=/usr/local/nagios/etc/services.cfg
#cfg_file=/usr/local/nagios/etc/timeperiods.cfg
#cfg_dir=/usr/local/nagios/etc/servers
#cfg_dir=/usr/local/nagios/etc/printers
#cfg_dir=/usr/local/nagios/etc/switches
#cfg_dir=/usr/local/nagios/etc/routers
以上這些分塊功能的定義配置文件需要保持註釋,因爲這些文件的作用都已經集中到localhost.cfg這一個文件當中去了。
 
nagios_user=nagios
nagios_group=nagios
設定Nagios服務的宿主用戶和組。
 
#check_external_commands=0
check_external_commands=1
設定Nagios外部檢查命令功能開關,默認是值0。將原來的註釋掉,更改爲值1,必須要打開該功能,否則CGI將不能調用。
 
command_check_interval=15s
#command_check_interval=-1
設定命令檢查的時間間隔,將原來值-1的項註釋掉,啓用原來備用值爲15s(15秒)。這項的時間間隔值請按實際需求情況設定。
-------------------------------------------------------------
對Nagios的主日誌文件的操作如下:
手動建立Nagios主日誌文件
[root@KCentOS5C ~]# touch /usr/local/nagios/var/nagios.log

改變Nagios主日誌文件的屬主。
[root@KCentOS5C ~]# chown nagios.nagios /usr/local/nagios/var/nagios.log

添加同組成員的讀寫權限。
[root@KCentOS5C ~]# chmod g+rw /usr/local/nagios/var/nagios.log

查看日誌文件的屬性。
[root@KCentOS5C ~]# ll /usr/local/nagios/var/nagios.log
-rw-rw-r-- 1 nagios nagios 0 Oct  5 15:28 /usr/local/nagios/var/nagios.log

3.處理Nagios的CGI配置文件:
[root@KCentOS5C ~]# cp /usr/local/nagios/etc/cgi.cfg-sample /usr/local/nagios/etc/cgi.cfg
 
4.配置Nagios的CGI配置文件
[root@KCentOS5C ~]# vi /usr/local/nagios/etc/cgi.cfg
這裏主要注意關於訪問用戶身份驗證的配置項的設定。Nagios系統將各種操作行爲的授權用戶分的非常的仔細,所以在實際應用當中可以分的非常細,什麼用戶可以做哪些操作而哪些用戶又沒有權限做哪些操作。但是這裏我只簡單的授權給同一個用戶。請特別注意,這裏的授權用戶的對象必須只能夠是在htpasswd中添加註冊過的用戶。
-------------------------------------------------------------
use_authentication=1
設定啓用身份驗證。
 
authorized_for_system_information=kanecruise
設定系統信息的授權用戶。
 
authorized_for_configuration_information=kanecruise
設定配置信息的授權用戶。
 
authorized_for_system_commands=kanecruise
設定系統命令的授權用戶。
 
authorized_for_all_services=kanecruise
設定全部監測服務的授權用戶。
 
authorized_for_all_hosts=kanecruise
設定全部被監測主機的授權用戶。
 
authorized_for_all_service_commands=kanecruise
設定全部監測服務命令的授權用戶。
 
authorized_for_all_host_commands=kanecruise
設定全部主機命令的授權用戶。
-------------------------------------------------------------
 
5.處理Nagios的命令定義文件
[root@KCentOS5C ~]# cp /usr/local/nagios/etc/commands.cfg-sample /usr/local/nagios/etc/commands.cfg
 
6.處理Nagios的資源文件
[root@KCentOS5C ~]# cp /usr/local/nagios/etc/resource.cfg-sample /usr/local/nagios/etc/resource.cfg

目前來說暫時不需要對Nagios命令定義文件做什麼修改。但是如果要添加新的命令或者修改現有命令的定義的話,則需要修改該文件。
接下來要講的是Nagios的功能定義配置文件。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章