当前位置:网站首页>ZABBIX API creates hosts in batches according to the host information in Excel files
ZABBIX API creates hosts in batches according to the host information in Excel files
2022-07-02 02:53:00 【Xiaobo】
example :excel The file consists of the following data : Host name - Visible name -ip Address - Port number - Host group - Mount template name

Py The code is as follows :
from openpyxl import load_workbook
import json
import urllib.request
min_line = 2 # Define the row from which to read data , Need to be revised
max_line = 9 # What line of data does the definition end in , Need to be revised
# The number of columns ( I.e. header ) Should be fixed , In this example, the data is divided into 6 Class is 6 Column , No longer define variables , Adjust according to the number of columns GetHostInfo The value used in the function Need to be revised
HostName_list = [] # Define host Hostname list
Visible_HostName_list = [] # Define the host visible name list
HostIP_list = [] # Define host IP list
HostPort_list = [] # Define host port list
HostGroup_list = [] # Define host group list
HostTemplate_list = [] # Define the host template list
class ZabbixApi:
def __init__(self): # Initialization information modify
self.url = 'http://192.10.10.xx/api_jsonrpc.php'
self.user = 'Admin'
self.passwd = 'zabbix'
self.header = {"Content-Type": "application/json-rpc"}
# self.auth = self.UserLogin()
def UserLogin(self): # api Login operation
data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": self.user,
"password": self.passwd
},
"id": 0
}
return self.UrlRequest(data)
def UrlRequest(self, data):
request = urllib.request.Request(self.url, json.dumps(data).encode('utf-8'), self.header)
result = urllib.request.urlopen(request)
response = json.loads(result.read().decode('utf-8'))
return response
def GetHostInfo(self): # take excel All host information in the file is stored in the corresponding list
wb = load_workbook('zabbix_host_add.xlsx')
ws = wb.active
for row in ws.iter_rows(min_row=min_line, max_col=6, max_row=max_line, values_only=True):
HostName_list.append(row[0])
Visible_HostName_list.append(row[1])
HostIP_list.append(row[2])
HostPort_list.append(row[3])
HostGroup_list.append(row[4])
HostTemplate_list.append(row[5])
def CreateHost(self): # Create add host
rows_number = max_line - min_line + 1
for i in range(rows_number):
# following ID Modify according to the actual value of the environment
if HostGroup_list[i] == 'zabbix_test1':
GroupID = 18
if HostGroup_list[i] == 'zabbix_test2':
GroupID = 19
if HostTemplate_list[i] == 'Template App Zabbix Server':
TemplateID = 10047
if HostTemplate_list[i] == 'Template Module Linux CPU by Zabbix agent':
TemplateID = 10272
# Pay attention to the parameters "interfaces" Fill in all required items in ,(dns Don't ignore ) Otherwise, an error will be reported 'data': 'Incorrect arguments passed to function.'
data = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": HostName_list[i],
"name": Visible_HostName_list[i],
"groups": {
"name": HostGroup_list[i],
"groupid": GroupID
},
"interfaces": {
"main": 1,
"useip": 1,
"type": 1,
"dns": "",
"port": HostPort_list[i],
"ip": HostIP_list[i]
},
"templates": {
"host": HostTemplate_list[i],
"templateid": TemplateID
}
},
"auth": token,
"id": 1
}
self.UrlRequest(data)
print(f" Creating host -{i}: {HostName_list[i]}")
if i == rows_number - 1:
return " All hosts have been created !"
if __name__ == '__main__':
zapi = ZabbixApi()
response = zapi.UserLogin()
token = response['result']
print(f' Identity token is "{token}"')
zapi.GetHostInfo()
info = zapi.CreateHost()
print(info)
After executing the script, the effect is as follows :


边栏推荐
- Kibana controls es
- LFM signal denoising, time-frequency analysis, filtering
- es面試題
- 批量检测url是否存在cdn—高准确率
- What is the difference between an intermediate human resource manager and an intermediate economist (human resources direction)?
- Which brand of sports headset is better? Bluetooth headset suitable for sports
- tarjan2
- Stack - es - official documents - filter search results
- Realize the code scanning function of a custom layout
- Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
猜你喜欢

What kind of good and cost-effective Bluetooth sports headset to buy
![[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)](/img/2e/8fe55393ccca6663d98c0b3dd9a146.png)
[learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)

Leetcode question brushing (10) - sequential question brushing 46 to 50

The basic steps of using information theory to deal with scientific problems are

多线程查询,效率翻倍

Feature query of hypergraph iserver rest Service
![[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the](/img/fe/d97b25f702bbc05f941d08147259e0.jpg)
[staff] the direction of the symbol stem and the connecting line (the symbol stem faces | the symbol stem below the third line faces upward | the symbol stem above the third line faces downward | the

Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched

Mmsegmentation series training and reasoning their own data set (3)

LFM signal denoising, time-frequency analysis, filtering
随机推荐
【带你学c带你飞】day 5 第2章 用C语言编写程序(习题2)
Batch detect whether there is CDN in URL - high accuracy
2022安全员-C证考试题及模拟考试
LFM signal denoising, time-frequency analysis, filtering
A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
STM32__ 05 - PWM controlled DC motor
旋转框目标检测mmrotate v0.3.1 学习模型
[pit] how to understand "parameter fishing"
Software testing learning notes - network knowledge
多线程查询,效率翻倍
[staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
es面試題
Basic 01: print string
【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度
What is the function of the headphone driver
MongoDB非关系型数据库
The video number will not be allowed to be put on the shelves of "0 yuan goods" in the live broadcasting room?
Face++ realizes face detection in the way of flow
超图iServer rest服务之feature查询
Oracle creates a user with read-only permission in four simple steps