-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzbxwmi
executable file
·153 lines (128 loc) · 4.86 KB
/
zbxwmi
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
#
"""
zbxwmi : discovery and bulk checks of WMI items with Zabbix
v0.1 : initial release
v0.2 : added "get" action
"""
import sys
import json
import logging
import wmi_client_wrapper as wmi
from zsend import ZSend
from docopt import docopt
class NullDevice():
def write(self, s):
pass
def main():
usage="""
Usage:
zbxwmi [-d] get <host> <item> <class> [-f <filter>] [-z <server>] [-D <domain>] [-U <username>] [-P <password] [-o <logfile>]
zbxwmi [-d] (bulk|discover) <host> <keys> <items> <class> [-f <filter>] [-z <server>] [-D <domain>] [-U <username>] [-P <password] [-o <logfile>]
zbxwmi [-d] <host> <keys> <items> <class> [-f <filter>] [-z <server>] [-D <domain>] [-U <username>] [-P <password] [-o <logfile>]
zbxwmi --help
zbxwmi --version
Options:
(get|bulk|discover|both) The action to take. Possible values : get, bulk, discover, both
[default: both]
<host> The host to query
-z, --zabbix <server> The Zabbix server or proxy
[default: localhost]
-v, --version Display version and exit
<keys> The keys to use as indexes for Zabbix LLD discovery
<items> The list of items to discover
<item> The item to query
<class> The class to use in the query
-f <filter>, --filter <filter> An optional filter to the query
-D <domain>, --domain <domain> The domain to use for authentication
[default: DOMAIN]
-U <username>, --username <username> The username to use for authentication
[default: wmiuser]
-P <password>, --password <password> The password to use for authentication
[default: password]
-d, --debug Debug mode, be more verbose
-o <logfile>, --output <logfile> The log file to use
"""
args = docopt(usage, version="0.1")
# Set the log file
level = logging.CRITICAL
if args['--debug']:
level = logging.DEBUG
elif args['--output']:
level = logging.INFO
else:
level = logging.CRITICAL
if level:
logging.basicConfig(
filename=args['--output'],
format='%(asctime)s - %(levelname)s - zbxwmi[%(process)s]: %(message)s',
level=level
)
# Extract the arguments
username = args['--username'] if args['--username'] else username
password = args['--password'] if args['--password'] else password
domain = args['--domain'] if args['--domain'] else domain
host = args['<host>']
keys = args['<keys>'].split(',') if args['<keys>'] else []
server = args['--zabbix']
items = args['<items>'].split(',') if args['<items>'] else args['<item>'].split(',')
cls = args['<class>']
filter = args['--filter']
if (args['get'] and len(items) <> 1):
logging.error("action 'get' requires only one item")
exit(1)
# Construct the query
query = "SELECT " + ",".join(items + keys) + " FROM " + cls
if filter:
query = query + " WHERE " + filter
# Run the query
logging.info("New query for host " + host + " : " + query)
wmic = wmi.WmiClientWrapper(
"%s\%s" % (domain, username),
password,
host,
)
try:
result = wmic.query(query)
except Exception, e:
logging.error("An error occured with WMI : " + str(e))
# print "An error occured with WMI."
exit(1)
# What to do with the results ?
if args['get']:
print str(result[0][items[0]])
elif args['bulk']:
sendToZabbix(result, keys, host, server, args['--debug'])
elif args['discover']:
showJSON(result, keys)
else: # Discover + bulk
showJSON(result, keys)
sendToZabbix(result, keys, host, server, args['--debug'])
logging.info("Done")
def sendToZabbix(data, keys, host, server, verbose = False):
"""
Bulk inserts data into Zabbix using zabbix_sender
"""
z = ZSend(server = server, verbose = verbose)
for eachItem in data:
value = eachItem[keys[0]]
[eachItem.pop(eachKey) for eachKey in keys]
for k in eachItem.keys():
z.add_data(host,"%s[%s]" % (k, value), eachItem[k])
if not verbose:
sys.stderr = NullDevice() # redirect the real STDERR
z.send(z.build_all())
def showJSON(data, keys):
"""
Display a JSON-formatted index for Zabbix LLD discovery
"""
output = []
logging.info("Creating the JSON output for Zabbix LLD discovery")
for eachItem in data:
props = {}
for i,k in enumerate(keys):
props["{#WMIINDEX" + str(i) + "}"] = eachItem[k]
output.append(dict(props))
print json.dumps({ 'data': output }, indent=4)
if __name__ == '__main__':
main()