`
zhengdl126
  • 浏览: 2509690 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

nagios自定义监控nginx php和ip_conn

 
阅读更多


自定义ip_conn.sh :http://zhumeng8337797.blog.163.com/blog/static/100768914201171664928247/

用php自定义 nagios监控插件
http://blog.csdn.net/sudoers/article/details/6913368

Writing a Nagios plugin with PHP
http://benedmunds.com/2012/04/25/writing-a-nagios-plugin-with-php/

 

 

/etc/init.d/nagios stop
/etc/init.d/nagios start


============== 1 增加nginx和php的监控


-----------1.1 主

 

基本配置 /usr/local/nagios/etc/nagios.cfg

cfg_file=/usr/local/nagios/etc/objects/commands.cfg  图3定义commands
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg     联系人 图4
cfg_file=/usr/local/nagios/etc/objects/templates.cfg  模板 图5
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg          localhost.cfg中有类似的主机组设置
cfg_file=/usr/local/nagios/etc/objects/services.cfg


#vim /usr/local/nagios/etc/objects/commands.cfg

#check_php_test
define command{
        command_name check_php_test  
        command_line $USER1$/check_php_test.sh -H $HOSTADDRESS$ -c $ARG1$ -t 30 # -t 30   # 加上一个 -t 30 指定限定时间为 30 秒
}


#vim /usr/local/nagios/etc/objects/services.cfg

define service{
        use   generic-service
        host_name               R620_web67,R620_web68
        service_description check_php_test
        contact_groups        admins-ts
       check_command check_nrpe!check_php_test
       notifications_enabled 1
        normal_check_interval 3
        retry_check_interval  2
        notification_interval 10
        notification_period   24x7
        notification_options  w,u,c,r
}


 編輯完主Nagios後可以用這一行检查配置
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

 

检查配置:
#/etc/init.d/nagios checkconfig
reload服务:
#/etc/init.d/nagios reload


-----------1.2 从:

 


客户机基本配置/usr/local/nrpe/etc/nrpe.cfg
或者 /usr/local/nagios/etc/nrpe.cfg


#vim /var/www/t.php
1

#vim /usr/local/nagios/libexec/check_php_test.sh
#!/bin/bash

#!/bin/bash
r=`/usr/bin/curl -s http://localhost/t.php`
if [ $r ] ;then
    if [ $r == "1" ] ;then
        echo "ok"
        exit 0
    else
        echo "Please confirm the content of the file(t.php)!"
        exit 1
    fi
fi
echo "Service exception,May be the file(t.php) does not exist!"
exit 2

 

 

----------------------------------------- nagios检测并重启服务完整脚本 check_php_test.sh

 

 

#!/bin/bash

#nagios默认state
nagstate=0
#nagios默认描述
nagrs="ok"


#检测服务
function checkService () {
    r=`/usr/bin/curl -s http://localhost/t.php`
echo "curl content:$r"
    if [ $r ] ;then
        if [ $r -eq 1 ] ;then
             nagstate=0
             nagrs="OK"
             return 1
        else
             nagstate=1
             nagrs="Please confirm the content of the file(t.php)!"
             return 1
        fi
    fi
    nagstate=2
    nagrs="Service exception,attempts to restart services,please pay attention to the following message!"
    return 1
}


#重新拉起ngin, php服务
function restartService () {
    #pid /dev/shm/pid/nginx.pid;
    nginxPath="/usr/local/nginx/conf/nginx.conf"
    #pid = /dev/shm/pid/php-fpm.pid
    phpPath="/usr/local/php/etc/php-fpm.conf"
  `sudo chmod +x $nginxPath`
  `sudo chmod +x $phpPath`
    nginxPid=`cat $nginxPath | grep "pid" | tr -d ";" |awk 'BEGIN {FS=" "}{print $2}' | awk '{gsub(/ /,"")}1'`
    phpPid=`cat $phpPath | grep "pid" | awk 'BEGIN {FS="="}{print $2}' | awk '{gsub(/ /,"")}1'`

    if [ -f $nginxPid ] ;then
      echo "restart nginx"
      sudo /bin/kill -HUP `sudo cat $nginxPid`
    else
      echo "start nginx"
      `sudo chmod +x /usr/local/nginx/sbin/nginx`
        `sudo /usr/local/nginx/sbin/nginx`
    fi

    if [ -f $phpPid ] ;then
     echo "retart php"
      sudo /bin/kill -USR2 `sudo cat $phpPid`
    else
     echo "starg php-fpm"
     `sudo chmod +x /usr/local/php/sbin/php-fpm`
     `sudo /usr/local/php/sbin/php-fpm`
    fi
    return 1
}

#创建t.php文件
function toucthTphp () {
    wwwTphp="/var/www/ssc/www/t.php"
    htmlTphp="/var/www/html/t.php"
    [ -d "/var/www/ssc/www" ] && `sudo chmod -R 777 /var/www/ssc/www/`;echo 1 > $wwwTphp
    [ -d "/var/www/html" ] && `sudo chmod -R 777 /var/www/html/`;echo 1 > $htmlTphp
    return 1
}


### 主程序###
function main () {
    toucthTphp
    checkService
    [ $nagstate -ne 0 ] && restartService
    checkService
    return 1
}

main
echo $nagrs
exit $nagstate

------------------------ nagios的sudo需要手动输入密码问题
su - nagios
sudo /etc/init.d/sshd restart
需要输入密码

修改:
su -
#visudo   (或者sudo vi /etc/sudoers

