-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwinrm_checks.py
310 lines (261 loc) · 9.57 KB
/
winrm_checks.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# -*- 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 NONINFRINGEMEsNT. 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 = "Sebastien Pasche"
maintainer = "Sebastien Pasche"
version = "0.0.1"
import json
import base64
import optparse
from pprint import pprint
class PowerShellHelpers(object):
@classmethod
def ececute_powershell(
cls,
client,
script,
debug=False
):
"""
Execute powershel `script` on `client`
This function is build to receive Base64(Json(data)) output from the
powershell script
:param client: connection
:type client: pywinrm object
:param script: powershell script to execute
:type script: str
:return: Dict with returned values
"""
response = client.run_ps(script)
if debug:
print("raw output")
print("----------")
print(response)
print(response.std_err)
if not response:
raise Exception("cannot fetch values from host")
#normalize result
result = base64.decodestring(response.std_out)
#pprint(result)
result = json.loads(result)
#pprint(result)
return result
PS_INPUT_JSON = """
#Format input
$CheckInputJsonBytes = [System.Convert]::FromBase64String("{data}")
$CheckInputJson = [System.Text.Encoding]::UTF8.GetString($CheckInputJsonBytes)
$CheckInputDaTa = $CheckInputJson | ConvertFrom-Json
"""
@classmethod
def generate_ps(
cls,
script,
input_data={}
):
"""
Generate the powershell script by resolving `{palceholder}` with `input_data` and
Constant {placeholéder}
:param input_data: data to include within the script
:type input_data: Dict
:param script: The powershell script
:type script: str
:return: Fullfiled script
"""
output_script = script
json_data = json.dumps(
input_data,
sort_keys=True
)
json_data = base64.encodestring(json_data)
data_string = cls.PS_INPUT_JSON.format(data=json_data)
output_script = output_script.format(
check_input_json=data_string
)
return output_script
class WindowsSystemHelpers(object):
PS_SCRIPT_MOUNTED_VOLUME = """
#Functions
#---------
#check input data
#----------------
{check_input_json}
#Obtain data
#-----------
$TotalGB = @{{Name="Capacity(GB)";expression={{[math]::round(($_.Capacity/ 1073741824),2)}}}}
$FreeGB = @{{Name="FreeSpace(GB)";expression={{[math]::round(($_.FreeSpace / 1073741824),2)}}}}
$UsedPerc = @{{Name="Used(%)";expression={{[math]::round(((($_.Capacity / 1073741824)-($_.FreeSpace / 1073741824))/($_.Capacity / 1073741824)*100),0)}}}}
$volumes = Get-WmiObject win32_volume | Where-object {{$_.DriveLetter -eq $null}}
$CheckOutputObj = $volumes | Select Label, $TotalGB, $FreeGB, $UsedPerc
#Format output
$CheckOuputJson = $CheckOutputObj | ConvertTo-Json
$CheckOuputJsonBytes = [System.Text.Encoding]::UTF8.GetBytes($CheckOuputJson)
$CheckOuputJsonBytesBase64 = [System.Convert]::ToBase64String($CheckOuputJsonBytes)
Write-Host $CheckOuputJsonBytesBase64
"""
@classmethod
def add_winrm_parser_options(
cls,
parser
):
parser.add_option('-H', '--hostname',
dest="hostname",
help='Hostname to connect to')
parser.add_option('-p', '--port',
dest="port", type="int", default=5986,
help='WinRM HTTP port to connect to. Default : HTTPS - 5986')
parser.add_option('-s', '--http-scheme',
dest="scheme", default="https",
help='WinRM HTTP scheme to connect to. Default : https://')
parser.add_option('-U', '--user',
dest="user", default="shinken",
help='remote use to use. By default shinken.')
parser.add_option('-P', '--password',
dest="password",
help='Password. By default will use void')
parser.add_option('--debug',
dest="debug", default=False, action="store_true",
help='Enable debug')
return parser
class MSSQLHelpers(object):
PS_SCRIPT_MSSQL_QUERY = """
#Functions
#---------
#check input data
#----------------
{check_input_json}
#Obtain data
#-----------
Import-Module sqlps -WarningAction Ignore
$CheckOutputObj = Invoke-Sqlcmd -Query $CheckInputDaTa.sql_script -SuppressProviderContextWarning -serverInstance ("localhost\\" + $CheckInputDaTa.mssqlinstance_to_check) | foreach-object {{$_.nb_db_errors}}
$CheckOutputObj = [double]$CheckOutputObj
#Format output
$CheckOuputJson = $CheckOutputObj | ConvertTo-Json
$CheckOuputJsonBytes = [System.Text.Encoding]::UTF8.GetBytes($CheckOuputJson)
$CheckOuputJsonBytesBase64 = [System.Convert]::ToBase64String($CheckOuputJsonBytes)
Write-Host $CheckOuputJsonBytesBase64
"""
PS_SCRIPT_MSSQL_COUNTER = """
#Functions
#---------
#check input data
#----------------
{check_input_json}
#Obtain data
#-----------
$CheckOutputObj = Get-counter -Counter ("\MSSQL`$" + $CheckInputDaTa.mssqlinstance_to_check + $CheckInputDaTa.perfcounter_to_check) | Select-Object -ExpandProperty CounterSamples | foreach {{$_.CookedValue}}
#Format output
$CheckOuputJson = $CheckOutputObj | ConvertTo-Json
$CheckOuputJsonBytes = [System.Text.Encoding]::UTF8.GetBytes($CheckOuputJson)
$CheckOuputJsonBytesBase64 = [System.Convert]::ToBase64String($CheckOuputJsonBytes)
Write-Host $CheckOuputJsonBytesBase64
"""
@classmethod
def add_mssql_perfmon_parser_options(
cls,
parser
):
parser.add_option('--property',
dest="property_script", default=None,
help='property id to display')
parser.add_option('-I',
dest="mssqlinstance_to_check", default="MSSQLSERVER",
help='MSSQL Instance to check')
return parser
class OutputFormatHelpers(object):
@classmethod
def perf_data_string(
cls,
label,
value,
warn,
crit,
UOM='',
min='',
max=''
):
"""
Generate perf data string from perf data input
http://docs.icinga.org/latest/en/perfdata.html#formatperfdata
:param label: Name of the measured data
:type label: str
:param value: Value of the current measured data
:param warn: Warning level
:param crit: Critical level
:param UOM: Unit of the value
:param min: Minimal value
:param max: maximal value
:return: formated perf_data string
"""
if UOM:
perf_data_template = "'{label}'={value}[{UOM}];{warn};{crit};{min};{max};"
else:
perf_data_template = "'{label}'={value};{warn};{crit};{min};{max};"
return perf_data_template.format(
label=label,
value=value,
warn=warn,
crit=crit,
UOM=UOM,
min=min,
max=max
)
@classmethod
def check_output_string(
cls,
state,
message,
perfdata
):
"""
Generate check output string with perf data
:param state: State of the check in ['Critical', 'Warning', 'OK', 'Unknown']
:type state: str
:param message: Output message
:type message: str
:param perfdata: Array of perf data string
:type perfdata: Array
:return: check output formated string
"""
if state not in ['Critical', 'Warning', 'OK', 'Unknown']:
raise Exception("bad check output state")
if not message:
message = '-'
if perfdata is not None:
if not hasattr(perfdata, '__iter__'):
raise Exception("Submited perf data list is not iterable")
perfdata_string = ''.join(' {s} '.format(s=data) for data in perfdata)
output_template = "{s}: {m} |{d}"
else:
output_template = "{s}: {m} "
perfdata_string = ''
return output_template.format(
s=state,
m=message,
d=perfdata_string
)
#toujours_tout_en_minuscul
#sauf LeNomDesCLass
#et des CONSTANTE
#_PS_SCRIPT_COUNTER
if __name__ == '__main__':
pass