添加脚本 addhost.py

#coding:utf-8

import json
import urllib2
from urllib2 import URLError
import sys
import xlrd # 测试ok
class ZabbixTools:
def __init__(self):
self.url = 'http://172.20.10.3/zabbix/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'] # 创建host
#def host_create(self, hostName,visibleName,hostIp,dnsName,proxyName, hostgroupName, templateName1, templateName2):
def host_create(self, hostName,visibleName,hostIp, hostgroupName, templateName1, port):
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": "",
"port": port
}
],
"groups": [
{
"groupid": self.hostgroup_get(hostgroupName)
}
],
"templates": [
{
"templateid": self.template_get(templateName1) }
],
},
"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'] # 程序的入口
if __name__ == "__main__":
# 实例化ZabbixTools对象
test = ZabbixTools() #result = test.host_get('node5.chinasoft.com')
#print result # 获取host列表
workbook = xlrd.open_workbook('host_list.xls')
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,3).value
hosttemp=workbook.sheets()[0].cell(row,4).value
hostport=workbook.sheets()[0].cell(row,5).value
#hosttemp2=workbook.sheets()[0].cell(row,5).value
#hosttemp3=workbook.sheets()[0].cell(row,6).value hostgroup=hostgroup.strip() hostnameGet=test.host_get(hostname) print hostname + ',' + visible + ',' + hostip + ',' + hostgroup + ',' + hosttemp
test.host_create(hostname,visible,hostip,hostgroup,hosttemp,hostport)

host_list.xls

{hostname} {visible name} {ip} {groups} {templates} {port}

Zabbix批量添加Hosts的更多相关文章

  1. zabbix 批量添加web场景监控

    公司有大量测试环境的url需要监控是否能够访问,即url状态不为200即报警.状态为200即正常.因url比较多,且经常发生改变,如通过web场景配置(我没配过)会比较繁琐,工作量比较大.通过网上查找 ...

  2. zabbix批量添加被监控windows客户端

    由于公司大部分用的是windows服务器,大概有50多台.如果是一台一台添加的话很是麻烦,如果数量更多的话那工作量可想而知.所以网络管理员通常都是非常懒的. 环境:公司虽是域环境,但是除了几台域服务器 ...

  3. zabbix批量添加SNMP监听H3C端口检测以及流量图

    由于之前网络设备不是很多,监控网络设备接口就直接使用模版中的item来实现了,可是现在公司上线了一大批网络设备,如果要每个网络设备都做模板,添加item......那就该废了,于是迫于压力今天来测试使 ...

  4. zabbix 批量添加聚合图形

    环境为centos 脚本要在centos zabbix服务器上运行,zabbix server上运行 1.先把脚本部署到zabbix客户端,把脚本保存为nic.sh 存放路径确保zabbix可以访问 ...

  5. Zabbix 4.0 API 实践,主机/主机群组 批量添加模板和删除模板

    场景 我们日常在管理Zabbix 的时候,经常会需要批量添加模板和批量删除模板,Zabbix页面是提供的批量链接的功能,但是它链接的也只是当前页的主机,我们想扩展这个功能,在链接的时候,可以批量链接整 ...

  6. zabbix利用api批量添加item,并且批量配置添加graph

    关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...

  7. Zabbix网络自动发现规则和自动添加hosts及link模板

    Version: zabbix 3.0 一.配置网络发现规则 Device uniqueness criteria:选择主机名作为唯一标识(Configuation Hosts中显示的NAME) 二. ...

  8. 通过zabbix自带api进行主机的批量添加操作

    通过zabbix自带api进行批量添加主机 我们需要监控一台服务器的时候,当客户端装好zabbix-agent端并正确配置以后,需要在zabbix-server的web gui界面进行添加zabbix ...

  9. Zabbix使用python批量添加主机及主机资产信息-从零到无

    - - 时间:2020年11月10日 - - 作者:飞翔的小胖猪 前言: 使用zabbix作为基础环境的监控系统时,面对现网在用的2000+台把这些主机添加到zabbix监控中是一个问题,当然zabb ...

随机推荐

  1. C语言专项错题集

    2020-08-10 记录 #1 1 struct student{ 2 int num; 3 int age; 4 }; 5 struct student stu[3]={{6001,20},{60 ...

  2. Spark Extracting,transforming,selecting features

    Spark(3) - Extracting, transforming, selecting features 官方文档链接:https://spark.apache.org/docs/2.2.0/m ...

  3. 【Django】将多个querysets拼接,使用djangorestframework序列化

    concern_set = models.Concern.objects.filter(user_id=1).values("concern_id") querysets = mo ...

  4. 纯粹极简的react状态管理组件unstated

    简介 unstated是一个极简的状态管理组件 看它的简介:State so simple, it goes without saying 对比 对比redux: 更加灵活(相对的缺点是缺少规则,需要 ...

  5. spring ioc 源码分析(三)--finishBeanFactoryInitialization(beanFactory)

    之前的博客专门分析了一个类到BeanDefinition的创建过程,现在分析BeanDefinition 到一个bean的创建过程:从refresh() 方法的---->finishBeanFa ...

  6. Java 实现常见内排序

    一.内排序 1.排序基本概念 (1)什么是排序? 排序指将一个数据元素集合或者序列 按照某种规则 重新排列成一个 有序的集合或者序列.分为内排序.外排序.排序算法的好坏直接影响程序的执行速度以及存储空 ...

  7. Python-如何在一个for循环中迭代多个可迭代对象?

    案例: 某班学生期末考试成绩,语文.数学.英语分别存储在3个列表中,同时迭代三个列表.,计算每个学生的总分(并行) 某年级有4个班,某次英语成绩分别记录在4个列表中,依次迭代每个列表,统计全年级高于9 ...

  8. Python正则表达式 re.sub()函数:标志位flags与参数个数问题

    这两天在写爬虫程序,涉及英文文本处理,需要规范化英文标点符号的写法.正常情况下,英文句号「.」后面需要保证有且只有一个空格,但也有例外情况,比如「i.e.」.「e.g.」.「P.S.」这种.由于无法预 ...

  9. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  10. 国产化之路-安装达梦DM8数据库

    专题目录 国产化之路-统信UOS操作系统安装 国产化之路-国产操作系统安装.net core 3.1 sdk 国产化之路-安装WEB服务器 国产化之路-安装达梦DM8数据库 国产化之路-统信UOS + ...