当前位置:   article > 正文

linux服务篇-Nagios监控_linux nagios

linux nagios

       Nagios是一款开源免费的网路监视工具(不准确,它也收费),可以监控的设备:Windows,Linux,Unix,Router,Switch,具有报警功能,是一个网络监控系统。它可以监视你指定的主机和服务,并在那些设备“变坏”和“变好”的时候通知管理员。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。Nagios Core 原先设计在Linux下运行,当然,它在其他的Unix系统下应该也能进行工作。

Nagios Ain't Gonna Insist On Sainthood" Sainthood 翻译为圣徒

官方网站:http://www.nagios.org

也分为很多个Nagios版本

其中,我们看到Nagios 4.0 数字版本命名的为开源免费的版本(core开源免费也有很多版本是收费的,如上图),Nagios XI 英文版后缀再跟版本的命名为商业版本,如下图

针对于Nagios,我们需要配合许多组件和插件来工作(打开很慢,需要FQ):

Nagios UI. Nagios Frontend. Nagios.com     //Nagios界面很不美观,可以更换主题

Nagios Addon Projects - Nagios        //Nagios插件

https://exchange.nagios.org                                           //各类拓展组件官方网站,包括脚本

Addons - Nagios Exchange                               //监控插件或脚本

Nagios运行模式和优点

运行模式:数据收集是C/S模式,用户查看监控信息是B/S模式

优点:     1:监控网络服务状态(HTTPD,FTP,SSH,MySql……)

              2:监控主机资源(处理器负载,硬盘利用率……)

              3:拓展,根据自己的需求实现拓展检测功能(插件开发)

              4:自动日志回滚

              5:具备定义网络分层结构的能力,用“parent”主机定义来表达网络主机间的关系

              6:警告,基于状态的警告:OK,Warning(警告),critical(关键),unknown(未知)

              7:可以支持并实现对主机的冗余监控

              8:WEB界面

Nagios运行原理

       Nagios Server 采集数据,客户端就需要收集数据,Nagios采集数据加以分析,呈现给用户

       两张比较经典的监控图

在大规模生产环境中,如果需要浏览历史数据,需要结合db

获取数据的方式:主动发送、NRPE插件、SNMP、NSClient++

Nagios所需要的软件

       LAMP环境,不需要mysql

nagios-3.5.1.tar.gz        #Nagios核心文件,Nagios服务文件,不建议用最新,很多插件没做好

nagios-plugins-2.1.1.tar.gz                  #Nagios 插件,用于存放各种脚本和命令

NSCP-0.5.0                          #也就是Nsclient++,用来监控Windows,分为64位和 32位版本

nrpe-2.15.tar.gz                   #代理服务,用于监控非Nagios服务器的服务器本地私有信息代理

部署Nagios监控服务器

部署之前,需要做好最基本的缓解,包括LAP环境的构建(这里直接使用yum方式安装即可,在做zabbix的时候我们需要使用我们编译的LNMP环境来做)

解决依赖和安装LAP环境

[root@taijitao63 ~]# yum install -y gcc glibc glibc-common php gd gd-devel libpng libmng libjpeg zlib

[root@taijitao63 ~]# yum install -y httpd ; systemctl restart httpd

[root@taijitao63 ~]# yum install http://mirrors.ustc.edu.cn/centos/6.7/os/x86_64/Packages/gd-devel-2.0.35-11.el6.x86_64.rpm

//部分镜像没有gd-devel这个软件包,去Centos镜像去拿下来,或者在线装

[root@taijitao63 ~]# echo "<?php phpinfo(); ?>" >/var/www/html/index.php        //测试是否支持php

Nagios运行用户

[root@taijitao63 ~]# useradd nagios   //建立Nagios用户,这里不能使用nologin的shell,nagios会用到

[root@taijitao63 ~]# groupadd nagcmd                                                      //建立一个组

[root@taijitao63 ~]# usermod -G nagcmd nagios                    //将Nagios和apache加入该组

[root@taijitao63 ~]# usermod -G nagcmd apache

Nagios核心安装

       [root@taijitao63 ~]# mkdir Nagios                                         //所有软件包放在此目录下

       [root@taijitao63 ~]# cd Nagios ; ls

