当前位置:网站首页>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 :
边栏推荐
- Face++ realizes face detection in the way of flow
- 设置状态栏颜色
- Ten minutes will take you in-depth understanding of multithreading - multithreaded teamwork: synchronous control
- 自定义组件的 v-model
- [JSON] gson use and step on the pit
- QT uses sqllite
- [staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
- AcWing 245. Can you answer these questions (line segment tree)
- Golang configure export goprivate to pull private library code
- Multi threaded query, double efficiency
猜你喜欢
Missing numbers from 0 to n-1 (simple difficulty)
Query word weight, search word weight calculation
Pat a-1165 block reversing (25 points)
Deployment practice and problem solving of dash application development environment based on jupyter Lab
How to develop digital collections? How to develop your own digital collections
Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
Xiaomi, a young engineer, was just going to make soy sauce
Vsocde has cli every time it is opened js
[road of system analyst] collection of wrong topics in enterprise informatization chapter
query词权重, 搜索词权重计算
随机推荐
QT implementation interface jump
How to develop digital collections? How to develop your own digital collections
GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面
[learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
自定义组件的 v-model
Baohong industry | 6 financial management models at different stages of life
【无标题】
Kibana controls es
2022-2028 global deep sea generator controller industry research and trend analysis report
[staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)
2022-2028 global military computer industry research and trend analysis report
buu_ re_ crackMe
Cesium dynamic diffusion point effect
Deployment practice and problem solving of dash application development environment based on jupyter Lab
离婚3年以发现尚未分割的共同财产,还可以要么
What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
2022-2028 global encryption software industry research and trend analysis report
Missing numbers from 0 to n-1 (simple difficulty)
Systemserver service and servicemanager service analysis
[learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table