-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_monitor.py
59 lines (48 loc) · 1.56 KB
/
task_monitor.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
#-*- coding: UTF-8 -*-
"""
创建于2015-10-12
@作者:陈军1654
@简介:监控集群任务
"""
import re
import os
import time
from libs.DB_Mysql import Connection as m_db
from dc_scs_jobs.RTWorker import RTWorker
from sys import path
from dc_scs_jobs import config
"""
监控实时任务的运行状况,失败时发送告警
"""
def monitor_RT_task(mdb):
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),"Begin to monitor real time tasks..."
"""创建实时任务管理类"""
RT_worker = RTWorker(mdb)
while True:
RT_worker.check()
time.sleep(1800)
def main():
#数据库链接
mdb = m_db( config.DB_HOST, config.DB_NAME, user=config.DB_USER, password=config.DB_PSWD )
"""
启动实时任务监控
"""
try:
monitor_RT_task(mdb)
except Exception, e:
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),"Fail to start real time tasks monitor: ",str(e)
raise e
# Main
if __name__ == '__main__':
cmd='ps -ef | grep "task_monitor.py"'
a=os.popen(cmd).read()
pattern=re.compile(r'task_monitor.py')
result=pattern.findall(a)
if len(result)==0:
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'task_monitor.py is not running')
elif len(result) ==3 :
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'task_monitor.py is starting'
main()
elif len(result) > 3 :
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'task_monitor.py is running'
##END