1. 安裝前的準備:
(1) 安裝必要的插件:
#yum -y install gcc glibc glibc-common gd gd-devel libxml2 libxml2-devel
(2) 創建nagios用戶和用戶組
#useradd -s /sbin/nologin nagios
#mkdir /opt/app/nagios
#chown -R nagios:nagios /opt/app/nagios
(3) 開啓系統的senmail服務
在nagios監控服務器上開啓sendmail服務的主要作用是讓nagios在檢測到故障時可以發送報警郵件。目前幾乎所有的linux發行版本都默認自帶了sendmail服務,所以,在安裝系統時,只需開啓sendmail服務即可,並不需要在sendmail上進行任何配置;可以用下邊的命令進行測試能否正常發送郵件:
#echo “hello” | mail 1352206****@139.com
2. 編譯安裝nagios
#cd /opt/software
# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios.3.5.0.tar.gz
#tar zxvf nagios-3.5.0.tar.gz
#cd nagios
#./configure --prefix=/opt/app/nagios
#make all
#make install (安裝nagios主程序的CGI和HTML文件)
#make install-init (在/etc/init.d/目錄下創建nagios啓動腳本)
#make install-commandmode (配置目錄權限)
#make install-config (安裝nagios示例配置文件,路徑爲/opt/app/nagios/etc)
設置開機自啓動:
#chkconfig - -add nagios#chkconfig --level 35 nagios on
3. 安裝nagios插件
#cd /opt/software
#wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-
1.4.14.tar.gz
#tar zxvf nagios-plugins-1.4.14.tar.gz
#cd nagios-plugins-1.4.14
#./configure --prefix=/opt/app/nagios
#make
#make install
插件安裝路徑最好和nagios安裝路徑一致,這樣安裝完插件後會在nagios主程序目錄(即/opt/app/nagios下的libexec目錄)下生成很多可執行文件,這些就是nagios所需要的插件。
4. 安裝nagios漢化插件
#cd /opt/software
#wget http://jaist.dl.sourceforge.net/project/nagios-cn/sourcecode/zh_CN%203.2.3/nagios-
cn-3.2.3.tar.bz2
#tar jxvf nagios-cn-3.2.3.tar.bz2
#cd nagios-cn-3.2.3
#./configure --prefix=/opt/app/nagios
#make all
#make install
5. 安裝與配置apache和php
Apache不是安裝nagios所必須的,但是nagios提供了web監控頁面,通過web監控界面可以清晰的看到被監控的主機和資源的運行狀態,所以,安裝一個web服務是很必要的,可選的web服務器有apache和nginx等,這裏選擇apache,操作過程如下:
(1) 安裝apache和php
首先安裝apache:
#cd /opt/software
#tar zxvf httpd-2.2.4.tar.gz
#cd httpd-2.2.4
#./configure --prefix=/opt/app/apache2 --enable-modules=so --enable-rewrite
#make
#make install
#tar zxvf php-5.2.10.tar.gz
#cd php-5.2.10
#./configure --prefix=/opt/app/php5 --with-apxs2=/opt/app/apache2/bin/apxs
#make ZEND_EXTRA_LIBS='-liconv'
#make install
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
(2)配置apache
首先在apache的配置文件httpd.conf中修改apache進程的啓動用戶爲nagios,即找到:
User nobody
Group nobody
修改爲:
User nagios
Group nagios
DirectoryIndex index.html index.html.var
修改爲:
DirectoryIndex index.html index.php
接着增加如下內容:
AddType application/x-httpd-php .php
爲了安全起見,一般要求必須經過授權才能訪問nagios的web監控頁面,因此需要增加驗證配置,即在httpd.conf文件的最後添加如下信息:
#setting for nagios
ScriptAlias /nagios/cgi-bin /opt/app/nagios/sbin
<Directory "/opt/app/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /opt/app/nagios/etc/htpasswd
Require valid-user
</Directory>
Alias /nagios /opt/app/nagios/share
<Directory "/opt/app/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /opt/app/nagios/etc/htpasswd
Require valid-user
</Directory>
在配置文件最後加上一句:
AddDefaultCharset utf-8 (此語句能解決中文漢化後,nagios界面亂碼的問題)
(3) 創建apache目錄驗證文件
在上面的配置中,指定了目錄驗證文件hapassword,下面創建一個這個樣的文件:
#/opt/app/apache2/bin/htpassword -c /opt/app/nagios/etc/htpasswd test
輸入兩次密碼即可;(第一次添加賬號,需要-c參數,第二次以後就不能使用-c參數了,否則,前邊的賬號都會被覆蓋掉)
當通過http://ip/nagios/訪問web監控界面時,就需要輸入用戶名和密碼了。
(4)啓動apache服務
#/opt/app/apache2/bin/apachectl -t
#/opt/app/apache2/bin/apachectl start
關閉防火牆和selinux,然後就可以通過web界面,看到nagios默認的監控界面了;
第二部分:配置主機是否存活
1. 主配置文件nagios.cfg的配置主配置文件的內容很多,對於這個版本,我們需要修改和添加的主要是對象配置文件,即:
log_file=/opt/app/nagios/var/nagios.log
cfg_file=<file_name>
cfg_file=/opt/app/nagios/etc/objects/commands.cfg (命令定義文件)
cfg_file=/opt/app/nagios/etc/objects/contacts.cfg (聯繫人信息定義文件)
cfg_file=/opt/app/nagios/etc/objects/contactgroups.cfg (添加此行 聯繫人組定義文件)
cfg_file=/opt/app/nagios/etc/objects/hosts.cfg (添加此行 主機定義文件)
cfg_file=/opt/app/nagios/etc/objects/hostgroups.cfg (添加此行 主機組定義文件)
cfg_file=/opt/app/nagios/etc/objects/services.cfg (添加此行 服務定義文件)
cfg_file=<file_name>
cfg_file=/opt/app/nagios/etc/objects/commands.cfg (命令定義文件)
cfg_file=/opt/app/nagios/etc/objects/contacts.cfg (聯繫人信息定義文件)
cfg_file=/opt/app/nagios/etc/objects/contactgroups.cfg (添加此行 聯繫人組定義文件)
cfg_file=/opt/app/nagios/etc/objects/hosts.cfg (添加此行 主機定義文件)
cfg_file=/opt/app/nagios/etc/objects/hostgroups.cfg (添加此行 主機組定義文件)
cfg_file=/opt/app/nagios/etc/objects/services.cfg (添加此行 服務定義文件)
#cfg_file=/opt/app/nagios/etc/objects/servicegroup.cfg (註釋掉此行)
cfg_file=/opt/app/nagios/etc/objects/timeperiods.cfg (時間週期定義文件)
cfg_file=/opt/app/nagios/etc/objects/templates.cfg
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg (註釋掉此行)
在web界面下重啓nagios,停止主機/服務器檢查等操作,默認值爲:
check_external_commands=1
根據自己的情況定義這個命令檢查時間間隔,默認值爲1秒:
command_check_interval=10s
2. 修改cgi配置文件cgi.cfg。這裏只列出被修改過的地方
cfg_file=/opt/app/nagios/etc/objects/timeperiods.cfg (時間週期定義文件)
cfg_file=/opt/app/nagios/etc/objects/templates.cfg
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg (註釋掉此行)
在web界面下重啓nagios,停止主機/服務器檢查等操作,默認值爲:
check_external_commands=1
根據自己的情況定義這個命令檢查時間間隔,默認值爲1秒:
command_check_interval=10s
2. 修改cgi配置文件cgi.cfg。這裏只列出被修改過的地方
main_config_file=/opt/app/nagios/etc/nagios.cfg
physical_html_path=/opt/app/nagios/share
#如果有多個用戶,中間用逗號隔開
authorized_for_system_information=test,nagiosadmin
authorized_for_configuration_information=test,nagiosadmin
authorized_for_system_commands=test,nagiosadmin
authorized_for_all_services=test,nagiosadmin
authorized_for_all_hosts=test,nagiosadmin
authorized_for_all_services_commands=test,nagiosadmin
#如果有多個用戶,中間用逗號隔開
authorized_for_system_information=test,nagiosadmin
authorized_for_configuration_information=test,nagiosadmin
authorized_for_system_commands=test,nagiosadmin
authorized_for_all_services=test,nagiosadmin
authorized_for_all_hosts=test,nagiosadmin
authorized_for_all_services_commands=test,nagiosadmin
authorized_for_all_host_commands=test,nagiosadmin
這裏指定用戶“test”可以通過瀏覽器操作nagios服務的關閉、重啓等各種操作。
3. 主機定義文件的配置
#vim objects/hosts.cfg
這裏指定用戶“test”可以通過瀏覽器操作nagios服務的關閉、重啓等各種操作。
3. 主機定義文件的配置
#vim objects/hosts.cfg
define host{
host_name cms-1 #監控的名字
alias 192.168.1.38 #IP
address 192.168.1.38
check_command check-host-alive
check_interval 5
retry_interval 1
max_check_attempts 5
check_period 24x7
process_perf_data 0
retain_nonstatus_information 0
contact_groups cms #組的名字
notification_interval 30
notification_period 24x7
notification_options d,u,r
}
define host{
host_name cms-2
alias 192.168.1.78
address 192.168.1.78
check_command check-host-alive
check_interval 5
retry_interval 1
max_check_attempts 5
check_period 24x7
process_perf_data 0
retain_nonstatus_information 0
contact_groups cms #組的名字
notification_interval 30
notification_period 24x7
notification_options d,u,r
}
4. 主機組定義文件配置
#vim objects/hostgroups.cfg
host_name cms-1 #監控的名字
alias 192.168.1.38 #IP
address 192.168.1.38
check_command check-host-alive
check_interval 5
retry_interval 1
max_check_attempts 5
check_period 24x7
process_perf_data 0
retain_nonstatus_information 0
contact_groups cms #組的名字
notification_interval 30
notification_period 24x7
notification_options d,u,r
}
define host{
host_name cms-2
alias 192.168.1.78
address 192.168.1.78
check_command check-host-alive
check_interval 5
retry_interval 1
max_check_attempts 5
check_period 24x7
process_perf_data 0
retain_nonstatus_information 0
contact_groups cms #組的名字
notification_interval 30
notification_period 24x7
notification_options d,u,r
}
4. 主機組定義文件配置
#vim objects/hostgroups.cfg
define hostgroup {
hostgroup_name cms #輸入監控組的名稱
alias nagios admin
members cms-1,cms-2 #加入監控機器的名字
alias nagios admin
members cms-1,cms-2 #加入監控機器的名字
}
5. 服務定義文件的配置
#vim objects/services.cfg
5. 服務定義文件的配置
#vim objects/services.cfg
define service {
host_name stoneage-1
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups stoneage
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name stoneage-2
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups stoneage
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
6. 聯繫人定義文件的配置
#vim objects/contacts.cfg
define contact{
contact_name test
use generic-contact
alias Nagios Admin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email 這個命令在command.cfg中定義
host_notification_commands notify-host-by-email 這個命令在command.cfg中定義
email 137777****@139.com
}
刪除contactsgroup的配置
7. 聯繫人組定義文件的配置
#vim objects/contactgroups.cfg
define contactgroup {
contactgroup_name stoneage
alias system administrator group
members test
}
8. 驗證配置文件的正確性
# /opt/app/nagios/bin/nagios -v /opt/app/nagios/etc/nagios.cfg
如果驗證沒有問題,就啓動nagios和apache,啓動方法如下:
#/etc/init.d/nagios start
#/opt/app/apache2/bin/apachectl start
host_name stoneage-1
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups stoneage
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
define service {
host_name stoneage-2
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups stoneage
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}
6. 聯繫人定義文件的配置
#vim objects/contacts.cfg
define contact{
contact_name test
use generic-contact
alias Nagios Admin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email 這個命令在command.cfg中定義
host_notification_commands notify-host-by-email 這個命令在command.cfg中定義
email 137777****@139.com
}
刪除contactsgroup的配置
7. 聯繫人組定義文件的配置
#vim objects/contactgroups.cfg
define contactgroup {
contactgroup_name stoneage
alias system administrator group
members test
}
8. 驗證配置文件的正確性
# /opt/app/nagios/bin/nagios -v /opt/app/nagios/etc/nagios.cfg
如果驗證沒有問題,就啓動nagios和apache,啓動方法如下:
#/etc/init.d/nagios start
#/opt/app/apache2/bin/apachectl start