-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_mounted_disk_winrm.py
267 lines (223 loc) · 7.73 KB
/
check_mounted_disk_winrm.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2013:
# Sébastien Pasche, [email protected]
# Mikael Bugnon, [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
author = "Mikael Bugnon"
maintainer = "Sebastien Pasche"
version = "0.0.1"
import optparse
import sys
import os
import traceback
import collections
from pprint import pprint
from statistics import mean
try:
import winrm
from winrm.protocol import Protocol
except ImportError:
print "ERROR : this plugin needs the local winrm lib. Please install it"
sys.exit(2)
#Ok try to load our directory to load the plugin utils.
my_dir = os.path.dirname(__file__)
sys.path.insert(0, my_dir)
try:
from winrm_checks import OutputFormatHelpers, PowerShellHelpers, WindowsSystemHelpers
except ImportError:
print "ERROR : this plugin needs the local winrm lib. Please install it"
sys.exit(2)
#DEFAULT LIMITS
#--------------
DEFAULT_WARNING = 80.0
DEFAULT_CRITICAL = 90.0
def disk_capacity_array_to_object(
disk_array,
element_to_discard
):
#If there is only one element, cast as an array
if not isinstance(disk_array, list):
disk_array = [disk_array]
return {
element['Label']: {
"Capacity(GB)": element['FreeSpace(GB)'],
"FreeSpace(GB)": element['FreeSpace(GB)'],
"Used(%)": element['Used(%)']
} for element in disk_array if element['Label'] not in element_to_discard and element['Label'] is not None
}
def disk_elements_labels_filtering(
disk_elements,
labels
):
return {
label: disk_elements[label] for label in disk_elements.keys() if label in labels
}
def disk_elements_threshold_filtering(
disk_elements,
threshold
):
return {
label : disk_usage_info
for label,disk_usage_info in disk_elements.items() if disk_usage_info['Used(%)'] >= threshold
}
# OPT parsing
# -----------
parser = optparse.OptionParser(
"%prog [options]", version="%prog " + version)
usage = """%prog [options]
Monitor mounted disk without drive letter assigned
"""
parser = WindowsSystemHelpers.add_winrm_parser_options(parser)
parser.add_option('-w', '--warning',
dest="warning", type="float",
help='Warning value for disk usage. In [%]. Default : 80.0 [%]')
parser.add_option('-c', '--critical',
dest="critical", type="float",
help='Critical value for disk usage. In [%]. Default : 90.0 [%]')
parser.add_option('--disk-labels',
dest="labels",
help='Disk labels to check on a comma separated list: "a,b,c"')
if __name__ == '__main__':
# Ok first job : parse args
opts, args = parser.parse_args()
if args:
parser.error("Does not accept any argument.")
# connection parameters
port = opts.port
hostname = opts.hostname or ''
scheme = opts.scheme
user = opts.user
password = opts.password
debug = opts.debug
labels = opts.labels
if opts.labels is not None:
labels = opts.labels.split(',')
# Try to get numeric warning/critical values
s_warning = opts.warning or DEFAULT_WARNING
s_critical = opts.critical or DEFAULT_CRITICAL
try:
# Connect to the remote host
client = winrm.Session(
'{s}://{h}:{p}'.format(
s=scheme,
h=hostname,
p=port
),
auth=(
user,
password
)
)
#prepare the script
executable_ps_script = PowerShellHelpers.generate_ps(
WindowsSystemHelpers.PS_SCRIPT_MOUNTED_VOLUME
)
if debug:
print("Script to execute")
print("-----------------")
print(executable_ps_script)
#execute the scripte
disk_usage = PowerShellHelpers.ececute_powershell(
client,
executable_ps_script,
debug
)
if debug:
print("check output")
print("------------")
pprint(disk_usage)
#Prepare all the datas
#---------------------
disk_usage_dict = disk_capacity_array_to_object(
disk_array=disk_usage,
element_to_discard=['System Reserved']
)
#check logic
#-----------
#filter on labels
if labels is not None:
disk_usage_dict = disk_elements_labels_filtering(disk_usage_dict,labels)
#filter for >= critical
disks_critical = disk_elements_threshold_filtering(
disk_elements=disk_usage_dict,
threshold=s_critical
)
#filter for >= warning disk
disks_warning = disk_elements_threshold_filtering(
disk_elements=disk_usage_dict,
threshold=s_warning
)
#prepare check output
#--------------------
status = 'OK'
disk_report_message_format="[{l}] usage >= {t}%"
message="All mounted disk usage within the limits"
if len(disks_warning):
status='Warning'
message_content = ''.join(' {s} '.format(s=label) for label in disks_warning.keys())
message=disk_report_message_format.format(
l=message_content,
t=s_warning
)
if len(disks_critical):
status='Critical'
message_content = ''.join(' {s} '.format(s=label) for label in disks_critical.keys())
message=disk_report_message_format.format(
l=message_content,
t=s_critical
)
#Assembly perf data string
#-------------------------
perf_datas = []
for label,element in disk_usage_dict.items():
perf_datas.append(
OutputFormatHelpers.perf_data_string(
label="{t}_usage".format(t=label),
value=element['Used(%)'],
warn=s_warning,
crit=s_critical,
min='0.0',
max='100.0',
UOM='%'
)
)
#print output
#------------
output = OutputFormatHelpers.check_output_string(
status,
message,
perf_datas
)
print(output)
except Exception as e:
if debug:
print(e)
the_type, value, tb = sys.exc_info()
traceback.print_tb(tb)
print("Error: {m}".format(m=e))
sys.exit(2)
finally:
if status == "Critical":
sys.exit(2)
if status == "Warning":
sys.exit(1)
sys.exit(0)