hhnf333 发表于 2019-1-12 14:22:28

nagios自定义监控API插件

  
监控思路:通过url访问某一接口文件的具体返回内容,正则匹配某一接口存活必定含有的字符,若有则证明接口存活,若无则接口有问题。
  废话不多说,上插件:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import urllib
import sys
import getopt
def usage():
    print """Usage: check_api [-h|--help] [-u|--url url] [-S|--Str String]
"
Url: the url that you want to check;
String: the string that you want to match;
"
For example,#/usr/local/nagios/libexec/check_api -u 'http://xxx.com/ch1.ashx?jsoncallback=jQuery111104933076921983046_1479864376576&classid=12&systemtype=2&dataType=json&cityid=129&videoid=36065812&pageid=1&_=1479864376577' -S AdList
"""
    sys.exit(3)
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html
def getString(Str, html):
    matchObj = re.search(Str, html, re.M)
    if matchObj:
      print("GET API OK:get %s successfully!" % Str)
      sys.exit(0)
    elif not matchObj:
      print("GET API CRITICAL:the API was error!")
      sys.exit(2)
    else:
      print("GET API UNKNOWN:the API was unknow!")
      sys.exit(3)
try:
    options, args = getopt.getopt(sys.argv,
      "hu:S:",
      "--help --url= --Str=",
      )
except getopt.GetoptError:
    usage()
    sys.exit(3)
for name, value in options:
    if name in ("-h", "--help"):
      usage()
    if name in ("-u", "--url"):
      url = value
    if name in ("-S", "--Str"):
      Str = value
html = getHtml(url)
getString(Str, html)  返回结果:
# /usr/local/nagios/libexec/check_api -u 'http://xxx.com/ch1.ashx?jsoncallback=jQuery111104933076921983046_1479864376576&classid=12&systemtype=2&dataType=json&cityid=129&videoid=36065812&pageid=1&_=1479864376577' -S AdList
GET API OK:get AdList successfully!  一、上述插件在客户机上部署,将此插件放入/usr/local/nagios/libexec/赋予可执行权限和属主组,修改/usr/local/nagios/etc/nrpe.cfg配置文件,添加
command=/usr/local/nagios/libexec/check_api -u 'http://xxx.com/ch1.ashx?jsoncallback=jQuery111104933076921983046_1479864376576&classid=12&systemtype=2&dataType=json&cityid=129&videoid=36065812&pageid=1&_=1479864376577' -S AdList  重启nagios客户端。
  二、修改监控主机/usr/local/nagios/etc/objects/commands.cfg文件,添加
define command{
      command_name check_api
      command_line $USER1$/check_api -u $ARG1$ -S $ARG2$
      }  修改监控主机/usr/local/nagios/etc/objects/services.cfg文件,添加
##############################M-WEB-065 check_api#####################################
define service{
      host_name               M-WEB-065
      service_description   check_api
      check_command         check_nrpe!check_api
      max_check_attempts      5
      normal_check_interval   5
      retry_check_interval    2
      check_period            24x7
      notification_interval   10
      notification_period   24x7
      notification_options    u,c,r
      contact_groups          yunwei
}  最后重启nagios,查看监控结果

http://s4.运维网.com/wyfs02/M02/8A/A0/wKiom1g1YHHQfSKQAAAngyVqCUQ707.png



页: [1]
查看完整版本: nagios自定义监控API插件