關閉selinux
注意:(如沒有,可跳過)
修改selinux配置:
getenforce
setenforce 0
如果要使用selinux,需要修改如下
chcon -R -t https_sys_content_t /usr/local/nagios/sbin
chcon -R -t https_sys_content_t /usr/local/nagios/share
1、安裝必備軟件:
yum -y install httpd php gcc glibc glibc-common gd gd-devel openssl-devel xinetd
2、下載相關程序:
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
創建Nagios用戶及組:
/usr/sbin/useradd nagios -s /bin/bash
創建一個名爲nagcmd的用戶組,用於從web接口執行外部命令。將Nagios用戶和Apache用戶加入組中:
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -G nagcmd nagios
/usr/sbin/usermod -G nagcmd apache
注:上面的apache是Apache用戶所屬的組,如有不同請自行調整。
編譯安裝nagios:
tar zxf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
注:make install 用於安裝主要的程序、CGI及HTML文件
make install-init 用於生成init啓動腳本
make install-config 用於安裝示例配置文件
make install-commandmode 用於設置相應的目錄權限
make install-webconf 用於安裝Apache配置文件
配置Nagios Web界面登陸帳號及密碼:
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
安裝nagios插件:
tar xzf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make && make install
啓動前先檢查下配置文件是否正確:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
啓動nagios服務:
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
查看Nagios運行狀態:
/usr/local/nagios/bin/nagiostats
加入nagios到啓動組:
chkconfig --add nagios
chkconfig nagios on
注意:如果運行/etc/init.d/nagios restart 出現Starting nagios:This account is currently not available.
執行:
usermod -s /bin/bash nagios
給用戶指定默認shell。
登陸nagios:
重啓apache服務:
/etc/init.d/httpd restart
http://localhost/nagios
安裝NRPE:
注意:由於NRPE是通過SSL方式在監控和被監控主機上進行數據傳輸的,所以必須先安裝ssl相關的軟件包。
tar zxf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
編輯:
vi /etc/xinetd.d/nrpe
only_from = 127.0.0.1 #修改成nagios服務器的地址
添加nrpe後臺服務端口到/etc/services文件中:
vi /etc/services
nrpe 5666/tcp #NRPE
重啓xinetd服務:
service xinetd restart
注意:
如果開啓了防火牆,打開防火牆規則:
iptables -I RH-Firewall-1-INPUT -p tcp -m tcp --dport 5666 -j ACCEPT
保存新規則:
service iptabls save
驗證NRPE是否正確安裝:
/usr/local/nagios/libexec/check_nrpe -H localhost
注:如果成功,會返回NRPE的版本號。
監控主機上的操作:
安裝nagios插件和nrpe,步驟同上。
nagios服務器添加nrpe命令模板:
vi /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 60
}
新建存放主機文件的目錄:
vi /usr/local/nagios/etc/nagios.cfg
cfg_dir=/usr/local/nagios/etc/servers
mkdir /usr/local/nagios/etc/servers
chown -R nagios.nagios /usr/local/nagios/etc/servers
在模板裏添加一個host主機模板包含pnp:
vi /usr/local/nagios/etc/objects/templates.cfg
define host{
name linux-server ; The name of this host template
use generic-host,hosts-pnp ; This template inherits other values from the generic-host template
check_period 24x7 ; By default, Linux hosts are checked round the clock
check_interval 5 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-host-alive ; Default command to check Linux hosts
notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day
; Note that the notification_period variable is being overridden from
; the value that is inherited from the generic-host template!
notification_interval 120 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
在模板裏添加一個pnp服務模板:
vi /usr/local/nagios/etc/objects/templates.cfg
define service{
name pnp-service ; The name of this service template
use generic-service,services-pnp ; Inherit default values from the generic-service definition
max_check_attempts 4 ; Re-check the service up to 4 times in order to determine its final (hard) state
normal_check_interval 5 ; Check the service every 5 minutes under normal conditions
retry_check_interval 1 ; Re-check the service every minute until a hard state can be determined
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
創建一個主機配置文件:
vi /usr/local/nagios/etc/servers/192.168.56.151.cfg
修改聯繫人信息:
vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name backkom ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias system Admin ; Full name of user
email [email protected] ;<<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
把用戶加入admin組:
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin,backkom
}
創建主機組:
vi /usr/local/nagios/etc/objects/groups.cfg
cfg_file=/usr/local/nagios/etc/objects/groups.cfg
define hostgroup{
hostgroup_name server; The name of the hostgroup
alias App server; Long name of the group
members server1,server2,app1,app2,db1,db2 }