本文转载自:http://blog.mreald.com/178

Zabbix可以通过自发现添加主机,不过有时候不准确,通过API添加会更加准确!

脚本使用的跟zabbix相关的内容.参考的是zabbix2.2版本的手册

Excel 格式:(列名从前到后使用空格分割)

hostname visible  hostip  dnsname proxy hostgroup hosttemp  hosttemp1

#!/usr/local/bin/python
#coding:utf-8 import json
import urllib2
from urllib2 import URLError
import sys
import xlrd class ZabbixTools:
def __init__(self):
self.url = 'http://10.10.147.183/api_jsonrpc.php'
self.header = {"Content-Type":"application/json"} def user_login(self):
data = json.dumps({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "admin",
"password": "zabbix"
},
"id": 0
}) request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key]) 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()
self.authID = response['result']
return self.authID def host_get(self,hostName):
data = json.dumps({
"jsonrpc":"2.0",
"method":"host.get",
"params":{
"output":["hostid","name"],
"filter":{"host":hostName}
},
"auth":self.user_login(),
"id":1,
}) request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key]) 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 %s: " % hostName, len(response['result'])
lens=len(response['result'])
if lens > 0:
return response['result'][0]['name']
else:
return "" def hostgroup_get(self, hostgroupName):
data = json.dumps({
"jsonrpc":"2.0",
"method":"hostgroup.get",
"params":{
"output": "extend",
"filter": {
"name": [
hostgroupName,
]
}
},
"auth":self.user_login(),
"id":1,
}) request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key]) try:
result = urllib2.urlopen(request)
except URLError as e:
print "Error as ", e
else:
response = json.loads(result.read())
result.close() lens=len(response['result'])
if lens > 0:
self.hostgroupID = response['result'][0]['groupid']
return response['result'][0]['groupid']
else:
print "no GroupGet result"
return "" def template_get(self, templateName):
data = json.dumps({
"jsonrpc":"2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
templateName,
]
}
},
"auth":self.user_login(),
"id":1,
}) request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key]) try:
result = urllib2.urlopen(request)
except URLError as e:
print "Error as ", e
else:
response = json.loads(result.read())
result.close()
self.templateID = response['result'][0]['templateid']
return response['result'][0]['templateid'] def host_create(self, hostName,visibleName,hostIp,dnsName,proxyName, hostgroupName, templateName1, templateName2):
data = json.dumps({
"jsonrpc":"2.0",
"method":"host.create",
"params":{
"host": hostName,
"name": visibleName,
"proxy_hostid": self.proxy_get(proxyName),
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": hostIp,
"dns": dnsName,
"port": ""
}
],
"groups": [
{
"groupid": self.hostgroup_get(hostgroupName)
}
],
"templates": [
{
"templateid": self.template_get(templateName1) },
{ "templateid": self.template_get(templateName2)
}
],
},
"auth": self.user_login(),
"id":1
})
request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key]) try:
result = urllib2.urlopen(request)
except URLError as e:
print "Error as ", e
else:
response = json.loads(result.read())
result.close()
print "host : %s is created! id is %s\n" % (hostip, response['result']['hostids'][0])
self.hostid = response['result']['hostids']
return response['result']['hostids'] def proxy_get(self, ProxyName):
data = json.dumps({
"jsonrpc":"2.0",
"method": "proxy.get",
"params": {
"output": "extend",
"selectInterface": "extend",
"filter": {
"host": [ ProxyName, ]
}
},
"auth":self.user_login(),
"id":1,
}) request = urllib2.Request(self.url, data)
for key in self.header:
request.add_header(key, self.header[key]) try:
result = urllib2.urlopen(request)
except URLError as e:
print "Error as ", e
else:
response = json.loads(result.read())
result.close()
self.templateID = response['result'][0]['proxyid']
return response['result'][0]['proxyid'] if __name__ == "__main__": test = ZabbixTools() workbook = xlrd.open_workbook('test.xlsx')
for row in xrange(workbook.sheets()[0].nrows):
hostname=workbook.sheets()[0].cell(row,0).value
visible=workbook.sheets()[0].cell(row,1).value
hostip=workbook.sheets()[0].cell(row,2).value
dnsname=workbook.sheets()[0].cell(row,3).value
proxy=workbook.sheets()[0].cell(row,4).value
hostgroup=workbook.sheets()[0].cell(row,5).value
hosttemp=workbook.sheets()[0].cell(row,6).value
hosttemp1=workbook.sheets()[0].cell(row,7).value hostgroup=hostgroup.strip() hostnameGet=test.host_get(hostname)
if hostnameGet.strip()=='':
test.host_create(hostname,visible,hostip,dnsname,proxy,hostgroup,hosttemp,hosttemp1)
else:
print "%s have exist! Cannot recreate !\n" % hostnameGet

  

