-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
322 lines (236 loc) · 8.9 KB
/
Copy pathmonitor.py
File metadata and controls
322 lines (236 loc) · 8.9 KB
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
318
319
320
############################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
# scripts that starts up a number of Tactic and monitors them
__all__ = ['TacticThread', 'TacticTimedThread', 'TacticMonitor']
import os, sys, threading, time, urllib, random
import tacticenv
tactic_install_dir = tacticenv.get_install_dir()
tactic_site_dir = tacticenv.get_site_dir()
app_server = "cherrypy"
#try:
# import setproctitle
# setproctitle.setproctitle("TACTICmaster")
#except:
# pass
from pyasm.common import Environment, Common, Date, Config
from pyasm.search import Search, DbContainer
python = Config.get_value("services", "python")
if not python:
python = os.environ.get('PYTHON')
if not python:
python = 'python'
STARTUP_EXEC = '%s "%s/src/bin/startup.py"' % (python, tactic_install_dir)
STARTUP_DEV_EXEC = '%s "%s/src/bin/startup_dev.py"' % (python, tactic_install_dir)
class TacticThread(threading.Thread):
def __init__(my, port):
my.port = port
my.end = False
my.dev_mode = False
my.num_checks = 0
my.kill_interval = 30 + random.randint(0,30)
my.kill_interval = 1
super(TacticThread,my).__init__()
def set_dev(my, mode):
my.dev_mode = mode
def run(my):
while 1:
if my.dev_mode:
exec_file = STARTUP_DEV_EXEC
else:
exec_file = STARTUP_EXEC
os.system('%s %s' % (exec_file, my.port) )
# TACTIC has exited
time.sleep(1)
if my.end:
print "Stopping port %s ..." % my.port
break
else:
print "Restarting %s ..." % my.port
def check(my):
# This will kill the TACTIC process
# This is very harsh and should be used sparingly if at all
use_restart = Config.get_value("services", "use_periodic_restart")
if use_restart in [True, 'true']:
if my.num_checks and my.num_checks % my.kill_interval == 0:
# read pid file
log_dir = "%s/log" % Environment.get_tmp_dir()
file = open("%s/pid.%s" % (log_dir,my.port), "r")
pid = file.read()
file.close()
print "Killing process: ", pid
Common.kill(pid)
#my.run()
my.num_checks += 1
return
my.num_checks += 1
start = time.clock()
try:
f = urllib.urlopen("http://localhost:%s/test" % my.port )
response = f.readlines()
f.close()
except IOError, e:
print "Tactic IOError: ", str(e)
# Kill if unresponsive ... (only on linux)
log_dir = "%s/log" % Environment.get_tmp_dir()
file = open("%s/pid.%s" % (log_dir,my.port), "r")
pid = file.read()
file.close()
print "Killing process: ", pid
Common.kill(pid)
else:
if response[0] != "OK":
my.end = True
return
'''
# check the time it took to respond
end = time.clock()
interval = end - start
# if greater than 5 second, kill the process and restart
if interval > 5:
# read pid file
log_dir = "%s/log" % Environment.get_tmp_dir()
file = open("%s/pid.%s" % (log_dir,my.port), "r")
pid = file.read()
file.close()
print "WARNING : port %s is not responsive!!" % my.port
'''
class TacticTimedThread(threading.Thread):
def __init__(my):
my.end = False
super(TacticTimedThread,my).__init__()
def check(my):
pass
def run(my):
print "starting timed thread"
# checks are done every 60 seconds
chunk = 60
# FIXME: not sure why we have to do a batch here
from pyasm.security import Batch
Batch(login_code="admin")
# get the all of the timed triggers
#search = Search("sthpw/timed_trigger")
#search.add_filter("type", "timed")
search = Search("sthpw/trigger")
search.add_filter("event", "timed")
timed_trigger_sobjs = search.get_sobjects()
timed_triggers = []
for trigger_sobj in timed_trigger_sobjs:
trigger_class = trigger_sobj.get_value("class_name")
try:
timed_trigger = Common.create_from_class_path(trigger_class)
except ImportError:
raise Exception("WARNING: [%s] does not exist" % trigger_class)
timed_triggers.append(timed_trigger)
while 1:
time.sleep(chunk)
#print "Running timer"
date = Date()
#print "utc: ", date.get_display_time()
# go through each trigger
for timed_trigger in timed_triggers:
print timed_trigger
if not timed_trigger.is_ready():
print "... not ready"
continue
if timed_trigger.is_in_separate_thread():
class xxx(threading.Thread):
def run(my):
try:
Batch()
timed_trigger._do_execute()
finally:
DbContainer.close_thread_sql()
xxx().start()
else:
timed_trigger._do_execute()
DbContainer.close_thread_sql()
if my.end:
print "Stopping timed thread"
break
class TacticMonitor(object):
def __init__(my, num_processes=None):
my.check_interval = 120
my.num_processes = num_processes
my.dev_mode = False
sql = DbContainer.get("sthpw")
# before batch, clean up the ticket with a NULL code
sql.do_update('DELETE from "ticket" where "code" is NULL;')
def set_check_interval(my, check_interval):
my.check_interval = check_interval
def set_dev(my, mode):
my.dev_mode = mode
def execute(my):
from pyasm.security import Batch
Batch(login_code="admin")
os.environ["TACTIC_MONITOR"] = "true"
if not my.num_processes:
my.num_processes = Config.get_value("services", "process_count")
if my.num_processes:
my.num_processes = int(my.num_processes)
else:
my.num_processes = 3
start_port = Config.get_value("services", "start_port")
ports_str = os.environ.get("TACTIC_PORTS")
if not ports_str:
ports_str = Config.get_value("services", "ports")
if ports_str:
ports = ports_str.split("|")
ports = [int(x) for x in ports]
else:
if start_port:
start_port = int(start_port)
else:
start_port = 8081
ports = []
for i in range(0, my.num_processes):
ports.append( start_port + i )
# create a number of processes
tactic_threads = []
#for i in range(0, my.num_processes):
for port in ports:
# start cherrypy
tactic_thread = TacticThread(port)
tactic_thread.set_dev(my.dev_mode)
tactic_thread.start()
tactic_threads.append(tactic_thread)
time.sleep(1)
#port += 1
# start up scheduled triggers
#from tactic.command import ScheduledTriggerMonitor
#ScheduledTriggerMonitor.start()
# create a separate thread for timed processes
tactic_timed_thread = TacticTimedThread()
tactic_timed_thread.start()
tactic_threads.append(tactic_timed_thread)
DbContainer.close_thread_sql()
# check each thread every 20 seconds
while 1:
end = False
try:
if my.check_interval:
time.sleep(my.check_interval)
for tactic_thread in tactic_threads:
tactic_thread.check()
else:
# FIXME: break for now (for windows service)
break
except KeyboardInterrupt, e:
print "Keyboard interrupt ... exiting Tactic"
for tactic_thread in tactic_threads:
tactic_thread.end = True
end = True
if end:
break
#print "exiting Tactic"
#sys.exit(0)
if __name__ == '__main__':
monitor = TacticMonitor()
monitor.execute()