change: added host creation and removal scripts to test the loaded server

This commit is contained in:
2025-10-23 12:01:17 +02:00
parent ae4e83dafe
commit d750ad84a5
2 changed files with 145 additions and 0 deletions

68
zabbix-tests/host-cleanup.py Executable file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/env python3
import requests
import json
import time
# === CONFIGURATION ===
ZABBIX_URL = "http://10.0.0.101:8887/api_jsonrpc.php"
ZABBIX_TOKEN = "c785634354e760a6843055ba4581bc7b6cd6eb2ec75f7c2a79f251c1719933f7"
GROUP_ID = "19"
BATCH_SIZE = 100
HOST_PATTERN = "dummy-host-"
HEADERS = {
"Content-Type": "application/json-rpc",
"Authorization": f"Bearer {ZABBIX_TOKEN}"
}
def zbx_request(method, params):
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": int(time.time())
}
r = requests.post(ZABBIX_URL, headers=HEADERS, data=json.dumps(payload))
r.raise_for_status()
resp = r.json()
if "error" in resp:
raise Exception(f"API error: {resp['error']}")
return resp
def cleanup_hosts():
# Get all hosts in the group
resp = zbx_request("host.get", {
"groupids": [GROUP_ID],
"output": ["hostid", "host"]
})
# Filter hosts that contain the dummy pattern
hosts = [h for h in resp.get("result", []) if HOST_PATTERN in h["host"]]
if not hosts:
print("No dummy hosts found")
return
print(f"Deleting {len(hosts)} hosts")
# Delete in batches
host_ids = [h["hostid"] for h in hosts]
total_deleted = 0
for i in range(0, len(host_ids), BATCH_SIZE):
batch = host_ids[i:i + BATCH_SIZE]
try:
resp = zbx_request("host.delete", batch)
deleted = len(resp.get("result", {}).get("hostids", []))
total_deleted += deleted
print(f"Deleted batch {i//BATCH_SIZE + 1}: {deleted} hosts")
except Exception as e:
print(f"Error in batch {i//BATCH_SIZE + 1}: {e}")
if i + BATCH_SIZE < len(host_ids):
time.sleep(0.5)
print(f"Total deleted: {total_deleted}")
if __name__ == "__main__":
cleanup_hosts()

77
zabbix-tests/host-creator.py Executable file
View File

@@ -0,0 +1,77 @@
#!/usr/bin/env python3
import requests
import json
import time
# === CONFIGURATION ===
ZABBIX_URL = "http://10.0.0.101:8887/api_jsonrpc.php"
ZABBIX_TOKEN = "c785634354e760a6843055ba4581bc7b6cd6eb2ec75f7c2a79f251c1719933f7"
PROXY_GROUP_ID = "1" # your proxy group ID
GROUP_ID = "19" # host group for these hosts
NUM_HOSTS = 1000 # number of hosts to create
BATCH_SIZE = 100 # how many to create per call
HEADERS = {
"Content-Type": "application/json-rpc",
"Authorization": f"Bearer {ZABBIX_TOKEN}"
}
def zbx_request(method, params):
"""Send Zabbix API request using Bearer token authentication."""
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": int(time.time())
}
r = requests.post(ZABBIX_URL, headers=HEADERS, data=json.dumps(payload))
r.raise_for_status()
resp = r.json()
if "error" in resp:
raise Exception(f"Zabbix API error: {resp['error']}")
return resp
def create_hosts():
hosts = []
for i in range(1, NUM_HOSTS + 1):
host_name = f"dummy-host-{i:04d}"
host = {
"host": host_name,
"interfaces": [{
"type": 1, # Zabbix agent
"main": 1,
"useip": 1,
"ip": "127.0.0.1",
"dns": "",
"port": "10050"
}],
"groups": [{"groupid": GROUP_ID}],
"templates": [{"templateid": "10048"}], # assign template ID 10048
"monitored_by": 2, # 2 = proxy group
"proxy_groupid": PROXY_GROUP_ID, # your proxy group ID
"items": [{
"name": "Dummy metric",
"key_": "dummy.value",
"type": 2, # trapper
"value_type": 0, # numeric float
"delay": "60s"
}]
}
hosts.append(host)
for i in range(0, len(hosts), BATCH_SIZE):
batch = hosts[i:i + BATCH_SIZE]
print(f"Creating hosts {i+1}{i+len(batch)}...")
try:
resp = zbx_request("host.create", batch)
created = len(resp.get("result", {}).get("hostids", []))
print(f"✅ Created {created} hosts.")
except Exception as e:
print(f"❌ Error in batch {i+1}{i+len(batch)}: {e}")
time.sleep(1)
if __name__ == "__main__":
try:
create_hosts()
except Exception as e:
print(f"Fatal error: {e}")