Nagios
Nagios是一款LInux上成熟的监视系统运行状态和网络信息的开源IT基础设施监控系统。Nagios能够监控所指定的本地或远程主机及服务,同时提供异常通知、事件处理等功能。它可以运行在Linux平台,同时还提供了基于浏览器的web界面,以方便系统管理人员查看系统的运行状态,网络状态,各种系统问题及日志异常等。
工作原理
Nagios的功能是监控服务和主机,但是其自身并不包含这些功能,所有的监控、检测功能都是通过各种插件来完成的。启动Nagios后,它会周期性地自动调用插件去检测服务器状态,同时Nagios会维持一个队列,所有插件返回的状态信息都进入队列,Nagios每次都从队列读取信息,进行处理后,再把状态结果通过Web界面显示出来,这是被动模式。常用于监控主机的系统资源,比如系统负载、磁盘使用率、内存使用率、网络状态、系统进程数等。另一种是主动模式,主要是Nagios服务器主动去获取数据,常用于探测URL的监控和服务的状态监控。
Nagios的主要特点:
- 监视本地或远程主机的资源;
- 监视网络服务资源;
- 允许用户编写自己的插件来监控特定的服务;
- 当被监控对象出现异常时,可以通过邮件、短信等方式通知管理人员;
- 可以事先定义事件处理程序,当主机或服务出现故障时自动调用指定的处理程序;
- 可以通过web界面来监控各个主机或服务的运行状态。
系统环境
主机名 | 操作系统 | IP地址 | 服务名 |
---|---|---|---|
nagios | centos7.4 | 192.168.96.81 | nagios-4.3.4.tar.gz、nagios-cn.4.3.4.tar.gz、nagios-plugins-2.2.1.tar.gz、pnp4nagios-0.6.26.tar.gz |
www | centos7.4 | 192.168.96.11 | nrpe-3.1.0.tar.gz |
客户端 | windows 10 | 192.168.96.2 | 网页浏览器 |
nagios/www服务器均关闭防火墙及SElinux功能
setenforece 0
systemctl stop firewalld
软件包下载 密码:yfdi
开始部署
一、安装nagios
1.安装系统环境包、apache、php
yum install -y wget httpd php php-devel php-gd gcc glibc glibc-common gd gd-devel make net-snmp
2.添加nagios用户
useradd -s /sbin/nologin nagios
3.创建nagios安装目录,并修改目录属主信息
mkdir /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/
4.安装nagios软件包
#解压
tar zxvf nagios-4.3.4.tar.gz
cd nagios-4.3.4
#配置
./configure --prefix=/nagios --with-command-group=nagios --with-nagios-group=nagios
#编译安装
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
5.复制软件包自带的插件至nagios/libexec目录下
cp -R contrib/eventhandlers /usr/local/nagios/libexec/
二、nagios-plugins插件
1.解压
tar zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1/
2.配置
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
3.编译及安装
make && make install
三、nagios汉化插件
1.解压
tar zxvf nagios-cn.4.3.4.tar.gz
cd nagios-4.3.4/
2.配置
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
3.编译及安装
make all
make install
四、配置Apache服务
1.为保证nagios安全访问,添加身份验证功能
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
2.编辑Apache服务配置文件
vim /etc/httpd/conf/httpd.conf
Listen 192.168.96.81:80 #41行,监听地址及端口
#Listen 80 #42行,注释该行
User nagios #66行,用户为nagios
Group nagios #67行,用户组为nagios
ServerName www.test01.com:80 #95行,域名
DirectoryIndex index.html index.php #164行,主页类型添加index.php类型
AddType application/x-httpd-php .php #285行,添加php支持
3.添加自启动并启动httpd服务
systemctl enable httpd
systemctl start httpd
4.查看httpd服务信息
netstat -tunlp
5.添加自启动并启动nagios服务
systemctl enable nagios
systemctl start nagios
6.查看ngios服务状态
systemctl status nagios
7.客户端访问nagios监控(http://192.168.96.81/nagios)
五、安装图标pnp4nagios
1.安装软件支持包
yum install perl-Time-HiRes rrdtool rrdtool-perl -y
2.创建pnp4nagios目录
mkdir /usr/local/nagios/pnp4nagios
3.解压
tar zxvf pnp4nagios-0.6.26.tar.gz
cd pnp4nagios-0.6.26/
4.配置
./configure --prefix=/usr/local/nagios/pnp4nagios --with-nagios-user=nagios --with-nagios-group=nagios
5.编译
make all
make fullinstall
6.复制软件包中ssi文件至/pnp4nagios/share目录下
cp -R contrib/ssi /usr/local/nagios/pnp4nagios/share/
7.生成pnp4nagios文件
cd /usr/local/nagios/pnp4nagios/etc/
mv misccommands.cfg-sample misccommands.cfg
mv nagios.cfg-sample nagios.cfg
mv rra.cfg-sample rra.cfg
cd pages/
mv web_traffic.cfg-sample web_traffic.cfg
cd ../check_commands/
mv check_all_local_disks.cfg-sample check_all_local_disks.cfg
mv check_nrpe.cfg-sample check_nrpe.cfg
mv check_nwstat.cfg-sample check_nwstat.cfg
8.加入自启动并启动npcd服务
chkconfig npcd on
service npcd start
9.编辑pnp4nagios各配置文件
cd /usr/local/nagios/pnp4nagios/etc/
vim /usr/local/nagios/pnp4nagios/etc/process_perfdata.cfg
#调整日志级别
LOG_LEVEL = 2
10.编辑nagios.conf配置
vim /usr/local/nagios/etc/nagios.cfg
process_performance_data=1
enable_environment_macros=1
增加如下(如果不直接增加也可以去掉注释自己改)
service_perfdata_command=process-service-perfdata
host_perfdata_command=process-host-perfdata
host_perfdata_file=/usr/local/nagios/pnp4nagios/var/host-perfdata
service_perfdata_file=/usr/local/nagios/pnp4nagios/var/service-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$
host_perfdata_file_mode=a
service_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
service_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file
service_perfdata_file_processing_command=process-service-perfdata-file
11.编辑commands.cfg配置文件
vim /usr/local/nagios/etc/objects/commands.cfg
把原有的两个注释掉,加入下面的
define command{
command_name process-host-perfdata
command_line /usr/bin/perl /usr/local/nagios/pnp4nagios/libexec/process_perfdata.pl
}
define command{
command_name process-service-perfdata
command_line /usr/bin/perl /usr/local/nagios/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}
define command{
command_name process-service-perfdata-file
command_line /usr/local/nagios/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/service-perfdata
}
define command{
command_name process-host-perfdata-file
command_line /usr/local/nagios/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/host-perfdata
}
12.在模版里添加定义的主机和服务
vim /usr/local/nagios/etc/objects/templates.cfg
define host {
name host-pnp
action_url /pnp4nagios/share/index.php/graph?host=$HOSTNAME$
register 0
}
define service {
name srv-pnp
action_url /pnp4nagios/share/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
register 0
}
13.检查配置文件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
14.重启nagios、httpd服务
systemctl restart nagios
systemctl restart httpd
15.客户端访问(http://192.168.96.81/pnp4nagios/graph?host=192.168.96.11)
提醒删除install.php页面
cd /usr/local/nagios/pnp4nagios/share/
rm -rf install.php
如果pnp页面报session的错,则需修改一下缓存文件的权限
chown -R nagios.root /var/lib/php/session/