nagios-3.5.1.tar.gz          nrpe-2.15.tar.gz

nagios-plugins-2.1.1.tar.gz  vautour_style.zip

       [root@taijitao63 Nagios]# tar xvf nagios-3.5.1.tar.gz -C /usr/local/src/

[root@taijitao63 Nagios]# cd /usr/local/src/nagios/

[root@taijitao63 nagios]# ./configure --with-command-group=nagcmd  //指定Nagios运行用户组

……

General Options:

 -------------------------

        Nagios executable:  nagios

        Nagios user/group:  nagios,nagios           #运行用户

       Command user/group:  nagios,nagcmd           #运行用户组

            Embedded Perl:  no

             Event Broker:  yes

        Install ${prefix}:  /usr/local/Nagios       #默认安装到了/usr/local/nagios

                Lock file:  ${prefix}/var/nagios.lock

   Check result directory:  ${prefix}/var/spool/checkresults

           Init directory:  /etc/rc.d/init.d

  Apache conf.d directory:  /etc/httpd/conf.d       #Apacheconf.d配置文件目录

             Mail program:  /bin/mail

                  Host OS:  linux-gnu

 Web Interface Options:

 ------------------------

                 HTML URL:  http://localhost/nagios/        #登陆地址

                  CGI URL:  http://localhost/nagios/cgi-bin/

 Traceroute (used by WAP):  /bin/traceroute

Review the options above for accuracy.  If they look okay,

type 'make all' to compile the main program and CGIs.

[root@taijitao63 nagios]# make all                    //根据上面提示,Make all,下图出现,就无问题

** Compile finished ***

If the main program and CGIs compiled without any errors, you

can continue with installing Nagios as follows (type 'make'

without any arguments for a list of all possible options):         

#下面已经提示安装步骤,我们就使用这个步骤来操作

  make install

     - This installs the main program, CGIs, and HTML files

  make install-init

     - This installs the init script in /etc/rc.d/init.d

  make install-commandmode

     - This installs and configures permissions on the

       directory for holding the external command file

  make install-config

     - This installs *SAMPLE* config files in /usr/local/nagios/etc

       You‘ll have to modify these sample files before you can

       use Nagios.  Read the HTML documentation for more info

       on doing this.  Pay particular attention to the docs on

       object configuration files, as they determine what/how

       things get monitored!

  make install-webconf

     - This installs the Apache config file for the Nagios

       web interface

  make install-exfoliation          #主题相关,不需要安装

     - This installs the Exfoliation theme for the Nagios

       web interface

  make install-classicui                #主题相关,不需要安装,安装到web-conf就可以了

     - This installs the classic theme for the Nagios

       web interface

*** Support Notes *******************************************

If you have questions about configuring or running Nagios,

please make sure that you:

     - Look at the sample config files

     - Read the documentation on the Nagios Library at:

           http://library.nagios.com

before you post a question to one of the mailing lists.

Also make sure to include pertinent information that could

help others help you.  This might include:

     - What version of Nagios you are using

     - What version of the plugins you are using

     - Relevant snippets from your config files

     - Relevant error messages from the Nagios log file

For more information on obtaining support for Nagios, visit:

       http://support.nagios.com

*************************************************************

Enjoy.

[root@taijitao63 nagios]# make install && make install-init && make install-commandmode && make install-config && make install-webconf                                        //若是单步执行,每次执行一部,都会提示你下一步需要干什么,非常easy

//make install安装生成/usr/local/nagios/,其中/usr/local/nagios/share即nagiosWEB访问界面的站点目录

//make install-init                      安装生成/etc/rc.d/init.d/nagios  启动脚本                                                     

//make install-config                   安装生成/usr/local/nagios/etc下的nagios相关配置文件                                 

//make install-commandmode     设定相应nagios工作目录的权限

//make install-webconf                安装Nagios的WEB配置文件到Apache的conf.d目录下

到此时,安装过程就结束了

Nagios文件熟悉

       在安装的时候,make install时生存了share这个目录,这个目录是访问界面目录:

       [root@taijitao63 nagios]# cd /root/Nagios ; ls /usr/local/nagios/share/

