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
import os
import sys
import syslog
import subprocess
import time
import re
from sys import path
path.append(r'../py_common') #将存放module的路径添加进来
from common_modules_operator import *
from common_whoami import *
from common_modules_deploy import *
##define KERN_EMERG "<0>" /* system is unusable */
##define KERN_ALERT "<1>" /* action must be taken immediately */
##define KERN_CRIT "<2>" /* critical conditions */
##define KERN_ERR "<3>" /* error conditions */
##define KERN_WARNING "<4>" /* warning conditions */
##define KERN_NOTICE "<5>" /* normal but significant condition */
##define KERN_INFO "<6>" /* informational */
##define KERN_DEBUG "<7>" /* debug-level messages */
G_SYS_LOG_STRING = ['EMERG', 'ALERT', 'CRIT', 'ERR', 'WARNING', 'NOTICE', 'INFO', 'DEBUG']
G_LOCAL_NODE_NAME = "TSG_MXN"
class CommandException(Exception):
pass
def tsg_restart_err_log(error_num, user_msg):
msg = "[%s] %s" %(G_SYS_LOG_STRING[syslog.LOG_ERR], user_msg)
syslog.syslog(syslog.LOG_ERR, msg)
print (msg)
msg = "[%s] %s" %(G_SYS_LOG_STRING[syslog.LOG_ERR], "tsg software reboot error")
syslog.syslog(syslog.LOG_ERR, msg)
print (msg)
sys.exit(error_num)
def tsg_restart_succ_log():
msg = "[%s] %s" %(G_SYS_LOG_STRING[syslog.LOG_NOTICE], "tsg software reboot success")
syslog.syslog(syslog.LOG_NOTICE, msg)
print (msg)
sys.exit(0)
#return exitcode value + output message:
# 0: succ
# 1: error
def system_cmd_run(cmd_str):
dangerous_cmd = {"rm", "mv", "poweroff", "shutdown"}
for cmd in dangerous_cmd:
pattern = "\s*%s" %(cmd)
match_str = re.match(pattern, cmd_str)
if not match_str is None:
print("can't run this cmd:%s" %(cmd_str))
sys.exit(1)
try:
exitcode, output = subprocess.getstatusoutput(cmd_str)
except Exception as e:
print(e)
print("###### %s" %(e.message))
#if exitcode != 0:
# output = ""
return 1, e.message
return exitcode, output
#return value:
# 1: progcess of prog_name is exist
# 0: progcess of prog_name is not exist
def tsg_check_process_health_by_ps(module_name):
cmd_str = "ps -afx | grep %s | grep -v grep" %(module_name)
exitcode, output = system_cmd_run(cmd_str)
if exitcode == 0:
return 1
return 0
#return value:
# 1: progcess of prog_name is exist
# 0: progcess of prog_name is not exist
def tsg_check_process_health_by_systemctl_status(module_name):
print("systemctl_status check method TODO!")
sys.exit(1)
return 0
def tsg_kill_app_process_by_killall(module_name, extra_progs):
#todo , stop sapp, xxx, check process exist or not, maybe zombie, maybe very slow
command = "killall -9 %s %s" %(module_name, extra_progs)
try:
exitcode, output = subprocess.getstatusoutput(command)
#print("%d" %(exitcode))
except Exception as e:
pass
if exitcode != 0:
return 1
return 0
def tsg_kill_app_process_by_systemctl_stop(module_name):
print("tsg_kill_app_process_by_systemctl_stop TODO!!!")
sys.exit(1)
return 0
def tsg_stop_app_process(module_name, extra_progs, stop_func, check_func):
res_code = 0
running_flag = 0
running_flag = check_func(module_name)
if running_flag == 0:
logger.debug("%s is not running, start it..." %(module_name))
return 0
#ready to stop progcess, retry for 3 times
for times in range(3):
stop_func(module_name, extra_progs)
#此处不判断stop_func的返回值, 可能程序不存在, 可能守护不存在,等原因
#直接用check()方法检测stop()的成功
res_code = tsg_check_process_health_by_ps(module_name)
if res_code != 0:
continue
else:
break
if res_code != 0:
errmsg = "can't stop process %s" %(module_name)
logger.error(errmsg)
tsg_restart_err_log(res_code, errmsg)
return res_code
def tsg_start_app_process_by_exec_call(module_name, module_cwd, module_exe):
logger.debug("try cd to dir:%s" %(module_cwd))
try:
os.chdir(module_cwd)
except Exception as e:
print("%s" %(e))
return 1
logger.debug("try call exec :%s" %(module_exe))
cmd_str = "./%s" %(module_exe)
ret_code, output = system_cmd_run(cmd_str)
if ret_code != 0:
errmsg = "start program %s error, call %s/%s failed" %(module_name, module_cwd, module_exe)
tsg_restart_err_log(ret_code, errmsg)
return 0
def tsg_start_app_process_by_systemctl_start(module_name, module_cwd, module_exe, check_method):
print("tsg_start_app_process_by_systemctl_start TODO!!!!!")
sys.exit(1)
def tsg_start_app_process(module_name, module_cwd, module_exe, start_func, check_func):
ret = start_func(module_name, module_cwd, module_exe)
if ret != 0:
return 1
running_flag = check_func(module_name)
if running_flag == 0:
errmsg = "start process %s error" %(module_name)
return 1
return 0
#参数说明:
#
# module_name: 模块名称
# extra_progs: 其他需要kill的附加程序, 如sapp的r3守护, 需要杀掉, 否则后台可能会重复启动sapp
# module_cwd: 应用的绝对路径
# module_exe: 启动应用的名称, 可能跟module_name不一样, 比如用r2启动sapp
# stop_method: 停止应用方法
# start_method: 启动应用方法
# check_method: 检测应用是否运行方法
#
def tsg_restart_app_process(module_name, extra_progs, module_cwd, module_exe, start_func, stop_func, check_func):
res_code = 0
res_code = tsg_stop_app_process(module_name, extra_progs, stop_func, check_func)
if res_code != 0:
return res_code
res_code = tsg_start_app_process(module_name, module_cwd, module_exe, start_func, check_func)
if res_code != 0:
return res_code
return 0
#根据配置文件的参数, 选择用那种操作函数继续下一步
#返回值, 函数指针:
#ret_code, start_fun, stop_fun, check_health_fun
def tsg_get_operator_by_config(module_operator):
if module_operator[TSG_OP_MODULE_START_INDEX] == 'exec':
start_func = tsg_start_app_process_by_exec_call
elif module_operator[TSG_OP_MODULE_START_INDEX] == 'systemctl_start':
start_func = tsg_start_app_process_by_systemctl_start
else:
errmsg = "not support start method:%s, only be [exec, systemctl_start]" %(module_operator[TSG_OP_MODULE_START_INDEX])
tsg_restart_err_log(1, errmsg)
if module_operator[TSG_OP_MODULE_STOP_INDEX] == 'killall':
stop_func = tsg_kill_app_process_by_killall
elif module_operator[TSG_OP_MODULE_STOP_INDEX] == 'systemctl_stop':
stop_func = tsg_kill_app_process_by_systemctl_stop
else:
errmsg = "not support stop method:%s, only be [killall, systemctl_stop]" %(module_operator[TSG_OP_MODULE_STOP_INDEX])
tsg_restart_err_log(1, errmsg)
if module_operator[TSG_OP_MODULE_STATUS_INDEX] == 'ps':
check_func = tsg_check_process_health_by_ps
elif module_operator[TSG_OP_MODULE_STATUS_INDEX] == 'systemctl_status':
check_func = tsg_check_process_health_by_systemctl_status
else:
errmsg = "not support check method:%s, only be [ps, systemctl_status]" %(module_operator[TSG_OP_MODULE_STATUS_INDEX])
tsg_restart_err_log(1, errmsg)
return 0, start_func, stop_func, check_func
def tsg_software_reboot():
#G_LOCAL_NODE_NAME = get_local_node_name()
log_handle = syslog.openlog(G_LOCAL_NODE_NAME)
sled_type,sled_id,sled_name = tsg_whoami()
if sled_name == "":
tsg_restart_err_log(1, "can't get local sled name")
sys.exit(1)
module_array = tsg_get_local_sled_modules(sled_name)
if len(module_array) <= 0:
tsg_restart_err_log(1, "can't get local sled modules")
sys.exit(1)
logger.debug("len(module_array) = %d" %(len(module_array)))
for module_name in module_array:
module_operator = tsg_get_module_opertor(module_name)
if len(module_operator) <= 0:
tsg_restart_err_log(1, "can't get local sled module operator for %s" %(module_name))
sys.exit(1)
ret, start_func, stop_func, check_func = tsg_get_operator_by_config(module_operator)
if ret != 0:
tsg_restart_err_log(1, "can't get operator for %s" %(module_operator[TSG_OP_MODULE_NAME_INDEX]))
sys.exit(1)
tsg_restart_app_process(module_operator[TSG_OP_MODULE_NAME_INDEX], module_operator[TSG_OP_MODULE_EXTRA_INDEX],
module_operator[TSG_OP_MODULE_CWD_INDEX],
module_operator[TSG_OP_MODULE_EXE_INDEX],
start_func, stop_func, check_func)
#tsg_restart_app_process("sapp", "r3", "/home/tsg/kni", "r2", "killall", "exec_call", "ps")
#tsg_restart_app_process("telegraf", "systemctl_stop", "systemctl_start", "systemctl_status")
#tsg_restart_app_process("marsio", "systemctl_stop", "systemctl_start", "systemctl_status")
#tsg_restart_app_process("influxd", "systemctl_stop", "systemctl_start", "systemctl_status")
tsg_restart_succ_log()
if __name__ == '__main__':
global logger
if len(sys.argv) >= 2 and sys.argv[1] == "debug":
logger = logger_init(logging.DEBUG)
else:
logger = logger_init(logging.CRITICAL)
tsg_software_reboot()
|