Zabbix-API

参考手册 https://www.zabbix.com/documentation/4.0/zh/manual/api

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
API使用:

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.6.10/zabbix/api_jsonrpc.php | python -m json.tool

显示token结果
{
"id": 1,
"jsonrpc": "2.0",
"result": "a55c7355a1063784b021f7c0b186001c"
}


2、示例:
a、通过名称获取数据
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"Zabbix server",
"192.168.0.10"
]
}
},
"auth": "a55c7355a1063784b021f7c0b186001c",
"id": 1
}' http://192.168.6.10/zabbix/api_jsonrpc.php | python -m json.tool

b、显示结果
{
"id": 1,
"jsonrpc": "2.0",
"result": [
{
"auto_compress": "1",
"available": "1",
"description": "",
"disable_until": "0",
"error": "",
"errors_from": "0",
"flags": "0",
"host": "Zabbix server",
"hostid": "10084",
"ipmi_authtype": "-1",
"ipmi_available": "0",
"ipmi_disable_until": "0",
"ipmi_error": "",
"ipmi_errors_from": "0",
"ipmi_password": "",
......
......
......


a、获取所有主机
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "a55c7355a1063784b021f7c0b186001c",
"id": 1
}' http://192.168.6.10/zabbix/api_jsonrpc.php | python -m json.tool

b、显示效果
{
"id": 1,
"jsonrpc": "2.0",
"result": [
{
"host": "Zabbix server",
"hostid": "10084"
},
{
"host": "192.168.2.10",
"hostid": "10269"
},
{
"host": "192.168.0.20",
"hostid": "10275"
}
......
......
......


a、获取用户信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.get",
"params": {
"output": "extend"
},
"auth": "a55c7355a1063784b021f7c0b186001c",
"id": 1
}' http://192.168.6.10/zabbix/api_jsonrpc.php | python -m json.tool

b、显示效果
{
"id": 1,
"jsonrpc": "2.0",
"result": [
{
"alias": "zabbixadmin",
"attempt_clock": "1563333327",
"attempt_failed": "0",
"attempt_ip": "192.168.0.1",
"autologin": "1",
"autologout": "0",
"lang": "zh_CN",
"name": "Zabbix",
"refresh": "30s",
"rows_per_page": "50",
"surname": "Administrator",
"theme": "default",
"type": "3",
"url": "",
"userid": "1"
},
......
......
......

a、获取模板信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"check_nginx"
]
}
},
"auth": "a55c7355a1063784b021f7c0b186001c",
"id": 1
}' http://192.168.6.10/zabbix/api_jsonrpc.php | python -m json.tool



3、基于脚本方式获取token
[root@Host1 ~]# vim token.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-

url = 'http://192.168.6.10/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')


[root@Host1 ~]# yum install python-pip
[root@Host1 ~]# pip install requests #安装requests模块

执行token.py脚本
[root@Host1 ~]# python token.py
ec4018039a773cfe05328dae0b965eec

=========================================================================

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

通过API添加主机-不带proxy模式
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "192.168.0.20",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.0.20",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10265"
}
]
},
"auth": "ec4018039a773cfe05328dae0b965eec",
"id": 1
}' http://192.168.6.10/zabbix/api_jsonrpc.php | python -m json.tool

显示结果
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10278"
]
}
}

zabbix_server web界面验证是否添加成功

img

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
79
80
81
82
83
84
85
86
脚本批量创建
[root@localhost ~]# vim linux-add-host.sh
#!/bin/bash
IP="
192.168.1.10
192.168.1.20
192.168.1.30
"
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}'",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'${node_ip}'",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10265"
}
]
},
"auth": "ec4018039a773cfe05328dae0b965eec",
"id": 1
}' http://192.168.6.10/zabbix/api_jsonrpc.php | python -m json.tool
done



通过API添加主机-proxy模式
[root@localhost ~]# vim linux-add-host.sh
#!/bin/bash
IP="
192.168.1.10
192.168.1.20
192.168.1.30
"

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": "'web1-nginx-${node_ip}'",
"proxy_hostid": "10268",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'${node_ip}'",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10265"
}
]
},
"auth": "ec4018039a773cfe05328dae0b965eec",
"id": 1
}' http://192.168.6.10/zabbix/api_jsonrpc.php | python -m json.tool
done