config.inc.php  docs    includes   js        media       rss-corefeed.php  side.php  stylesheets

contexthelp     images  index.php  main.php  robots.txt  rss-newsfeed.php  ssi

       在make install-init的时候,生存了启动脚本

       [root@taijitao63 Nagios]# ls /etc/init.d/nagios

/etc/init.d/nagios      #其实就是在/etc/rc.d/init.d/nagios

       在make install-config的时候,生成了Nagios的相关配置文件

       [root@taijitao63 Nagios]# ls /usr/local/nagios/etc/

cgi.cfg  nagios.cfg  objects  resource.cfg

#包括以后安装了plugin等软件后,配置文件也会放入此目录

       在make install-webconf的时候,已经把web-conf的相配置文件放入了/etc/httpd/conf.d/下面

       [root@taijitao63 Nagios]# ls /etc/httpd/conf.d/nagios.conf

/etc/httpd/conf.d/nagios.conf     #里面配置了一个别名,直接引用了/usr/local/nagios/share目录

       [root@taijitao63 Nagios]# vim /etc/httpd/conf.d/nagios.conf

……

Alias /nagios "/usr/local/nagios/share"

Nagios主目录

[root@taijitao63 Nagios]# ll /usr/local/nagios/

bin             #Nagios执行程序所在目录

etc             #nagios配置文件坐在目录,初始安装只有几个*.cfg文件

libexec         #监控所用命令,需要安装了nagios-plugins插件了才会有,检测命令,不装是空的

sbin            #NagiosCgi文件所在目录,外部命令所需要的文件存放目录

share           #Nagios前端页面

var             #日志文件,pid文件等

Nagios前端访问测试

       [root@taijitao63 Nagios]# service httpd restart

       需要用户名和密码,查看vim /etc/httpd/conf.d/nagios.conf配置文件

       [root@taijitao63 Nagios]# vim /etc/httpd/conf.d/nagios.conf

Alias /nagios "/usr/local/nagios/share"

<Directory "/usr/local/nagios/share">

#  SSLRequireSSL

   Options None

   AllowOverride None

   Order allow,deny

   Allow from all

#  Order deny,allow

#  Deny from all

#  Allow from 127.0.0.1

   AuthName "Nagios Access"       #这三个字段,就是访问nagios的认证用户名和密码相关配置

   AuthType Basic

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

   Require valid-user

</Directory>

Directory字段里面有Auth这三个字段,那么就可以使用htpasswd来生成用户名和密码

       [root@taijitao63 Nagios]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

       -c表示创建,二次添加用户,不能使用-c参数

[root@taijitao63 Nagios]# cat /usr/local/nagios/etc/htpasswd.users

nagionsadmin:HQolRVjV67MiQ          #密码

       [root@taijitao63 Nagios]# service nagios restart //再次测试(我的用户名是nagiosadmin,密码123456)  目前只能监控本机  #centos7也是执行该条命令,没有找到service文件

       但是现在还没有做任何深入配置,所以现在只是可以登录而已!

安装Nagios-plugins插件

Nagios-plugins含有丰富的检测命令插件,安装完成之后放在了/usr/local/Nagios/libexec下面

[root@taijitao63 Nagios]# tar xvf nagios-plugins-2.1.1.tar.gz -C /usr/local/src/

[root@taijitao63 Nagios]# cd /usr/local/src/nagios-plugins-2.1.1/

