-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsyspce_console.py
270 lines (214 loc) · 10.1 KB
/
syspce_console.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import json
import os
import re
import pprint
from prompt_toolkit import PromptSession
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import WordCompleter
from prompt_toolkit.shortcuts import CompleteStyle, prompt
from syspce_message import *
log = logging.getLogger('sysmoncorrelator')
class Console(object):
''' Console user module '''
def __init__(self, data_buffer_in,
data_condition_in,
output_lock):
self.data_buffer_in = data_buffer_in
self.data_condition_in = data_condition_in
self._running = False
self.name = 'Console'
self.module_id = Module.CONSOLE
self.output_lock = output_lock
# First message to console , current jobs started
self.send_message(MessageSubType.SHOW_JOBS,
Module.CONTROL_MANAGER, [])
self.console_history = FileHistory('history.dat')
self.session = PromptSession(history=self.console_history,
auto_suggest=AutoSuggestFromHistory(),
enable_history_search=True)
self.syspce_completer = WordCompleter(
[
"run",
"jobs",
"stop_job",
"show_config",
"show_alerts",
"stats",
"set",
"info",
"ps",
"exit",
],
meta_dict={
"run": "Run actions based on syspce config",
"jobs": "Show current active Jobs",
"set": "[var] [value] Sets config parameters",
"stop_job": "[JobId] Stops a Job by job name",
"show_config": "Show current config",
"show_alerts": "Show alerts detected",
"stats": "List statistics for hostsnames",
"info": "[pid] [eventid] [computer] Show eventid details from a process",
"ps": "[tree_id] [computer] Processes list, treeid and computer is optional",
"exit": "Bye bye",
},
ignore_case=True,
)
def run(self):
''' Thread console main code'''
self._running = True
log.debug("%s working..." % (self.name))
while self._running:
try:
#command = unicode(raw_input("SYSPCE#>"), 'utf-8')
command = self.session.prompt('SYSPCE#>',
completer=self.syspce_completer,
complete_style=CompleteStyle.MULTI_COLUMN)
except ValueError, e:
print "Input error: %s" % str(e)
command = "exit"
if (command == "jobs"):
self.send_message(MessageSubType.SHOW_JOBS,
Module.CONTROL_MANAGER, [])
elif(re.match("^run", command)):
self.send_message(MessageSubType.RUN,
Module.CONTROL_MANAGER, [])
self.s_print('Runnig actions in config...')
elif(re.match("^stats", command)):
self.send_message(MessageSubType.STATS,
Module.CONTROL_MANAGER, [])
self.s_print('Runnig stats')
elif(("show commands" in command) or ("help" in command)):
self.help()
elif(re.match("^show_config", command)):
self.send_message(MessageSubType.SHOW_CONFIG,
Module.CONTROL_MANAGER, [])
elif(re.match("^stop_job ", command)):
try:
job_name = command.split('stop_job ')[1].replace(' ','')
self.send_message(MessageSubType.STOP_JOB,
Module.CONTROL_MANAGER, [job_name])
except Exception, e:
self.s_print('Command error %s' % e)
elif(re.match("^set", command)):
c_splited = self.get_params(command)
try:
var = c_splited[1]
value = c_splited[2]
self.send_message(MessageSubType.SET_CONFIG,
Module.CONTROL_MANAGER,
[var, value])
except Exception, e:
self.s_print('Command error %s' % e)
elif(re.match("^info", command)):
try:
c_splited = self.get_params(command)
n_params = len(c_splited)
if n_params == 4:
pid = c_splited[1]
eventid = c_splited[2]
computer = c_splited[3]
elif n_params == 3:
pid = c_splited[1]
eventid = c_splited[2]
computer = ''
elif n_params == 2:
pid = c_splited[1]
eventid = '1'
computer = ''
self.send_message(MessageSubType.INFO_EVENTID,
Module.CONTROL_MANAGER,
[pid ,eventid, computer])
except Exception, e:
self.s_print('Command error %s' % e)
elif(re.match("^ps", command)):
self.s_print("\nGetting Process/Sessions list...\n")
try:
c_splited = self.get_params(command)
if len(c_splited) == 3:
tree_id = c_splited[1]
computer = c_splited[2]
else:
tree_id = '-1'
computer = ''
self.send_message(MessageSubType.PS,
Module.CONTROL_MANAGER,
[tree_id, computer])
except Exception, e:
self.s_print('Command error %s' % e)
elif(command == "exit" or command == "quit"):
self.terminate()
self.quit()
log.debug("%s terminated." % (self.name))
## COMMAND METHODS
##################
def help(self):
''' Basic console help message'''
help = '''
---------------------------------------------------------------------------------------------------
|HELP |
---------------------------------------------------------------------------------------------------
COMMANDS
--------
jobs - Show current active Jobs
stop_job <Name> - Stops a Job by job name
set <Var> <Name> - Stops a Job by job name
ps [treeid] [computer] - Processes list, treeid and computer is optional
info <pid> [eventid] [computer] - Show eventid details from a process
help - List commads helps
show_config - Show current config
show_alerts - Show alerts detected
stats - List statistics for hostnames
exit|quit - Bye bye.
'''
self.s_print(help)
def quit(self):
''' Terminate all modules and program execution
'''
self.send_message(MessageSubType.TERMINATE, Module.ENGINE_MANAGER, [])
self.send_message(MessageSubType.TERMINATE, Module.INPUT_MANAGER, [])
self.send_message(MessageSubType.TERMINATE, Module.CONTROL_MANAGER, [])
## ADDITIONAL METHODS
#####################
def send_message(self, subtype, destination, payload):
''' General method for communication with other modules
'''
message = Message(self.data_buffer_in, self.data_condition_in)
message.send(MessageType.COMMAND,
subtype,
Module.CONSOLE,
destination,
Module.CONSOLE,
payload)
def print_search_result(self, results):
self.s_print(pprint.pformat(results))
def print_alert_hierarchy(self, alerts):
for alert in alerts:
self.s_print(alert)
def print_alert_baseline(self, alerts):
self.s_print(alerts)
def print_command_result(self, result):
self.s_print(result)
def print_notification(self, results):
self.s_print("\nNOTIFICATION RES: %s" % results)
def s_print(self, string):
''' Safe print method avoiding collisions when printing out to
console
'''
self.output_lock.acquire()
print string
self.output_lock.release()
def get_params(self, commadline):
p_list = []
cl_splited = commadline.split(' ')
for param in cl_splited:
if not ' ' in param and param != '':
param = param.replace('\r', '')
p_list.append(param)
return p_list
def terminate(self):
self._running = False
log.debug("%s ending..." % (self.name))