您好,登錄后才能下訂單哦!
實驗需求:在服務器(192.168.100.1)上搭建nagios服務,監控本機和遠端主機(192.168.100.2)上的主機資源和網絡服務
實現:
一.監控端(192.168.100.1)配置
1.安裝相關的軟件包
# yum install httpd php gcc glibc glibc-common gd gd-devel libpng libjpeg zlib
2.創建相關的用戶和組
# useradd -s /sbin/nologin nagios
# passwd nagios
# groupadd nagcmd
# usermod -G nagcmd nagios
# usermod -G nagcmd apache
3.安裝nagios軟件
# tar xzf nagios-3.2.1.tar.gz
# cd nagios-3.2.1
# ./configure --with-command-group=nagcmd
# make all
# make install
# make install-init
# make install-config
# make install-commandmode
# make install-webconf
4.創建登錄nagios的web頁面授權用戶
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //用戶名最好使用nagiosadmin,因為配置文件中定義的管理員名稱就是它。
# service httpd restart
5.安裝nagios插件
# tar xzf nagios-plugins-1.4.11.tar.gz
# cd nagios-plugins-1.4.11
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make
# make install
6.啟動nagios服務
# chkconfig --add nagios
# chkconfig nagios on
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# service nagios start
7.通過Web頁面訪問
# http://localhost/nagios/
二.監控端(192.168.100.1)配置nagios監控本機
1.定義監控命令
# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check-host-alive
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
}
默認已經定義一個命令叫做check-host-alive,當執行這個命令的時候,實際上就是執行/usr/local/nagios/libexec/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
其中/usr/local/nagios/libexec/目錄很長,而且經常使用,所以被定義為宏變量,名為$USER1$。該變量在/usr/local/nagios/etc/resource.cfg中定義,$HOSTADDRESS$是執行命令時要傳遞給check_ping的參數
根據需要按照既定的格式定義自己需要的命令:
define command{
command_name //定義監控命令的名字(自定義)
command_line //定義命令具體實現的功能
}
例:自定義監控對象,檢查NFS服務是否在運行
define command{
command_name check_nfs
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p 2049
}
定義的命令名稱為check_nfs,實際上是調用check_tcp命令檢查2049端口
2.定義監控對象
# vim /usr/local/nagios/etc/objects/localhost.cfg //localhost.cfg相當于是一個監控LINUX主機的模板,如果需要監控其他LINUX主機,可以把這個文件復制一份進行修改。
define host{
use linux-server //監控使用的模板
host_name localhost //被監控主機的主機名
alias localhost //被監控主機的別名
address 127.0.0.1 //被監控主機的IP地址
}
……
define service{
use local-service
host_name localhost
service_description NFS
check_command check_nfs
notifications_enabled 0
}
3.修改主配置文件(加載監控對象配置文件)
# vim /usr/local/nagios/etc/nagios.cfg //默認監控本機,不需要改動
……
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
……
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
4.檢查配置文件是否有語法錯誤
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
5.重啟nagios服務
# service nagios restart
6.登錄web監控頁面測試
http://localhost/nagios/
三.監控遠程主機(192.168.100.2)公有數據
1.新建監控對象文件
# cd /usr/local/nagios/etc/object
# cp -p localhost.cfg webserver1.cfg //名稱自定義
2.修改監控對象配置文件
# vim webserver1.cfg
define host{
use linux-server
host_name webserver1 //修改被監控端主機名
alias webserver1
address 192.168.100.2 //修改被監控端IP
}
……
把主機組注釋掉或刪除掉
#define hostgroup{
# hostgroup_name linux-servers ; The name of the hostgroup
# alias Linux Servers ; Long name of the group
# members localhost ; Comma separated list of hosts that belong to this group
# }
后續部分的服務,把localhost改為webserver1
3.修改主配置文件加載監控對象配置文件
# vim /usr/local/nagios/etc/nagios.cfg
……
cfg_file=/usr/local/nagios/etc/objects/webserver1.cfg
4.重啟nagios服務即可
四.監控遠程主機(192.168.100.2)私有數據
4.1 被監控端配置
(1)在被控端創建相關用戶和組
# useradd nagios
# passwd nagios
(2)安裝nagios插件
# tar xzf nagios-plugins-1.4.6.tar.gz
# cd nagios-plugins-1.4.6
# ./configure
# make
# make install
# chown nagios.nagios /usr/local/nagios
# chown -R nagios.nagios /usr/local/nagios/libexec
(3)安裝NRPE
# tar xzf 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
(4)啟動NRPE
# vim /etc/xinetd.d/nrpe
……
only_from = 127.0.0.1 192.168.100.1 //添加監控端IP
vim /etc/services
……
nrpe5666/tcp# NRPE //添加在最后
# service xinetd restart
# netstat -tulnp | grep 5666
tcp 0 0 :::5666 :::* LISTEN 2075/xinetd
(5)檢查NRPE是否已經運行
# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12 //顯示版本則成功
(6)定義監控命令
# vim /usr/local/nagios/etc/nrpe.cfg
……
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
command[check_sda3]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda3
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
……
# service xinetd restart
4.2 監控端配置
(1)安裝NRPE
# tar zxvf nrpe-2.12.tar.gz
# cd nrpe-2.12/
# ./configure
# make
# make install
# make install-plugin
(2)檢測是否可以和被監控端進行通信
# /usr/local/nagios/libexec/check_nrpe -H 192.168.100.2
NRPE v2.12 //顯示遠程主機版本則成功
(3)定義NRPE監控命令
# vim /usr/local/nagios/etc/object/commands.cfg
……
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
……
(4)定義監控的內容
# vim /usr/local/nagios/etc/object/webserver1.cfg
……
define service{
use local-service
host_name webserver1
service_description Current Users
check_command check_nrpe!check_users
}
調用check_nrpe命令,在遠程主機webserver1上執行check_users的命令,check_users命令在遠程主機/usr/local/nagios/etc/nrpe.cfg中定義。其他配置類似。
define service{
use local-service
host_name webserver1
service_description Current Load
check_command check_nrpe!check_load
}
define service{
use local-service
host_name webserver1
service_description Boot Partition
check_command check_nrpe!check_sda1
}
define service{
use local-service
host_name webserver1
service_description Root Partition
check_command check_nrpe!check_sda3
}
define service{
use local-service
host_name webserver1
service_description Zombie Processes
check_command check_nrpe!check_zombie_procs
}
define service{
use local-service
host_name webserver1
service_description Total Processes
check_command check_nrpe!check_total_procs
}
define service{
use local-service
host_name webserver1
service_description Swap Usage
check_command check_nrpe!check_swap
}
……
(5)重啟nagios服務,查看web頁面
# service nagios restart
http://localhost/nagios/
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。