53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import sys
|
|
import json
|
|
import urllib.request
|
|
import urllib.error
|
|
|
|
def get_group_uuid(url, token, group_name):
|
|
api_url = url.rstrip('/') + "/api_jsonrpc.php"
|
|
headers = {
|
|
'Content-Type': 'application/json-rpc',
|
|
'Authorization': f'Bearer {token}'
|
|
}
|
|
|
|
# Looking for 'template_groups' (Zabbix 7.0+) or 'hostgroups'
|
|
# Try template groups first
|
|
payload = {
|
|
"jsonrpc": "2.0",
|
|
"method": "templategroup.get",
|
|
"params": {
|
|
"output": ["uuid", "name"],
|
|
"filter": {
|
|
"name": [group_name]
|
|
}
|
|
},
|
|
"id": 1
|
|
}
|
|
|
|
data = json.dumps(payload).encode('utf-8')
|
|
req = urllib.request.Request(api_url, data=data, headers=headers, method='POST')
|
|
|
|
try:
|
|
with urllib.request.urlopen(req) as response:
|
|
resp = json.loads(response.read().decode('utf-8'))
|
|
if 'result' in resp and resp['result']:
|
|
print(f"UUID: {resp['result'][0]['uuid']}")
|
|
return
|
|
else:
|
|
# Try hostgroup.get as fallback if strict 6.0/7.0 compat varies
|
|
payload['method'] = "hostgroup.get"
|
|
data = json.dumps(payload).encode('utf-8')
|
|
req = urllib.request.Request(api_url, data=data, headers=headers, method='POST')
|
|
with urllib.request.urlopen(req) as response2:
|
|
resp2 = json.loads(response2.read().decode('utf-8'))
|
|
if 'result' in resp2 and resp2['result']:
|
|
print(f"UUID: {resp2['result'][0]['uuid']}")
|
|
else:
|
|
print("NOT FOUND")
|
|
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
get_group_uuid("https://noc.itguys.com.br/", "e59ff1a478bfb6c82a5654145c65498719b448c3f7f7af9e56d1e833c42d3fef", "Templates/Applications")
|