当前位置:网站首页>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 :


边栏推荐
- JVM面试篇
- 2022 safety officer-c certificate examination questions and mock examination
- Start a business
- [punch in questions] integrated daily 5-question sharing (phase II)
- 結婚後
- C return multiple values getter setter queries the database and adds the list return value to the window
- STM32__ 05 - PWM controlled DC motor
- [untitled]
- Rotating frame target detection mmrotate v0.3.1 learning model
- QT使用sqllite
猜你喜欢

2022-2028 global military computer industry research and trend analysis report

Multi threaded query, double efficiency

Jvm-01 (phased learning)

MMSegmentation系列之训练与推理自己的数据集(三)

2022-2028 global wood vacuum coating machine industry research and trend analysis report

Batch detect whether there is CDN in URL - high accuracy

Pat a-1165 block reversing (25 points)

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

Which brand of sports headset is better? Bluetooth headset suitable for sports

【JVM】创建对象的流程详解
随机推荐
Rotating frame target detection mmrotate v0.3.1 learning model
Special symbols in SAP ui5 data binding syntax, and detailed explanation of absolute binding and relative binding concepts
What are the common proxy servers and what are the differences?
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
LFM信号加噪、时频分析、滤波
2022 low voltage electrician test question simulation test question bank simulation test platform operation
Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度
Duplicate keys detected: ‘0‘. This may cause an update error. found in
Après le mariage
小米青年工程师,本来只是去打个酱油
oracle创建只读权限的用户简单四步走
[road of system analyst] collection of wrong topics in enterprise informatization chapter
【无标题】
【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
[JVM] detailed description of the process of creating objects
Redis cluster
Use the open source project [banner] to achieve the effect of rotating pictures (with dots)
[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)