Nagios安装配置及使用

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

1、工作原理图:

154033192.jpg

2、Nagios简述

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

Nagios主要监控服务/资源方面,适合多服务器上面的多服务监控,重点并不在图形化的监控,报警系统比cacti强大很多,nagios核心不做任何监控,所有监控都是由插件(脚本)来完成的。

CactiSNMP协议收集被监控服务器信息,而nagios有自己的agent收集信息(NRPE)。

3、Nagios插件说明

Nagios都是通过插件实现的,通常由一个主程序(nagios)、一个插件程序(nagios-plugins

四个可选的附件(NRPENSCA工作客户端,NSclient 工作服务器和客户端、NDOUtils 工作服务端)

NRPE监控linux/unix主机上执行脚本检测以实现对这些主机服务/资源的监控。

NSCA让被监控的linux/unix主机主动将监控信息发送给nagios服务器。

NSClient++用来监控windows主机时安装在windows主机上的组件(NSClient++ plugin)。

NDOUtils:将nagios的配置信息和事件产生的数据存入数据库,以实现这些数据的快速检索和处理,用于与Cacti整合

Cacti与Nagios整合需要插件:

ndoutils下载:http://sourceforge.net/projects/nagios/files/ndoutils-2.x/ndoutils-2.0.0/ndoutils-2.0.0.tar.gz

npc下载:http://dl.cactifans.org/plugins/npc-2.0.4.tar.gz

4、Nagios插件下载

监控主机:

wget http://nchc.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.4.4/nagios-3.4.4.tar.gz

wget http://nchc.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz

wget http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz

被监控 Linux 主机:

wget http://nchc.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz

wget http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz

被监控 Windows 主机:

http://nsclient.org/nscp/downloads

http://files.nsclient.org/0.3.x/NSClient%2B%2B-0.3.9-Win32.zip

http://files.nsclient.org/0.3.x/NSClient%2B%2B-0.3.9-x64.zip

一、安装apache和php

1
2
3
yum  install  -y httpd httpd-devel php php-gd php-devel php-mysql php-pdo php-mbstring php-snmp net-snmp-* perl-DBD-MySQL glibc glibc-common libjpeg libjpeg-devel libpng libpng-devel zlib zlib-devel gd gd-devel
service httpd start
chkconfig httpd on

二、安装nagios

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
groupadd nagcmd
useradd  -G nagcmd nagios
usermod  -a -G nagcmd apache
tar  zxvf nagios-3.4.1. tar .gz
cd  nagios
. /configure  --with-nagios-group=nagios -- enable -event-broker  #整合时间代理程序,默认安装/usr/local/nagios
make  all
make  install              #安装主要的程序、CGI及HTML文件
make  install -init         #生成init启动脚本
make  install -config       #安装示例配置文件
make  install -commandmode  #设置相应的目录权限
make  install -webconf      #安装Apache配置文件,Nginx下不需要
chown  nagios:nagios -R  /usr/local/nagios/
/usr/local/nagios/bin/nagios  - v  /usr/local/nagios/etc/nagios .cfg  #验证nagios配置文件
Service nagios start
chkconfig nagios on

安装完成后,/usr/local/ngios下生成六个文件夹:

etc:Nagios配置文件位置

sbin:Nagios cgi文件所在目录,也就是执行外部命令所需文件所在的目录

Share:Nagios网页文件所在的目录

var:Nagios日志文件、spid 等文件所在的目录

var/archives:日志归档目录

var/rw:用来存放外部命令文件

创建nagiosweb登陆web页面用户和密码

1
2
htpasswd -c  /usr/local/nagios/etc/htpasswd . users  test
New password: 123456

修改nagios默认登陆web页面用户

1
2
vi  /usr/local/nagios/etc/objects/contacts .cfg
contact_name nagiosadmin  #把nagiosadmin修改为test

访问nagios页面:http://ip/nagios

三、安装nagios-plugins插件

1
2
3
4
tar  zxvf nagios-plugins-1.4.16. tar .gz
cd  nagios-plugins-1.4.16
. /configure  --with-nagios-user=nagios --with-nagios-group=nagios --with-mysql= /usr/local/mysql  #添加监控mysql支持
make  &&  make  install

四、修改所有用户都有权限访问web页面

1
2
3
4
5
6
7
8
vi  nagios /etc/cgi .cfg  #默认授权的都是nagiosadmin,改为*所有用户
authorized_for_system_information=*
authorized_for_configuration_information=*
authorized_for_system_commands=*
authorized_for_all_services=*
authorized_for_all_hosts=*
authorized_for_all_service_commands=*
authorized_for_all_host_commands=*

五、cfg配置文件介绍

120807901.jpg

监控Linux主机

一、nagios监控端安装nrpe

1
2
3
4
5
tar  zxvf nrpe-2.13. tar .gz  #基于nrpe检测,所以必须也要安装
cd  nrpe-2.13
. /configure  --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios
make  all
make  install -plugin

2、编辑 commands.cfg 文件定义 nrpe 对nagios使用

1
2
3
4
5
vi  /usr/local/nagios/etc/objects/commands .cfg 末尾添加:
define  command {
command_name check_nrpe  #定义命令名称为check_nrpe,在services.cfg中要使用这个名称
command_line $USER1$ /check_nrpe  -H $HOSTADDRESS$ -c $ARG1$
#这是定义实际运行的插件程序,-c后面带的$ARG1$参数是传给nrpe daemon执行的检测命令

3、配置加载监控对象文件:

1
2
3
vi  /usr/local/nagios/etc/nagios .cfg
在 cfg_file= /usr/local/nagios/etc/objects/localhost .cfg 下添加:
cfg_file= /usr/local/nagios/etc/objects/linux .cfg

4、设置要监控的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
vi  /usr/local/nagios/etc/objects/linux .cfg
define host{
  use linux-server
  host_name squid. test .com
  alias  my squid  #web页面显示的名字
  address 192.168.0.202  #被监控主机ip
}
define service{
  use generic-service
  host_name squid. test .com
  service_description check-swap
  check_command check_nrpe!check_swap
}
define service{
  use generic-service
  host_name squid. test .com
  service_description check-disk
  check_command check_nrpe!check_disk
}
define service{
  use generic-service
  host_name squid. test .com
  service_description check- ssh
  check_command check_nrpe!check_ssh
}
define service{
  use generic-service
  host_name squid. test .com
  service_description check-http
  check_command check_nrpe!check_http
}
保存退出!
/usr/local/nagios/bin/nagios  - v  /usr/local/nagios/etc/nagios .cfg  #验证nagios配置文件
service nagios restart
设置开机启动:
echo  "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d"  >>  /etc/rc . local

二、Linux被监控端安装nagios-plugin和nrpe

1、安装nagios插件

1
2
3
4
5
useradd  -s  /sbin/nologin  nagios
tar  -zxvf nagios-plugins-1.4.14. tar .gz
cd  nagios-plugins-1.4.14
. /configure  --with-nagios-user=nagios --with-nagios-group=naigos
make  all &&  make  install

2、安装nrpe

1
2
3
4
5
6
7
8
tar  -zxvf 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
make  all
make  install -plugin
make  install -daemon
make  install -daemon-config
make  install -xinetd  #nrpe安装为xinetd服务

3、配置nrpe

1
2
3
4
5
6
7
8
9
vi  /etc/xinetd .d /nrpe
only_from = 127.0.0.1,192.168.0.204  #添加监控主机地址
vi  /usr/local/nagios/etc/nrpe .cfg
allowed_hosts=127.0.0.1,192.168.0.204  #允许监控主机对其监控
/usr/local/nagios/bin/nrpe  -c  /usr/local/nagios/etc/nrpe .cfg -d  #启动nrpe
netstat  -tuplna |  grep  5666 或  ps  -elf | grep  nrpe  #查看是否启动
/usr/local/nagios/libexec/check_nrpe  -H 192.168.0.203  #在监控端检测被监控端信息,显示版本则成功。
设置开机启动:
echo  "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d"  >>  /etc/rc . local

登陆web页面查看被监控主机信息!

4、nagios-web界面提示:

It appears as though you do not have permission to view information for any of the services you requested...

解决:vi /usr/local/nagios/etc/cgi.cfg

use_authentication=0 #默认1,设置为0

service nagios restart


监控Windows主机

一、nagios监控端配置

1、配置加载监控对象文件

1
2
vi  /usr/local/nagios/etc/nagios .cfg
cfg_file= /usr/local/nagios/etc/objects/windows .cfg  #去掉注释

2、编辑 commands.cfg 文件定义check_nt对nagios使用(默认已经定义,不需要再配置)

1
2
3
4
define  command {
  command_name check_nt
  command_line $USER1$ /check_nt  -H $HOSTADDRESS$ -p 12489 - v  $ARG1$ $ARG2$
  }

3、设置要监控的内容(默认配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
vi  /usr/local/nagios/etc/objects/windows .cfg
define host{
  use windows-server
  host_name winserver
  alias  My Windows Server
  address 192.168.0.202  #被监控主机ip
  }
define hostgroup{
  hostgroup_name windows-servers
  alias  Windows Servers
  }
define service{
  use generic-service
  host_name winserver
  service_description NSClient++ Version
  check_command check_nt!CLIENTVERSION
  }
define service{
  use generic-service
  host_name winserver
  service_description Uptime
  check_command check_nt!UPTIME
  }
define service{
  use generic-service
  host_name winserver
  service_description CPU Load
  check_command check_nt!CPULOAD!-l 5,80,90
  }
define service{
  use generic-service
  host_name winserver
  service_description Memory Usage
  check_command check_nt!MEMUSE!-w 80 -c 90
  }
define service{
  use generic-service
  host_name winserver
  service_description C:\ Drive Space
  check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
  }
define service{
  use generic-service
  host_name winserver
  service_description W3SVC
  check_command check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
  }
define service{
  use generic-service
  host_name winserver
  service_description Explorer
  check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
  }

二、Windows被监控端配置

NSClient++与 Nagios 服务器通信,使用的是Nagios 服务器的 check_nt 插件与nsclient连接


181951187.jpg

安装配置NSClient++

下载:http://nsclient.org/nscp/downloads,下面使用是不用安装的包,也可以使用安装包。

1、将NSClient++-0.3.9-Win32解压到C盘下NSClient++下

2、打开cmd切换到C盘NSClient++下,安装:"NSClient++.exe" /install,再"NSClient++.exe" SysTray

3、运行services.msc->右击属性NSClientpp->登陆->打钩允许服务与桌面交互

4、配置NSClient

打开nsc.ini去掉注释:CheckWMI.dll、RemoteConfiguration.dll和port=12489

allowed_hosts=127.0.0.1/32,192.168.0.204 #添加监控主机ip

5、启动NSClient++,进入NSClient++目录下,输入NSClient++ /start

6、监控端验证:/usr/local/nagios/libexec/check_nrpe -H 192.168.0.200

打开nagios web页面即可看到windows监控信息!




本文转自 李振良OK 51CTO博客,原文链接:http://blog.51cto.com/lizhenliang/1297365,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
监控 关系型数据库 MySQL
zabbix4.0安装配置及使用
zabbix4.0安装配置及使用
5845 0
|
监控 测试技术 Linux
|
监控 网络协议 测试技术
|
PHP Apache 数据安全/隐私保护
|
监控 关系型数据库 MySQL
|
监控 Apache 开发工具