-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor_tool_gui.py
317 lines (268 loc) · 9.79 KB
/
monitor_tool_gui.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
311
312
313
314
315
316
317
from Tkinter import *
from ScrolledText import ScrolledText
import tkMessageBox
import logging
import os
import time
import threading
from multiprocessing import Process
from Queue import Queue
import utility
from monitor_tool import monitor
from monitor_tool import verify_email
log = logging.getLogger(__name__)
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(filename=os.path.join(os.getcwd(),'monitor_tool.log'),
filemode='w',
level=logging.DEBUG,
format=FORMAT)
def email_verify(event):
hostname = e1.get()
username = e2.get()
password = e3.get()
if len(hostname) == 0:
tkMessageBox.showinfo('Message', 'please input smtp server')
elif len(username) == 0:
tkMessageBox.showinfo('Message', 'please input QQ email user')
elif len(password) == 0:
tkMessageBox.showinfo('Message', 'please input QQ email passwd')
elif verify_email(hostname,username,password):
tkMessageBox.showinfo('Message', 'email is okay')
else:
tkMessageBox.showerror('Message', 'Error: smtp email is not working.\n\
1. check the user/passwd is right\n\
2. check your QQ email setting, make sure POP3/SMTP is supported\n\
3. turn off your symantec firewall, then try again')
def start_monitor():
from_smtp = e1.get()
from_user = e2.get()
from_passwd = e3.get()
to_user = e4.get()
ip = e6.get()
user = e7.get()
passwd = e8.get()
command = e9.get()
log_path = e10.get()
frequency = float(e11.get())
if frequency > 15 or frequency < 0:
log.error ('Error: The frequency should be between 0 and 15')
e11.delete(0, END)
e11.insert(0, '5')
return
if c1State.get():
report = report_time_table[w1State.get()]
log.info ("Report progress enabled, schedule: every %s sec" % report)
else:
report = 0
m = monitor(ip,user,passwd,command,log_path,from_smtp,from_user,from_passwd,to_user,frequency,report)
log.info ('%s started' % threading.current_thread().name)
result = m.run()
input = [command, result]
queue.put(input)
def queue_consumer():
master.after(2000,queue_consumer)
if not queue.empty():
output = queue.get()
if output[1] == True:
tkMessageBox.showinfo('Message', '%s already finish, check the email for result' % output[0])
else:
tkMessageBox.showerror('Message', '%s monitor failed: %s' % (output[0], output[1]))
num = threading.activeCount() - 1 #explude main thread
for item in threading.enumerate():
if str(item).find('paramiko') >= 0:
num = num - 1
if num:
string = utility.getmovie("running")
numThread.set("Total %d monitor job is %s" % (num,string))
else:
numThread.set("No monitor job is running ")
def start_process(event):
log.debug ("current thread total numbers: %d" % threading.activeCount())
response = tkMessageBox.askokcancel('Message', 'Will start monitor script %s, please click OK to continue, or cancel' % e9.get())
if response == False:
return
th = threading.Thread(target=start_monitor, name="Monitor-%s" % (e9.get()))
th.setDaemon(True)
th.start()
def export():
config = []
config.append(e1.get())
config.append(e2.get())
config.append(e3.get())
config.append(e4.get())
config.append(e6.get())
config.append(e7.get())
config.append(e8.get())
config.append(e9.get())
config.append(e10.get())
config.append(e11.get())
if utility.export_config(config) == True:
tkMessageBox.showinfo('Message', 'export finish')
def load():
config = utility.load_config()
if config == None:
tkMessageBox.showerror('Message', 'Configuration file not existed, please input manually')
else:
e1.delete(0, END)
e1.insert(0, config[0].strip())
e2.delete(0, END)
e2.insert(0, config[1].strip())
e3.delete(0, END)
e3.insert(0, config[2].strip())
e4.delete(0, END)
e4.insert(0, config[3].strip())
e6.delete(0, END)
e6.insert(0, config[4].strip())
e7.delete(0, END)
e7.insert(0, config[5].strip())
e8.delete(0, END)
e8.insert(0, config[6].strip())
e9.delete(0, END)
e9.insert(0, config[7].strip())
e10.delete(0, END)
e10.insert(0, config[8].strip())
e11.delete(0, END)
e11.insert(0, config[9].strip())
tkMessageBox.showinfo('Message', 'import finish')
def readme():
popup()
class redirect(object):
def __init__(self, text):
self.output = text
def write(self, string):
self.output.insert(END, string)
self.output.see("end")
class popup(Toplevel):
def __init__(self):
Toplevel.__init__(self)
self.geometry('500x400')
Label(self, text = 'Read Me').pack()
text= ScrolledText(self, bg="lightgray")
text['font'] = ('console','10')
text.insert(END," welcome to monitor tool\n\n \
Here is some notes which will help you use monitor tool\n\n\
1. About SMTP setting, please input your smtp user/passwd information, till now only QQ email smtp is varified\
for other smtp emails can not garantee. also you should open POP3/SMTP setting, the passwd is grant access passwd\
you can click verity email button to check the input smtp server is okay or not\n\n\
2. About server information, please input the target server ip/user/passwd and command keyword which will be used\
to search its pid, ps -ef | grep <command keyword>. also input the command log path, which will be used to fetch command\
result, the frequency(mins), default value is 5 mins, means monitor will ssh to server every 5 mins to fetch pid status\n\n\
3. About the email to address, you can add more then one email address into this enty, like ***@qq.com;***@ericsson.com\n\n\
4. About Enable report progress, if want to know the script progress every certain period(30 mins, 1 hour...), you can select this checkbutton\
the monitor will fetch script log and send out by email")
text.see("end")
text.pack()
text.config(state=DISABLED)
def callCheckbutton():
if c1State.get():
w1.config(state=NORMAL)
log.info ("Select %s" % c1['text'])
log.info ("Monitor will send script log(tail -10) by email every 30 minutes")
else:
w1.config(state=DISABLED)
log.info ("Unselect %s" % c1['text'])
master = Tk()
master.title('Script Monitor Tool')
#------ Label Entry defination---------#
# Email from information
Label(master, text="Email From:").grid(row=0, column=0, sticky="w")
Label(master, text="Smtp server:").grid(row=1, column=1, sticky="w")
e1 = Entry(master)
e1.insert(0, 'smtp.qq.com')
e1.grid(row=1, column=2,sticky="ew")
Label(master, text="User:").grid(row=2, column=1, sticky="w")
e2 = Entry(master)
e2.grid(row=2, column=2,sticky="ew")
Label(master, text="Passwd:").grid(row=3, column=1, sticky="w")
e3 = Entry(master)
e3['show'] = '*'
e3.grid(row=3, column=2,sticky="ew")
# Email to information
Label(master, text="Email To:").grid(row=5, column=0, sticky="w")
e4 = Entry(master)
e4.grid(row=5, column=2, sticky="ew")
#e5 = Entry(master)
#e5.grid(row=6, column=2)
Label(master, text="Example: ***@qq.com;***@ericsson.com").grid(row=6, column=1, columnspan=2, sticky="w")
numThread = StringVar()
Label(master, textvariable=numThread,fg="red").grid(row=7, column=0, columnspan=3, sticky="w")
# Monitor Server information
Label(master, text="Server Information:").grid(row=0, column=3, sticky="w")
Label(master, text="IP address:").grid(row=1, column=4, sticky="w")
e6 = Entry(master)
e6.grid(row=1, column=5,sticky="ew")
Label(master, text="User:").grid(row=2, column=4, sticky="w")
e7 = Entry(master)
e7.grid(row=2, column=5,sticky="ew")
Label(master, text="Passwd:").grid(row=3, column=4, sticky="w")
e8 = Entry(master)
e8['show'] = '*'
e8.grid(row=3, column=5,sticky="ew")
Label(master, text="Command:").grid(row=4, column=4, sticky="w")
e9 = Entry(master)
e9.grid(row=4, column=5,sticky="ew")
Label(master, text="Log path:").grid(row=5, column=4, sticky="w")
e10 = Entry(master)
e10.grid(row=5, column=5,sticky="ew")
Label(master, text="Frequency\n(minutes):").grid(row=6, column=4, rowspan=1, sticky="w")
e11 = Entry(master)
e11.insert(0, '5')
e11.grid(row=6, column=5, sticky="w")
e11['width'] = 3
#------- Buttion defination ----------#
# Verity email address
b1 = Button(master,text="Verify email")
b1.grid(row=4, column=1)
b1.bind("<ButtonRelease>",email_verify)
# Start button
b2 = Button(master, text="Start monitor")
b2['background'] = 'green'
b2['width'] = '15'
b2.grid(row=7, column=6)
b2.bind("<ButtonRelease>",start_process)
#------- Checkbutton defination ----------#
c1State = IntVar()
c1 = Checkbutton(master, variable=c1State, text = "Enable report progress", command=callCheckbutton)
c1.grid(row=1, column=6)
report_time_table = {
"2 hours" : 120 * 60,
"1.5 hours" : 90 * 60,
"1 hour" : 60 * 60,
"30 mins" : 30 * 60
}
w1State = StringVar()
w1State.set("30 mins") # default value
w1 = apply(OptionMenu, (master, w1State) + tuple(report_time_table.keys()))
w1.grid(row=2, column=6)
w1.config(state=DISABLED)
#--------Text scrollar defination--------------#
s1= ScrolledText(master)
s1['font'] = ('console','10')
s1.grid(row=8,columnspan=8,sticky="ew")
redir = redirect(s1)
sys.stdout = redir
sys.stderr = redir
console = logging.StreamHandler(sys.stdout)
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
#log.addHandler(console)
#------- Menu defination -------------#
# parent menu
menubar = Menu()
# child menu
fmenu = Menu()
fmenu.add_command(label = 'Export config', command = export)
fmenu.add_command(label = 'Import config', command = load)
fmenu.add_command(label = 'Exit', command = master.quit)
hmenu = Menu()
hmenu.add_command(label = 'Reame', command = readme)
# add menu tree
menubar.add_cascade(label = 'File', menu = fmenu)
menubar.add_cascade(label = 'Help', menu = hmenu)
# add menu to wget
master['menu'] = menubar
queue = Queue()
master.after(2000,queue_consumer)
master.columnconfigure(2, weight=1)
master.columnconfigure(5, weight=1)
master.mainloop()