1、安裝前準備
yum install libgd libgb-devel libpng libpng-devel libjpeg libjpeg-devel zlib zlib-devel gd gd-devel
創建用戶和用戶組
useradd -s /sbin/nologin nagios
mkdir /home/apps/nagios
chown -R nagios.nagios /home/apps/nagios
開啓sendmail服務
service sendmail start
2、安裝nagios核心
wget http://heanet.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.2.0/nagios-3.2.0.tar.gz
tar zxvf /nagios-3.2.0.tar.gz
cd nagios-3.2.0
./configure --prefix=/home/apps/nagios //安裝路徑/home/apps/nagios
make all
#make install安裝nagios主程序的CGI和HTML文件
make install
#make install-init命令可以在/etc/rc.d/init.d目錄下創建nagios啓動腳本
make install-init
#make install-commandmode命令配置目錄權限
make install-commandmode
#make install-config命令用來安裝nagios示例配置文件,這裏安裝路徑爲/home/apps/nagios
make install-config
設置開機啓動
chkconfig --add nagios
chkconfig --level 35 nagios on
chkconfig --list nagios //查看設置是否生效
nagios 0:關閉 1:關閉 2:關閉 3:啓用 4:啓用 5:啓用 6:關閉
3、安裝nagios插件
wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz#_ga=1.169254566.64720271.1463550979
tar zxvf nagios-plugins-2.1.1.tar.gz
cd nagios-plugins-2.1.1
./configure --prefix=/home/apps/nagios
make
make all //安裝完插件後會在nagios主程序目錄(即/home/apps/nagios下的libexec目錄)下生成很多執行文件,這些都是nagios所需的插件
4、安裝nagios漢化插件
wget http://jaist.dl.sourceforge.net/project/nagios-cn/sourcecode/zh_CN%203.2.0/nagios-cn-3.2.0.tar.bz2
tar jxvf nagios-cn-3.2.0.tar.bz2
cd nagios-cn-3.2.0
./configure --prefix=/home/apps/nagios
make all
make install
5、安裝配置apache、php
(1)yum安裝apache、php
yum install httpd
yum install php
(2)配置apache
配置文件路徑/etc/httpd/conf/httpd.conf
把
User nobody
Group #-1
修改爲:
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文件的最後添加如下信息:
ScriptAlias /nagios/cgi-bin "/home/apps/nagios/sbin"
<Directory "/home/apps/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile "/home/apps/nagios/etc/htpasswd"
Require valid-user
</Directory>
Alias /nagios "/home/apps/nagios/share"
<Directory "/home/apps/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile "/home/apps/nagios/etc/htpasswd"
Require valid-user
</Directory>
(3)創建apache目錄驗證文件
在上面的配置中,指定了目錄驗證文件htpasswd,下面創建這樣一個文件:
創建用戶monitor
htpasswd -c /home/apps/nagios/etc/htpasswd monitor
然後輸入密碼,創建完成。
這樣就在/home/apps/nagios/etc/htpasswd目錄下創建了一個htpasswd驗證文件,對應的用戶爲monitor。
通過http://ip/nagios/訪問web監控界面時就需要輸入用戶名和密碼。
(4)啓動apache
service httpd start
然後通過http://ip/nagios/登錄nagios監控界面,如果httpd啓動後,無法正常訪問web,查看httpd log有哪些錯誤,然後做相應的修改。如果nagios登錄時,輸入的用戶名、密碼正確,但無法登錄進去。可能是selinux禁用了某些服務。把selinux關閉,執行setenforce 0。永久關閉selinux,把/etc/selinux/config中的SELINUX設置成disabled,SELINUX=disabled。然後重啓httpd
6、nagios配置
/home/apps/nagios/etc/objects
(1)hosts.cfg文件
此文件默認情況下不存在,需要手動創建。
hosts.cfg主要用來指定被監控的主機地址及相關屬性信息。一個配置好的示例如下:
########### 360xh06 #####################
define host{
use linux-server,hosts-pnp
host_name 360xh06
alias web
address 10.9.0.25
}
########### 360xh07 #####################
define host{
use linux-server,hosts-pnp
host_name 360xh07
alias drawboard
address 10.9.0.27
}
define hostgroup{ #定義一個主機組
hostgroup_name 360xh-servers #主機組名稱,可以隨意指定
alias 360xh servers #主機組別名
members 360xh06,360xh07 #主機組成員,其中360xh06、360xh07就是上面定義
#的兩個主機
}
(2)services.cfg文件
此文件在默認情況下也不存在,需要手動創建。services.cfg文件主要用於定義監控的服務和主機資源,列如監控HTTP服務、FTP服務、主機磁盤空間、主機系統負載、IO、內存等。
一個配置好的示例:
define service{
use local-service,services-pnp
host_name 360xh06
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,services-pnp
host_name 360xh07
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,services-pnp
host_name 360xh06
service_description SSH
check_command check_ssh
}
define service{
use local-service,services-pnp
host_name 360xh0
service_description SSH
check_command check_ssh
}
define service{
use local-service,services-pnp
host_name 360xh06
service_description SSHD
check_command check_tcp!22
}
define service{
use local-service,services-pnp
host_name 360xh07
service_description SSHD
check_command check_tcp!22
nagios報告彙總中因爲告警太多顯示不出來,可以通過設置limit值來顯示
http://nagios.dtdt2.com/nagios/cgi-bin/summary.cgi?report=1&displaytype=1&timeperiod=last7days&smon=9&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=9&eday=23&eyear=2016&ehour=24&emin=0&esec=0&hostgroup=all&servicegroup=all&host=all&alerttypes=3&statetypes=3&hoststates=7&servicestates=120&limit=25
yum install libgd libgb-devel libpng libpng-devel libjpeg libjpeg-devel zlib zlib-devel gd gd-devel
創建用戶和用戶組
useradd -s /sbin/nologin nagios
mkdir /home/apps/nagios
chown -R nagios.nagios /home/apps/nagios
開啓sendmail服務
service sendmail start
2、安裝nagios核心
wget http://heanet.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.2.0/nagios-3.2.0.tar.gz
tar zxvf /nagios-3.2.0.tar.gz
cd nagios-3.2.0
./configure --prefix=/home/apps/nagios //安裝路徑/home/apps/nagios
make all
#make install安裝nagios主程序的CGI和HTML文件
make install
#make install-init命令可以在/etc/rc.d/init.d目錄下創建nagios啓動腳本
make install-init
#make install-commandmode命令配置目錄權限
make install-commandmode
#make install-config命令用來安裝nagios示例配置文件,這裏安裝路徑爲/home/apps/nagios
make install-config
設置開機啓動
chkconfig --add nagios
chkconfig --level 35 nagios on
chkconfig --list nagios //查看設置是否生效
nagios 0:關閉 1:關閉 2:關閉 3:啓用 4:啓用 5:啓用 6:關閉
3、安裝nagios插件
wget http://nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz#_ga=1.169254566.64720271.1463550979
tar zxvf nagios-plugins-2.1.1.tar.gz
cd nagios-plugins-2.1.1
./configure --prefix=/home/apps/nagios
make
make all //安裝完插件後會在nagios主程序目錄(即/home/apps/nagios下的libexec目錄)下生成很多執行文件,這些都是nagios所需的插件
4、安裝nagios漢化插件
wget http://jaist.dl.sourceforge.net/project/nagios-cn/sourcecode/zh_CN%203.2.0/nagios-cn-3.2.0.tar.bz2
tar jxvf nagios-cn-3.2.0.tar.bz2
cd nagios-cn-3.2.0
./configure --prefix=/home/apps/nagios
make all
make install
5、安裝配置apache、php
(1)yum安裝apache、php
yum install httpd
yum install php
(2)配置apache
配置文件路徑/etc/httpd/conf/httpd.conf
把
User nobody
Group #-1
修改爲:
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文件的最後添加如下信息:
ScriptAlias /nagios/cgi-bin "/home/apps/nagios/sbin"
<Directory "/home/apps/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile "/home/apps/nagios/etc/htpasswd"
Require valid-user
</Directory>
Alias /nagios "/home/apps/nagios/share"
<Directory "/home/apps/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile "/home/apps/nagios/etc/htpasswd"
Require valid-user
</Directory>
(3)創建apache目錄驗證文件
在上面的配置中,指定了目錄驗證文件htpasswd,下面創建這樣一個文件:
創建用戶monitor
htpasswd -c /home/apps/nagios/etc/htpasswd monitor
然後輸入密碼,創建完成。
這樣就在/home/apps/nagios/etc/htpasswd目錄下創建了一個htpasswd驗證文件,對應的用戶爲monitor。
通過http://ip/nagios/訪問web監控界面時就需要輸入用戶名和密碼。
(4)啓動apache
service httpd start
然後通過http://ip/nagios/登錄nagios監控界面,如果httpd啓動後,無法正常訪問web,查看httpd log有哪些錯誤,然後做相應的修改。如果nagios登錄時,輸入的用戶名、密碼正確,但無法登錄進去。可能是selinux禁用了某些服務。把selinux關閉,執行setenforce 0。永久關閉selinux,把/etc/selinux/config中的SELINUX設置成disabled,SELINUX=disabled。然後重啓httpd
6、nagios配置
/home/apps/nagios/etc/objects
(1)hosts.cfg文件
此文件默認情況下不存在,需要手動創建。
hosts.cfg主要用來指定被監控的主機地址及相關屬性信息。一個配置好的示例如下:
########### 360xh06 #####################
define host{
use linux-server,hosts-pnp
host_name 360xh06
alias web
address 10.9.0.25
}
########### 360xh07 #####################
define host{
use linux-server,hosts-pnp
host_name 360xh07
alias drawboard
address 10.9.0.27
}
define hostgroup{ #定義一個主機組
hostgroup_name 360xh-servers #主機組名稱,可以隨意指定
alias 360xh servers #主機組別名
members 360xh06,360xh07 #主機組成員,其中360xh06、360xh07就是上面定義
#的兩個主機
}
(2)services.cfg文件
此文件在默認情況下也不存在,需要手動創建。services.cfg文件主要用於定義監控的服務和主機資源,列如監控HTTP服務、FTP服務、主機磁盤空間、主機系統負載、IO、內存等。
一個配置好的示例:
define service{
use local-service,services-pnp
host_name 360xh06
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,services-pnp
host_name 360xh07
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,services-pnp
host_name 360xh06
service_description SSH
check_command check_ssh
}
define service{
use local-service,services-pnp
host_name 360xh0
service_description SSH
check_command check_ssh
}
define service{
use local-service,services-pnp
host_name 360xh06
service_description SSHD
check_command check_tcp!22
}
define service{
use local-service,services-pnp
host_name 360xh07
service_description SSHD
check_command check_tcp!22
nagios報告彙總中因爲告警太多顯示不出來,可以通過設置limit值來顯示
http://nagios.dtdt2.com/nagios/cgi-bin/summary.cgi?report=1&displaytype=1&timeperiod=last7days&smon=9&sday=1&syear=2016&shour=0&smin=0&ssec=0&emon=9&eday=23&eyear=2016&ehour=24&emin=0&esec=0&hostgroup=all&servicegroup=all&host=all&alerttypes=3&statetypes=3&hoststates=7&servicestates=120&limit=25