zabbix 添加web监控始终是大问题,不能自动发现只能手动添加


写了个py脚本

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import  json
import  urllib2
import  sys
class  zabbixtools:
     def  __init__( self ):
         self .url  =  "Zabbixserver"
         self .header  =  { "Content-Type" "application/json" }
         self .authID  =  self .user_login()
     def  user_login( self ):
         data  =  json.dumps(
                 {   
                     "jsonrpc" "2.0" ,
                     "method" "user.login" ,
                     "params" : { 
                         "user" "USER" ,
                         "password" "PASSWORD"
                         },
                     "id" 0
                     })
         request  =  urllib2.Request( self .url,data)
         for  key  in  self .header:
             request.add_header(key, self .header[key])
         try :
             result  =  urllib2.urlopen(request)
         except  URLError as e:
             print  "Auth Failed, Please Check Your Name And Password:" ,e.code
         else :
             response  =  json.loads(result.read())
             result.close()
             authID  =  response[ 'result' ]
             return  authID
     def  get_data( self ,data,hostip = ""):
         request  =  urllib2.Request( self .url,data)
         for  key  in  self .header:
             request.add_header(key, self .header[key])
         try :
             result  =  urllib2.urlopen(request)
         except  URLError as e:
             if  hasattr (e,  'reason' ):
                 print  'We failed to reach a server.'
                 print  'Reason: ' , e.reason
             elif  hasattr (e,  'code' ):
                 print  'The server could not fulfill the request.'
                 print  'Error code: ' , e.code
             return  0
         else :
             response  =  json.loads(result.read())
             result.close()
             return  response
     def  host_create( self ):
         data  =  json.dumps(
                     {
                         "jsonrpc" "2.0" ,
                         "method" "httptest.create" ,
                         "params" : {
                             "hostid" "10788" ,
                             "name" "Homepage" ,
                             "steps" : [
                                 {
                                     "name" "1" ,
                                     "url" "http://mycompany.com" ,
                                     "no" 1
                                 }
                             ]
                     },
                         "auth" self .authID,
                         "id" 1 ,
                         })
         res  =  self .get_data(data)
         print  res
 
def  main():
     test  =  zabbixtools()
     test.host_create()
if  __name__  = =  "__main__" :
     main()

本文转自 superbigsea 51CTO博客,原文链接:http://blog.51cto.com/superbigsea/1745399