您好,登錄后才能下訂單哦!
實(shí)驗(yàn)需求:在服務(wù)器(192.168.100.1)上搭建nagios服務(wù),監(jiān)控本機(jī)和遠(yuǎn)端主機(jī)(192.168.100.2)上的主機(jī)資源和網(wǎng)絡(luò)服務(wù)
實(shí)現(xiàn):
一.監(jiān)控端(192.168.100.1)配置
1.安裝相關(guān)的軟件包
# yum install httpd php gcc glibc glibc-common gd gd-devel libpng libjpeg zlib
2.創(chuàng)建相關(guān)的用戶和組
# 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.創(chuàng)建登錄nagios的web頁面授權(quán)用戶
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //用戶名最好使用nagiosadmin,因?yàn)榕渲梦募卸x的管理員名稱就是它。
# 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.啟動(dòng)nagios服務(wù)
# 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/
二.監(jiān)控端(192.168.100.1)配置nagios監(jiān)控本機(jī)
1.定義監(jiān)控命令
# 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
}
默認(rèn)已經(jīng)定義一個(gè)命令叫做check-host-alive,當(dāng)執(zhí)行這個(gè)命令的時(shí)候,實(shí)際上就是執(zhí)行/usr/local/nagios/libexec/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
其中/usr/local/nagios/libexec/目錄很長,而且經(jīng)常使用,所以被定義為宏變量,名為$USER1$。該變量在/usr/local/nagios/etc/resource.cfg中定義,$HOSTADDRESS$是執(zhí)行命令時(shí)要傳遞給check_ping的參數(shù)
根據(jù)需要按照既定的格式定義自己需要的命令:
define command{
command_name //定義監(jiān)控命令的名字(自定義)
command_line //定義命令具體實(shí)現(xiàn)的功能
}
例:自定義監(jiān)控對象,檢查NFS服務(wù)是否在運(yùn)行
define command{
command_name check_nfs
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p 2049
}
定義的命令名稱為check_nfs,實(shí)際上是調(diào)用check_tcp命令檢查2049端口
2.定義監(jiān)控對象
# vim /usr/local/nagios/etc/objects/localhost.cfg //localhost.cfg相當(dāng)于是一個(gè)監(jiān)控LINUX主機(jī)的模板,如果需要監(jiān)控其他LINUX主機(jī),可以把這個(gè)文件復(fù)制一份進(jìn)行修改。
define host{
use linux-server //監(jiān)控使用的模板
host_name localhost //被監(jiān)控主機(jī)的主機(jī)名
alias localhost //被監(jiān)控主機(jī)的別名
address 127.0.0.1 //被監(jiān)控主機(jī)的IP地址
}
……
define service{
use local-service
host_name localhost
service_description NFS
check_command check_nfs
notifications_enabled 0
}
3.修改主配置文件(加載監(jiān)控對象配置文件)
# vim /usr/local/nagios/etc/nagios.cfg //默認(rèn)監(jiān)控本機(jī),不需要改動(dòng)
……
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.檢查配置文件是否有語法錯(cuò)誤
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
5.重啟nagios服務(wù)
# service nagios restart
6.登錄web監(jiān)控頁面測試
http://localhost/nagios/
三.監(jiān)控遠(yuǎn)程主機(jī)(192.168.100.2)公有數(shù)據(jù)
1.新建監(jiān)控對象文件
# cd /usr/local/nagios/etc/object
# cp -p localhost.cfg webserver1.cfg //名稱自定義
2.修改監(jiān)控對象配置文件
# vim webserver1.cfg
define host{
use linux-server
host_name webserver1 //修改被監(jiān)控端主機(jī)名
alias webserver1
address 192.168.100.2 //修改被監(jiān)控端IP
}
……
把主機(jī)組注釋掉或刪除掉
#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
# }
后續(xù)部分的服務(wù),把localhost改為webserver1
3.修改主配置文件加載監(jiān)控對象配置文件
# vim /usr/local/nagios/etc/nagios.cfg
……
cfg_file=/usr/local/nagios/etc/objects/webserver1.cfg
4.重啟nagios服務(wù)即可
四.監(jiān)控遠(yuǎn)程主機(jī)(192.168.100.2)私有數(shù)據(jù)
4.1 被監(jiān)控端配置
(1)在被控端創(chuàng)建相關(guān)用戶和組
# 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)啟動(dòng)NRPE
# vim /etc/xinetd.d/nrpe
……
only_from = 127.0.0.1 192.168.100.1 //添加監(jiān)控端IP
vim /etc/services
……
nrpe5666/tcp# NRPE //添加在最后
# service xinetd restart
# netstat -tulnp | grep 5666
tcp 0 0 :::5666 :::* LISTEN 2075/xinetd
(5)檢查NRPE是否已經(jīng)運(yùn)行
# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12 //顯示版本則成功
(6)定義監(jiān)控命令
# 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 監(jiān)控端配置
(1)安裝NRPE
# tar zxvf nrpe-2.12.tar.gz
# cd nrpe-2.12/
# ./configure
# make
# make install
# make install-plugin
(2)檢測是否可以和被監(jiān)控端進(jìn)行通信
# /usr/local/nagios/libexec/check_nrpe -H 192.168.100.2
NRPE v2.12 //顯示遠(yuǎn)程主機(jī)版本則成功
(3)定義NRPE監(jiān)控命令
# vim /usr/local/nagios/etc/object/commands.cfg
……
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
……
(4)定義監(jiān)控的內(nèi)容
# 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
}
調(diào)用check_nrpe命令,在遠(yuǎn)程主機(jī)webserver1上執(zhí)行check_users的命令,check_users命令在遠(yuǎn)程主機(jī)/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服務(wù),查看web頁面
# service nagios restart
http://localhost/nagios/
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。