[root@taijitao63 nagios-plugins-2.1.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd

[root@taijitao63 nagios-plugins-2.1.1]# make && make install ; cd /root/Nagios/

[root@taijitao63 Nagios]# ls /usr/local/nagios/libexec/                   //已经有了很多check命令

check_apt       check_ftp           check_mailq        check_oracle   check_ssmtp

check_breeze    check_http          check_mrtg         check_overcr   check_swap

check_by_ssh    check_icmp          check_mrtgtraf     check_pgsql    check_tcp

check_clamd     check_ide_smart     check_mysql        check_ping     check_time

check_cluster   check_ifoperstatus  check_mysql_query  check_pop      check_udp

……

启动Nagios服务,检查环境

       [root@taijitao63 Nagios]# systemctl restart httpd;systemctl restart nagios

浏览器测试:登陆之后,点击hosts,可以看到,默认监控的是本机,说明环境没有什么问题

Nagios监控本服务器

配置监控之前,我们需要做一系列的准备工作(指定主配置文件需要加载的配置文件—定义主机—定义服务 –定义监控命令—检查配置文件—启动Nagios)

[root@taijitao63 Nagios]# vim /usr/local/nagios/etc/nagios.cfg

log_file=/var/log/nagios/nagios.log                         #日志位置

cfg_file=/etc/nagios/objects/commands.cfg                   #这个文件定义了很多命令

cfg_file=/etc/nagios/objects/contacts.cfg                   #定义联系人,怎么联系

cfg_file=/etc/nagios/objects/timeperiods.cfg                #定义了时间段

cfg_file=/etc/nagios/objects/templates.cfg                  #模板(联系人,主机,时间)

cfg_file=/etc/nagios/objects/localhost.cfg                  #监控本机相关配置文件

#cfg_file=/etc/nagios/objects/windows.cfg                   #windows,默认不监控

#cfg_file=/etc/nagios/objects/switch.cfg                    #交换机路由器监控,默认不监控

#cfg_file=/etc/nagios/objects/printer.cfg                   #打印机监控,默认不监控

#cfg_dir=/etc/nagios/servers                                #定义了服务合集(多个使用)

#cfg_dir=/etc/nagios/printers                               #定义了打印机合集(多个使用)

#cfg_dir=/etc/nagios/switches                               #定义了交换合集(多个使用)

#cfg_dir=/etc/nagios/routers                                #定义了路由合集(多个使用)

cfg_dir=/etc/nagios/conf.d                     #配置文件目录(下面的cfg文件一样有效)

resource_file=/etc/nagios/private/resource.cfg 资源变量配置文件,包括$USER1$变量(一个路径)等

status_update_interval=10                                   #状态更新时间,单位s

log_rotation_method=d                                       #日志滚动,默认天

service_check_timeout=60                                    #服务检查超时时间

host_check_timeout=30                                        #主机检查超时时间

event_handler_timeout=30                               

notification_timeout=30

ocsp_timeout=5

perfdata_timeout=5

       [root@taijitao63 Nagios]# vim /usr/local/nagios/etc/cgi.cfg     //此配置文件比nagios.cfg优先级高

main_config_file=/etc/nagios/nagios.cfg             #主配置文件

physical_html_path=/usr/share/nagios/html           #物理路径

url_html_path=/nagios                               #URL后面加上/nagios才能访问

use_authentication=1                                #使用认证

use_ssl_authentication=0                            #不使用ssl

authorized_for_system_information=nagiosadmin       #认证用户

监控本地NFS状态

       [root@taijitao63 Nagios]# cd /usr/local/nagios/etc/objects

       [root@taijitao63 objects]# cp localhost.cfg localhost.cfg.bak                   //备份配置文件

       [root@taijitao63 objects]# vim localhost.cfg

define service{

        use                             local-service        

        host_name                       localhost

        service_description             NFS

        check_command                   check_tcp!2049

        }

       [root@taijitao63 objects]# vim /etc/exports                     //创建一个共享

/tmp    *(rw)                 

       [root@taijitao63 objects]# /etc/init.d/nfs restart

       [root@taijitao63 objects]# showmount -e 192.168.1.63            //检测NFS目前是否正常

Export list for 192.168.1.63:

/tmp *

检测localhost.cfg是否正常启用

       [root@taijitao63 objects]# vim /usr/local/nagios/etc/nagios.cfg

cfg_file=/usr/local/nagios/etc/objects/localhost.cfg        #localhost默认已经启用了

检测配置文件是否有误

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

Total Warnings: 0

Total Errors:   0

       [root@taijitao63 objects]# service nagios restart                     //重启nagios服务

网页测试

       正在检测,稍等一会儿

       停止NFS服务

       [root@taijitao63 objects]# service nfs stop                       //再观察

这里注意,一般没回立马又显示出来状态,需要一段时间,最好最快的方法就是手动强制刷新,否则你需要等待,强制刷新,点击NFS后有一个Re-schedule the next check of this service,强制刷新

可以看到状态了,一般状态有四种颜色,如下图:

Nagios监控远程MYSQL 数据库状态

Nagios:192.168.1.63           MySQL Server :192.168.1.64

taijitao64上安装msyql

[root@taijitao64 ~]# yum install -y mysql-server mysql

[root@taijitao64 ~]# service mysqld restart

[root@taijitao64 ~]# mysql                        //登陆创建一个测试数据库

mysql> create database nagiosdb;

mysql> grant select on nagiosdb.* to nagiosdb@'192.168.1.63';

mysql> exit

taijitao63上配置Nagios服务

       [root@taijitao63 objects]# vim /usr/local/nagios/etc/nagios.cfg

# Definitions for monitoring the local (Linux) host

cfg_file=/usr/local/nagios/etc/objects/localhost.cfg            #在这两行下面添加下面两行

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

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

       [root@taijitao63 objects]# vim hosts.cfg

define host{

        use             linux-server    #使用的模板名字

        host_name       taijitao64        #主机名

        alias           MySQL-Server    #别名

        address         192.168.1.64    #IP地址

        icon_image      server.gif      #/usr/local/nagios/share/images/logos有各种图片

        statusmap_image server.gd2      #状态图

        2d_coords       500,200

        3d_coords       500,200,100

}                

       [root@taijitao63 objects]# vim service.cfg

define service{

        use                     local-service

        host_name               taijitao64

        service_groups          MysqlGroup

        service_description     MySqlSev

        check_command           check_mysql

}

define servicegroup{

        servicegroup_name       MysqlGroup

        alias                   MySQLServer

        members                 taijitao64,MySqlSev

}             

       [root@taijitao63 objects]# vim commands.cfg

define command{

        command_name    check_mysql

        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u nagiosdb -d nagiosdb

}

注意:所有的check_command字段中的所使用的命令,必须在command.cfg中定义好才能使用,而command.cfg中的command_line中使用的命令,必须在/usr/local/Nagios/libexec/目录下存在,安装plugins会生成

手动检测command.cfg的命令有效性

[root@taijitao63 objects]# /usr/local/nagios/libexec/check_mysql -H 192.168.1.64 -u nagiosdb -d nagiosdb

Uptime: 1810  Threads: 1  Questions: 6  Slow queries: 0  Opens: 15  Flush tables: 1  Open tables: 8  Queries per second avg: 0.3|Connections=4c;;; Open_files=16;;; Open_tables=8;;; Qcache_free_memory=0;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=0c;;; Qcache_queries_in_cache=0;;; Queries=6c;;; Questions=6c;;; Table_locks_waited=0c;;; Threads_connected=1;;; Threads_running=1;;; Uptime=1810c;;;

       [root@taijitao64 ~]# service mysqld stop                         //taijitao64停到服务再检测

[root@taijitao63 objects]# /usr/local/nagios/libexec/check_mysql -H 192.168.1.64 -u nagiosdb -d nagiosdb

       can't connect to MySQL server on '192.168.1.64' (111)

前段测试

       [root@taijitao63 objects]# service nagios restart

       [root@taijitao64 ~]# service mysqld start

       去停掉数据库[root@taijitao64 ~]# service mysqld stop

Nagios外部主机私有信息

       私有信息,包括默认的硬盘使用,进程数目,SWAP分区等等

nrpe插件

通过NRPE(一个插件)服务可以添加本地信息的监控,将数据发送到我们的Nagios服务器

NRPE两部分组成,一部分是监控机check_nrpe,一部分是被监控机的NRPE守护进程

Nagios 服务器执行check_nrpe 插件并告诉他检查哪个服务,check_nrpe 插件通过SSL 连接方式联系远程服务器上的NRPE守护进程,NRPE守护进程执行相应的插件完成指定的检查,并返回结果。

nrpe工作原理

Nrpe是基于SSL的机制,那么我们需要做的事情就是构建SSL环境,所以需要使用NRPE工作的时候,都需要安装SSL,而且服务端和客户端都需要安装nrpe软件,而我们客户端不需要安装NAGIOS服务端

解决依赖

[root@taijitao63 Nagios]# yum install -y openssl openssl-devel

[root@taijitao64 ~]# yum install -y openssl openssl-devel

实验所需设备都需要安装SSL的支持

服务端安装NRPE

[root@taijitao63 Nagios]# tar xvf nrpe-2.15.tar.gz -C /usr/local/src/

[root@taijitao63 Nagios]# cd /usr/local/src/nrpe-2.15/

[root@taijitao63 nrpe-2.15]# ./configure && make && make install

[root@taijitao63 nrpe-2.15]# make install-plugin && make install-daemon

                                                        //前者安装插件,后者以守护经常来运行

[root@taijitao63 nrpe-2.15]# cd /root/Nagios/ ; ls /usr/local/nagios/libexec/check_nrpe

/usr/local/nagios/libexec/check_nrpe    #这个命令需要安装nrpe之后才有

定义被监控主机

       [root@taijitao63 Nagios]# cd /usr/local/nagios/etc/objects/

[root@taijitao63 objects]# vim hosts.cfg

define host{

        use             linux-server

        host_name       taijitao64

        alias           MySQL-Server

        address         192.168.1.64

        icon_image      workstation.gif

        statusmap_image workstation.gd2

        2d_coords       500,200

        3d_coords       500,200,100

}

#我们现在还是监控的taijitao64,所以信息不用更改

定义需要监控的服务

       [root@taijitao63 objects]# vim service.cfg

define service{

        use                             local-service

        host_name                       taijitao64

        service_description              Root Partition

        check_command                   check_nrpe!check_sda2           #/root分区

        }

define service{

        use                             local-service

        host_name                       taijitao64

        service_description             Total Processes                 #进程总数

        check_command                   check_nrpe!check_total_procs

        }

define service{

        use                             local-service

        host_name                       taijitao64

        service_description             Current Load                    #负载

        check_command                   check_nrpe!check_load

}

定义NRPE监控命令

[root@taijitao63 objects]# vim commands.cfg

define command{

        command_name    check_nrpe

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

        }

[root@taijitao63 objects]# service nagios restart

客户端配置

       [root@taijitao63 objects]# cd /root/Nagios/

[root@taijitao63 Nagios]# scp nagios-plugins-2.1.1.tar.gz nrpe-2.15.tar.gz root@192.168.1.64:/root

[root@taijitao64 ~]# useradd -s /sbin/nologin nagios

[root@taijitao64 ~]# groupadd nagcmd

[root@taijitao64 ~]# usermod -G nagcmd nagios

[root@taijitao64 ~]# tar xvf nagios-plugins-2.1.1.tar.gz -C /usr/local/src/

[root@taijitao64 ~]# tar xvf nrpe-2.15.tar.gz -C /usr/local/src/

[root@taijitao64 ~]# cd /usr/local/src/nagios-plugins-2.1.1/

[root@taijitao64 nagios-plugins-2.1.1]# ./configure && make && make install

[root@taijitao64 nagios-plugins-2.1.1]# cd ../nrpe-2.15/

[root@taijitao64 nrpe-2.15]# ./configure && make && make install

[root@taijitao64 nrpe-2.15]# make install-daemon-config        //这里是客户端,不用make-install-plugin,非提示操作

[root@taijitao64 nrpe-2.15]# make install-xinetd

[root@taijitao64 nrpe-2.15]# yum install -y xinetd ; cd

[root@taijitao64 ~]# vim /etc/xinetd.d/nrpe

# default: on

# description: NRPE (Nagios Remote Plugin Executor)

service nrpe

{

        flags           = REUSE

        socket_type     = stream

        port            = 5666

        wait            = no

        user            = nagios

        group           = nagios

        server          = /usr/local/nagios/bin/nrpe

        server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd

        log_on_failure  += USERID

        disable         = no

        only_from       = 127.0.0.1 192.168.1.63        # #添加红色 nagios服务器地址, 允许235这台机器来连接自己的nrpe服务,多个IP地址空格分隔

}

[root@taijitao64 ~]# vim /etc/services               //端口注册

nrpe            5666/tcp                # NRPE

#/etc/services文件的作用:

#作用1xinet.d来启动服务时他就会在/etc/services找相应服务对应的端口来启动服务。找不到对应端口,将不启动服务。

#作用2: 显示对应端口对应的协议名。

#例如 iptables  -L  不加-n参数,   查看时,把 80   -> www  http

#作用3 查看常用端口

[root@taijitao64 ~]# /etc/init.d/xinetd restart

[root@taijitao64 ~]# chkconfig xinetd on

[root@taijitao64 ~]# netstat -antup | grep 5666

tcp        0      0 :::5666                     :::*                LISTEN      68667/xinetd 

客户端nrpe命令

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

command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 38% -c 35% -p /dev/sda1

command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 42% -c 43% -p /dev/sda2

command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

#-w为警告 -C为告急  sda2中,我们是指占用42%

 [root@taijitao64 ~]# service xinetd restart

服务端手动测试

       [root@taijitao63 Nagios]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.64

NRPE v2.15

#如果出现以一下错误,说明没有允许192.168.1.63 访问我们的64服务器

#CHECK_NRPE: Error - Could not complete SSL handshake.

#我们需要检查/etc/init.d/nrpe 下面的only from中的有无添加

root@taijitao63 Nagios]# service nagios restart

