nagios安装和配置(个人笔记)

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

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

Nagioscacti有什么区别呢?简单的来说cacti主要监控流量,服务器状态页面展示;nagios主要监控服务,邮件及短信报警灯,当然也有简单的流量监控界面,二者综合使用效果更好。(附Nagios工作简单逻辑图)

Nagios监控客户端需要借助插件及NRPE软件来实现,NRPE作为中间的代理程序,接收Nagios服务器端发来的请求,另一端在远程主机上指定的相关的监控信息。

Nagios配置及使用方法简介
 已安装完成的Nagios存在etc、bin、sbin、share、var 这五个目录,如果存在则可以表明程序被正确的安装到系统了。Nagios 各个目录用途说明如下:
bin Nagios 可执行程序所在目录
etc Nagios 配置文件所在目录
sbin Nagios CGI 文件所在目录,也就是执行外部命令所需文件所在的目录
share Nagios网页文件所在的目录
libexec Nagios 外部插件所在目录
var Nagios 日志文件、lock 等文件所在的目录
var/archives Nagios 日志自动归档目录
var/rw 用来存放外部命令文件的目录

1.监控端安装步骤(IP:192.168.77.133)

(1)


    1. 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      安装nagios相关依赖
      yum  install  -y gcc glibc glibc-common gdgd-devel xinetd openssl-devel php.x86_64 perl;
        
      yum  install   httpd  mysql   mysql-server    php   php-mysql  -y 
      创建nagiso用户及相关路径    
      mkdir  /usr/local/nagios
      chown  -R nagios:nagios /usr/local/nagios
      useradd  nagios
        
      编译下载安装nagios
      wget http: //sourceforge .net /projects/nagios/files/nagios-3 .x /nagios-3 .2.1 /nagios-3 .2.1. tar .gz /download
      http: //down1 .chinaunix.net /distfiles/nagios-plugins-1 .4.14. tar .gz http: //nchc .dl.sourceforge.net /project/nagios/nrpe-2 .x /nrpe-2 .14 /nrpe-2 .14. tar .gz
      useradd
      nagios
      tar  zxvf nagios-3.2.1. tar .gz
      cd   nagios-3.2.1
      . /configure   --prefix= /usr/local/nagios   --with- command -group=nagios
      make  all
      makeinstall  // 来安装主程序,CGI和HTML文件
      makeinstall-init  // /etc/rc .d /init .d安装启动脚本
      makeinstall-config  // 来安装示例配置文件,安装的路径是 /usr/local/nagios/etc
      makeinstall-commandmode  // 来配置目录权限
      makeinstall-webconf  //  配置nagios跟apache整合
      make  all ;  make  install make  install -init; make  install -config; make  install -commadnmode; make  install -webconf;

(2)安装nagios-plugins

1
2
3
4
5
6
7
8
tar  zxvf
nagios-plugins-1.4.14. tar .gz
      cd
nagios-plugins-1.4.14 
     . /configure
--prefix= /usr/local/nagios  --with-nagios-user=nagios --with-nagios-group=nagios
      make
&&  make  install

(3)插件nrpe安装

1
tar  -xzf nrpe-2.14. tar .gz &&  cd  nrpe-2.14&&. /configure  -- enable -ssl --with-ssl-lib && make  all && make  install -plugin &&  make  install -daemon && makeinstall-daemon-config

      (4)nagios访问控制设置和测试

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

输入两次密码即可,登录页面的时候会用到这个密码.

重启nagios,/etc/init.d/nagios restart ;/etc/init.d/httpd restart ;

http://192.168.77.133/nagios/如下图wKiom1WOxPDAfCZEAAN6fAnsV24673.jpg

    (4)被监控端安装(IP:192.168.77.166)

1
2
3
4
5
useradd  nagios ; tar  -xzf nagios-plugins-1.4.14. tar .gz && cd  nagios-plugins-1.4.14&&. /configure  –prefix= /usr/local/nagios && make  && make  install
tar  -xzf nrpe-2.14. tar .gz &&  cd  nrpe-2.14&&. /configure  -- enable -ssl --with-ssl-lib && make  all && make  install -plugin &&  make  install -daemon && makeinstall-daemon-config
       chown
-R nagios:nagios  /usr/local/nagios/
       启动nrpe客户端命令: /usr/local/nagios/bin/nrpe  -c  /usr/local/nagios/etc/nrpe .cfg -d

    (5)监控配置

    a. 监控端配置:

cp localhost.cfg  192.168.77.166.cfg

把默认配置文件里面的locahost、127.0.0.1、check_local替换成最新,

在nagios.cfg 36行后加入cfg_file=/usr/local/nagios/etc/objects/192.168.77.166.cfg

