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

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

(附Nagios工作简单逻辑图)

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监

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

一、nagios服务器配置

1、准备LAMP环境

yum install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server -y

2、安装nagios

下载、解压、创建nagios用户、编译安装:

下载这步可以使用软件winscp直接上传;

tar xf nagios-3.2.1.tar.gz

cd nagios-3.2.1

useradd nagios

./configure --prefix=/usr/local/nagios  --with-command-group=nagios 

make all

make install;make install-init;make install-config;make install-commandmode ;make install-webconf

命令注解:

make install              //来安装主程序,CGI和HTML文件

makeinstall-init         //在/etc/rc.d/init.d安装启动脚本

makeinstall-config    //来安装示例配置文件,安装的路径是/usr/local/nagios/etc

makeinstall-commandmode //来配置目录权限

makeinstall-webconf // 配置nagios跟apache整合

 

3、安装Nagios-plugins

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

 

4、定义nagios 访问控制配置, 创建一个登录nagios web程序的用户,这个用户帐号在以后通过
web登录nagios认证时所用;

apache与nagios整合的目录在这里:/etc/httpd/conf.d/nagios.conf

打开该配置文件,查看到内容:

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_02

htpasswd:专用来生成密码!

往这个文件中:/usr/local/nagios/etc/htpasswd.users,写入密码;

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_03

在这里会用到:与/etc/httpd/conf.d/nagios.conf 路径下面的认证文件要一致!

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_04

5、配置nagios

把nagios添加为系统服务并将之加入到自动启动服务队列:

chkconfig --add nagios

chkconfig nagios on

检查其主配置文件的语法是否正确:

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

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_05

启动nagios、apache服务,并打开首页登录:http://ip/nagios

用户名:nagiosadmin;密码:1

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_06

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_07

注:如果登陆了该页面,无法进入,需要重启一次nagios服务!

6、启用对http的监控报警

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_08

无法打开该目录,Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_09

查找一下该目录,发现确即使是有了读写权限,没有执行权限,也是无法运行的!赋予可执行的权限!

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_10

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_11

再次进入该页面,完成!

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_12

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_13

 

我们要成功配置出一台nagios监控系统,必须要弄清楚每个配置文件之间依赖与被依赖的关系。

最重要的有四点:

第一:定义监控哪些主机、主机组、服务和服务组;

第二:定义这个监控要用什么命令实现;

第三:定义监控的时间段;

 第四:定义主机或服务出现问题时要通知的联系人和联系人组。

 

二、初识Nagios

Nagios可以监控的功能有:

1、监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);

2、监控主机资源(处理器负荷磁盘利用率等);

3、简单地插件设计使得用户可以方便地扩展自己服务的检测方法;

4、并行服务检查机制;

5、具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;

6、当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);

7、可以定义一些处理程序,使之能够在服务或者主机发生故障时起到预防作用;

8、自动的日志滚动功能;

9、可以支持并实现对主机的冗余监控;

10、可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等。

Nagios 对系统的需求:

Nagios 所需要的运行条件是机器必须可以运行Linux(或是Unix变种)并且有C语言编译器。你必须正确地配置TCP/IP协议栈以使大多数的服务检测可以通 过网络得以进行。

默认安装完nagios,配置文件主目录在/usr/local/nagios/下,目录各种的功能如下:

bin

Nagios 可执行程序所在目录

etc

Nagios 配置文件所在目录

sbin

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

share

Nagios网页文件所在的目录

libexec

Nagios 外部插件所在目录

var

Nagios 日志文件、lock 等文件所在的目录

var/archives

Nagios 日志自动归档目录

var/rw

用来存放外部命令文件的目录

了解etc/objects目录主要包括监控主机的配置、模板、监控时间段等配置文件:

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_14

每个文件或目录含义如下表所示:

目录名                 用途

cgi.cfg:控制CGI访问的配置文件;

nagios.cfg:Nagios 主配置文件;