#添加nagios 请求sudo,允许特定指令时(可跟参数),不需要密码
nagios ALL=(ALL)       NOPASSWD: /etc/init.d/sshd restart

#找到 #Defaults  requiretty 并取消注释,另外新增一行。表示nagios用户不需要登陆终端就可以调用命令

Defaults  requiretty  

Defaults:nagios   !requiretty

 


su - nagios
sudo /etc/init.d/sshd restart
就不需要输入密码了

 

 

---------------------------------------END 检测并重启服务脚本

 

 

 

 

 

 

 


nrpe.cfg定义command: check_php_test  (如果缺少此步骤,在服务器端执行会提示NRPE: Command 'check_php_test' not defined)
#vim /usr/local/nagios/etc/nrpe.cfg
command[check_php_test]=/usr/local/nagios/libexec/check_php_test.sh

重启nrpe:
#/etc/init.d/xinetd restart


报错:NRPE: Unable to read output (页面上nagios的文字描述就是通过echo获取到的)
 客户端脚本要有输出内容
 echo "ok"
 exit 0



****修改sh文件不需要重启任何服务




-----------1.3 主: 配置邮件联系人
#vim /usr/local/nagios/etc/objects/contacts.cfg

define contactgroup {
        contactgroup_name       admins-ts
        alias                   Nagios Administrators
        members                 it-ts
        }

define contact {
        contact_name                    it-ts                   ; Short name of user
        use                             generic-contact         ; Inherit default values from generic-contact template (defined abov
e)
        alias                           sy.zed               ; Full name of user
      
         email                          idc.it@test.com   ;#最终的邮件组
                pager                           1
       }

 

 

host_name设置

#vim /usr/local/nagios/etc/objects/localhost.cfg

define host {
        host_name             R620_web67
        alias                 Linux R620_web67
        address               192.168.102.67
        contact_groups        admins-ts
        check_command         check-host-alive
        notifications_enabled 1
        process_perf_data     1
        max_check_attempts    5
        notification_interval 10
        notification_period   24x7
        notification_options  d,u,r
        action_url      /nagios/pnp/index.php?host=$HOSTNAME$
        parents         TW_TS
        }


----------- 1.4验证
 
在监控机上运行check_nrpe -H IP
可以查看到客户端的nrpe信息,说明监控机与被监控机的nrpedaemon通信是正常。



在服务器端:验证
/usr/local/nagios/libexec/check_nrpe -H 192.168.102.67 -c check_php_test

 

 

nagios页面查看

Hostgroup Overview -> R620_web67 -> check-php-test

 


----------其他:php方式简述

#check_php_test
define command{
        command_name check_php_test
        command_line php $USER1$/check_php_test.php -H $HOSTADDRESS$ -c $ARG1$ -t 60
}


#vim /usr/local/nagios/libexec/check_php_test.php
<?php
  error_reporting(0);
  #fwrite(STDOUT, 'This check passed');
  try{
    $r = url_get_contents("http://localhost/t.php");
    if ( false === $r || '1' != $r) {
      exit(2);
    }
  } catch(Exception $e) {
      exit(3);
  }
  exit(0);

  function url_get_contents($strUrl, $boolUseCookie=false)
  {
    $ch = curl_init($strUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    if ($boolUseCookie && is_array($_COOKIE) && count($_COOKIE) > 0) {
      $cookie_str = '';
      foreach($_COOKIE as $key => $value) {
        $cookie_str .= "$key=$value; ";
      }
      curl_setopt($ch, CURLOPT_COOKIE, $cookie_str);
    }
    $response = curl_exec($ch);
    if (curl_errno($ch) != 0) {
      return false;
    }
    curl_close($ch);
    return $response;
}





==============2 ip_conn.sh


客户端/usr/local/nrpe/etc/nrpe.cfg
或者 /usr/local/nagios/etc/nrpe.cfg

 command[check_ips]=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000 


vim /usr/local/nrpe/libexec/ip_conn.sh
    #!/bin/sh 
    #if [ $# -ne 2 ] 
    #       echo "Usage:$0 -w num1 -c num2"
    #exit 3 
    #fi 
    ip_conns=`nestat -an | grep tcp |grep EST |wc -l` 
    if [ $ip_conns -lt $1 ] 
            then
          echo "OK -connect counts is $ip_conns"
           exit 0 
    fi 
    if [ $ip_conns -gt $1 -a $ip_conns -lt $2 ] 
            then
            echo "Warning -connect counts is $ip_conns"
           exit 1 

    fi 
    if [ $ip_conns -gt $2 ] 
            then
            echo "Critical -connect counts is $ip_conns"
            exit 2 
    fi 
   #指定2个连接数,当连接数大于指定的第一个数的时候,给予warning警报,当连接数大于指定的第二个数的时候,给予Critical警报。


各客户端测试nrpe服务中各个插件的功能

[root@nagios-server libexec]# /usr/local/nrpe/libexec/ip_conn.sh 8000 10000
/usr/local/nrpe/libexec/ip_conn.sh: line 6: nestat: command not found
OK -connect counts is 0


服务端测试check_nrpe功能
[root@nagios-server libexec]# ./check_nrpe -H nagios-server -c check_ips
OK -connect counts is 0



各机器以独立守护进程启动nrpe服务
/usr/local/nrpe/bin/nrpe -c /usr/local/nrpe/etc/nrpe.cfg -d
 
服务器端启动nagios监控服务
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
 
启动apache服务器
/usr/local/apache/bin/apachectl -k start

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics