Nagios 搭建与配置
实验环境 centos6.5 两台
实验步骤
一.安装Nagios的依赖关系:
yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server xinetd
二.安装配置Nagios(监控本机)
- 添加nagios运行所需要的用户和组:
useradd nagios
- 把apache加入到nagios组
usermod -a -G nagios apache
- 上传nagios包,加压后编译安装nagios:
tar -zxf nagios-4.0.3.tar.gz
cd nagios-4.0.3
./configure --with-command-group=nagios --enable-event-broker
make all (编译全部)
make install (安装)
make install-init (生成启动脚本文件)
make install-commandmode (设置组的执行权限)
make install-config (生成配置文件)
make install-webconf (安装nagios的web文件到httpd的conf.d目录下)
- 为email指定您想用来接收nagios警告信息的邮件地址,默认是本机的nagios用户:
vim /usr/local/nagios/etc/objects/contacts.cfg
- 修改配置 email [email protected] 这个是默认设置
- 创建一个登录nagios web程序的用户,这个用户帐号在以后通过web登录nagios认证时所用:htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
- 以上过程配置结束以后需要启动httpd:
/etc/init.d/httpd start
- 编译、安装nagios-plugins(插件)
tar -zxf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
- 检查其主配置文件的语法是否正确:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
- 如果上面的语法检查没有问题,接下来就可以正式启动nagios服务了: service nagios start
关闭selinux:setenforce 0
关闭防火墙:service iptables stop
修改本地监控配置文件:vim /usr/local/nagios/etc/objects/localhost.cfg
- 通过web界面查看nagios:
http://your_nagios_IP/nagios
这时候已将可以监控Linux本机。浏览器测试:‘IP地址’/nagios
三.基于NRPE(桥梁)监控远程Linux主机
- 配置-监控端
1).安装NRPE
tar -xf 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-args \
--enable-ssl
make all
make install-plugin (设置插件权限)
2).添加策略
vim /usr/local/nagios/etc/objects/commands.cfg
添加:define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
3).定义监控对象,复制一个模板 cp localhost.cfg linux.cfg
编辑模块文件:
vim linux.cfg (:%s /localhost/linux/g[修改localhost为linux])
host_name linux
address 被监控端的IP
将这五行注释掉
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members linux ; Comma separated list of hosts that belong to this group
}
修改 check_command 为 check_nrpe_procs(中间的部分不用动只修改最后的部分)
4).添加模块
vim /usr/local/nagios/etc/objects/commands.cfg
添加:
define command{
command_name check_nrpe_disk
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c "check_disk"
}
define command{
command_name check_nrpe_users
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c "check_users"
}
define command{
command_name check_nrpe_procs
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c "check_procs"
}
define command{
command_name check_nrpe_load
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c "check_load"
}
define command{
command_name check_nrpe_swap
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c "check_swap"
}
vim vim /usr/local/nagios/etc/nagios.cfg 添加一条
cfg_file=/usr/local/nagios/etc/objects/linux.cfg
cd objects/
5).给权限:
chown nagios.nagios linux.cfg chmod 664 linux.cfg
6).启动服务:
service nagios restart
2.安装配置-被监控端
1)先添加nagios用户
useradd -s /sbin/nologin nagios
2)NRPE依赖于nagios-plugins,因此,需要先安装nagios-plugins
tar -zxf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
3)安装NRPE
tar -xf nrpe-2.12.tar.gz
cd nrpe-2.12.tar.gz
./configure --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args \
--enable-ssl
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd (生成临时服务)
4).修改文件:
vim /etc/xinetd.d/nrpe
only-from=127.0.0.1 10.1.1.2(这个是监控端nagios的IP)(nrpe允许以上IP的机器通过nrpe查询服务)
vim /etc/services
文件最后添加一条:nrpe 5666/tcp nrpe
5).启动守护进程
service xinetd start
6).配置NRPE
vim /usr/local/nagios/etc/nrpe.cfg
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_address=被监控端的IP
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=监控端的IP
command_timeout=60
connection_timeout=300
debug=0
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p / (记得修改!)
command[check_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200(记得修改!)
添加一条:command[check_swap]=/usr/local/nagios/libexec/check_disk -w 40% -c 20%
7).启动NRPE
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d(临时启动)
或者将如下内容定义为vim /etc/init.d/nrped脚本:
!/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 -R 777 /etc/init.d/nrped
8).启动服务
/etc/init.d/nrped start
service xinetd restart