resource.cfg:变量定义文件,称为资源文件,文件中定义变量,以便由其他配置文件引用,  如 $USER1$;

objects:objects 是一个目录,在此目录下有很多配置文件模板,用于定义Nagios对象;

objects/commands.cfg:定义配置文件,其中定义的命令可以被其他配置文件引用;

存放监控的命令、参数、配置:而需要调用该命令的插件是:/usr/local/nagios/libexec/

需要到commands.cfg里面定义:

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_15

objects/contacts.cfg:定义联系人和联系人组的配置文件;

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_16

objects/localhost.cfg: 定义监控本地主机的配置文件;

objects/printer.cfg :定义监控打印机的一个配置文件模板,默认没有启用此文件;

objects/switch.cfg :定义监控路由器的一个配置文件模板,默认没有启用此文件;

objects/templates.cfg:定义主机和服务的一个模板配置文件,可以在其他配置文件中引用;

objects/timeperiods.cfg:定义Nagios 监控时间段的配置文件;

 objects/windows.cfg:监控Windows 主机的一个配置文件模板,默认没有启用此文件;

nagios.cfg内容详解

log_file=/usr/local/nagios/var/nagios.log                        # 定义nagios日志文件的路径

cfg_file=/usr/local/nagios/etc/objects/commands.cfg     #“cfg_file”变量用来引用对象配置文件,如果有更多的对象配置文件,在这里依次添加即可。

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

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

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

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

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

cfg_file=/usr/local/nagios/etc/objects/localhost.cfg         # 本机配置文件

cfg_file=/usr/local/nagios/etc/objects/windows.cfg         # windows 主机配置文件

object_cache_file=/usr/local/nagios/var/objects.cache     # 该变量用于指定一个“所有对象配置文件”的副本文件,或者叫对象缓冲文件。

precached_object_file=/usr/local/nagios/var/objects.precache

resource_file=/usr/local/nagios/etc/resource.cfg              # 该变量用于指定nagios资源文件的路径,可以在nagios.cfg中定义多个资源文件

status_file=/usr/local/nagios/var/status.dat                      # 该变量用于定义一个状态文件,此文件用于保存nagios的当前状态、注释和宕机信息等。

status_update_interval=10                                                 # 该变量用于定义状态文件(即status.dat)的更新时间间隔,单位是秒,最小更新间隔是1秒。

nagios_user=nagios                                                            # 该变量指定了Nagios进程使用哪个用户运行

nagios_group=nagios                                                         # 该变量用于指定Nagios使用哪个用户组运行

check_external_commands=1                                            # 该变量用于设置是否允许nagios在web监控界面运行cgi命令。# 也就是是否允许nagios在web界面下执行重启nagios、停止主机/服务检查等操作。# “1”为运行,“0”为不允许。

command_check_interval=10s                                           # 该变量用于设置nagios对外部命令检测的时间间隔,如果指定了一个数字加一个"s"(如10s); # 那么外部检测命令的间隔是这个数值以秒为单位的时间间隔; # 如果没有用"s",那么外部检测命令的间隔是以这个数值的“时间单位”的时间间隔。

interval_length=60  # 该变量指定了nagios的时间单位,默认值是60秒,也就是1分钟; # 即在nagios配置中所有的时间单位都是分钟

timeperiods.cfg文件详解

definecontact{

name      generic-contact    ; 联系人名称

service_notification_period   24x7; 当服务出现异常时,发送通知的时间段,这个时间段"24x7"在timeperiods.cfg文件中定义。

host_notification_period   24x7 ; 当主机出现异常时,发送通知的时间段,这个时间段"24x7"在timeperiods.cfg文件中定义。

service_notification_options   w,u,c,r; 这个定义的是“通知可以被发出的情况”。w即warn,表示警告状态,u即unknown,表示不明状态。

c即criticle,表示紧急状态,r即recover,表示恢复状态也就是在服务出现警告状态、未知状态、紧急状态和重新恢复状态时都发送通知给使用者。

host_notification_options   d,u,r   ; 定义主机在什么状态下需要发送通知给使用者,d即down,表示宕机状态;

u即unreachable,表示不可到达状态,r即recovery,表示重新恢复状态。

service_notification_commands   notify-service-by-email ; 服务故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件;其中“notify-service-by-email”在commands.cfg文件中定义。

register                        0                    ; DONT REGISTER THISDEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

 } 

