shuijingping 发表于 2018-12-15 14:38:39

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_interval2
  notification_interval 10
  notification_period   24x7
  notification_optionsw,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>  -----------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
  #找到 #Defaultsrequiretty 并取消注释,另外新增一行。表示nagios用户不需要登陆终端就可以调用命令 。
  Defaultsrequiretty
  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=/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_optionsd,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
  
页: [1]
查看完整版本: nagios自定义监控nginx php和ip_conn