nagios是 一款功能強大的網絡監視工具,它可以有效的監控windows、linux、unix主機狀態以及路由器交換機的網絡設置,打印機工作狀態等,並將狀態出 現異常的服務及時以郵件、msn短信等形式第一時間通告管理員。由於它是一款遵循GPLv2的開源網絡監控軟件,以其出色的性能低廉的使用成本,深受廣大 用戶的好評。
2.安裝前提
2. 1確定是否有以下組件:
gcc glibc glibc-common gd gd-devel xinetd openssl-devel
proc-devel pcre-devel perl libxml2-devel
直接通過yum安裝:
yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel proc-devel pcre-devel perl libxml2-devel
3.安裝nagios,nagios-plugin
創建nagios用戶
# adduser nagios
安裝nagios
# tar xvf nagios-cn-3.2.0.tar
# cd nagios-cn-3.2.0
# ./configure –prefix=/usr/local/nagios
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
安裝nagios-plugin
# tar xvf nagios-plugins-2.0.tar.gz
# cd nagios-plugins-2.0
# ./configure –prefix=/usr/local/nagios
# make && make install
4.安裝apache
# tar xvf httpd-2.4.9.tar.gz
#cd httpd-2.4.9
#./configure --prefix=/usr/local/apache2
# make && make install
如果安裝提示缺少apr和apr-util包
#tar xvf apr-1.5.0.tar.gz
#cd apr-1.5.0
#./configure –prefix=/usr/local/apr
#make && make install
#tar xvf apr-1.5.0.tar.gz
#cd apr-1.5.0
#./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr
#make && make install
安裝好了之後在安裝apache
#./configure --prefix=/usr/local/apache2 –with-apr-util=/usr/local/apr-util
#make && make install
5.安裝php
#tar xvf php-5.4.10.tar.gz
#cd php-5.4.10
#./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs
#make && make install
6.配置apahe
找到apache 的配置文件/usr/local/apache2/conf/httpd.conf
找到:
User daemon
Group daemon
修改爲
User nagios
Group nagios
然後找到
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
修改爲
<IfModule dir_module>
DirectoryIndex index.html index.php
AddType application/x-httpd-php .php
</IfModule>
爲了安全起見,一般情況下要讓nagios 的web 監控頁面必須經過授權才能訪問,這需要增加驗證配置,即在httpd.conf 文件最後添加如下信息:
#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
創建apache目錄驗證文件
# /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd ssap
7.啓動apache和nagios
啓動apache
# /usr/local/apache2/bin/apachectl start
啓動nagios
Service nagios start
nagios登錄
nagios首頁
8.配置nagios
Nagios安裝完畢後,需要配置nagios,配置文件在/usr/local/nagios/etc目錄下。
文件名或目錄名 |
用途 |
cgi.cfg |
控制CGI訪問的配置文件 |
nagios.cfg |
Nagios 主配置文件 |
resource.cfg |
變量定義文件,又稱爲資源文件,在些文件中定義變量,以便由其他配置文件引用,如$USER1$ |
objects |
objects 是一個目錄,在此目錄下有很多配置文件模板,用於定義Nagios 對象 |
objects/commands.cfg |
命令定義配置文件,其中定義的命令可以被其他配置文件引用 |
objects/contacts.cfg |
定義聯繫人和聯繫人組的配置文件 |
objects/localhost.cfg |
定義監控本地主機的配置文件 |
objects/printer.cfg |
定義監控打印機的一個配置文件模板,默認沒有啓用此文件 |
objects/switch.cfg |
定義監控路由器的一個配置文件模板,默認沒有啓用此文件 |
objects/templates.cfg |
定義主機和服務的一個模板配置文件,可以在其他配置文件中引用 |
objects/timeperiods.cfg |
定義Nagios 監控時間段的配置文件 |
objects/windows.cfg |
監控Windows 主機的一個配置文件模板,默認沒有啓用此文件 |
假設我們nagios安裝在IP爲192.168.195.136的服務器上,需要監控192.168.195.136服務器,將對配置文件做以下修改:
修改localhost.cfg
define host{
use linux-server
host_name Nagios-Server
alias Nagios-Server
address 127.0.0.1
}
define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members Nagios-Server
}
define service{
use local-service
host_name Nagios-Server
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service
host_name Nagios-Server
service_description Root Partition
check_command check_local_disk!20%!10%!/
}
define service{
use local-service
host_name Nagios-Server
service_description Current Users
check_command check_local_users!20!50
}
define service{
use local-service
host_name Nagios-Server
service_description Total Processes
check_command check_local_procs!250!400!RSZDT
}
define service{
use local-service,srv-pnp
host_name Nagios-Server
service_description Current Load
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
define service{
use local-service
host_name Nagios-Server
service_description Swap Usage
check_command check_local_swap!20!10
}
define service{
use local-service
host_name Nagios-Server
service_description SSH
check_command check_ssh
notifications_enabled 0
}
define service{
use local-service
host_name Nagios-Server
service_description HTTP
check_command check_http
notifications_enabled 0
}
新增hosts.cfg
define host{
use linux-server
host_name test-Linux
alias test-Linux
address 192.168.195.137
}
新增service.cfg
define service{
use local-service
host_name test-Linux
service_description check-host-alive
check_command check-host-alive
}
修改nagios.cfg
添加配置文件hosts.cfg,service.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/service.cfg
檢測修改的配置
/usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg
檢測無誤後,重新啓動nagios服務
Service nagios restart
nagios監控頁
注意事項:
1) 如果設置的用戶名不是nagiosadmin,會存在權限問題,不能訪問監控頁面,需要修改cgi.cfg,在nagiosadmin後追加自己設置的用戶名。
nagios登錄用戶權限配置
2)Nagios亂碼問題
主要是apache沒有開啓cgi腳本的緣故,進入apache的主配置文件httpd.conf;
#LoadModule cgid_module modules/mod_cgid.so;
#LoadModule actions_module modules/mod_actions.so;
將上面2行的#去掉,重啓apache就OK了。