官方文档:​​https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host​

ZAbbix-API是通过命令直接添加主机,即可不用去web界面手动去添加了,添加的主机展示界面如下:

zabbix-API_zabbix-API

1.修改登录账户及密码

1.1.修改账户

zabbix-API_zabbix-API_02zabbix-API_zabbix-API_03

1.2.修改密码

zabbix-API_zabbix-API_04zabbix-API_zabbix-API_05

2.添加主机

2.1.获取token

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "zabbixadmin",
"password": "123456"
},
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

返回结果

{
"jsonrpc": "2.0",
"result": "832c05076e2627715d408877863bdba6",
"id": 1
}

获取token脚本

需要使用python2.7(ubuntu系统)

token.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
import json

url = 'http://192.168.47.50/zabbix/api_jsonrpc.php'
post_data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "zabbixadmin",
"password": "123456"
},
"id": 1
}
post_header = {'Content-Type': 'application/json'}

ret = requests.post(url, data=json.dumps(post_data), headers=post_header)

zabbix_ret = json.loads(ret.text)
if not zabbix_ret.has_key('result'):
print 'login error'
else:
print zabbix_ret.get('result')

2.2.获取所有主机

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

返回结果

{
"jsonrpc": "2.0",
"result": [
{
"hostid": "10084",
"host": "Zabbix server"
},
{
"hostid": "10278",
"host": "192.168.47.52"
},
{
"hostid": "10281",
"host": "192.168.47.54"
}
],
"id": 1
}

2.3.获取指定主机信息

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"192.168.47.52"
]
}
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

返回结果

{
"jsonrpc": "2.0",
"result": [
{
"hostid": "10278",
"proxy_hostid": "10279",
"host": "192.168.47.52",
"status": "0",
"disable_until": "0",
"error": "",
"available": "1",
"errors_from": "0",
"lastaccess": "0",
"ipmi_authtype": "-1",
"ipmi_privilege": "2",
"ipmi_username": "",
"ipmi_password": "",
"ipmi_disable_until": "0",
"ipmi_available": "0",
"snmp_disable_until": "0",
"snmp_available": "0",
"maintenanceid": "0",
"maintenance_status": "0",
"maintenance_type": "0",
"maintenance_from": "0",
"ipmi_errors_from": "0",
"snmp_errors_from": "0",
"ipmi_error": "",
"snmp_error": "",
"jmx_disable_until": "0",
"jmx_available": "0",
"jmx_errors_from": "0",
"jmx_error": "",
"name": "192.168.47.52",
"flags": "0",
"templateid": "0",
"description": "",
"tls_connect": "1",
"tls_accept": "1",
"tls_issuer": "",
"tls_subject": "",
"tls_psk_identity": "",
"tls_psk": "",
"proxy_address": "",
"auto_compress": "1"
}
],
"id": 1
}

2.4.获取所有用户

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.get",
"params": {
"output": "extend"
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

返回结果

{
"jsonrpc": "2.0",
"result": [
{
"userid": "1",
"alias": "zabbixadmin",
"name": "Zabbix",
"surname": "Administrator",
"url": "",
"autologin": "1",
"autologout": "0",
"lang": "zh_CN",
"refresh": "30s",
"type": "3",
"theme": "default",
"attempt_failed": "0",
"attempt_ip": "",
"attempt_clock": "0",
"rows_per_page": "50"
},
{
"userid": "2",
"alias": "guest",
"name": "",
"surname": "",
"url": "",
"autologin": "0",
"autologout": "15m",
"lang": "en_GB",
"refresh": "30s",
"type": "1",
"theme": "default",
"attempt_failed": "0",
"attempt_ip": "",
"attempt_clock": "0",
"rows_per_page": "50"
}
],
"id": 1
}

2.5.获取某个模板信息

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"Template OS Linux-active"
]
}
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

返回结果

{
"jsonrpc": "2.0",
"result": [
{
"proxy_hostid": "0",
"host": "Template OS Linux-active",
"status": "3",
"disable_until": "0",
"error": "",
"available": "0",
"errors_from": "0",
"lastaccess": "0",
"ipmi_authtype": "-1",
"ipmi_privilege": "2",
"ipmi_username": "",
"ipmi_password": "",
"ipmi_disable_until": "0",
"ipmi_available": "0",
"snmp_disable_until": "0",
"snmp_available": "0",
"maintenanceid": "0",
"maintenance_status": "0",
"maintenance_type": "0",
"maintenance_from": "0",
"ipmi_errors_from": "0",
"snmp_errors_from": "0",
"ipmi_error": "",
"snmp_error": "",
"jmx_disable_until": "0",
"jmx_available": "0",
"jmx_errors_from": "0",
"jmx_error": "",
"name": "Template OS Linux-active",
"flags": "0",
"templateid": "10277",
"description": "",
"tls_connect": "1",
"tls_accept": "1",
"tls_issuer": "",
"tls_subject": "",
"tls_psk_identity": "",
"tls_psk": "",
"proxy_address": "",
"auto_compress": "1"
}
],
"id": 1
}

2.6.通过API添加主机命令格式

API添加主机要预先知道要添加的主机IP、预先安装并配置好zabbix agent、预先知道要关联的模板ID/组ID等信息,然后同API提交请求添加

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create", #定义方法,N多钟
"params": {
"host": "192.168.0.24", #自定义添加后的agent的名称
"interfaces": [
{
"type": 1, #类型为1表示agent,2是SNMP,3是IMPI,4是JMX
"main": 1, #more接口
"useip": 1, #0是使用DNS,1是使用IP地址
"ip": "192.168.0.24", #添加的zabbix agent的IP地址
"dns": "",
"port": "10050" #agent端口
}
],
"groups": [
{
"groupid": "2" #添加到的组的ID
}
],
"templates": [
{
"templateid": "10001" #关联的模板的ID
}
]
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

2.6.1.通过API添加主机-不带proxy模式

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "192.168.47.52",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.47.52",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

zabbix-API_zabbix-API_06

注意:对于templates模板,只能关联一个,如果你想关联多个模板,可以先把你要关联的模板,去链接其他模板,这样你只需要关联该模板,就达到关联多个模板的效果

链接模板如下图所示:

zabbix-API_zabbix-API_07

2.6.2.通过API添加主机-带proxy模式

curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "192.168.47.52",
"proxy_hostid": "10279",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.47.52",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool

2.6.3.通过脚本调用API快速添加

zabbix_add_node.sh 

#!/bin/bash
IP="
192.168.47.52
192.168.47.53
"
for node_ip in ${IP};do
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "'${node_ip}'",
"name": "linux36-nginx-web_'${node_ip}'",
"proxy_hostid": "10279",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'${node_ip}'",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"auth": "3791fcdf915514bb6523dc008720c6fb",
"id": 1
}' http://192.168.47.50/zabbix/api_jsonrpc.php | python3 -m json.tool
done

zabbix-API_zabbix-API_08

注意:

host-->对应主机名称

name-->对应可见的名称

zabbix-API_zabbix-API_09