Nagios是一个监视系统运行状态和网络信息的监视系统。Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能可运行在Linux/Unix平台之上,同时提供一个可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等。

工作原理图(取自网上)

wKioL1WantLgvHFBAAHXUPPNcog069.jpg

配置图

wKioL1WbMbGwhUPXAAEkYSSY70o300.jpg

一、配置前准备:

1、同步两台机子时间

ntpdate ntpdate time.nist.gov

添加定时任务,每10分钟同步一次网络时间

[root@mail ~]# crontab -l

0-59/10 * * * * /usr/sbin/ntpdate time.nist.gov

2、环境安装

安装gcc编译工具

[root@resin ~]# yum install gcc glibc glibc-common 

安装gd库 

yum install gd gd-devel

安装lamp

yum install php* 

yum install httpd

安装mysql

yum install mysql mysql-server mysql-devel

3、添加nagios 所需的组及用户

useradd  nagios

passwd nagios

groupadd nagios

usermod -a -G nagios nagios

usermod -a -G nagios apache

检查组信息

id apache

id -n -G nagios


二、

nagios-server端配置

1、下载所需安装包

nagios、nagios-plugins、nrpe

解压

tar -zxf nagios-3.3.1.tar.gz

tar -zxf nagios-plugins-1.4.14.tar.gz

tar -zxf nrpe-1.8.tar.gz

创建目录并将解压文件移至目录下

[root@resin ~]# mkdir /var/application/nagios -p

[root@resin ~]# mv nagios /var/application/nagios/

[root@resin ~]# mv nagios-plugins-1.4.14 /var/application/nagios/

[root@resin ~]# mv nrpe-1.8  /var/application/nagios/

wKiom1WbNcrSt0Y6AABSMH06kao075.jpg

2、编辑安装nagios

进入nagios目录

cd /var/application/nagios/nagios

编译

./configure --with-command-group=nagios --enable-even-broker

make all

wKioL1WbOELA4G0-AAJXvgLTuGQ255.jpg

make install 

make install-init

make install-commandmode

make install-config

安装nagios web配置文件

make install-webconf

创建web登陆用户nagiosadmin

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

cat /usr/local/nagios/etc/htpasswd.users

wKioL1WbOKDjUGPuAAEH3GJFKQk624.jpg

添加监控报警接收email地址

vi /usr/local/nagios/etc/objects/contacts.cfg +35

wKioL1WbOLuDPHI2AADi1wrWBgc933.jpg

需要启动postfix

[root@resin nagios]# service postfix start

Starting postfix: [  OK  ]

启动httpd

service httpd start

添加服务自启动

chkconfig --add nagios

chkconfig --level 3 nagios


检查配置文件语法

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

/etc/init.d/nagios checkconfig

wKioL1WbOV_zq8koAAFFM2mxbFY627.jpg

启动nagios

/etc/init.d/nagios start

ps -ef |grep nagios |grep -v grep

wKioL1WbOXuiPqT_AAEZSJRe9hs965.jpg

 在win下测试登陆,如下表示安装成功

wKiom1WbOGmD5KbPAAHfEUq31jw724.jpg

3、编译安装nagios-plugins

./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules

make

make install

检查插件个数

ls /usr/local/nagios/libexec/ |wc -l

wKiom1WbOPHR38qxAADGTldRGgY119.jpg

4、编译安装nrpe

./configure

make all 

make install-plugin

make install-daemon

make install-daemon-config

注意nrpe启动依赖于xinetd服务,若没有需先安装

[root@resin nrpe-1.8]# service xinted start

xinted: unrecognized service

解决: [root@resin nrpe-1.8]#  yum -y install xinetd

[root@resin nrpe-1.8]# service xinetd restart 

Stopping xinetd: [FAILED]

Starting xinetd: [  OK  ]


nrpe端口为5666,检查启动

wKioL1WbPBbziAHJAAIJFHFPoUI482.jpg


5、本机测试nrpe 

[root@mail nrpe-1.8]#  /usr/local/nagios/libexec/check_nrpe -H localhost -c check_load 

OK - load average: 0.06, 0.08, 0.16|load1=0.060;15.000;30.000;0; load5=0.080;10.000;25.000;0; load15=0.160;5.000;20.000;0; 


三、nagios-client端配置

1、编译安装nagios-plugins(同服务端安装)

./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules

make

make install

ls /usr/local/nagios/libexec/ |wc -l 


2、编译安装nrpe 

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config


四、Server端实现监控Client端


1、nagios-server编辑nrpe配置文件

vi /etc/xinetd.d/nrpe

wKiom1WbPw-yyfr6AAFUq2cKZKw917.jpg

编辑nagios配置文件,添加监控实例

[root@mail ~]# vi /usr/local/nagios/etc/nagios.cfg 

# Definitions for monitoring the Linux host

cfg_file=/usr/local/nagios/etc/objects/mylinux.cfg

复制localhost.cfg 成实例文件mylinux.cfg

cp -r /usr/local/nagios/etc/objects/localhost.cfg /usr/local/nagios/etc/objects/mylinux.cfg

修改实例文件mylinux.cfg

vi /usr/local/nagios/etc/objects/mylinux.cfg

wKiom1WbP9aChuSJAACYd6H5n-w397.jpg

wKioL1WbQ3mjnqMgAAI_IEfNs5Q773.jpg

....部分监控内容省略,内容可以根据需求调整。


2、naggios-client编辑nrpe.cfg

wKiom1WbQHLgH_x1AABSCwGTAIU266.jpg

完成后,重启:

pkill nrpe

[root@mail ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d


3、Server主机测试

/usr/local/nagios/libexec/check_nrpe -H 192.168.6.129 -c check_total_procs 

[root@mail ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.6.129 -c check_total_procs 

PROCS OK: 79 processes


五、测试

通过web页面测试

wKiom1WbQV3Dgj5fAADxbn6K00Y017.jpg

wKioL1WbQyuBjWl6AAMGnusMPVw015.jpg

至此简单nagios监控配置完成。