|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +# export ZABBIX_SERVER='https://your_zabbix_host/zabbix/' |
| 6 | +# export ZABBIX_PASSWORD='admin' |
| 7 | +# export ZABBIX_USERNAME='secretPassword' |
| 8 | +
|
| 9 | +# ./getBulkStatus.py |
| 10 | +HostID: 14062 - IP: 10.15.78.254 - Type: SNMP - Bulk: OFF |
| 11 | +HostID: 13206 - IP: 10.4.25.25 - Type: SNMP - Bulk: OFF |
| 12 | +HostID: 14063 - IP: 10.2.204.3 - Type: agent - Bulk: ON |
| 13 | +HostID: 12643 - IP: 10.10.4.1 - Type: SNMP - Bulk: OFF |
| 14 | +HostID: 14064 - IP: 172.30.11.11 - Type: agent - Bulk: ON |
| 15 | +HostID: 14064 - IP: 172.30.11.15 - Type: SNMP - Bulk: OFF |
| 16 | +HostID: 14065 - IP: 192.168.4.5 - Type: agent - Bulk: ON |
| 17 | +
|
| 18 | +# ./getBulkStatus.py SomeHost |
| 19 | +HostID: 14065 - IP: 192.168.4.5 - Type: agent - Bulk: ON |
| 20 | +
|
| 21 | +
|
| 22 | +# ./getBulkStatus.py AnotherHost | grep SNMP | grep ON |
| 23 | +HostID: 13818 - IP: 10.20.180.14 - Type: SNMP - Bulk: ON |
| 24 | +""" |
| 25 | + |
| 26 | +from zabbix.api import ZabbixAPI |
| 27 | +import sys |
| 28 | +import argparse |
| 29 | +import time |
| 30 | +import datetime |
| 31 | +import os |
| 32 | +import json |
| 33 | + |
| 34 | +# Class for argparse env variable support |
| 35 | + |
| 36 | + |
| 37 | +class EnvDefault(argparse.Action): |
| 38 | + # From https://stackoverflow.com/questions/10551117/ |
| 39 | + def __init__(self, envvar, required=True, default=None, **kwargs): |
| 40 | + if not default and envvar: |
| 41 | + if envvar in os.environ: |
| 42 | + default = os.environ[envvar] |
| 43 | + if required and default: |
| 44 | + required = False |
| 45 | + super(EnvDefault, self).__init__(default=default, required=required, |
| 46 | + **kwargs) |
| 47 | + |
| 48 | + def __call__(self, parser, namespace, values, option_string=None): |
| 49 | + setattr(namespace, self.dest, values) |
| 50 | + |
| 51 | + |
| 52 | +def jsonPrint(jsonUgly): |
| 53 | + print(json.dumps(jsonUgly, indent=4, separators=(',', ': '))) |
| 54 | + |
| 55 | + |
| 56 | +def ArgumentParser(): |
| 57 | + parser = argparse.ArgumentParser() |
| 58 | + |
| 59 | + parser.add_argument('-Z', |
| 60 | + required=True, |
| 61 | + action=EnvDefault, |
| 62 | + envvar='ZABBIX_SERVER', |
| 63 | + help="Specify the zabbix server URL ie: http://yourserver/zabbix/ (ZABBIX_SERVER environment variable)", |
| 64 | + metavar='zabbix-server-url') |
| 65 | + |
| 66 | + parser.add_argument('-u', |
| 67 | + required=True, |
| 68 | + action=EnvDefault, |
| 69 | + envvar='ZABBIX_USERNAME', |
| 70 | + help="Specify the zabbix username (ZABBIX_USERNAME environment variable)", |
| 71 | + metavar='Username') |
| 72 | + |
| 73 | + parser.add_argument('-p', |
| 74 | + required=True, |
| 75 | + action=EnvDefault, |
| 76 | + envvar='ZABBIX_PASSWORD', |
| 77 | + help="Specify the zabbix username (ZABBIX_PASSWORD environment variable)", |
| 78 | + metavar='Password') |
| 79 | + |
| 80 | + parser.add_argument('-f', |
| 81 | + required=False, |
| 82 | + help="Hostname to search", |
| 83 | + metavar='hostname') |
| 84 | + |
| 85 | + return parser.parse_args() |
| 86 | + |
| 87 | + |
| 88 | +def main(argv): |
| 89 | + # Parse arguments and build work variables |
| 90 | + args = ArgumentParser() |
| 91 | + zabbixURL = args.Z |
| 92 | + zabbixUsername = args.u |
| 93 | + zabbixPassword = args.p |
| 94 | + hostNameFilter = args.f |
| 95 | + |
| 96 | + interfaceType = { |
| 97 | + '1': 'agent', |
| 98 | + '2': 'SNMP', |
| 99 | + '3': 'IPMI', |
| 100 | + '4': 'JMX', |
| 101 | + } |
| 102 | + |
| 103 | + bulkStatus = { |
| 104 | + '0': 'OFF', |
| 105 | + '1': 'ON', |
| 106 | + } |
| 107 | + |
| 108 | + zapi = ZabbixAPI(url=zabbixURL, user=zabbixUsername, |
| 109 | + password=zabbixPassword) |
| 110 | + |
| 111 | + if (hostNameFilter): |
| 112 | + # Filter based on the cmdline argument |
| 113 | + f = {'host': hostNameFilter} |
| 114 | + hosts = zapi.host.get(search=f, output='extend', selectTags='extend') |
| 115 | + interfaces = zapi.hostinterface.get(hostids=hosts[0]['hostid']) |
| 116 | + else: |
| 117 | + interfaces = zapi.hostinterface.get() |
| 118 | + |
| 119 | + for interface in (interfaces): |
| 120 | + print('HostID: {} - IP: {} - Type: {} - Bulk: {}'.format( |
| 121 | + interface['hostid'], interface['ip'], interfaceType[interface['type']], bulkStatus[interface['bulk']])) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + main(sys.argv[1:]) |
0 commit comments