From d750ad84a5bee1b124759c9bb850b9e220f1b450 Mon Sep 17 00:00:00 2001 From: Maksym Buz Date: Thu, 23 Oct 2025 12:01:17 +0200 Subject: [PATCH] change: added host creation and removal scripts to test the loaded server --- zabbix-tests/host-cleanup.py | 68 +++++++++++++++++++++++++++++++ zabbix-tests/host-creator.py | 77 ++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100755 zabbix-tests/host-cleanup.py create mode 100755 zabbix-tests/host-creator.py diff --git a/zabbix-tests/host-cleanup.py b/zabbix-tests/host-cleanup.py new file mode 100755 index 0000000..cc41980 --- /dev/null +++ b/zabbix-tests/host-cleanup.py @@ -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() \ No newline at end of file diff --git a/zabbix-tests/host-creator.py b/zabbix-tests/host-creator.py new file mode 100755 index 0000000..fb384b7 --- /dev/null +++ b/zabbix-tests/host-creator.py @@ -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}")