前端监控

测试:硬盘数据写入测试,看看root partition会不会告急

[root@taijitao64 ~]# dd if=/dev/zero of=1.txt count=100 bs=30M

       [root@taijitao64 ~]# rm -rf *                             //清除这些临时测试文件

配置邮件报警

       root@taijitao63 Nagios]# yum install -y sendmail

[root@taijitao63 Nagios]# /etc/init.d/sendmail start

[root@taijitao63 Nagios]# chkconfig sendmail on

[root@taijitao63 Nagios]# cd /usr/local/nagios/etc/objects/

[root@taijitao63 objects]# vim contacts.cfg

define contact{

        contact_name                    nagiosadmin 

        use                             generic-contact

        alias                           Nagios Admin

        email                           nagiostest@163.com      #修改为自己的邮箱

        }

[root@taijitao63 objects]# service nagios restart

[root@taijitao64 ~]# service mysqld restart              //taijitao64上的数据库启动

       [root@taijitao64 ~]# /etc/init.d/mysqld stop                    //然后再停掉,测试能否触发邮件

点击强制检测,避免时间等待

查看邮件

若要使用短信报警,可以配139邮件或者其他有短信提醒功能的邮件

安装PNP4Nagios

pnp4nagios是一款强大的工具,它可以通过process_perfdata.pl脚本调用rrdtool对nagios的性能数据进行分析并绘制成相应的性能图。Pnp4nagios使用的是RRDtool工具来实现画图的