define host{ 

 name   generic-host   ; 主机名称,这里的主机名,并不是直接对应到真正机器的主机名; ; 乃是对应到在主机配置文件里所设定的主机名。

notifications_enabled           1               ; Host notifications are enabled 

event_handler_enabled           1               ; Host event handler is enabled 

flap_detection_enabled          1               ; Flap detection is enabled

failure_prediction_enabled      1              ; Failure prediction is enabled

process_perf_data               1               ; 其值可以为0或1,其作用为是否启用Nagios的数据输出功能;; 如果将此项赋值为1,那么Nagios就会将收集的数据写入某个文件中,以备提取。

retain_status_information       1               ; Retain status informationacross program restarts

retain_nonstatus_information    1               ; Retain non-status informationacross program restarts

notification_period             24x7            ; 指定“发送通知”的时间段,也就是可以在什么时候发送通知给使用者

register                        0               ; DONT REGISTER THIS DEFINITION- ITS NOT A REAL HOST, JUST A TEMPLATE! 

        }

 

测试:增加一台新的主机来监控

1、复制一个配置文件:cp localhost.cfg 192.168.174.16.cfg

2、然后再修改这个配置文件的内容:

把所有localhost替换成主机ip::%s/localhost/192.168.174.16/g

把所有127.0.0.1替换成主机ip::%s/127.0.0.1/192.168.174.16/g

check_local_swap,在被监控的客户端是不需要配置的! 
 chkck_local,需要全部删除,如果不删除,监控会出错,只会监控到本地主机!

把hostgroup_name  linux-servers改成你所定义的名称:linux-servers->10.0.0.186

sed –i ‘s/localhost/10.0.0.186/g;s/127.0.0.1/10.0.0.186/g;s/check_local/check/g;s/linux-servers/10.0.0.186/g’ 10.0.0.186.cfg

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

sed -i '36a cfg_file=/usr/local/nagios/etc/objects/192.168.33.10.cfg' /usr/local/nagios/etc/nagios.cfg

4、行: /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

有报错!因为没有在客户端安装nagios插件及NRPE!

5、需注销掉配置文件里disk、swap、process、user、cpu等监控配置段!

做以上替换之后,就没有了对磁盘、cpu等等的监控了!

6、在检查一下主配置文件是否有问题:

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

7、如果没有问题,重启一下服务即可:/etc/init.d/nagios restart

 

NRPE(Nagios Remote Plugin Executor)是用于在远端服务器上运行检测命令的守护进程,它用于让Nagios监控端基于安装的方式触发远端主机上的检测命令,并将检测结果输出至监控端。而其执行的开销远低于基于SSH的检测方式,而且检测过程并不需要远程主机上的系统帐号等信息,其安全性也高于SSH的检测方式。

 

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_17

测试2:正式开始监控客户端的配置

一、在服务器、客户端都安装nrpe,另需要在客户端安装nagioss-plugins

1、安装nrpe(服务器端和客户端)nrpe可以支持更多功能,如磁盘、CPU、关键字等等监控;  

tar xf nrpe-2.15.tar.gz

cd nrpe-2.15

./configure --enable-ssl --with-ssl-lib &&make all && make install-plugin && make install-daemon && make install-daemon-config

安装后的目录在:/usr/local/nagios/etc/nrpe.cfg

命令目录在:/usr/local/nagios/bin/nrpe

配置文件目录在:/usr/local/nagios/etc/nrpe.cfg

