开源监控-ngios

Nagios是一款开源的免费网络监视工具,能有效监控WindowsLinuxUnix主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一间通知网站运维人员,在状态恢复后发出

正常的邮件或短信通知。



实验拓扑


164730925.png

主机ngios安装过程

准备环境

挂载光驱 配置yum

[root@chh1 ~]# mount /dev/cdrom /mnt/cdrom

[root@chh1 ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo

164731167.png

yum安装软件包

[root@chh1 ~]# yum install httpd php php-gd php-mbstring mysql-devel

[root@chh1 ~]# service httpd start

[root@chh1 ~]# chkconfig httpd on

安装NMS监管服务器所需要的软件包

164732721.png

下面开始安装这些软件包

拆解 ngios的数据包

[root@chh1 ~]# tar -zxvf nagios-3\[1\].2.1.tar.gz -C /usr/local/src/

创建一个组 nagcmd

groupadd nagcmd

创建一个账号 nagios

useradd -m nagios

加入组

usermod -a -G nagcmd nagios

将apache 账号也添加竟来

usermod -a -G nagcmd apache

来到源码目录,开始配置nagios-3\[1\].2.1

[root@chh1 nagios-3.2.1]# ./configure --with-command-group=nagcmd --prefix=/usr/local/nagios --sysconfdir=/etc/nagios --enable-event-broker

[root@chh1 nagios-3.2.1]# make all

[root@chh1 nagios-3.2.1]# make install

[root@chh1 nagios-3.2.1]# make install-init

[root@chh1 nagios-3.2.1]# make install-commandmode

[root@chh1 nagios-3.2.1]# make install-config

[root@chh1 nagios-3.2.1]# make install-webconf

创建一个登陆nagios web程序的用户 这个账号在以后通过web登陆nagios认证时使用

[root@chh1 conf.d]# htpasswd -c /etc/nagios/htpasswd.users nagiosadmin

New password:

Re-type new password:

Adding password for user nagiosadmin 密码:123

添加nagios进行chkconfig管理

[root@chh1 conf.d]# chkconfig --add nagios

[root@chh1 conf.d]# chkconfig --list |grep nagios

nagios 0:off1:off2:off3:on4:on5:on6:off

安装插件

拆解插件包

[root@chh1 ~]# tar -zxvf nagios-plugins-1.4.15.tar.gz -C /usr/local/src/

来进行插件的配置

[root@chh1 nagios-plugins-1.4.15]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios

安装

[root@chh1 nagios-plugins-1.4.15]# make && make install

对nagios的配置文件进行语法的检测

[root@chh1 libexec]# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg

检测结果是这样就说明没有问题

164732808.png

启动nagios

[root@chh1 libexec]# service nagios start

Starting nagios: done.

[root@chh1 libexec]# chkconfig nagios on

重启apache加载文件

[root@chh1 libexec]# service httpd restart

Stopping httpd: [ OK ]

Starting httpd: [ OK ]

下面我们访问一下试试

164733822.png

164733344.png

164735974.png

164736806.png

164739825.png

在图上的各种服务都是在/etc/nagios/objects/commands.cfg中定义的,我们可以打开看看

下面给大家来看个小例子

打开/etc/nagios/objects/commands.cfg中命令的定义

我们找到check_ping

164740884.png

那么上图中的那一行命令到底是什么意思呢?下面我给大家好好解释一下

首先看一下$USER1$:

164741749.png

从上图我们可以看出在/etc/nagios/resource.cfg我们会找到$USER1$,其实就是调用了我们的插件库目录而已。

在来到我们的插件库看看/usr/local/nagios/ libexec,看看到底有没有check_ping

164742601.png

结果我们可以找到check_ping,但是这个该怎么用呢,小编我也很郁闷,那我们再来看一下/etc/nagios/objects/commands.cfg中对check_ping的定义

command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5

那我们再来看看check_ping的用法吧

[root@chh1 libexec]# ./check_ping -h |less

164744458.png

这下大家应该清楚那一行命令的意思了吧

下面来来个例子ping一下我们的主机试试吧。

164744213.png

关于更多的插件的使用,大家可以使用帮助来查看,我就不一个个解释了。

接着我们来看一下主机文件在/etc/nagios/objects/localhost.cfg

164745127.png

164745269.png

164745258.png

164746759.png

在控制台上的服务都是在这里面定义的服务。在上面我看到了报警,但是报警需要联系人,下面我们就来看一下联系人文件/etc/nagios/objects/contacts.cfg

164747289.png

164747274.png

为了测试发送邮件,小编多登陆了几个用户。看看邮件日志

164751325.png

到我的163邮箱看看

164753649.png

相信看了这几个文件大家对nagios就有了一个比较清晰的了解吧。


下面我们开始监控一台windows主机试试。

164754251.png

监控windows主机我们还可以采用NRPE,SNMP

首先我们需要在windows主机上安装NSCLient++

164755824.png

164756202.png

164758983.png

164759735.png

打开模块,引用他们

164801752.png

164803257.png

然后保存,重启我们的服务。

修改/etc/nagios/objects/windows.cfg

[root@chh1 objects]# vim windows.cfg

164803920.png

修改nagios的配置文件让它监控windows

[root@chh1 nagios]# pwd

/etc/nagios

[root@chh1 nagios]# vim nagios.cfg

164804728.png

检查语法参数。重启nagios

[root@chh1 nagios]# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg

[root@chh1 nagios]# service nagios restart

打开页面看看,已经监控到了。

164806864.png

下面我们再来看看监控linux主机试试。这次我们采用NRPE,但是我们的nagios是没有这个插件。所以我们需要自己安装。。。

我们需要监控mysqlApache。所以我们就安装一下他们

[root@chh1 ~]# yum install mysql-devel mysql mysql-server httpd

启动一下httpmysql

164919617.png

mysql添加密码

[root@chh1 ~]# chkconfig mysqld on

[root@chh1 ~]# mysqladmin -u root -p password '123'

下面开始在我们的linux客户端上安装插件

先创建账号 nagios

[root@chh1 ~]# useradd nagios

拆解插件包

[root@chh1 ~]# tar -zxvf nagios-plugins-1.4.15.tar.gz -C /usr/local/src/

来到拆解目录配置文件

[root@chh1 nagios-plugins-1.4.15]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios

[root@chh1 nagios-plugins-1.4.15]# make && make install

在客户端上安装nrpe

[root@chh1 ~]# tar -zxvf nrpe-2.12.tar.gz -C /usr/local/src/

[root@chh1 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

# make install-daemon

# make install-daemon-config

编辑客户端 nrpe配置文件

[root@chh1 nrpe-2.12]# vim /usr/local/nagios/etc/nrpe.cfg

164920184.png

164920269.png

启动nrpe

[root@chh1 nrpe-2.12]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios

[root@chh1 nrpe-2.12]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >>/etc/rc.d/rc.local

[root@chh1 nrpe-2.12]#

1. ngios服务器上安装插件。

[root@chh1 ~]# tar -zxvf nrpe-2.12.tar.gz -C /usr/local/src/

对拆解文件进行配置

./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. commands.cfg 定义外部构件nrpe

[root@chh1 nrpe-2.12]# vim /etc/nagios/objects/contacts.cfg

增加

define command{

command_name check_nrpe

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

164921204.png

3.创建监控linux主机的文件。

/usr/local/nagios/etc/objects 创建 mylinux.cfg

164922788.png

164924913.png

编辑客户端文件添加我们监控中没有命令

[root@chh1 ~]# vim /usr/local/nagios/etc/nrpe.cfg

164926636.png

重启

[root@chh1 ~]# pkill -9 nrpe

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

4.为了监控linux系统 修改 nagios.cfg

[root@chh1 objects]# vim /etc/nagios/nagios.cfg

164927935.png

做一下语法检测

[root@chh1 objects]# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg

重启ngios服务。

164927283.png

下面开始添加我们监控web的命令。

在客户端上

[root@chh1 ~]# cd /usr/local/nagios/etc/

[root@chh1 etc]# vim nrpe.cfg

164928228.png

然后要重启nrpe

164929860.png

来到服务器上

修改mylinux

[root@chh1 ~]# vim /etc/nagios/objects/mylinux.cfg

164931640.png

重启一下

164931657.png

那我们来到打开nagios看看(windows主机没有启动)

164934191.png