依赖解决

       [root@taijitao63 Nagios]# yum install -y rrdtool php-gd

[root@taijitao63 Nagios]# tar xvf pnp4nagios-0.6.25.tar.gz

       [root@taijitao63 Nagios]# cd pnp4nagios-0.6.25

       [root@taijitao63 pnp4nagios-0.6.25]# ./configure --prefix=/usr/local/pnp4nagios --with-nagios-user=nagios --with-nagios-group=nagcmd

       [root@taijitao63 pnp4nagios-0.6.25]# make all && make install && make install-webconf && make install-config && make install-init

       [root@taijitao63 pnp4nagios-0.6.25]# make fullinstall

配置pnp4naigosnagios整合

[root@taijitao63 pnp4nagios]# vim /usr/local/nagios/etc/nagios.cfg

#修改以下几个内容

...

process_performance_data=1                      #修改

...

host_perfdata_command=process-host-perfdata         #取消注释

service_perfdata_command=process-service-perfdata

...

[root@taijitao63 pnp4nagios]# vim /usr/local/nagios/etc/objects/commands.cfg

# 'process-host-perfdata' command definition   次行在原有基础上修改,要么将上面的同名4行注释

define command{

        command_name    process-host-perfdata

        command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA

        }

# 'process-service-perfdata' command definition    次行在原有基础上修改,要么将上面的同名4行注释

