-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·86 lines (69 loc) · 2.12 KB
/
main.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
#!/usr/bin/env python
# coding:utf8
# sky
from module import logger, common, stats, config
from core import install,stat_dump, health
import gevent
from gevent import monkey
import sys
import os, time
def Install():
log.log("info", "安装程序启动")
install.soft_install()
def Status():
log.log("info", "监控程序启动")
stat_dump.dump()
def Health():
log.log("info", "检查程序启动")
health.check()
if __name__ == "__main__":
monkey.patch_all()
if len(sys.argv)==2:
action=sys.argv[1]
if action=="restart":
force=1
elif action=="start":
force=0
base_dir=common.base_dir(__file__)
os.chdir(base_dir)
#os.chroot(base_dir)
# 日志
logger_config="conf/logger.yml"
log=logger.logger(logger_config)
# 获取配置
conf_config="conf/monitor.yml"
conf=config.config(conf_config)
conf_res=conf.get_monitor_conf()
pid_file=conf_res["pid"]
log.log("info", "main process: %s" % os.getpid())
# 生成pid文件
try:
if os.path.exists(pid_file):
with open(pid_file, "r", encoding="utf8") as f:
pid=int(f.read())
pid_obj=stats.soft_status(pid)
if pid_obj.pid_exist():
if force==0:
log.log("info", "监控程序(Pid: %s)正在运行, 请不要重复启动" % pid)
exit()
elif force==1:
os.kill(pid, 9)
with open(pid_file, "w", encoding="utf8") as f:
pid=str(os.getpid())
f.write(pid)
else:
with open(pid_file, "w+", encoding="utf8") as f:
pid=str(os.getpid())
f.write(pid)
except Exception as e:
log.log("error", "无法生成pid文件:", e)
exit()
# 启动本地安装和监控程序
try:
g1=gevent.spawn(Install, )
g2=gevent.spawn(Status, )
#g3=gevent.spawn(Health, )
gevent_list=[g1, g2]
gevent.joinall(gevent_list)
except Exception as e:
log.log("critical", "监控程序无法启动: %s" % e)