“Zabbix 调用API 批量添加主机”的版本间的差异
(→进阶) |
|||
(未显示同一用户的22个中间版本) | |||
第109行: | 第109行: | ||
<pre> | <pre> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
+ | # -*- coding: UTF-8 -*- | ||
# Version = 3.5.2 | # Version = 3.5.2 | ||
# __auth__ = '' | # __auth__ = '' | ||
import json | import json | ||
from urllib import request, parse | from urllib import request, parse | ||
− | + | ||
+ | #有时可能是https哦 | ||
ZABBIX_URL = 'http://z.cn' | ZABBIX_URL = 'http://z.cn' | ||
ZABBIX_USERNAME = "Admin" | ZABBIX_USERNAME = "Admin" | ||
第164行: | 第166行: | ||
===获得 groupid === | ===获得 groupid === | ||
<pre> | <pre> | ||
− | cat getgroupid.sh | + | cat getgroupid.sh #记住token 会变的 可能要多次获取 Oct 15 2021 |
#获取指定groupid | #获取指定groupid | ||
curl -s -X POST -H 'Content-Type:application/json' -d ' | curl -s -X POST -H 'Content-Type:application/json' -d ' | ||
第185行: | 第187行: | ||
#这里的id一定是1 | #这里的id一定是1 | ||
+ | |||
+ | #Oct 2021 | ||
+ | bash getgroupid.sh | ||
+ | #得知Linux server gid 为2 | ||
+ | { | ||
+ | "jsonrpc": "2.0", | ||
+ | "result": [ | ||
+ | { | ||
+ | "groupid": "2", | ||
+ | "name": "Linux servers", | ||
+ | "internal": "0", | ||
+ | "flags": "0" | ||
+ | }, | ||
+ | { | ||
+ | "groupid": "4", | ||
+ | "name": "Zabbix servers", | ||
+ | "internal": "0", | ||
+ | "flags": "0" | ||
+ | } | ||
+ | ], | ||
+ | "id": 1 | ||
+ | } | ||
+ | |||
+ | |||
</pre> | </pre> | ||
[https://www.cnblogs.com/zdoubly/p/9777122.html zabbix--api学习之路--get_hostgroup获取] | [https://www.cnblogs.com/zdoubly/p/9777122.html zabbix--api学习之路--get_hostgroup获取] | ||
第190行: | 第216行: | ||
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/hostgroup/get | https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/hostgroup/get | ||
− | === | + | === 获取templateid=== |
+ | https://www.zabbix.com/documentation/5.0/manual/api/reference/template/get | ||
<pre> | <pre> | ||
− | # | + | #获取所有模板的信息 |
− | # | + | curl -s -X POST -H 'Content-Type:application/json' -d ' |
− | #获取指定groupid | + | { |
+ | "jsonrpc": "2.0", | ||
+ | "method": "template.get", | ||
+ | "params": { | ||
+ | "output": "extend" | ||
+ | }, | ||
+ | "auth": "1794bcbe6d818069bff5aa423a07a960", | ||
+ | "id": 1 | ||
+ | }' http://148.66.11.55/zabbix/api_jsonrpc.php | python3 -m json.tool | ||
+ | |||
+ | |||
+ | #Oct 15 2021 | ||
+ | 获取指定模板的信息 "Template OS Linux by Zabbix agent ,不是 Template OS Linux" | ||
+ | |||
+ | curl -s -X POST -H 'Content-Type:application/json' -d ' | ||
+ | { | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "template.get", | ||
+ | "params": { | ||
+ | "output": "extend", | ||
+ | "filter": { | ||
+ | "host": [ | ||
+ | "Template OS Linux by Zabbix agent" | ||
+ | ] | ||
+ | } | ||
+ | }, | ||
+ | "auth": "fc0f09dbddddd9ef1a81adada93a", | ||
+ | "id": 1 | ||
+ | }' https://zbx.com/api_jsonrpc.php | python3 -m json.tool | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | #"templateid": "10001", | ||
+ | |||
+ | |||
+ | |||
+ | 获取指定groupid | ||
curl -s -X POST -H 'Content-Type:application/json' -d ' | curl -s -X POST -H 'Content-Type:application/json' -d ' | ||
{ | { | ||
第224行: | 第288行: | ||
"groupid": "4", | "groupid": "4", | ||
"name": "Zabbix servers", | "name": "Zabbix servers", | ||
+ | |||
+ | |||
+ | |||
</pre> | </pre> | ||
第338行: | 第405行: | ||
"auth": "1794bcbe6d818069bff5aa423a07a960", | "auth": "1794bcbe6d818069bff5aa423a07a960", | ||
"id": 1 | "id": 1 | ||
− | }' http://148.66.11.55/zabbix/api_jsonrpc.php | | + | }' http://148.66.11.55/zabbix/api_jsonrpc.php | python3 -m json.tool |
done | done | ||
#要添加多一个群组 | #要添加多一个群组 | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | === 别人例子=== | ||
+ | <pre> | ||
+ | 获取 KEY | ||
+ | |||
+ | !/usr/bin/env python2.7 | ||
+ | #coding=utf-8 | ||
+ | import json | ||
+ | import urllib2 | ||
+ | # based url and required header | ||
+ | url = "http://monitor.example.com/api_jsonrpc.php" | ||
+ | header = {"Content-Type": "application/json"} | ||
+ | # auth user and password | ||
+ | data = json.dumps( | ||
+ | { | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "user.login", | ||
+ | "params": { | ||
+ | "user": "Admin", | ||
+ | "password": "zabbix" | ||
+ | }, | ||
+ | "id": 0 | ||
+ | }) | ||
+ | # create request object | ||
+ | request = urllib2.Request(url,data) | ||
+ | for key in header: | ||
+ | request.add_header(key,header[key]) | ||
+ | # auth and get authid | ||
+ | try: | ||
+ | result = urllib2.urlopen(request) | ||
+ | except URLError as e: | ||
+ | print "Auth Failed, Please Check Your Name And Password:",e.code | ||
+ | else: | ||
+ | response = json.loads(result.read()) | ||
+ | result.close() | ||
+ | print "Auth Successful. The Auth ID Is:",response['result'] | ||
+ | |||
+ | 获取 hostlist 所有主机列表 | ||
+ | |||
+ | #!/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.get", | ||
+ | "params":{ | ||
+ | "output":["hostid","name"], | ||
+ | "filter":{"host":""} | ||
+ | }, | ||
+ | "auth":"dbcd2bd8abc0f0320fffab34c6d749d3", | ||
+ | "id":1, | ||
+ | }) | ||
+ | # create request object | ||
+ | request = urllib2.Request(url,data) | ||
+ | for key in header: | ||
+ | request.add_header(key,header[key]) | ||
+ | # 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 "Number Of Hosts: ", len(response['result']) | ||
+ | for host in response['result']: | ||
+ | print "Host ID:",host['hostid'],"Host Name:",host['name'] | ||
+ | |||
+ | |||
+ | eg | ||
+ | Number Of Hosts: 2 | ||
+ | Host ID: 10084 Host Name: Zabbix server | ||
+ | Host ID: 10413 Host Name: xps | ||
+ | 添加主机 | ||
+ | |||
+ | #!/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[key]) | ||
+ | # 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 | ||
+ | |||
</pre> | </pre> | ||
第373行: | 第568行: | ||
=进阶= | =进阶= | ||
写成通用的py脚本 ? | 写成通用的py脚本 ? | ||
+ | |||
+ | 还可以主机管理 创建主机 修改主机信息 删除主机, 监控项管理, 触发器管理,用户和用户组管理 | ||
[https://www.jianshu.com/p/e087cace8ddf 利用zabbix API(python)批量查询主机、模板、添加删除主机] | [https://www.jianshu.com/p/e087cace8ddf 利用zabbix API(python)批量查询主机、模板、添加删除主机] | ||
第396行: | 第593行: | ||
=zabbix api 数据展示= | =zabbix api 数据展示= | ||
+ | |||
+ | https://github.com/evan886/cmdb_salt_zab | ||
+ | |||
+ | [email protected]:evan886/cmdb_salt_zab.git | ||
Zabbix数据库模型(二次开发必备) | Zabbix数据库模型(二次开发必备) | ||
− | + | https://github.com/livestalker-archive/django-zabbix-api | |
[https://www.zhihu.com/question/62351896 如何通过zabbix的api获取前端最近20个问题?] | [https://www.zhihu.com/question/62351896 如何通过zabbix的api获取前端最近20个问题?] | ||
第405行: | 第606行: | ||
先 [https://blog.csdn.net/u011085172/article/details/77374879 python 通过zabbix api来获取当前trigger(告警)并打印出信息] | 先 [https://blog.csdn.net/u011085172/article/details/77374879 python 通过zabbix api来获取当前trigger(告警)并打印出信息] | ||
[https://blog.csdn.net/u011085172/article/details/77480245 django bootstrap 获取zabbix告警信息并展示] | [https://blog.csdn.net/u011085172/article/details/77480245 django bootstrap 获取zabbix告警信息并展示] | ||
+ | |||
+ | [https://blog.51cto.com/yangrong/1559123 python调用zabbix api接口实时展示数据] | ||
+ | |||
和那个项目是一样的呀 就是那个作者 | 和那个项目是一样的呀 就是那个作者 | ||
[https://blog.csdn.net/qq_28513801/article/details/100828148?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-5.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-5.nonecase 基于Python调用zabbix监控的API接口详解,将数据展现到前台过程、思路分析] | [https://blog.csdn.net/qq_28513801/article/details/100828148?utm_medium=distribute.pc_relevant_download.none-task-blog-baidujs-5.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-baidujs-5.nonecase 基于Python调用zabbix监控的API接口详解,将数据展现到前台过程、思路分析] | ||
+ | |||
+ | [https://zhuanlan.zhihu.com/p/38489604 安全管理平台项目记录(1):zabbix API测试] | ||
+ | [https://blog.51cto.com/rfyiamcool/1358792 关于python调用zabbix api接口的自动化实例 [结合saltstack<nowiki>]</nowiki>] | ||
应该最终是出图 | 应该最终是出图 | ||
第420行: | 第627行: | ||
[https://blog.csdn.net/qq_35465132/article/details/78963269?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-15.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-15.control 前端调用zabbix告警数据] | [https://blog.csdn.net/qq_35465132/article/details/78963269?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-15.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-15.control 前端调用zabbix告警数据] | ||
+ | |||
+ | [https://www.zhihu.com/question/62351896 如何通过zabbix的api获取前端最近20个问题?] 简单实现了一下,根据自己的需要处理数据就行了,好好看看官方文档,写的还是比较全的。 | ||
[https://blog.51cto.com/dreamgirl1314/2044918 python脚本采集服务器数据通过API提交到django web服务器,然后展示在页面上 原创] | [https://blog.51cto.com/dreamgirl1314/2044918 python脚本采集服务器数据通过API提交到django web服务器,然后展示在页面上 原创] | ||
第455行: | 第664行: | ||
[https://blog.51cto.com/dreamgirl1314/2044918 python脚本采集服务器数据通过API提交到django web服务器,然后展示在页面上] | [https://blog.51cto.com/dreamgirl1314/2044918 python脚本采集服务器数据通过API提交到django web服务器,然后展示在页面上] | ||
+ | |||
+ | [https://blog.csdn.net/h106140873/article/details/90678121?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-4&spm=1001.2101.3001.4242 python调用zabbix api接口实时展示数据] | ||
+ | |||
+ | [https://github.com/icyway/reportform 调用zabbix的api接口,获取所有监控主机的cpu负载、内存、磁盘使用情况,将采集的数据存入数据库, 最后并使用django框架,进行前端展示。] | ||
=see also= | =see also= | ||
第467行: | 第680行: | ||
[https://segmentfault.com/a/1190000014241994 调用ZABBIX的API获取节点主机信息小记] | [https://segmentfault.com/a/1190000014241994 调用ZABBIX的API获取节点主机信息小记] | ||
+ | |||
+ | [https://blog.51cto.com/u_4722372/1717410 Zabbix Api的使用不错,不过是2015年的] | ||
+ | |||
+ | [https://blog.csdn.net/Kammingo/article/details/107174246 Zabbix API批量处理脚本] | ||
[https://www.cnblogs.com/ssgeek/p/9279775.html zabbix api的使用] | [https://www.cnblogs.com/ssgeek/p/9279775.html zabbix api的使用] |
2024年10月21日 (一) 14:24的最新版本
目录
pre
已有 salt Salt yum安装
可以见 批量添加 salt Salt-ssh批量自动安装被控端minion
批量添加zabbix agent
直接用cmd.run
salt 'prod-mq03' cmd.run 'yum install -y zabbix-agent' #systemctl restart zabbix-agent systemctl enable zabbix-agent
zabbix state.sls
有空要试一下
SaltStack批量安装zabbix-agent(yum安装)
SaltStack 使用pillar安装配置管理zabbix
saltstack自动化运维系列⑦SaltStack实践配置管理安装zabbix
salt 使用state文件来源码安装和配置zabbix客户端文件
批量修改zabbix agent配置
Server=148.66.11.55 ServerActive=148.66.11.55 Hostname=prod-sns-mq01 sed -i '/^Hostname/ s/.*/Hostname=zabbix_hk/' /etc/zabbix/zabbix_agentd.conf #zbx hostname salt 'prod-mq03' cmd.run 'hostname' prod-mq03: prod-mq03 #这里可以取出 hostname 作变量 和作循环 Server=148.66.01.05 ServerActive=148.66.01.05 myhost=`salt 'prod-mq03' cmd.run 'hostname' |sed -n '2p' |awk '{print $1}'` salt 'prod-sns-mq02' cmd.run "sed -i '/^Hostname/ s/.*/Hostname=$myhost/' /etc/zabbix/zabbix_agentd.conf" #这个替换太多了 不太好写匹配 要如下添加 = or 直接 inser 就是了 salt 'prod-mq03' cmd.run "sed -i '/^Server=/ s/.*/Server=$Server/' /etc/zabbix/zabbix_agentd.conf" salt 'prod-mq03' cmd.run "sed -i '/^ServerActive=/ s/.*/ServerActive=$ServerActive/' /etc/zabbix/zabbix_agentd.conf" 注意 "" 结果不太对的 salt 'prod-sns-mq02' cmd.run "hostname |sed -n '2p' |awk '{print $1}'" prod-sns-mq02:
利用api批量添加主机
zbx_server version 4.0
get token 获得自己的token
py2
cat api.py # -*- coding:utf-8 -*- #py2 import urllib2 import json #u zbx server ip or domain url = 'http://myzabbix.com/api_jsonrpc.php' header = {'Content-Type': 'application/json'} req = json.dumps( { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "123456" }, "id": 0, } ) def auth(): r = urllib2.Request(url=url, headers=header, data=req) response = urllib2.urlopen(r) token = json.loads(response.read()) print(token) if __name__ == '__main__': auth() #token {u'jsonrpc': u'2.0', u'result': u'1794bcbe6d818069bff5aa499a07a960', u'id': 0}
py3
#!/usr/bin/env python # -*- coding: UTF-8 -*- # Version = 3.5.2 # __auth__ = '' import json from urllib import request, parse #有时可能是https哦 ZABBIX_URL = 'http://z.cn' ZABBIX_USERNAME = "Admin" ZABBIX_PASSWORD = "zabbix" url = "{}/api_jsonrpc.php".format(ZABBIX_URL) header = {"Content-Type": "application/json"} # auth user and password data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": ZABBIX_USERNAME, "password": ZABBIX_PASSWORD }, "id": 1, } # 由于API接收的是json字符串,故需要转化一下 value = json.dumps(data).encode('utf-8') # 对请求进行包装 req = request.Request(url, headers=header, data=value) # 验证并获取Auth ID try: # 打开包装过的url result = request.urlopen(req) except Exception as e: print("Auth Failed, Please Check Your Name And Password:", e) else: response = result.read() # 上面获取的是bytes类型数据,故需要decode转化成字符串 page = response.decode('utf-8') # 将此json字符串转化为python字典 page = json.loads(page) result.close() print("Auth Successful. The Auth ID Is: {}".format(page.get('result'))) python3 tmp/zapi3.py Auth Successful. The Auth ID Is: cef1be0881b355e801678036cdc8c685
Python调用Zabbix api之从入门到放弃——登录并获取身份验证令牌
我们获取必要的相关信息 ,eg所有主机list信息
获得 groupid
cat getgroupid.sh #记住token 会变的 可能要多次获取 Oct 15 2021 #获取指定groupid curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "filter": { "name": [ "Zabbix servers", "Linux servers", "sns servers" ] } }, "auth": "1794bcbe6d818069bff5aa423a07a960", "id": 1 }' http://myzabbix.com//api_jsonrpc.php | python3 -m json.tool #这里的id一定是1 #Oct 2021 bash getgroupid.sh #得知Linux server gid 为2 { "jsonrpc": "2.0", "result": [ { "groupid": "2", "name": "Linux servers", "internal": "0", "flags": "0" }, { "groupid": "4", "name": "Zabbix servers", "internal": "0", "flags": "0" } ], "id": 1 }
zabbix--api学习之路--get_hostgroup获取
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/hostgroup/get
获取templateid
https://www.zabbix.com/documentation/5.0/manual/api/reference/template/get
#获取所有模板的信息 curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "template.get", "params": { "output": "extend" }, "auth": "1794bcbe6d818069bff5aa423a07a960", "id": 1 }' http://148.66.11.55/zabbix/api_jsonrpc.php | python3 -m json.tool #Oct 15 2021 获取指定模板的信息 "Template OS Linux by Zabbix agent ,不是 Template OS Linux" curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "template.get", "params": { "output": "extend", "filter": { "host": [ "Template OS Linux by Zabbix agent" ] } }, "auth": "fc0f09dbddddd9ef1a81adada93a", "id": 1 }' https://zbx.com/api_jsonrpc.php | python3 -m json.tool #"templateid": "10001", 获取指定groupid curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "filter": { "name": [ "Zabbix servers", "Linux servers" ] } }, "auth": "1794bcbe6d818069bff5aa423a07a960", "id": 1 }' http://148.66.11.55/zabbix/api_jsonrpc.php | python3 -m json.tool #output "result": [ { "groupid": "2", "name": "Linux servers", "internal": "0", "flags": "0" }, { "groupid": "4", "name": "Zabbix servers",
hostadd
https://www.zabbix.com/documentation/4.0/manual/api/reference/host/create
批量添加版 用法 分别添加 ip and hostname #!/bin/bash #注释中文导致不成功 #"host": "'prod-sns-mq02'", #这里一般改为你要的hostname IP=( 40.243.52 41.102.111 40.218.197 40.216.19 ) HOSTNAME=( prod-sns-php01 prod-sns-php02 prod-sns-mq01 prod-sns-mq03 ) myend=`expr ${#IP[@]} - 1` for no in `seq 0 ${myend}` #for no in `seq 2 3` #这个是手工决定 数量 do curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'${HOSTNAME[$no]}'", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "'${IP[$no]}'", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "2", "groupid": "16" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "1794bcbe6d818069bff5aa4207a960", "id": 1 }' http://zbx.com/zabbix/api_jsonrpc.php | python -m json.tool done #要添加多一个群组 #!/bin/bash #注释中文导致不成功 #"host": "'prod-sns-mq02'", #这里一般改为你要的hostname IP=" 121.41.103.113 " for node_ip in ${IP}; do curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'prod-sns-es01'", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "'${node_ip}'", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "2", "groupid": "16" } ], "templates": [ { "templateid": "10001" } ] }, "auth": "1794bcbe6d818069bff5aa423a07a960", "id": 1 }' http://148.66.11.55/zabbix/api_jsonrpc.php | python3 -m json.tool done #要添加多一个群组
别人例子
获取 KEY !/usr/bin/env python2.7 #coding=utf-8 import json import urllib2 # based url and required header url = "http://monitor.example.com/api_jsonrpc.php" header = {"Content-Type": "application/json"} # auth user and password data = json.dumps( { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 0 }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # auth and get authid try: result = urllib2.urlopen(request) except URLError as e: print "Auth Failed, Please Check Your Name And Password:",e.code else: response = json.loads(result.read()) result.close() print "Auth Successful. The Auth ID Is:",response['result'] 获取 hostlist 所有主机列表 #!/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.get", "params":{ "output":["hostid","name"], "filter":{"host":""} }, "auth":"dbcd2bd8abc0f0320fffab34c6d749d3", "id":1, }) # create request object request = urllib2.Request(url,data) for key in header: request.add_header(key,header[key]) # 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 "Number Of Hosts: ", len(response['result']) for host in response['result']: print "Host ID:",host['hostid'],"Host Name:",host['name'] eg Number Of Hosts: 2 Host ID: 10084 Host Name: Zabbix server Host ID: 10413 Host Name: xps 添加主机 #!/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[key]) # 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
批量改 agent 配置
ssh 方式
IP=( 172.31.27.83 172.31.25.239 172.31.16.171 172.31.26.183 172.31.31.249 172.31.25.199 172.31.22.218 172.31.24.104 172.31.19.164 ) myend=`expr ${#IP[@]} - 1` for no in `seq 0 ${myend}` do ssh -i id_rsa -o "StrictHostKeyChecking no" root@${IP[$no]} "sed -i 's!Server=148.66.11.55!Server=38.20.125.2!' /etc/zabbix/zabbix_agentd.conf && systemctl restart zabbix-agent" done
进阶
写成通用的py脚本 ?
还可以主机管理 创建主机 修改主机信息 删除主机, 监控项管理, 触发器管理,用户和用户组管理
利用zabbix API(python)批量查询主机、模板、添加删除主机
通过zabbix自带api进行主机的批量添加操作信息填写成列表
zabbix高级使用五:通过zabbix API添加监控主机has used
zabbix api 数据展示
https://github.com/evan886/cmdb_salt_zab
[email protected]:evan886/cmdb_salt_zab.git
Zabbix数据库模型(二次开发必备)
https://github.com/livestalker-archive/django-zabbix-api
先 python 通过zabbix api来获取当前trigger(告警)并打印出信息 django bootstrap 获取zabbix告警信息并展示
和那个项目是一样的呀 就是那个作者 基于Python调用zabbix监控的API接口详解,将数据展现到前台过程、思路分析
关于python调用zabbix api接口的自动化实例 [结合saltstack]
应该最终是出图
Python拉取zabbix数据并使用matplotlib模块绘图
python做监控界面_Zabbix与Python不得不说的基情——用Python定制自己的zabbix界面
如何通过zabbix的api获取前端最近20个问题? 简单实现了一下,根据自己的需要处理数据就行了,好好看看官方文档,写的还是比较全的。
python脚本采集服务器数据通过API提交到django web服务器,然后展示在页面上 原创
从无到有写一个运维APP(一) 从无到有写一个运维APP(二)
Python采集linux服务器数据在Django Web界面展示 原创
python脚本采集服务器数据通过API提交到django web服务器,然后展示在页面上 原创 没有采集部分呀
Python采集linux服务器数据在Django Web界面展示没有采集部分呀
Django在web页面展示linux服务器的文本内容 原创
zabbix二次开发之从mysql取值在运维平台js图表展现
python脚本采集服务器数据通过API提交到django web服务器,然后展示在页面上
调用zabbix的api接口,获取所有监控主机的cpu负载、内存、磁盘使用情况,将采集的数据存入数据库, 最后并使用django框架,进行前端展示。