本次实验简单实现用nagios监控linux主机和windows主机:
nagios包括2部分:nagios core和nagios-plugins;
nagios core:核心部分不做任何监控工作,仅是处理nagios-plugins信息;
nagios-plugins:主要负责与各被监控端主机通信;
被监控端:通过nagos各种插件来监控数据,并返回给主控端上的nagios-plugins;
nagios监控配置实现的简单原理介绍:
nagios主控端,需要远程主机的监控机制实现;通过各种插件监控:
在远程主机运行sshd进程,能接受主控端插件发送来的命令并执行,把执行结果返回给主控端插件,插件把获得的结果进行分析,主控插件把分析结果返回给nagios 核心,核心决定是否对分析结果报警;
nrpe专门用来监控linux或unix主机使用的服务(插件);用来在监控linux/unix主机上执行脚本插件以实现对这些主机资源的监控;需要在远程主机安装nrpe服务(inetd),且nrpe也需要安装nagios的各种插件,这些插件在远程主机运行监控,把监控结果通过nrpe返回给主控端插件,后续过程同理;
nrpe依赖于nagios-plugins;
远程主机上安装snmpd服务,只需在远程主机上每隔一段时间运行snmp命令,主控插件联系到远程主机的snmpd服务进程161端口获取监控数据,后续过程同理;
nsclient++:
用来监控windows主机时安装在windows主机上的组件;是windows上wmi组件,获取windows监控数据,返回给主控插件;后续过程同理;

用来让被监控的远程linux/unix主机主动将监控信息发送给nagios服务器(在冗余监控模式中特别要用到);在snmp中有一种trap机制,是被监控端主动通知主控端;那么,nsca就是nagios的一种被动监控机制;主控端安装nsca服务进程,远程主机安装ncsa客户端,客户端向主控端发送监控数据,后续过程同理;
所用设备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目录;
3、下载nagios软件包并安装:
]# 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
安装服务脚本;
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插件软件包:
]# 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
10、关闭selinux和iptbalese:
]# sed 's/SELINUX=enforcing/SELINUX=disable/' /etc/selinux/config
]# systemctl disable firewalld
]# systemctl disable iptables
11、浏览器登录nagios页面:
<a href="http://192.168.255.11/nagios/">http://192.168.255.11/nagios/</a>
用户名和密码为:nagiosadmin
输入此前设置的用户名和密码即可看见登录:
12、安装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 install-plugin
]# make install-daemon
]# make install-config
生成nrpe的配置文件/usr/local/nagios/etc/nrpe.cfg
以上步骤nagios服务器端基本完成;下面对被监控端的各主机操作:
在windows上(xp系统):
通过nsclient++监控,nagios与nsclient++通信实现监控有多种方式,如通过nrpe实现监控、check_nt实现监控等;
1、下载nslient++0.3.6并安装:
鼠标一路点下一步, 其中有一步要输入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
service_description Uptime
check_command check_nt!UPTIME
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90
service_description Memory Usage
check_command check_nt!MEMUSE!-w 80 -c 90
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
service_description W3SVC
check_command check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
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、打开浏览器:
即可看到有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
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 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
配置允许主控端监控的对象:
6、在nagios主控端安装nrpe:
7、手动测试监控l远程inux主机:
]# libexec/check_nrpe -H 192.168.255.12
8、定义如何监控远程主机及服务:
创建/usr/local/nagios/etc/objects/linhost.cfg文件:
use linux-server
host_name node2
alias My linux host
address 192.168.255.12
service_description check_load
check_command check_nrpe!check_load
service_description check_mem
check_command check_nrpe!check_mem
service_description check_disk
check_command check_nrpe!check_disk
service_description check_swap
check_command check_nrpe!check_swap
service_description check_iostat
check_command check_nrpe!check_iostat
9、检查语法:
10、重启nagios服务
即可看到新增加的一台linux主机了;
此时完成简单配置nagios监控远程linux主机和远程windows主机的功能,具体更多配置实现,后期再见。
本文转自 crystaleone 51CTO博客,原文链接:http://blog.51cto.com/linsj/2068786,如需转载请自行联系原作者