---
title: "Nagios 远程监控 (Linux)"
author: "xiaolin0199"
date: "2014-11-21T15:54:43Z"
tags: []
categories: ["互联网"]
slug: "nagios-linux"
draft: false
lastmod: "2014-11-21T13:37:18Z"
id: 2158579
url: "https://xiaolin0199.iteye.com/blog/2158579"
---

1\.  监控机 ( 10.10.10.145 ubuntu 14.04 已配置好 nagios )

2\.  被监控机 ( 10.10.10.146 ubuntu server 14.04  64bit )

 

**被监控端 ( 146 )**

 

写道

1\. 安装 nagios-plugins

sudo apt-get install nagios-plugins

2\. 安装 nagios-nrpe-server

sudo apt-get install nagios-nrpe-server

写道

3\. 修改 /etc/nagios/nrpe.cfg  二处修改

3.1  
找到

allowed\_hosts=127.0.0.1

修改为

allowed\_hosts=127.0.0.1,10.10.10.145       #10.10.10.145 即为控制端IP

3.2  dont\_blame\_nrpe=0 修改为 dont\_blame\_nrpe=1  

3.3   
然后重启服务

sudo /etc/init.d/nagios-nrpe-server restart

 

**监控端 (145)**

写道

1\. 安装 nagios-nrpe-plugin

sudo apt-get install nagios-nrpe-plugin

写道

2\. 测试是否能连上

xiaofei@xiaofei-O-E-M:/usr/lib/nagios/plugins$ ./check\_nrpe -H 10.10.10.146  
NRPE v2.15

 

 -----------------     安静的美男子分割线  ---------------------

 

正常情况下， 在  /etc/nagios-plugin/config 下已存在 check\_nrpe.cfg文件 ， 该文件对 check\_nrpe 命令进行了定义，如果没有，可以按以下方式进行添加 

 

**添加 check\_nrpe 命令**

 

写道

sudo vim /etc/nagios3/commands.cfg

\## check\_nrpe  
define command{  
        command\_name      check\_nrpe  
        command\_name      $USER1$/check\_nrpe -H $HOSTADDRESS$ -c $ARG1$  
}

 

好了，让我们来试试怎么对 146 机器 进行一些监控吧

 

写道

cd /etc/nagios3/conf.d/

sudo vim u146.cfg

添加以下代码

\# Total Processes  
define service{  
    use                             generic-service  
    host\_name                 10.10.10.146  
    service\_description    Total Processes  
    check\_command        check\_nrpe\_1arg!check\_total\_procs  
}

\# Zombie Processes  
define service{  
    use                              generic-service  
    host\_name                  10.10.10.146  
    service\_description     Zombie Processes  
    check\_command         check\_nrpe\_1arg!check\_zombie\_procs  
}

\# Disk  
define service{  
    use                            generic-service  
    host\_name                10.10.10.146  
    service\_description   Disk vda1  
    check\_command       check\_nrpe\_1arg!check\_vda1  
}

\# Current Load  
define service{  
    use                             generic-service  
    host\_name                 10.10.10.146  
    service\_description    Current Load  
    check\_command        check\_nrpe\_1arg!check\_load  
}

 

注意上面的命令  check\_load , check\_vda1 , check\_total\_procs  ..... 它们在哪里定义了的呢...

 

我们去到被控端  146 这台机器    

写道

cd /etc/nagios/

vim nrpe.cfg

\# Note: Any plugins that are used in the command lines must reside  
\# on the machine that this daemon is running on! The examples below  
\# assume that you have plugins installed in a /usr/local/nagios/libexec  
\# directory. Also note that you will have to modify the definitions below  
\# to match the argument format the plugins expect. Remember, these are  
\# examples only!

\# The following examples use hardcoded command arguments...

command\[check\_users]=/usr/lib/nagios/plugins/check\_users -w 5 -c 10  
command\[check\_load]=/usr/lib/nagios/plugins/check\_load -w 15,10,5 -c 30,25,20  
command\[check\_hda1]=/usr/lib/nagios/plugins/check\_disk -w 20% -c 10% -p /dev/hda1  
command\[check\_vda1]=/usr/lib/nagios/plugins/check\_disk -w 20% -c 10% -p /dev/vda1  
command\[check\_zombie\_procs]=/usr/lib/nagios/plugins/check\_procs -w 5 -c 10 -s Z  
command\[check\_total\_procs]=/usr/lib/nagios/plugins/check\_procs -w 150 -c 200

 

   
![](http://dl2.iteye.com/upload/attachment/0103/4360/d2c0fdea-fbf5-30bc-b39f-35fe1bb2b741.png)  
 

 \**   check\_mysql 可以监控mysql状态  ,  check\_tcp , check\_udp可以监控端口状态 \** 就不一一举例了

 

-------------   再次的华丽的美男子分割线   ----------------------------

 

nagios-plugin 自带了很多的服务，但我们发现，有些关键服务并没有提供，比如cpu使用情况 ，内存使用情况 ， memcached缓存使用情况 等 等 ，这时候就需要我们自己来创建 了

 

 http://exchange.nagios.org/directory/Plugins  这里有各种各样的监控插件，选择合适的

 

放到被监控机的 /usr/lib/nagios/plugins下即可，记得要修改一下文件权限...

 

然后回到 /etc/nagios/nrpe.cfg中，添加 \[command] 命令即可....

 

如:

command\[check\_mem]=/usr/lib/nagios/plugins/check\_mem.pl -w 10 -c 5 -f -U

command\[check\_memcached]=/usr/lib/nagios/plugins/check\_memcached.py -H localhost -p 11211