define command{

        command_name    process-service-perfdata       

        command_line    /usr/local/pnp4nagios/libexec/process_perfdata.pl

        }

[root@taijitao63 pnp4nagios]# vim /usr/local/nagios/etc/objects/templates.cfg

define host {

        name       host-pnp

        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=_HOST_

        register   0

        #process_perf_data       1

}

define service {

        name       srv-pnp

        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=$SERVICEDESC$

        register   0

        #process_perf_data       1

}

[root@taijitao63 pnp4nagios]# cp ~/Nagios/pnp4nagios-0.6.25/contrib/ssi/status-header.ssi /usr/local/nagios/share/ssi/

[root@taijitao63 pnp4nagios]# vim /usr/local/nagios/etc/objects/localhost.cfg

use                     linux-server,host-pnp         ; Name of host template to use

#or

use                     local-service,srv-pnp         ; Name of service #每一行加上,那么每一个服务就会有一个图标

[root@taijitao63 pnp4nagios]# mv /usr/local/pnp4nagios/share/install.php /usr/local/pnp4nagios/share/install.php.bak

[root@taijitao63 pnp4nagios]# chown -R nagios:nagios /var/www/html/ /usr/local/nagios/ /usr/local/pnp4nagios/

[root@taijitao63 pnp4nagios]# service nagios restart;service httpd restart; service npcd start

