监控思路:通过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[1:], "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)
返回结果:
[root@M-WEB-065 ~]# /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[check_api]=/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,查看监控结果
本文出自 “echo xiayun” 博客,谢绝转载!
原文:http://linuxerxy.blog.51cto.com/10707334/1875893