万达换票券 发表于 2019-1-15 08:06:59

nagios监控http页面

  监控端nagios
  vim /usr/local/nagios/etc/objects/services.cfg (添加)
  define service{
  use                     local-service
  host_name            mysqlhost
  service_groups          mysqlgroup
  service_description   check_http
  check_command         check_nrpe!check_http
  max_check_attempts      2
  normal_check_interval   3
  retry_check_interval    2
  check_period            24x7
  notification_interval   5
  notification_period   24x7
  notification_options    w,u,c,r
  contact_groups          admins
  }
  被监测端
  vim /usr/local/nagios/etc/nrpe.cfg
  command=/usr/local/nagios/libexec/check_http -H 172.16.1.171(本机ip) -u /index.html
  重启服务
  监测端
  # /usr/local/nagios/libexec/check_http-H 172.16.1.171 -u /index.html
  HTTP OK: HTTP/1.1 200 OK - 282 bytes in 0.002 second response time |time=0.002139s;;;0.000000 size=282B;;;0
  表示正常
  一些check_http用法
  用nagios的插件check_http可以方便监控各种web页面,当有故障或者异常时,第一时间发出告警,从而快速地发现问题,解决故障。最终提高网站的可用性。
  check_http –help
  Usage: check_http -H| -I[-u ] [-p ]
  [-w ] [-c ] [-t ] [-L]
  [-a auth] [-f ]
  [-e ] [-s string] [-l] [-r| -R ]
  [-P string] [-m :] [-4|-6] [-N] [-M ]
  [-A string] [-k string] [-S] [-C ] [-T ] [-j method]
  其中我在工作中常用的参数如下:
  -H, –hostname=ADDRESS:域名或者被监控机IP(监控主机要有DNS)
  -I, –IP-address=ADDRESS:被监控机 IP (当监控主机没有DNS时用)
  -p, –port=INTEGER:http服务端口,默认为80
  -e, –expect=STRING
  Comma-delimited list of strings, at least one of them is expected in the first (status) line of the server response (default: HTTP/1.) If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)
  -u, –url=PATH:URL to GET or POST (default: /)
  -s, –string=STRING:String to expect in the content
  举例:
  1.监控192.168.9.4机器http服务使用情况
  /usr/lib64/nagios/plugins/check_http -I 192.168.9.4
  HTTP OK: HTTP/1.1 302 Found – 619 bytes in 0.009 second response time |time=0.008873s;;;0.000000 size=619B;;;0
  2.监控192.168.120.2机器http9090端口服务的使用情况
  /usr/lib64/nagios/plugins/check_http -I 192.168.120.2 -p 9090
  HTTP WARNING: HTTP/1.1 403 Forbidden
  3.监控192.168.120.2机器http9090端口服务的使用情况,当http返回200,403时,nagios返回OK
  /usr/lib64/nagios/plugins/check_http -I 192.168.120.2 -p 9090 -e 403,200
  HTTP OK HTTP/1.1 403 Forbidden – 389 bytes in 0.004 seconds |time=0.003794s;;;0.000000 size=389B;;;0
  4.监测www.twitter.com能否访问;
  /usr/lib64/nagios/plugins/check_http -H www.twitter.com
  CRITICAL – Socket timeout after 10 seconds
  5.监测http://123.112.137.221:10009/nagios/能否访问;
  /usr/lib64/nagios/plugins/check_http -I 123.112.137.221 -p 10009 -u /nagios/
  HTTP WARNING: HTTP/1.1 401 Authorization
  6.检测http://www.abc.com/search.aspx?name=abc&id=100能否访问
  /usr/lib64/nagios/plugins/check_http -H www.abc.com -u “/search.aspx?name=abc&id=100″
  HTTP OK: HTTP/1.1 301 Moved Permanently – 486 bytes in 0.567 second response time |time=0.567176s;;;0.000000 size=486B;;;0
  ————————————————————————————————-
  自己写监控插件
  监控http页面也可以自己写监控插件实现。如监控http://www.ip138.com/ips.asp?ip=218.204.252.127 的情况。可以通过curl命令下载网页,再根据http返回情况写脚本来实现监控。下面是实现方式:
  1.监控http返回状态
  curl -o /dev/null -s -w %{http_code} http://www.ip138.com/ips.asp?ip=218.204.252.127
  -o 把下载的所有内容都重定向到/dev/null,-s命令,屏蔽了curl本身的输出,而-w参数,是根据我们自己的需要,自定义了curl的输出格式。这里输出http的返回状态码。
  2.监控web站点的响应时间
  curl -o /dev/null -s -w %{time_connect}:%{time_starttransfer}:%{time_total} http://www.ip138.com/ips.asp?ip=218.204.252.127
  其中:
  time_connect表示建立到服务器的 TCP 连接所用的时间
  time_starttransfer表示在发出请求之后,Web 服务器返回数据的第一个字节所用的时间
  time_total表示完成请求所用的时间
  3.check_dthttp脚本
  cat check_dthttp
  #!/bin/bash
  # Using to check dongtai http.
  # Made by yunhaozou@gmail.com, 2010/11/21.
  status=”OK: “
  status=”WARNING: “
  status=”CRITICAL: “
  status=”UNKNOWN: “
  if [ $# -lt 2 ]
  then
  echo “Usage:$0 -u url”
  exit 777
  fi
  httpcode=`curl -o /dev/null -s -w %{http_code} $2`
  if [ $httpcode -eq "200" ]
  then
  status=0
  echo “OK – http $httpcode.”
  exit 0
  else
  status=2
  echo “Critical – http $httpcode.”
  exit 2
  fi
  4.监控
  通过nrpe的方式实现:
  cat /etc/nagios/nrpe.cfg
  command=/usr/lib64/nagios/plugins/check_dthttp -u http://www.ip138.com/ips.asp?ip=218.204.252.127
  ——————————————————————————————————————————
  HTTP协议返回状态码表
  1**:请求收到,继续处理
  2**:操作成功收到,分析、接受
  3**:完成此请求必须进一步处理
  4**:请求包含一个错误语法或不能完成
  5**:服务器执行一个完全有效请求失败
  ————-
  100——客户必须继续发出请求
  101——客户要求服务器根据请求转换HTTP协议版本
  200——交易成功
  201——提示知道新文件的URL
  202——接受和处理、但处理未完成
  203——返回信息不确定或不完整
  204——请求收到,但返回信息为空
  205——服务器完成了请求,用户代理必须复位当前已经浏览过的文件
  206——服务器已经完成了部分用户的GET请求
  300——请求的资源可在多处得到
  301——删除请求数据
  302——在其他地址发现了请求数据
  303——建议客户访问其他URL或访问方式
  304——客户端已经执行了GET,但文件未变化
  305——请求的资源必须从服务器指定的地址得到
  306——前一版本HTTP中使用的代码,现行版本中不再使用
  307——申明请求的资源临时性删除
  ————–
  400——错误请求,如语法错误
  401——请求授权失败
  402——保留有效ChargeTo头响应
  403——请求不允许
  404——没有发现文件、查询或URl
  405——用户在Request-Line字段定义的方法不允许
  406——根据用户发送的Accept拖,请求资源不可访问
  407——类似401,用户必须首先在代理服务器上得到授权
  408——客户端没有在用户指定的饿时间内完成请求
  409——对当前资源状态,请求不能完成
  410——服务器上不再有此资源且无进一步的参考地址
  411——服务器拒绝用户定义的Content-Length属性请求
  412——一个或多个请求头字段在当前请求中错误
  413——请求的资源大于服务器允许的大小
  414——请求的资源URL长于服务器允许的长度
  415——请求资源不支持请求项目格式
  416——请求中包含Range请求头字段,在当前请求资源范围内没有range指示值,请求
  也不包含If-Range请求头字段
  417——服务器不满足请求Expect头字段指定的期望值,如果是代理服务器,可能是下
  一级服务器不能满足请求
  —————
  500——服务器产生内部错误
  501——服务器不支持请求的函数
  502——服务器暂时不可用,有时是为了防止发生系统过载
  503——服务器过载或暂停维修
  504——关口过载,服务器使用另一个关口或服务来响应用户,等待时间设定值较长
  505——服务器不支持或拒绝支请求头中指定的HTTP版本



页: [1]
查看完整版本: nagios监控http页面