[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_nrpe

如果在客户端上有这个插件命令,就说明nrpe已经安装成功了!

注:服务器、客户端的nrpe软件版本必须保持一致!

2、安装nagios-plugins(客户端)

nagios-plugins的作用:用来监控自己的服务,然后通过nrpe传给服务器端!

1、安装nagios-plugins

useradd nagios

tar xf nagios-plugins-1.4.14.tar.gz

cd nagios-plugins-1.4.14

./configure --with-nagios-user=nagios --with-nagios-group=nagios

make && make install

3、授权nrpe(服务器端和客户端)

chown -R nagios.nagios /usr/local/nagios/

4、启动nrpe服务(客户端)

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

[root@JTWeb01 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg  -d
[root@JTWeb01 etc]# ps -ef |grep nrpe
nagios    2092     1  0 15:26 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
root      5735  3815  0 22:27 pts/0    00:00:00 grep nrpe

 

二、定义监控项目

1、nagios监控的服务器ip地址填写进来

vim /usr/local/nagios/etc/nrpe.cfg

allow_hosts=10.0.0.186

2、在做正式监控之前,需要把所监控的项目测试一下:(在客户端)

2.1、检测登陆用户数:

[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_users -w 5 -c 10

USERS OK - 3 users currently logged in |users=3;5;10;0

2.2、检测负载:

[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
OK - load average: 1.00, 1.00, 1.00|load1=1.000;15.000;30.000;0; load5=1.000;10.000;25.000;0; load15=1.000;5.000;20.000;0;

[root@JTWeb01 etc]# df -h 查看一下本地的磁盘;
Filesystem         Size  Used   Avail  Use%  Mounted on
/dev/sda3        91G   21G    65G    25%    /
tmpfs              1.9G     0   1.9G    0%     /dev/shm
/dev/sda1       194M   34M  151M  19%    /boot

2.3、检测磁盘boot分区:
[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
DISK OK - free space: /boot 150 MB (81% inode=99%);| /boot=33MB;154;173;0;193

[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2
DISK OK - free space: / 65125 MB (74% inode=97%);| /=22704MB;74024;83277;0;92530
[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda3
DISK OK - free space: / 65125 MB (74% inode=97%);| /=22704MB;74024;83277;0;92530

2.4、检测僵尸进程:

[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
PROCS OK: 0 processes with STATE = Z

2.5、检测总的进程:

[root@JTWeb01 etc]# /usr/local/nagios/libexec/check_procs -w 210 -c 250  
PROCS OK: 206 processes

2.6、检测http:

[root@Nagios objects]# /usr/local/nagios/libexec/check_http -I 10.96.0.110
HTTP OK: HTTP/1.1 200 OK - 9385 bytes in 1.152 second response time |time=1.151831s;;;0.000000 size=9385B;;;0

其中的:check_userscheck_loadcheck_sda1check_sda2、check_sda3check_zombie_procs、check_total_procs是给服务器端来调用的!

注意:这些定义过的命令名称,不能与其他客户端相同!

3、 测试服务器端与客户端nrpe通信

说明本机与nrpe已经建立通信:

[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.15

再与客户端进行通信测试:

[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 10.0.0.38
NRPE v2.15

 

三、配置监控

1、在command中定义check_nrpe

1.1、在服务器端定义nrpe,在末尾添加:

vim command.cfg

define command{

    command_name check_nrpe

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

  }

1.2、检测一下配置文件是否正确:

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

2、定义监控项目(客户端)
command[check_JTWeb01_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_JTWeb01_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_JTWeb01_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
command[check_JTWeb01_sda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2
command[check_JTWeb01_sda3]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda3
command[check_JTWeb01_zombie]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_JTWeb01_procs]=/usr/local/nagios/libexec/check_procs -w 220 -c 250

3、启动nrpe服务(客户端)

vim nrpe.cfg                                                  
[root@JTWeb01 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@JTWeb01 etc]# ps -ef |grep nrpe
nagios   12424     1  0 08:48 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
root     13156 11456  0 09:03 pts/1    00:00:00 grep nrpe
[root@JTWeb01 etc]# kill -9 12424
[root@JTWeb01 etc]# ps -ef |grep nrpe
root     13164 11456  0 09:04 pts/1    00:00:00 grep nrpe
[root@JTWeb01 etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@JTWeb01 etc]# ps -ef |grep nrpe
nagios   13169     1  0 09:04 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
root     13173 11456  0 09:04 pts/1    00:00:00 grep nrpe

4、调用配置好的检测项目

define service{
        use                                  local-service        
        host_name                       10.96.0.110
        service_description            Monitor_Discuz_sda3
        check_command               check_nrpe!Monitor_Discuz_sda3
        notifications_enabled         1

        }

 

define service{
        use                                 local-service        
        host_name                      lo10.96.0.110       

service_description                  Monitor_Discuz_HTTP
        check_command              check_http
        }

注:http的检测,不需要定义,因为command.cfg中,已经定义过:

# 'check_http' command definition
define command{
        command_name    check_http
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
        }

5、检查配置文件和启动服务(服务器端)

启动服务器端的nrpe服务

[root@Nagios ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@Nagios ~]# ps -ef |grep nrpe
nagios    3325     1  0 Oct23 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
root     57868 55117  0 11:02 pts/3    00:00:00 grep nrpe

检测nagios配置文件

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

Checking contact groups...
        Checked 1 contact groups.
Checking service escalations...
        Checked 0 service escalations.
Checking service dependencies...
        Checked 0 service dependencies.
Checking host escalations...
        Checked 0 host escalations.
Checking host dependencies...
        Checked 0 host dependencies.
Checking commands...
        Checked 27 commands.
Checking time periods...
        Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

重启nagios服务

[root@Nagios ~]# /etc/init.d/nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.

查看监控效果

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_18

 

四、企业应用

1、服务器内存:不准确,不建议监控;

2、swap:企业中,一般监控这个swap,如果连swap都满了,服务也就要停止工作了!内存就溢出了!

真的出现内存溢出了,就需要处理故障:加内存、重启服务器或服务!

一般阀值设置为:80%,

[root@Nagios ~]# free -m
             total       used       free     shared    buffers     cached
Mem:     1869        593       1276          0        134        281
-/+ buffers/cache: 177       1691
Swap:     4095          0       4095

3、负载:负载与内存、cpu、磁盘读写、都有一定的关系;一般不能超过逻辑cpu的2倍;

如果cpu利用率很高,而负载很低,一般都是有因为有队列在等待!

top - 13:47:32 up 1 day, 21:13,  1 user,  load average: 0.00, 0.05, 0.08
Tasks: 199 total,   1 running, 198 sleeping,   0 stopped,   0 zombie
Cpu0  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu1  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  :  0.0%us,  0.3%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  :  0.0%us,  0.0%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.3%si,  0.0%st
Mem:   1914560k total,   605380k used,  1309180k free,   137984k buffers
Swap:  4194296k total,        0k used,  4194296k free,   288020k cached

4、cpu:一般超过90%,就需要报警;

5、检测http

http的检测,不需要定义,因为command.cfg中,已经定义过:

define service{
        use                                 local-service        
        host_name                      lo10.96.0.110       

service_description                  Monitor_Discuz_HTTP
        check_command              check_http
        }

可直接调用:

# 'check_http' command definition
define command{
        command_name    check_http
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
        }

测试检测http:

[root@Nagios objects]# /usr/local/nagios/libexec/check_http -I 10.96.0.110
HTTP OK: HTTP/1.1 200 OK - 9385 bytes in 1.152 second response time |time=1.151831s;;;0.000000 size=9385B;;;0

http的几种错误状态:

404:找不到页面;

500:内部服务器错误;

502、504:网关超时;

503:后端服务故障,超时;

6、检测关键词

检测Discuz论坛的:url的页面、80端口服务:

[root@Nagios objects]# /usr/local/nagios/libexec/check_http -I 10.96.0.110 -u /index.php -p 80
HTTP OK: HTTP/1.1 200 OK - 9397 bytes in 0.970 second response time |time=0.969871s;;;0.000000 size=9397B;;;0

检测Discuz论坛的关键词:防止恶意篡改代码:

[root@Nagios objects]# /usr/local/nagios/libexec/check_http -I 10.96.0.110 -u /index.php -p 80 -s "论 坛"
HTTP OK: HTTP/1.1 200 OK - 9396 bytes in 0.833 second response time |time=0.832788s;;;0.000000 size=9396B;;;0

在command.cfg中定义关键词:check_http_word

参数解析:-I指定IP或者主机名,-u指定URL,-p指定端口,-s指定关键词

define command{

        command_name    check_http_word

        command_line    $USER1$/check_http -I $HOSTADDRESS$ -u$ARG1$ -p $ARG2$ -s $ARG3$

        }

在10.96.0.110.cfg中调用:

define service{
        use                                   local-service ; Name of service template to use
        host_name                         10.96.0.110
        service_description              Monitor_Discuz_论坛
        check_command                  check_http_word!/index.php!80!论坛
        notifications_enabled           1
        }

测试一下配置文件是否有错:

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

重启nagios服务:

/etc/init.d/nagios restart

查看监控效果:

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_19

关键词"论坛"不存在,则nagios在监控页面上会显示CRITICAL紧急!

7、检测数据库

如何监控MSYQL主从、主主?

在mysql服务器安装两个软件,nagios-plugins-1.4.14.tar.gz和nrpe-2.14.tar.gz

监控Mysql可以用个nagiosmysql插件,首先在command.cfg中定义如下配置:

define command{

        command_name   check_mysql_slave

        command_line  $USER1$/check_mysql -H $HOSTADDRESS$ -S-uroot -p123

 }

然后在主机配置10.96.0.121、122.cfg文件中定义监控项目:

define service{

        use                                   local-service        

        host_name                         10.96.0.121

        service_description             MYSQL_Slave_Monitor

        check_command                 check_mysql_slave

}

检查配置文件,并重启服务

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

/etc/init.d/nagios restart

测试检测:

[root@Nagios ~]# /usr/local/nagios/libexec/check_mysql -H 10.96.0.121 -S -u root -p 123
Uptime: 180569  Threads: 2  Questions: 12732  Slow queries: 0  Opens: 95  Flush tables: 1  Open tables: 62  Queries per second avg: 0.070 Slave IO: Yes Slave SQL: Yes Seconds Behind Master: 0

配置完毕后,监控界面如下图:

Linux服务篇之十:构建Nagios监控平台_Linux服务篇之十:构建Nagios监_20

8、Nagios邮件及短信报警

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

默认command.cfg里面已经配置好了邮件报警设置,可以使用默认的配置,使用系统默认的mail发送邮件;还可以自己定义发送的内容格式及发送的邮件smtp服务器端软件。

这里使用默认的配置文件,要能收到短信报警,除了在139.com界面绑定139邮箱之外,还需要在nagios服务器端配置文件修改邮件收件人如下:

修改配置文件:

vim /usr/local/nagios/etc/objects/contacts.cfg内容如下:

define contact{
        contact_name                     nagiosadmin             ; Short name of user
        use                                   generic-contact         ; Inherit default values from generic-
contact template (defined above)
        alias                                  Nagios Admin            ; Full name of user

        email                                 baishubing@suning.com.cn,13913912384@139.com

同样也可以使用sed命令修改:cd  /usr/local/nagios/etc/objects/ ;

sed  -i 's#nagios@localhost#13913912384@139.com#g' contacts.cfg

其中的generic-connact是个模板,

define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                                  generic-contact         ; Inherit default values from generic-
contact template (defined above)
        alias                                Nagios Admin            ; Full name of user

        email                               baishubing@suning.com.cn,13913912384@139.com

这个模板在templates.cfg中默认已经调用:

define contact{
      name                          generic-contact     ; The name of this contact template
      service_notification_period   24x7              ; service notifications can be sent an
ytime
      host_notification_period        24x7                    ; host notifications can be sent anytime
     service_notification_options    w,u,c,r,f,s            ; send notifications for all service s
tates, flapping events, and scheduled downtime events
     host_notification_options       d,u,r,f,s               ; send notifications for all host stat
es, flapping events, and scheduled downtime events
     service_notification_commands   notify-service-by-email ; send service notifications via email
     host_notification_commands      notify-host-by-email    ; send host notifications via email
        register          
              0  
                    

并且通过这2个命令来发邮件的告警信息:

define contact{
      name                          generic-contact     ; The name of this contact template
      service_notification_period   24x7              ; service notifications can be sent an
ytime
      host_notification_period        24x7                    ; host notifications can be sent anytime
     service_notification_options    w,u,c,r,f,s            ; send notifications for all service s
tates, flapping events, and scheduled downtime events
     host_notification_options       d,u,r,f,s               ; send notifications for all host stat
es, flapping events, and scheduled downtime events
     service_notification_commands   notify-service-by-email ; send service notifications via email
     host_notification_commands       notify-host-by-email    ; send host notifications via email
        register          
              0  

这2个命令在command.cfg中:

# 'notify-host-by-email' command definition
define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTY
PE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $L
ONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTA
CTEMAIL$
        }

# 'notify-service-by-email' command definition
define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTY
PE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDat
e/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ S
ervice Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }

可以通过mail -s的这个命令来发送来发送邮件!

linux有2个邮件服务器:sendmail、postfix

[root@Nagios objects]# mail
No mail for root

如果没有这个客户端,就安装一下mail

yum install mail -y

测试发送一个邮件:可以收到!

echo ok |mail -s "test" baishubing@suning.com.cn

还可以通过sendmail来发送邮件!

yum install sendmail* -y
[root@Nagios objects]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN     
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN     
tcp        0      0 :::80                       :::*                        LISTEN     
tcp        0      0 :::22                       :::*                        LISTEN     
tcp        0      0 ::1:25                      :::*                        LISTEN     
tcp        0      0 :::5666                     :::*                        LISTEN

[root@localhost ~]# /etc/init.d/postfix status
master (pid  1608) 正在运行...

再次测试邮件发送,可以收到!

echo ok |mail -s "test" 13913912384@139.com

还可以使用这个来查看邮件的报警信息!

tail -fn 100 /var/log/message

ARNING;notify-service-by-email;PROCS WARNING: 182 processes
Oct 25 17:52:19 localhost nagios: SERVICE NOTIFICATION: nagiosadmin;10.96.0.122;Monitor_Mysql02_procs;CRITICAL;notify-service-by-email;PROCS CRITICAL: 386 processes
Oct 25 17:53:19 localhost nagios: SERVICE NOTIFICATION: nagiosadmin;10.96.0.121;Monitor_Mysql01_procs;CRITICAL;notify-service-by-email;PROCS CRITICAL: 384 processes
Oct 25 18:27:20 localhost nagios: Auto-save of retention data completed successfully.
Oct 25 18:41:12 localhost nagios: SERVICE NOTIFICATION: nagiosadmin;10.96.0.110;Monitor_Discuz_procs;WARNING;notify-service-by-email;PROCS WARNING: 186 processes
Oct 25 18:53:12 localhost nagios: SERVICE NOTIFICATION: nagiosadmin;10.96.0.122;Monitor_Mysql02_procs;CRITICAL;notify-service-by-email;PROCS CRITICAL: 390 processes
Oct 25 18:53:22 localhost nagios: SERVICE NOTIFICATION: nagiosadmin;10.96.0.121;Monitor_Mysql01_procs;CRITICAL;notify-service-by-email;PROCS CRITICAL: 386 processes
Oct 25 19:27:20 localhost nagios: Auto-save of retention data completed successfully.

sendemail nagios