forked from 0x7c2/cpme2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.py
270 lines (228 loc) · 7.27 KB
/
templates.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
#
# Copyright 2020 by 0x7c2, Simon Brecht.
# All rights reserved.
# This file is part of the Report/Analytic Tool - CPme,
# and is released under the "Apache License 2.0". Please see the LICENSE
# file that should have been included as part of this package.
#
import func
import sys, time
import threading
from datetime import datetime
class check:
#
# define cpme output page
#
page = ""
#
# define category of check
#
category = ""
#
# define check title
#
title = ""
#
# is check for firewall and/or management
#
isFirewall = False
isManagement = False
isClusterXL = False
isBlade = ""
#
# minimum version
#
minVersion = 8020
supported = False
#
# executeCommand
#
isCommand = False
command = ""
commandOut = ""
commandErr = ""
runOnStartup = True
#
# predefine results
#
results = []
#
# predefine debug log
#
debug_log = {}
def __init__(self, version, isFw = False, isMgmt = False, isCluster = False, enabledBlades = [], debugLevel = 0):
if version >= self.minVersion:
if self.isFirewall and self.isFirewall == isFw:
if self.isClusterXL and self.isClusterXL == isCluster:
self.supported = True
if not self.isClusterXL:
self.supported = True
if self.isManagement and isMgmt == self.isManagement:
self.supported = True
if self.supported and self.isBlade != "":
if not self.isBlade in enabledBlades:
self.supported = False
self.debugLevel = debugLevel
self.debug(2, '-----------------------------')
self.debug(1, "Class supported: " + str(self.supported))
self.debug(2, '-----------------------------')
self.debug(2, 'Environment:')
self.debug(2, "minVersion = " + str(self.minVersion) + ", version = " + str(version))
self.debug(2, "isFirewall = " + str(self.isFirewall) + ", firewall = " + str(isFw))
self.debug(2, "isClusterXL = " + str(self.isClusterXL) + ", cluster = " + str(isCluster))
self.debug(2, "isManagement= " + str(self.isManagement)+ ", management= " + str(isMgmt))
def clear(self):
self.debug(2, "Cleaning results")
self.results = []
self.commandOut = ""
def get_command(self):
if self.isCommand:
return command
else:
return False
def get_version(self):
return self.minVersion
def get_results(self):
return self.results
def run(self):
if self.supported:
self.debug(2, 'Class is supported, running check..')
if self.commandOut == "":
if self.isCommand:
out, err = func.execute_command(self.command)
self.commandOut = out.read().split('\n')
self.commandErr = err.read().split('\n')
else:
self.commandOut = eval(self.command)
if isinstance(self.commandOut, list):
self.commandOut = list(filter(None, self.commandOut))
self.debug(4, '-----------------------------')
self.debug(4, 'commandOut:')
for o in self.commandOut:
self.debug(5, str(o))
self.debug(4, '-----------------------------')
if isinstance(self.commandErr, list):
self.commandErr = list(filter(None, self.commandErr))
self.debug(5, 'commandErr:')
for o in self.commandErr:
self.debug(5, str(o))
self.debug(5, '-----------------------------')
self.run_check()
return self.results
else:
return self.supported
def set_commandOut(self, temp):
self.commandOut = temp
def set_command(self):
pass
def add_result(self, title, state, detail = ""):
self.debug(3, 'Adding results to array, state: ' + state)
self.results.append( { 'title': title, 'state': state, 'detail': detail } )
def debug(self, level, msg):
timestamp = datetime.now()
classname = self.__class__.__name__
if not self.__class__.__name__ in self.debug_log:
self.debug_log[self.__class__.__name__] = []
if self.debugLevel >= level:
self.debug_log[self.__class__.__name__].append( { 'title': str(timestamp) + " " + classname, 'state': 'DEBUG', 'detail': msg } )
def get_log(self):
return self.debug_log[self.__class__.__name__]
class diag:
page = ""
isFirewall = False
isManagement = False
isClusterXL = False
isBlade = ""
minVersion = 8020
supported = False
isDebugCommand = True
isEnabled = False
infoTxt = ""
isVisible = False
isRunning = False
debugLevel = 0
debug_log = {}
dialog_show = False
dialog_text = ""
intro = ['', 'This is a debug page view.', 'You have to explicit enable the debug command with typing "e",', 'and can disable this debug with typing "d".', '', 'Please note:', "During enabled debugging, you can't move the focus to other pages.", '', 'Hint:', 'Please take a note of possible commands in the bottom line/legend.', '', '-------------------------', '']
content = []
isTable = False
thread = None
def __init__(self, ver = 0, isFw = False, isMgmt = False, isCluster = False, enabledBlades = [], debugLevel = 0):
if ver >= self.minVersion:
if self.isFirewall and self.isFirewall == isFw:
if self.isClusterXL and self.isClusterXL == isCluster:
self.supported = True
if not self.isClusterXL:
self.supported = True
if self.isManagement and isMgmt == self.isManagement:
self.supported = True
if self.supported and self.isBlade != "":
if not self.isBlade in enabledBlades:
self.supported = False
self.debugLevel = debugLevel
self.debug(1, "Class supported: " + str(self.supported))
if self.supported and self.isDebugCommand:
self.content = self.intro + self.content
def run(self):
try:
while (self.isEnabled or self.isVisible):
self.isRunning = True
if self.isDebugCommand and self.isEnabled:
self.run_loop()
elif not self.isDebugCommand and self.isVisible:
self.run_loop()
time.sleep(1)
self.isRunning = False
self.debug(2, "Stopping threaded debug command")
return False
except Exception as e:
print(e)
sys.exit()
def debug(self, level, msg):
timestamp = datetime.now()
classname = self.__class__.__name__
if not self.__class__.__name__ in self.debug_log:
self.debug_log[self.__class__.__name__] = []
if self.debugLevel >= level:
self.debug_log[self.__class__.__name__].append( { 'title': str(timestamp) + " " + classname, 'state': 'DEBUG', 'detail': msg } )
def get_log(self):
return self.debug_log[self.__class__.__name__]
def get_content(self):
if self.isVisible and self.thread == None:
self.debug(2, "Class is now visible and now task is running")
self.debug(2, "Lets create a task/thread for this class....")
self.thread = threading.Thread(target=self.run, args=())
self.thread.daemon = True
self.thread.start()
return self.content
def get_legend(self):
if self.isDebugCommand:
if self.isEnabled:
return "d->disable"
else:
return "e->enable"
else:
return ""
def set_keypress_super(self, key):
if self.isEnabled and (key == ord('d') or key == ord('q') or key == 27):
self.isEnabled = False
self.infoTxt = []
self.debug(1, "Disabling running debug command")
self.set_disable()
if not self.isEnabled and key == ord('e'):
self.isEnabled = True
self.infoTxt = [ "Active Debugging of Class: ", self.__class__.__name__ ]
self.debug(1, "Enabling running debug command")
self.set_enable()
self.thread = threading.Thread(target=self.run, args=())
self.thread.daemon = True
self.thread.start()
self.debug(2, "Started thread for debug command")
self.set_keypress(key)
def set_keypress(self, key):
pass
def set_enable(self):
pass
def set_disable(self):
pass