#################################################
Nagios自動監控系統
安裝Nagios及插件
(a)安裝依賴包
#yum -y install gcc gcc-c++
(b)創建nagios軟件登錄用戶
#useradd nagios
#groupadd nagcmd
#gpasswd -a nagios nagcmd /把用戶nagios添加到nagcmd組中
#id nagios
(c)安裝Nagios
#tar zxf negios-4.2.4.tar.gz
#cd negios-4.2.4
#./configure --with-nagios-user=nagios \
>--with-nagios-group=nagcmd \
>--with-command-user=nagios \
>--with-command-group=nagcmd
#make all /編譯
#make install /安裝程序
#make install-init /安裝控制腳本
#make install-config /安裝配置 文件目錄下多個$USER1$/etc
#make install-commandmode /調權限
#make install-webconf /部署網站配置
#make install-exfoliation /nagios 新界面
軟件目錄爲
/usr/local/nagios
bin /程序命令
sbin /cgi腳本
etc /配置文件
share /網頁目錄
libexec /監控插件
var /運行數據
安裝nagios插件
#tar xfz nagios-plugins-2.1.4.tar.gz
#cd nagios-plugins-2.1.4/
#./configure && make && make install
啓動nagios監控服務
/etc/rc.d/init.d/nagios start
設置訪問控制頁面的用戶名
/etc/httpd/conf.d/nagios.conf
#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin 用戶名只能是nagiosadmin,想要修改爲別的用戶,需修改別的配置文件
password:123456
#firefox 192.168.4.17/nagios
用戶名:nagisadmin
密碼:123456
監控到的數據 < 警告值 顯示 ok
監控到的數據 > 警告值 < 錯誤值 顯示 WARNING
監控到的數據 > 錯誤值 顯示 critical
/usr/local/nagios/libexec
網站服務運行狀態 ./check_http -H192.168.4.254 -p 80
PING ./check_ping -H192.168.4.254 -c 10,50% -w 20,80% -p 3 /ping 3個包 10ms內丟包率爲50 報警,20ms內丟包率70 報錯
根分區 ./check_disk -w 80% -c 50% -p / /根分區剩餘空間小於80% 報警 小於50% 報錯
監控狀態分爲:
正常 警告 不知道 嚴重錯誤 監控中
OK WARNING UNKNOWN critical pending
cd /usr/local/nagios/etc
nagios.cfg /主配置文件
resource.cfg /宏定義文件
commands.cfg /監控命令配置
localhost.cfg /監控本機對象配置
contacts.cfg /報警收件郵箱設置
timeperiods.cfg /監控時間模版
templates.cfg /監控方式模版
Nagios監控配置實現過程
1)定義監控命令
commands.cfg
define command {
command_name 命令名
command_line 路徑/插件名 參數
}
2)定義監控對象
localhost.cfg
define service{
use local-service
host_name localhost
service_description 監控名
check_command 命令名
}
監控遠端主機
(a)監控公有數據(服務)
1)定義監控命令
command.cfg
define command{
command_name check_web_http
command_line $USER1$/check_tcp -H 192.168.4.19 -p 80
} /監控20主機的http服務的命令
2)定義監控對象
[monitor21]
vim /usr/local/nagios/etc/object/host19.cfg
define host{
use linux-server 模版名 位於templates.cfg
host_name host19 主機名 自定義
address 192.168.4.19
}
define service{
use local-service 模版名 位於templates.cfg
host_name host19 監控哪個主機,必須爲哪個主機的主機名
service-description HTTP 服務描述,自定義
check_command check_web_http 命令名,與定義命令模版的 command_name相同
}
/etc/rc.d/init.d/nagios start /啓動服務
firefox 192.168.4.21/nagios
(b)監控私有數據(系統運行情況)
監控遠端主機的私有數據不能簡單的在監控服務器上使用監控插件命令,需要通過配置被監控主機來實現。
[host19] 被監控主機
1)安裝nagios-plugins 插件
#yum -y install gcc gcc-c++
#tar xfz nagios-plugins-2.1.4.tar.gz
#cd nagios-plugins-2.1.4.tar.gz
#./configure && make && make install
2)安裝nrpe 服務
#useradd nagios
#yum -y install openssl openssl-devel 安裝依賴的加密包
#tar zxf nrpe-3.0.1.tar.gz
#cd nrpe-3.0.1.tar.gz
#./configure
#make all 編譯
#make install 安裝
#make install-config 安裝nrpe配置文件
#make install-plugin 安裝check_nrpe 插件
#make install-init 安裝systemctl啓動
此時查看 ls /usr/local/nagios/libexec/check_nrpe
修改nrpe配置文件 /usr/local/nagios/etc/nrpe.cfg
#sed -i '98s/127.0.0.1/127.0.0.1, 192.168.4.21' /usr/local/nagios/etc/nrpe.cfg
允許本機和192.168.4.21地址主機訪問
#sed -n '288,295p' /usr/local/nagios/etc/nrpe.cfg
查看配置文件中定義的監控命令
command[nrpe_check_users]=/usr/local/nagios/libexec/check_users -w 1 -c 2
command[nrpe_check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[nrpe_check_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[nrpe_check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[nrpe_check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
systemctl start nrpe
cd /usr/local/nagios/libexec/
./check_nrpe -H 127.0.0.1 -c nrpe_check_users 監控本機當前用戶
ok
[monitor21]
配置監控端
安裝nrpe
#yum -y install openssl openssl-devel
#tar zxf nrpe-3.0.1.tar.gz
#cd nrpe-3.0.1.tar.gz
#./configure
#make all
#make install
#make install-plugin /監控端添加nrpe 插件
1)定義監控命令
commands.cfg
define command{
command_name check_host19_users
command_line $USER1$/check_nrpe -H 192.168.4.19 -p 5666 -c nrpe_check_users
}
2)定義監控對象
host19.cfg
define service{
use linux-server
host_name host19
service-description USERS
check_command check_host19_users
}
/etc/rc.d/init.d/nagios start
firefox http://192.168.4.21/nagios