启动服务

[root@taijitao63 check_commands]# /etc/init.d/npcd restart

配置nagios结合php4nagios

       [root@taijitao63 check_commands]# cd /usr/local/nagios/etc/

[root@taijitao63 etc]# vim nagios.cfg

process_performance_data=1

host_perfdata_command=process-host-perfdata

service_perfdata_command=process-service-perfdata

[root@taijitao63 etc]# vim /usr/local/nagios/etc/objects/commands.cfg

[root@taijitao63 etc]# cd /usr/local/nagios/etc/objects/

[root@taijitao63 objects]# vim templates.cfg

define host{

        name                            host-pnp

        action_url                      /php4nagios/graph?host=$HOSTNAME$&srv=_HOST_

        register                        0

}

define service{

        name                            srv-php

        action_url                      /php4nagios/graph?host=$HOSTNAME$&srv=$SERVICEDESC$

        register                        0

}

[root@taijitao63 objects]# vim commands.cfg                  //下面的修改

# 'process-host-perfdata' command definition

define command{

        command_name    process-host-perfdata

        command_line    /usr/local/php4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA

        }

# 'process-service-perfdata' command definition

define command{

        command_name    process-service-perfdata

        command_line    /usr/local/php4nagios/libexec/process_perfdata.pl

[root@taijitao63 objects]# cd /root/Nagios/pnp4nagios-0.6.25/contrib/ssi/

[root@taijitao63 ssi]# cp status-header.ssi /usr/local/nagios/share/ssi/

测试

浏览器访问测试:192.168.1.63/Nagios

拓展

以下步骤可以不做,但是做了更科学

[root@taijitao63 pnp4nagios-0.6.25]# cd /usr/local/pnp4nagios/etc/

[root@taijitao63 etc]# mv misccommands.cfg-sample misccommands.cfg

[root@taijitao63 etc]# mv nagios.cfg-sample nagios.cfg

[root@taijitao63 etc]# mv rra.cfg-sample rra.cfg

[root@taijitao63 etc]# cd pages/

[root@taijitao63 pages]# mv web_traffic.cfg-sample web_traffic.cfg

[root@taijitao63 pages]# cd ../check_commands/

[root@taijitao63 check_commands]# mv check_all_local_disks.cfg-sample check_all_local_disks.cfg

[root@taijitao63 check_commands]# mv check_nrpe.cfg-sample check_nrpe.cfg

[root@taijitao63 check_commands]# mv check_nwstat.cfg-sample check_nwstat.cfg

Windows主机监控

       Nagios中文帮助:sourceforget.net -          //若打不开FQ

监控Windows

       # vim /etc/nagios/nagios.cfg

cfg_file=/etc/nagios/objects/windows.cfg                #取消注释

       # vim /etc/nagios/objects/windows.cfg

define host{

        use             windows-server 

        host_name       winserver      

        alias           My Windows Server      

        address         192.168.1.55            #修改此项地址为Windowsip地址

        }

       其余的,更具需求选择需要监控的内容

在windows 上安装NSCP软件,根据提示下一步即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/1005405
推荐阅读
相关标签
  

闽ICP备14008679号