最后执行:/usr/local/nagios/bin/nagios -v/usr/local/nagios/etc/nagios.cfg没有报错即可

定义nrpe命令,我这里监控mysql主从,http关键字需要在 commands.cfg中定义命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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$ -s -uroot -p12345
6
           }
define  command {
         command_name    check_http_word
         command_line    $USER1$ /check_http  -I $HOSTADDRESS$ -u $ARG1$ -p $ARG2
$ -s $ARG3$
           }

然后在192.168.77.166.cfg中定义所要监控的服务:

    define service{
        use                             local-service        
        host_name                       192.168.77.166
        service_description             sda1_monitor
        check_command                   check_nrpe!check_sda1
        notifications_enabled           1
        }
define service{
        use                             local-service        
        host_name                       192.168.77.166
        service_description             sda2_monitor
        check_command                   check_nrpe!check_sda2
        notifications_enabled           1
        }
define service{
        use                             local-service
        host_name                       192.168.77.166
        service_description             sda5_monitor
        check_command                   check_nrpe!check_sda5
        notifications_enabled           1
        }
define service {
         use                  local-service
        host_name             192.168.77.166
        service_description   check_mysql_slave
        check_command         check_mysql
        }

define service {
     use                  local-service
    host_name             192.168.77.166
    service_description   monitor_Discuz_论坛
    check_command         check_http_word!/index.php!80!论坛
    }

然后在被监控端定义监控命令:

1
2
command [check_mysql]= /usr/local/nagios/libexec/check_mysql
command [check_users]= /usr/local/nagios/libexec/check_users  -w 5 -c 10  command [check_load]= /usr/local/nagios/libexec/check_load  -w 15,10,5 -c 30,25,20command[check_sda1]= /usr/local/nagios/libexec/check_disk  -w 20% -c 10% -p  /dev/sda1command [check_sda2]= /usr/local/nagios/libexec/check_disk  -w 20% -c 10% -p  /dev/sda2command [check_sda5]= /usr/local/nagios/libexec/check_disk  -w 20% -c 10% -p  /dev/sda5

最后启动nrpe:

1
2
3
/usr/local/nagios/bin/nrpe  -c /usr/local/nagios/etc/nrpe .cfg -d
/usr/local/nagios/libexec/check_nrpe  -Hlocalhost被监控端验证
/usr/local/nagios/libexec/check_nrpe  -Hlocalhost -c check_users测试命令

    b.

监控客户端5个步骤:

 1、在服务器端和客户端都安装nrpe,在客户端安装nagios-plugins

2、在客户端定义监控的具体项目,修改allow允许的ip(服务器的ip)

3、分别启动客户端、服务器nrpe。

4、在服务器端commands.cfg中定义check_nrpe命令:

define command{

       command_name    check_nrpe

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

       }

5、在服务器端对应IP的配置文件中,加入需要监控的项目:

define service{

       use                            local-service        

       host_name                      192.168.77.166

       service_description            Sda1_Monitor

       check_command                  check_nrpe!check_u

       notifications_enabled           1

       }

(6)

  1. Nagios邮件及短信报警

使用nagios报警,以前可以用飞信发送报警,但是自从飞信更改接口后,就不方便了,那我们要发短信报警怎么办呢,我们可以139邮箱,机制是nagios给139邮箱发送信息,然后信息会自动发到我们绑定的手机。提前在139上绑定好手机即可。除此之外还可以使用短信猫(收费)设备来发送报警。

默认command.cfg里面已经配置好了邮件报警设置,可以使用默认的配置,使用系统默认的mail发送邮件;还可以自己定义发送的内容格式及发送的邮件smtp服务器端软件。需要在vi /usr/local/nagios/etc/objects/contacts.cfg中定义自己要接受的邮件地址。

需要安装邮件服务:yum –y install sendmail* mailxservice sendmail  restart;chkconfig  sendmail on,echo “ok” |mail  -s  “title” 邮箱测试即可


自此,Nagios相关的配置就到此为止,后序的进一步研究会持续更新



本文转自 Anonymous123 51CTO博客,原文链接:http://blog.51cto.com/woshitieren/1668679



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
监控 Linux PHP
Nagios学习实践系列——基本安装篇
开篇介绍      最近由于工作需要,学习研究了一下Nagios的安装、配置、使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇   实验环境     操作系统:Red Hat Enterprise Linux Server release 6.0 (Santiago)   64 bit。
1053 0
|
监控 Apache 数据安全/隐私保护
|
Web App开发 监控 应用服务中间件
|
监控 Ubuntu 数据安全/隐私保护
|
PHP Apache 数据安全/隐私保护
|
监控 测试技术 Linux
|
Apache 数据安全/隐私保护