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