Python调用zabbix API批量添加主机 (读取Excel)的更多相关文章

  1. python 调用zabbix api接口实现主机的增删改查

    python程序调用zabbix系统的api接口实现对zabbix_server端主机的增删改查,使用相关功能时候,需要打开脚本中的相关函数. 函数说明: zabbixtools()  调用zabbi ...

  2. python 调用zabbix api实现查询主机信息,输出所有主机ip

    之前发现搜索出来的主机调用zabbix api信息都不是那么明确,后来通过zabbix官方文档,查到想要的api信息,随后写一篇自己这次项目中用到的api. #!/usr/bin/env python ...

  3. 关于python调用zabbix api接口

    因公司业务需要,引进了自动化运维,所用到的监控平台为zbbix3.2,最近正在学习python,计划使用python调用zabbix api接口去做些事情,如生成报表,我想最基本的是要取得zabbix ...

  4. 『Python』Python 调用 ZoomEye API 批量获取目标网站IP

    #### 20160712 更新 原API的访问方式是以 HTTP 的方式访问的,根据官网最新文档,现在已经修改成 HTTPS 方式,测试可以正常使用API了. 0x 00 前言 ZoomEye 的 ...

  5. 使用Python调用Zabbix API

    Zabbix API官方文档: https://www.zabbix.com/documentation/4.0/zh/manual/api 1.向 api_jsonrpc.php 发送HTTP_PO ...

  6. python 通过zabbix api获得所有主机的ip

    #!/usr/bin/env python3 #coding=utf-8 import jsonimport requests#from urllib import requests, parse,e ...

  7. python调用zabbix接口实现Action配置

    要写这篇博客其实我的内心是纠结的,老实说,我对zabbix的了解实在不多.但新公司的需求不容置疑,当我顶着有两个头大的脑袋懵懵转入运维领域时,面前摆着两百多组.上千台机器等着写入zabbix监控的需求 ...

  8. 03: zabbix API接口 对 主机、主机组、模板、应用集、监控项、触发器等增删改查

    目录:Django其他篇 01: 安装zabbix server 02:zabbix-agent安装配置 及 web界面管理 03: zabbix API接口 对 主机.主机组.模板.应用集.监控项. ...

  9. 分布式监控系统Zabbix--完整安装记录-批量添加主机和自动发现端口

    一.Zabbix-3.0.3批量添加主机的配置如下: 0)被监控机上要安装zabbix_agent,并配置好zabbix_agentd.conf (如下172.29.8.50是zabbix_serve ...

随机推荐

  1. 【小笔记】斜率优化的结论(WC)

  2. SpringBoot拦截器中service或者redis注入为空的问题

    原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...

  3. ASP.NET MVC生命周期介绍(转)

    本文以IIS7中asp.net应用程序生命周期为例,介绍了asp.net mvc的生命周期. asp.net应用程序管道处理用户请求时特别强调"时机",对asp.net生命周期的了 ...

  4. flask前端优化:css/js/html压缩

    1.先压缩再传输,可以减少传输的大小,减少传输时间,但是压缩需要时间,所以最终页面显示是快了还是慢了,需要比较 2.先看html压缩模块:pip install Flask-HTMLmin 压缩前:大 ...

  5. Spark(九) -- SparkSQL API编程

    本文测试的Spark版本是1.3.1 Text文本文件测试 一个简单的person.txt文件内容为: JChubby,13 Looky,14 LL,15 分别是Name和Age 在Idea中新建Ob ...

  6. Python 爬基金数据

    爬科学基金共享服务网中基金数据 #coding=utf-8 import json import requests from lxml import etree from HTMLParser imp ...

  7. 手游产品经理初探(四)从Buybutton谈玩家付费

    付费模块一直是游戏中最最重要的一块,那么今天我们从玩家的角度来解说哪种方式付费更迎合玩家的心理.我还是着重从我做的Casino类型游戏说起. 一般来说游戏界面喜欢把付费button放在界面最醒目的位置 ...

  8. Win7如何开启管理员账户

    打开运行对话框,在LUSRMGR.MSC里,左边点用户,在右边栏里右击Administrator选择属性,去掉账户已禁用这个选项前面的勾.我也问过这个问题,确实如版主说的这样可解决这个问题,但有个问题 ...

  9. 微信小程序 解决 数字粗细不一 的bug

    1.bug 2.原因解析 微信小程序本身字体问题 3.解决方案 设置字体 font-family: Microsoft YaHei; .

  10. SAP query传输以后须要又一次生成程序

    近期有个需求,须要改动一个Query,在DEV改动好并測试通过后.传输到QAS,可是报表还是没变化,着实郁闷了一下,这是万能的google帮上忙了,原来传到其它系统以后还须要generate prog ...