1-zabbix API
根据用户名密码获取token
curl -i -X POST -H 'Content-Type:application/json' -d '{"jsonrpc": "2.0","method":"user.login","params":{"user":"readonly_api_user","password":"mKexI536MnHfM8nQ"},"id":1}' http://10.92.128.165:8080/api_jsonrpc.php
备注
1.token保存在zabbix的session表中,每次执行都会生成一个新的token,除非指定session调用user.logout使session失效(status=1)。
1.根据主机名获取hostid
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "host.get", "params": {"output":["hostid"],"filter":{"host":["zhongwen_rds"]}}, "auth": "xxxxxxxxx", "id":0}' http://10.92.128.165:8080/api_jsonrpc.php
1.根据IP获取hostid
search是包含,filter是精确过滤
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
def get_hostid(ip):
url = "http://10.x.x.x/api_jsonrpc.php"
payload = {
"jsonrpc": "2.0",
"method": "hostinterface.get",
"params": {
"filter": {
"ip": ip,
"port": "10050"
}
},
"auth": "xxxxx",
"id": 0
}
headers = {
'Content-Type': 'application/json',
'Cookie': 'PHPSESSID=d775118d60c64bba8ee089b082fd297d'
}
response = requests.request("GET", url, headers=headers, data=json.dumps(payload))
response = json.loads(response.text)
if len(response['result']) == 1:
hostid = response['result'][0]['hostid']
else:
print('ip {} response not only'.format(ip))
return hostid
ip_list = [
'172.16.39.132',
]
hosts = []
for ip in ip_list:
tmp = {}
hostid = get_hostid(ip)
tmp["hostid"] = hostid
hosts.append(tmp)
print(hosts)
2.查询指定item的数据,如下例子查看的是:system.cpu.util[,idle]
curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0", "method": "item.get", "params": {"output": ["key_", "lastclock", "lastvalue"],"hostids": [10659],"search":{"key_":"system.cpu.util[,idle]"}}, "auth": "xxxxxxxxx", "id":0}' http://10.x.x.x/api_jsonrpc.php
3.查询一个主机关联的模板id
curl --location --request GET 'http://10.x.x.x/api_jsonrpc.php' \
--header 'Content-Type: application/json' \
--header 'Cookie: PHPSESSID=d775118d60c64bba8ee089b082fd297d' \
--data-raw '{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": "parentTemplates",
"selectParentTemplates": [
"templateid",
"name"
],
"hostids": "40054"
},
"auth": "xxxxx",
"id": 1
}'
4.更新关联的模板(注意是覆盖更新)
更新一个主机
curl --location --request GET 'http://10.x.x.x/api_jsonrpc.php' \
--header 'Content-Type: application/json' \
--header 'Cookie: PHPSESSID=d775118d60c64bba8ee089b082fd297d' \
--data-raw '{
"jsonrpc": "2.0",
"method": "host.update",
"params": {
"hostid": "40070",
"templates": [
{
"templateid": "10338"
},
{
"templateid": "10340"
},
{
"templateid": "10353"
},
{
"templateid": "10491"
},
{
"templateid": "10273"
},
{
"templateid": "11859"
}
]
},
"auth": "xxxxx",
"id": 1
}'
批量更新
curl --location --request GET 'http://10.x.x.x/api_jsonrpc.php' \
--header 'Content-Type: application/json' \
--header 'Cookie: PHPSESSID=d775118d60c64bba8ee089b082fd297d' \
--data-raw '{
"jsonrpc": "2.0",
"method": "host.massupdate",
"params": {
"hosts": [
{
"hostid": "40073"
},
{
"hostid": "40074"
},
{
"hostid": "40075"
},
{
"hostid": "40076"
},
{
"hostid": "40077"
}
],
"templates": [
{
"templateid": "10338"
},
{
"templateid": "10340"
},
{
"templateid": "10353"
},
{
"templateid": "10491"
},
{
"templateid": "10273"
},
{
"templateid": "11859"
}
]
},
"auth": "xxxx",
"id": 1
}'
做有积累的事~~

浙公网安备 33010602011771号