zabbix-api
作者:84067176
api接口
1、api:所有的api接口就是一个网址,把某些功能封装好,方便别人调用。
2、api接口请求方式,return key(令牌口令).
3、html识别json格式.
api请求例子:
1、官网看user.login例子。
curl -i -X POST -H 'Content-Type:application/json' -d'
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
}' http://192.168.0.122/api_jsonrpc.php
返回json:
{"jsonrpc":"2.0","result":"67c2e0f01cf99e8385917f931deb2178","id":1}
2、使用zabbix的api创建用户。
{
"jsonrpc": "2.0",
"method": "user.create",
"params": {
"alias": "haha",
"passwd": "haha",
"usrgrps": [
{
"usrgrpid": "1"
}
],
"user_medias": [
{
"mediatypeid": "1",
"sendto": "123530861@qq.com",
"active": 0,
"severity": 63,
"period": "1-7,00:00-24:00"
}
]
},
"auth": "67c2e0f01cf99e8385917f931deb2178",
"id": 2
}
照着下图,修改类型的ID.
3、通过zabbix的api来修改和数据库里面服务名一致的监控主机名(自动化)
#-*- coding:utf-8 -*-
import urllib2,json,sys
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
url='http://192.168.0.122/api_jsonrpc.php'
cmdbip ='192.168.0.122'
cmdbport = 3306
cmdbname = 'ly'
cmdbpasswd = '123456'
cmdbuser ='root'
DB_URL = 'mysql+pymysql://{}:{}@{}:{}/{}'.format(cmdbuser,cmdbpasswd,cmdbip,cmdbport,cmdbname)
engine = create_engine(DB_URL,echo=False)
Session = sessionmaker(engine)
session = Session()
#从数据库获取所有的IP
def get_allip():
sql = 'select distinct av_vmip from gsys_admin_vm;'
res = session.execute(sql).fetchall()
return res
#从数据库获取所有IP的服务名称
def get_hostname(ip):
#print ip
sql = "select ar_role from gsys_admin_role where ar_vmid=(select sysid from gsys_admin_vm where av_vmip='%s');" % (
ip)
# sql="select sysid from gsys_admin_vm where av_vmip='%s';" % (ip)
ress = session.execute(sql).fetchall()
res = ''
for i in ress:
res += i[0]
return res
#写请求zpi功能
def requestJson(url,values):
data = json.dumps(values)
# 如果直接用urlopen方法,有些网站禁止爬虫,所以会报错,如果直接请求会出现以下错误:urllib2.HTTPError: HTTP Error 403: Forbidden
# 解决方法伪装成一个浏览器头,然后模拟浏览请求
req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'})
response = urllib2.urlopen(req, data)
output = json.loads(response.read())
# print output
try:
message = output['result']
except:
message = output['error']['data']
print message
quit()
return output['result']
#获取api口令函数
def authenticate(url):
values = {'jsonrpc': '2.0',
'method': 'user.login',
'params': {
'user': 'Admin',
'password': 'zabbix'
},
'id': '1'
}
idvalue = requestJson(url,values)
return idvalue
auth = authenticate(url)
#通过zpi来获取监控主机的IP地址
def getallip(url,auth):
values = {'jsonrpc': '2.0',
'method': 'host.get',
'params': {
'output': ["host"],
"selectInterfaces": ["ip"]
},
'auth': auth,
'id': '3'
}
output = requestJson(url, values)
return output
#通过zpi来获取监控主机的hostsid
def ipgetHostsid(ip,url,auth):
values = {'jsonrpc': '2.0',
'method': 'host.get',
'params': {
'output': [ "host" ],
'filter': {
'ip': ip
}
},
'auth': auth,
'id': '3'
}
output = requestJson(url,values)
return output
#通过api接口更新主机的hsotname
def updatehostname(hostid,hostname,url,auth):
values = {'jsonrpc': '2.0',
'method': 'host.update',
'params': {
"hostid": hostid,
"name": hostname,
},
'auth': auth,
'id': '4'
}
output = requestJson(url, values)
return output
allip = getallip(url,auth)
for getip in allip:
#print getip
ip = getip['interfaces'][0]['ip']
hostgetid = ipgetHostsid(ip, url, auth)
#print hostgetid
if hostgetid:
hostname = get_hostname(ip)
#print hostname
for i in hostgetid:
hostid = i['hostid']
print '---------->'
print hostid
updatehostname(hostid, hostname, url, auth)
验证结果: