本次实验简单实现用nagios监控linux主机和windows主机:
nagios包括2部分:nagios core和nagios-plugins;
nagios core:核心部分不做任何监控工作,仅是处理nagios-plugins信息;
nagios-plugins:主要负责与各被监控端主机通信;
被监控端:通过nagos各种插件来监控数据,并返回给主控端上的nagios-plugins;
nagios监控配置实现的简单原理介绍:
nagios主控端,需要远程主机的监控机制实现;通过各种插件监控:
ssh类插件:
在远程主机运行sshd进程,能接受主控端插件发送来的命令并执行,把执行结果返回给主控端插件,插件把获得的结果进行分析,主控插件把分析结果返回给nagios 核心,核心决定是否对分析结果报警;
nrpe类插件:
nrpe专门用来监控linux或unix主机使用的服务(插件);用来在监控linux/unix主机上执行脚本插件以实现对这些主机资源的监控;需要在远程主机安装nrpe服务(inetd),且nrpe也需要安装nagios的各种插件,这些插件在远程主机运行监控,把监控结果通过nrpe返回给主控端插件,后续过程同理;
nrpe依赖于nagios-plugins;
snmp类插件:
远程主机上安装snmpd服务,只需在远程主机上每隔一段时间运行snmp命令,主控插件联系到远程主机的snmpd服务进程161端口获取监控数据,后续过程同理;
nsclient++:
用来监控windows主机时安装在windows主机上的组件;是windows上wmi组件,获取windows监控数据,返回给主控插件;后续过程同理;
nsca类插件:
用来让被监控的远程linux/unix主机主动将监控信息发送给nagios服务器(在冗余监控模式中特别要用到);在snmp中有一种trap机制,是被监控端主动通知主控端;那么,nsca就是nagios的一种被动监控机制;主控端安装nsca服务进程,远程主机安装ncsa客户端,客户端向主控端发送监控数据,后续过程同理;
一般远程主机上的sshd、nrpe、nsclient等,有些是实现监控主机的、有些是监控服务的;
但监控本身sshd、nrpe、snmpd不是监控,主控端插件才是监控;
有些服务可直接被监控,而不需要依赖其它服务,主控端插件实现监控获取到远程主机的数据;
所用设备ip规划说明:
nagios主控端服务器(centos7系统):192.168.255.11
linux远程主机(centos7系统):192.168.255.12
windows远程主机(xp系统):192.168.255.107
在nagios主控端安装:
1、安装nagios基础依赖软件包:
]# yum install gcc glibc glibc-common gd gd-devel httpd php php-gd
2、创建nagios需要的用户及组:
]# useradd -m nagios
]# usermod -a -G nagios apache
说明:使apache和nagios用户都可访问nagios目录;
官方安装文档: https://support.nagios.com/kb/article/nagios-core-installing-nagios-core-from-source-96.html#CentOS
3、下载nagios软件包并安装:
]# wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.3.4.tar.gz
]# tar xzf nagios-4.3.4.tar.gz
]# cd nagioscore-nagios-4.3.4/
]# ./configure --with-command-group=nagcmd
]# make all
]# make install
]# make install-init
安装服务脚本;
]# make install-commandmode
安装并配置权限;
]# make install-config
安装样例配置文件/usr/local/nagios/etc;
]# make install-webconf
安装httpd下使用的nagios配置文件/etc/httpd/conf.d/nagios.conf;
为email指定想用来接收nagios警告信息的邮件,默认是本机的nagios用户:
]# vim /usr/local/nagios/etc/objects/contacts.cfg
email nagios@locaohost
4、创建nagios web监控界面登录时的用户为nagiosadmin和密码为nagiosadmin:
]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
密码为:nagiosadmin
5、启动httpd服务;
]# systemctl start httpd
6、下载并安装nagios-plugins插件软件包:
下载地址:https://www.nagios.org/downloads/nagios-plugins/
]# tar zxf nagios-plugins-2.2.1.tar.gz
]# cd nagios-plugins-2.2.1
]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
]# make -j 4 && make install
7、查看插件个数:
]# ls /usr/local/nagios/libexec/|wc -l
8、添加nagios服务到开机自启动:
]# chkconfig --add nagios
]# chkconfig --level 3 nagios on
9、启动nagios服务:
检查其主配置文件语法:
]# /usr/lcoal/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
]# systemctl start nagios
10、关闭selinux和iptbalese:
]# sed 's/SELINUX=enforcing/SELINUX=disable/' /etc/selinux/config
]# systemctl disable firewalld
]# systemctl disable iptables
11、浏览器登录nagios页面:
http://192.168.255.11/nagios/
用户名和密码为:nagiosadmin
输入此前设置的用户名和密码即可看见登录:
12、安装nrpe:
官方安装指南:https://github.com/NagiosEnterprises/nrpe
安装nrpe依赖:
]# yum install make net-snmp openssl-devel
安装nrpe:
]# tar zxf nrpe-3.2.1.tar.gz
]# cd nrpe-3.2.1
]# ./configure
]# make all
]# make install-plugin
]# make install-daemon
]# make install-config
生成nrpe的配置文件/usr/local/nagios/etc/nrpe.cfg
以上步骤nagios服务器端基本完成;下面对被监控端的各主机操作:
在windows上(xp系统):
通过nsclient++监控,nagios与nsclient++通信实现监控有多种方式,如通过nrpe实现监控、check_nt实现监控等;
先演示基于check_nt监控windows xp远程主机:
1、下载nslient++0.3.6并安装:
下载地址:http://www.nsclient.org/download/0.3/
鼠标一路点下一步, 其中有一步要输入nagios服务器端的地址、全选上即可:
最后完成时,选择立即启动:
2、打开cmd命令行:
输入:netstat -an
其中有本地端口12489(check_nt与nsclient++通信的端口)、5666(nrpe使用的端口)已经监听;
3、在nagios主控端手动测试监控:
]# /usr/local/nagios/libexec/check_nt -H 192.168.255.107 -v UPTIME -p 12489
其它手动测试:
]# ./check_nt -H 192.168.255.107 -v CPULOAD -p 12489 -w 80 -c 90 -l 5,80,90
]# ./check_nt -H 192.168.255.107 -p 12489 -v USEDDISKSPACE -w 80 -c 90 -l C
使用配置文件监控windows远程主机:
在nagios主控端:
4、配置commands.conf文件:
define command{
command_name check_nt
command_line $$USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
}
5、配置windows.cfg文件:
define host{
use windows-server host_name winhost alias My Windows host
address 192.168.255.107
}
define service{
use generic-service
host_name winhost
service_description NSClient++ Version
check_command check_nt!CLIENTVERSION
}
define service{
use generic-service
host_name winhost
service_description Uptime
check_command check_nt!UPTIME
}
define service{
use generic-service
host_name winhost
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90
}
define service{
use generic-service
host_name winhost
service_description Memory Usage
check_command check_nt!MEMUSE!-w 80 -c 90
}
define service{
use generic-service
host_name winhost
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
define service{
use generic-service
host_name winhost
service_description W3SVC
check_command check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
}
define service{
use generic-service
host_name winhost
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}
6、编辑nagios.cfg配置文件中启用windows.cfg
cfg_file=/usr/local/nagios/etc/objects/windows.cfg
7、检查语法:
]# bin/nagios -v etc/nagios.cfg
8、重启nagios服务:
]# systemctl restart nagios
9、打开浏览器:
http://192.168.255.11/nagios/
即可看到有winhost主机了;
以上步骤完成了简单监控远程windows主机;
下面演示基于nrpe监控远程linux主机:
在远程主机centos7上:
1、添加nagios用户:
]# useradd -s /sbin/nologin nagios
2、nrpe依赖于nagios-plugins安装:
]# tar zxf ngioas-plugins.1.4.15.tar.gz
]# cd nagios-plugins-1.4.15
]# ./confguire --with-nagios-user=nagios --with-nagios-group=nagios
]# make all
]# make install
3、安装nrpe:
]# tar zxf nrpe-2.12.tar.gz
]# cd nrpe-2.12
]# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-agrs --enable-ssl
]# make all
]# make install-plugin
]# make install-daemon
]# make instal-daemon-config
4、配置nrpe.cfg:
log_facility=daemon
debug=0
pid_file=/usr/local/nagios/var/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=192.168.255.11
dont_blame_nrpe=0
allow_bash_command_substitution=0
command_timeout=60
connection_timeout=300
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5, -c 30,25,20
command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 10% -c 3%
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p /
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10
5、启动nrpe:
]# /usr/local/nagios/bin/nrpe -c /usr/local/nagos/etc/nrpe.cfg -d
为了便于nrpe服务的启动,定义为/etc/init.dnrped脚本:
#!/bin/bash
# chkconfig: 2345 88 12
# description: nrpe daemon
nrpe=/usr/local/nagios/bin/nrpe
nrpeconf=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
start)
echo -n "starting nrpe daemon..."
$nrpe -c $nrpeconf -d
echo " done."
;;
stop)
echo -n "stopping nrpe daemon..."
pkill -u nagios nrpe
echo " done."
;;
restart)
$0 stop
sleep 2
$0 start
;;
*)
echo "usage: $0 start|stop|restart"
;;
esac
exit 0
]# chmod +x /etc/init.d/nrped
]# chkconfig --add nrped
]# chkconfig --list nrped
]# systemctl start nrped
]# netstat -tnlp
此时客户端监听了5666端口;
配置允许主控端监控的对象:
6、在nagios主控端安装nrpe:
]# tar zxf nrpe-2.12.tar.gz
]# cd nrpe-2.12
]# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-agrs --enable-ssl
]# make all
]# make install-plugin
7、手动测试监控l远程inux主机:
]# libexec/check_nrpe -H 192.168.255.12
8、定义如何监控远程主机及服务:
创建/usr/local/nagios/etc/objects/linhost.cfg文件:
define host{
use linux-server
host_name node2
alias My linux host
address 192.168.255.12
}
define service{
use generic-service
host_name node2
service_description check_load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name node2
service_description check_mem
check_command check_nrpe!check_mem
}
define service{
use generic-service
host_name node2
service_description check_disk
check_command check_nrpe!check_disk
}
define service{
use generic-service
host_name node2
service_description check_swap
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name node2
service_description check_iostat
check_command check_nrpe!check_iostat
}
在nrpe.cfg主配置文件中包含配置文件:
cfg_file=/usr/local/nagios/etc/objects/linhost.cfg
9、检查语法:
]# bin/nagios -v etc/nagios.cfg
10、重启nagios服务
]# systemctl restart nagios
11、打开浏览器:http://192.168.255.11/nagios/
即可看到新增加的一台linux主机了;
此时完成简单配置nagios监控远程linux主机和远程windows主机的功能,具体更多配置实现,后期再见。
注意:后面演示监控mysql时,前提必须安装:mysql-devel,再编译安装nagios-plugins,否则没有check-mysql插件;
]# yum install mysql-devel