157 lines
4.1 KiB
Python
157 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import requests
|
|
import json
|
|
import sys
|
|
import os
|
|
from urllib3.exceptions import InsecureRequestWarning
|
|
|
|
# Suppress SSL warnings if you're using self-signed certificates
|
|
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
|
|
|
|
# Configuration - set these as environment variables or modify directly
|
|
ZABBIX_URL = os.getenv('ZABBIX_URL', 'https://your-zabbix-server/api_jsonrpc.php')
|
|
ZABBIX_TOKEN = os.getenv('ZABBIX_TOKEN', 'your-api-token')
|
|
|
|
class ZabbixAPI:
|
|
def __init__(self, url, token):
|
|
self.url = url
|
|
self.token = token
|
|
self.request_id = 1
|
|
|
|
def call(self, method, params):
|
|
headers = {
|
|
'Content-Type': 'application/json-rpc',
|
|
'Authorization': f'Bearer {self.token}'
|
|
}
|
|
payload = {
|
|
'jsonrpc': '2.0',
|
|
'method': method,
|
|
'params': params,
|
|
'id': self.request_id,
|
|
}
|
|
|
|
self.request_id += 1
|
|
|
|
try:
|
|
response = requests.post(self.url, json=payload, headers=headers, verify=False)
|
|
response.raise_for_status()
|
|
result = response.json()
|
|
|
|
if 'error' in result:
|
|
print(f"API Error: {result['error']}")
|
|
return None
|
|
|
|
return result.get('result')
|
|
except Exception as e:
|
|
print(f"Request failed: {e}")
|
|
return None
|
|
|
|
def get_host_config(self, hostname):
|
|
# Get host with all related information
|
|
hosts = self.call('host.get', {
|
|
'filter': {'host': hostname},
|
|
'selectGroups': 'extend',
|
|
'selectParentTemplates': 'extend',
|
|
'selectInterfaces': 'extend',
|
|
'selectMacros': 'extend',
|
|
'selectInventory': 'extend'
|
|
})
|
|
|
|
if not hosts:
|
|
print(f"Host '{hostname}' not found")
|
|
return None
|
|
|
|
return hosts[0]
|
|
|
|
def format_host_info(host):
|
|
print("=" * 80)
|
|
print(f"HOST CONFIGURATION: {host['host']}")
|
|
print("=" * 80)
|
|
print()
|
|
|
|
# Basic info
|
|
print(f"Host ID: {host['hostid']}")
|
|
print(f"Name: {host['name']}")
|
|
print(f"Status: {'Enabled' if host['status'] == '0' else 'Disabled'}")
|
|
print()
|
|
|
|
# Host Groups
|
|
print("HOST GROUPS:")
|
|
if host['groups']:
|
|
for group in host['groups']:
|
|
print(f" - {group['name']}")
|
|
else:
|
|
print(" (none)")
|
|
print()
|
|
|
|
# Templates
|
|
print("TEMPLATES:")
|
|
if host['parentTemplates']:
|
|
for template in host['parentTemplates']:
|
|
print(f" - {template['name']}")
|
|
else:
|
|
print(" (none)")
|
|
print()
|
|
|
|
# Interfaces
|
|
print("INTERFACES:")
|
|
if host['interfaces']:
|
|
for iface in host['interfaces']:
|
|
iface_type = {
|
|
'1': 'Agent',
|
|
'2': 'SNMP',
|
|
'3': 'IPMI',
|
|
'4': 'JMX'
|
|
}.get(iface['type'], 'Unknown')
|
|
|
|
print(f" [{iface_type}] {iface['ip']}:{iface['port']}")
|
|
if iface['dns']:
|
|
print(f" DNS: {iface['dns']}")
|
|
print(f" Default: {'Yes' if iface['main'] == '1' else 'No'}")
|
|
else:
|
|
print(" (none)")
|
|
print()
|
|
|
|
# Proxy
|
|
if host.get('proxyid') and host['proxyid'] != '0':
|
|
print(f"PROXY ID: {host['proxyid']}")
|
|
else:
|
|
print("PROXY: (monitored directly by server)")
|
|
print()
|
|
|
|
# Macros
|
|
print("MACROS:")
|
|
if host['macros']:
|
|
for macro in host['macros']:
|
|
if macro.get('type') == '1': # Secret macro
|
|
value = '******'
|
|
else:
|
|
value = macro.get('value', '(no value)')
|
|
print(f" {macro['macro']} = {value}")
|
|
else:
|
|
print(" (none)")
|
|
print()
|
|
|
|
print("=" * 80)
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print(f"Usage: {sys.argv[0]} <hostname>")
|
|
sys.exit(1)
|
|
|
|
hostname = sys.argv[1]
|
|
|
|
# Initialize API
|
|
zapi = ZabbixAPI(ZABBIX_URL, ZABBIX_TOKEN)
|
|
|
|
# Get host configuration
|
|
host = zapi.get_host_config(hostname)
|
|
|
|
if host:
|
|
format_host_info(host)
|
|
else:
|
|
sys.exit(1)
|
|
|
|
if __name__ == '__main__':
|
|
main() |