2015-11-20 nagios监控服务搭建
服务器端要实现监控功能必须安装nagios主程序和nagiosplugins插件程序,如果还想通过nrpe插件实现监控也要安装nrpe插件,windows端安装的是 NSClient++插件
nagios安装完成以后在/usr/local/nagios/下生成的目录有
bin :主程序目录
libexec:插件程序目录
etc :主配置文件目录
sbin :cgi程序目录
share :网页文档目录
var :日志与进程号文件目录
/etc下的配置文件有
nagios.cfg :主配置文件
nrpe.cfg :远程监控配置文件
cgi.cfg :cgi程序配置文件
htpasswd.users :nagios用户名登录密码文件
objects :子目录里面是模板文件
resource.cfg:变量定义配置文件也成为宏
模板文件位于/object下
commands.cfg :定义命令文件
contacts.cfg :定义联系人,联系组报警信息接受者邮箱或者手机
localhost.cfg :监控本机的配置文件模板
printer.cfg :监控打印机配置文件模板
switch.cfg :监控交换机配置文件模板
templates.cfg :对象定义参考模板
timeperiods.cfg :时间定义周期,定义报警时间周期
windows.cfg :监控windows配置文件模板
nagios安装服务器端:
环境准备:
#yum -y install httpd php php-devel gd gd-devel openssl openssl-devel gcc gcc-c++ glibc mysql-devel php-mysql glibc-commom xinetd
#groupadd nagios
#useradd -g nagios nagios //创建nagios用户和组
#usermod -G nagios apache //apache加入nagios组
#tar zxf nagios-3.2.0.tar.gz -C /usr/local/
#ls /usr/local/
#cd /usr/local/nagios-3.2.0/
#./configure --prefix=/usr/local/nagios --with-nagios-user=nsgios --with-nagios-group=nagios
#make all
#make install //安装主程序
#make install-init //安装启动脚本/etc/init.d/nagios
#make install-config //安装配置文件模板
#make install-webconf //安装web配置文件并添加到/etc/httpd/conf.d/bagios.conf
#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //验证登陆时读取的文件htpasswd.users文件和密码 存放于/usr/local/nagios/etc/
#vim /etc/httpd/conf.d/nagios.conf
# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
# Last Modified: 11-26-2005
#
# This file contains examples of entries that need
# to be incorporated into your Apache web server
# configuration file. Customize the paths, etc. as
# needed to fit your system.
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
Alias /nagios "/usr/local/nagios/share"
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
#service httpd start
#netstat -anput
#tar zxf nagios-plugins-2.1.0.tar.gz
#cd nagios-plugins-2.1.0
#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
#make
#make install
#vim /etc/httpd/conf.d/auth_mysql.conf //这里有报错,第6行注释掉才行
#service nagios start //启动nagios服务
#chkconfig --add nagios
#chkconfig nagios on
#/usr/local/nagios/bin/nagios -v 10.10.0.251
打开浏览器输入http://10.10.0.251/nagios //验证安装结果,是否能使用nagiosadmin用户登录http,
到此监控服务器的基本监控程序安装完成
实现监控windows服务器的配置
监控windows端要在win端安装nsclient++插件
双击.exe文件安装,安装中需要指定监控服务端ip地址,ip地址对话框新面的选项全部启用,并且启动nsclient++服务,使用命令提示符查看监控端口命令是
c:>netstat - an
tcp 0.0.0.0:5666
tcp 0.0.0.0:12489
能看到端口即可,被监控端安装完成,check_nt 与NSClient++通信的端口是12489,通过nrpe通信的端口是5666
监控服务器端配置:
windows.cfg是模板文件如果定义其他的win主机,可以复制此模板但是不能重名,我直接用的模板文件,但是如果重新定义cfg文件的话还需要修改主配置文件启用此cfg文件
#vim windows.cfg
define host{
use windows-server ; Inherit default values from a template
host_name winserver ; The name we're giving to this host
alias My Windows Server ; A longer name associated with the host
address 10.10.0.16 ; IP address of the host
} //启用windows配置文件并修改host-name alias 和address:被监控主机ip
#/usr/local/nagios/libexec/check_nt -H 10.10.0.16 -p 12489 -v UPTIME //测试与win主机的通信
System Uptime - 43 day(s) 0 hour(s) 24 minute(s) |uptime=61944
#vim nagios.cfg
# Definitions for monitoring a Windows machine
cfg_file=/usr/local/nagios/etc/objects/windows.cfg //启用windows.cfg文件
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //检测主配置文件语法
#service nagios restart
输入http://10.10.0.251/nagios 打开server项,就能看到winserver的监控信息
监控linux服务器:
被监控linux端安装配置
#useradd nagios
#tar zxf nagios-plugins-2.1.0.tar.gz -C /usr/local/
#cd /usr/local/nagios-plugins-2.1.0/
#./configure --with-nagios-user=nagios --with-nagios-group=nagios
#make all
#make install
#tar zxf nrpe-2.12.tar.gz -C /usr/local
#cd /usr/local/nrpe-2.12/
#./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-nrpe-user=nagios --with-nrpe-group=nagios --enable-ssl --enable-command-args
#make all
#make install-plugin
#make install-daemon
#make install-daemon-config
#cp nrpe.cfg nrpe.cfg.bak
#vim nrpe.cfg
allowed_hosts=监控服务器ip
server_address=被监控机ip如果不改默认监听的是0.0.0.0
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d //被监控端启动nrpe进程
#netstat -anput |grep nrpe
#echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe,cfg -d" >> /etc/rc.local
#netstat -anput |grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 11272/nrpe
监控服务器端配置:
因为已经安装好nagios和nagiosplugins,所以只要在安装nrpe就行
#tar zxf nrpe-2.12.tar.gz -C /usr/local/
#cd /usr/local/nrpe-2.12/
#./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-ssl --enable-command-args
#make all
#make install-piugin
#make install-plugin
#make install-daemon
#make install-daemon-config
#cd /usr/local/nagios/libexec/
#./check_nrpe -H 10.10.0.17 //测试与linux主机的通信
NRPE v2.12
监控服务器端定义监控命令
#vim commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$
定义主机
#cp localhost.cfg linux01.cfg
#vim linux01.cfg
define host{
use linux-service ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name linux-0.17
alias linux01
address 10.10.0.17
}
定义服务
define service{
use generic-service ; Name of service template to use
host_name linux-0.17
service_description CHECK_LOAD
check_command check_nrpe!check_load
}
# Define a service to check the number of currently logged in
# users on the local machine. Warning if > 20 users, critical
# if > 50 users.
define service{
use generic-service ; Name of service template to use
host_name linux-0.17
service_description CHECK_SDA1
check_command check_nrpe!check_sda1
}
# Define a service to check the number of currently running procs
# on the local machine. Warning if > 250 processes, critical if
# > 400 users.
define service{
use generic-service ; Name of service template to use
host_name linux-0.17
service_description CHECK_ZOMBIE_PROCS
check_command check_nrpe!check_zombie_procs
}
define service{
use generic-service ; Name of service template to use
host_name linux-0.17
service_description CHECK_TOTAL_PROCS
check_command check_nrpe!check_total_peocs
}
define service{
use generic-service ; Name of service template to use
host_name linux-0.17
service_description Web Server
check_command check_http
}
define service{
use generic-service ; Name of service template to use
host_name linux-0.17
service_description Mysql
check_command check_mysql!root!redhat //root为用户名redhat为密码
}
启用linux01.cfg文件
#vim /usr/local/nagios/etc/nagios.cfg
# You can specify individual object config files as shown below:
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/linux01.cfg
cfg_file=/usr/local/nagios/etc/objects/windows.cfg
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios restart
输入http://10.10.0.251/nagios,验证配置结果