天堂1111 发表于 2018-8-1 11:22:56

关于python调用zabbix api接口的自动化实例 [结合saltstack]

#!/usr/bin/env python2.7  
#coding=utf-8
  
import json
  
import urllib2
  
#xiaorui.cc
  
url = "http://10.10.10.61/api_jsonrpc.php"
  
header = {"Content-Type": "application/json"}
  
# request json
  
data = json.dumps(
  
{
  
    "jsonrpc":"2.0",
  
    "method":"host.create",
  
    "params":{
  
      "host": "10.10.10.67","interfaces":
  
      [{"type": 1,"main": 1,"useip": 1,"ip": "10.10.10.67","dns": "","port": "10050"}],
  
      "groups": [{"groupid": "2"}],"templates": [{"templateid": "10087"}]
  
      },
  
    "auth":"dbcd2bd8abc0f0320fffab34c6d749d3",
  
    "id":1,
  
}
  
)
  
# create request object
  
request = urllib2.Request(url,data)
  
for key in header:
  
    request.add_header(key,header)
  
# get host list
  
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
  
else:
  
    response = json.loads(result.read())
  
    result.close()
  
    print 'ok'zai
页: [1]
查看完整版本: 关于python调用zabbix api接口的自动化实例 [结合saltstack]