安装配置nagios [ˈnædʒiɔs]
nagios需要依赖lamp环境,所有我们先在nagios服务器上安装配置lamp环境
[root@localhost ~]# mount /dev/cdrom /media/
[root@localhost ~]# rm -rf /etc/yum.repos.d/*
[root@localhost ~]# vim /etc/yum.repos.d/yum.repo
[yum]
baseurl=file:///media
enabled=1
gpgcheck=0
[root@localhost ~]# yum -y install httpd* php* mariadb mariadb-server mariadb-devel
1、解压相关软件
[root@localhost ~]# tar zxf nagios-4.0.1.tar.gz -C /usr/src/
[root@localhost ~]# tar zxf nagios-plugins-1.5.tar.gz -C /usr/src/
[root@localhost ~]# tar zxf nrpe-2.15.tar.gz -C /usr/src/
注意:nrpe的作用
Nagios的监测服务器能够远程对被监测主机系统上的信息进行获取,比如远程系统上的进程数、磁盘空间使用状况、所运行的服务等等这些必须要登录远程主机系统上才能了解的信息的话,就必须要依靠NRPE这个核心扩展插件程序,NRPE作为中间的代理程序,扮演着一手接受着Nagios监测服务器发来的请求,另一手在远程主机系统上获取指定的信息的中间人角色。
2、安装依赖环境
[root@localhost ~]# yum -y install openssl-devel
3、编译安装nagois
[root@localhost ~]# useradd -M -s /sbin/nologin nagios
[root@localhost ~]# mkdir /usr/local/nagios
[root@localhost ~]# chown -R nagios:nagios /usr/local/nagios/
[root@localhost ~]# cd /usr/src/nagios-4.0.1/
4、配置编译环境
[root@localhost nagios-4.0.1]# ./configure --prefix=/usr/local/nagios
5、编译安装
[root@localhost nagios-4.0.1]# make all
[root@localhost nagios-4.0.1]# make install
[root@localhost nagios-4.0.1]# make install-init
[root@localhost nagios-4.0.1]# make install-commandmode
[root@localhost nagios-4.0.1]# make install-config
解释:
make install #安装主程序,CGI和HTML文件
make install-init #在/etc/rc.d/init.d安装启动脚本
make install-commandmode #安装command模板
make install-config #安装示例配置文件
6、编译安装nagios-plugins
[root@localhost nagios-4.0.1]# cd /usr/src/nagios-plugins-1.5/
[root@localhost nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios
[root@localhost nagios-plugins-1.5]# make && make install
7、编译安装nrpe:
[root@localhost nagios-plugins-1.5]# cd /usr/src/nrpe-2.15/
[root@localhost nrpe-2.15]# ./configure && make all
[root@localhost nrpe-2.15]# make install-plugin
8、配置httpd.conf
添加nagios的访问界面
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
在最后面添加:
ScriptAlias /nagios/cgi-bin/ "/usr/local/nagios/sbin/"
<Directory "/usr/local/nagios/sbin">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
9、设置页面访问认证:
[root@localhost ~]# vim /usr/local/nagios/etc/cgi.cfg
修改:
use_authentication=0
(0:允许访问所有页面。1:禁止)
10、nagios监控服务器的配置文件:
[root@localhost ~]# cd /usr/local/nagios/etc
[root@localhost etc]# ls
cgi.cfg nagios.cfg objects resource.cfg
cgi.cfg #全局配置文件,定义软件接口,安全认证,监听地址,监听端口等
nagios.cfg
#配置文件信息,保存其他模块配置文件路径,日志文件,PID文件等文件路径
nagios.cfg主配置文件nagios.cfg是nagios服务端的主配置文件,它可以定义其它*.cfg配置文件的位置,是否启用性能监控(收集监控数据以生成图表),性能数据处理指令,性能数据文件格式模板及存放位置等,配置nagios日志文件存放的信息级别及位置,nagios进程的启动用户及组,日志文件的切割方式,显示的日期格式等等。如果只是实现对主机及服务的监控,不出监控图的话,默认可以不改动这一文件
[root@localhost etc]# cd objects/
[root@localhost objects]# ls
commands.cfg localhost.cfg switch.cfg timeperiods.cfg
contacts.cfg printer.cfg templates.cfg windows.cfg
objects/commands.cfg #保存测试语句模块
objects/contacts.cfg #保存管理员的联系方式
objects/localhost.cfg #本地主机测试选项
objects/printer.cfg #网络打印机测试选项
objects/switch.cfg #路由交换设备测试选项
objects/templates.cfg #监控任务模板,测试方式,测试周期,报错提醒等objects/timeperiods.cfg #监控测试时间 7*24 5*24 5*12
objects/windows.cfg #windows服务器测试选项
各配置文件的作用:[root@localhost objects]# vim templates.cfg
templates.cfg模板定义文件templates.cfg是监控的模板文件,对所有主机、服务、联系人的定义为简化配置都可以根据需求定制相应的模板,然后在主机、服务、联系人配置文件中去引用模板即可。
被控端设置
1、安装依赖环境
[root@localhost ~]# mount /dev/cdrom /media/
[root@localhost ~]# rm -rf /etc/yum.repos.d/*
[root@localhost ~]# vim /etc/yum.repos.d/yum.repo
[yum]
baseurl=file:///media
enabled=1
gpgcheck=0
[root@localhost ~]# yum -y install openssl openssl-devel
[root@localhost ~]# useradd -s /sbin/nologin nagios
2、安装nagios-plugins
[root@localhost ~]# tar zxf nagios-plugins-1.5.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nagios-plugins-1.5/
[root@localhost nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios [root@localhost nagios-plugins-1.5]# make && make install
[root@localhost nagios-plugins-1.5]# chown -R nagios:nagios /usr/local/nagios/
3、安装nrpe
[root@localhost ~]# tar zxf nrpe-2.15.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nrpe-2.15/
[root@localhost nrpe-2.15]# ./configure --prefix=/usr/local/nagios/ [root@localhost nrpe-2.15]# make all
[root@localhost nrpe-2.15]# make install-plugin
[root@localhost nrpe-2.15]# make install-daemon
[root@localhost nrpe-2.15]# make install-daemon-config
4、配置
[root@localhost nrpe-2.15]# vim /usr/local/nagios/etc/nrpe.cfg
修改:
allowed_hosts=127.0.0.1,192.168.6.10 //添加监控服务器的IP地址
[root@localhost nrpe-2.15]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d //启动 nrpe
[root@localhost nrpe-2.15]# netstat -anp | grep nrpe //端口号是TCP5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 20670/nrpe
tcp6 0 0 :::5666 :::* LISTEN 20670/nrpe
unix 2 [ ] DGRAM 43697 20670/nrpe
5、安装测试服务
[root@localhost ~]# yum -y install httpd vsftpd bind dhcp
将以上为进行简单的配置,并启动服务
ftp:
[root@localhost pub]# systemctl restart vsftpd
dns:
[root@localhost nrpe-2.15]# systemctl restart named
dhcp:
[root@localhost nrpe-2.15]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y
[root@localhost nrpe-2.15]# vim /etc/dhcp/dhcpd.conf
subnet 192.168.6.0 netmask 255.255.255.0 {
range 192.168.6.26 192.168.6.30;
default-lease-time 600;
max-lease-time 7200;
}
[root@localhost nrpe-2.15]# systemctl restart dhcpd
6、添加被监控客户端的测试选项(server1)
[root@localhost ~]# cd /usr/local/nagios/etc/objects/
[root@localhost objects]# cp -p localhost.cfg 192.168.6.20.cfg
[root@localhost objects]# vim 192.168.6.20.cfg
修改:
删除host group选项
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
}
define host{
use linux-server
host_name server2
alias server2
address 192.168.6.20
}
替换所有模块中的host_name的名称为server2
:% s/localhost/server2/g
添加模块:
define service{
use local-service
host_name server2
service_description FTP
check_command check_ftp
notifications_enabled 0
}
define service{
use local-service
host_name server2
service_description DHCP
check_command check_dhcp
notifications_enabled 0
}
define service{
use local-service
host_name server2
service_description DNS
check_command check_dns
notifications_enabled 0
}
7、配置nagios.cfg,添加监控节点
[root@localhost objects]# vim /usr/local/nagios/etc/nagios.cfg
添加: 32后
cfg_file=/usr/local/nagios/etc/objects/192.168.6.20.cfg
8、重启服务
killall -9 nagios
systemctl start nagios &
systemctl start httpd
9、查看监控结果
等待一段时间后,会对所有项目进行检测完毕
我们通过结果,可以看到有通过检测的选项(绿色),警告(黄色),未知服务(橘色),最后一列给出了故障和检测信息。http服务标记了403错误,我们给http服务添加一个主页文件。然后等待一下就能看到以下结果。
二、添加mysql监控
1、在mysql服务器(被监控服务器)上配置:
[root@localhost ~]# mysql -u root -p123.com mysql> create database abc;
mysql> grant all on abc.* to 'zhang1'@'192.168.1.%' identified by '123.com'; mysql> flush privileges;
mysql>quit
第1步:在监控服务器上安装mariadb与配套组件
[root@localhost ~]# yum -y install mariadb mariadb-devel
第2步:在nagios监控服务器中配置
1、检测连接数据库服务器
/usr/local/nagios/libexec/check_mysql -u zhang1 -d abc -H 192.168.1.100 -P3306 --password='123.com'
2、定义mysql命令
[root@localhost ~]# vim /usr/local/nagios/etc/objects/commands.cfg
添加:
define command{
command_name
check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -P3306 -u zhang1 -d abc --password='123.com'
}
第3步:在主机检测配置文件中添加检测信息
vim /usr/local/nagios/etc/objects/192.168.10.2.cfg
添加:
define service{
use local-service ;Name of service template to use
host_name server1
service_description MYSQL
check_command check_mysql
notifications_enabled 0
}
第 4 步:从新加载nagios服务,就可以在检测页面中看到mysql服务了。