nagios自定义监控API插件


  监控思路:通过url访问某一接口文件的具体返回内容,正则匹配某一接口存活必定含有的字符,若有则证明接口存活,若无则接口有问题。

  废话不多说,上插件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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)

返回结果:

1
2
[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配置文件,添加

1
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文件,添加

1
2
3
4
define  command {
         command_name check_api
         command_line $USER1$ /check_api  -u $ARG1$ -S $ARG2$
         }

修改监控主机/usr/local/nagios/etc/objects/services.cfg文件,添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
##############################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,查看监控结果

wKiom1g1YHHQfSKQAAAngyVqCUQ707.png





      本文转自YU文武貝 51CTO博客,原文链接:http://blog.51cto.com/linuxerxy/1875893,如需转载请自行联系原作者




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值