十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
实验需求:在服务器(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/