61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
#coding=utf-8
|
|
import sys
|
|
import re
|
|
import syslog
|
|
import subprocess
|
|
|
|
##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 */
|
|
|
|
MSG_PREFIX = ['EMERG', 'ALERT', 'CRIT', 'ERR', 'WARNING', 'NOTICE', 'INFO', 'DEBUG']
|
|
|
|
#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:
|
|
#if exitcode != 0:
|
|
# output = ""
|
|
print(e)
|
|
return 1, e
|
|
|
|
return exitcode, output
|
|
|
|
def tsg_hardware_reboot():
|
|
#G_LOCAL_NODE_NAME = get_local_node_name()
|
|
G_LOCAL_NODE_NAME = "TSG_MCN1"
|
|
|
|
log_handle = syslog.openlog(G_LOCAL_NODE_NAME)
|
|
msg = "[%s] %s" %(MSG_PREFIX[syslog.LOG_NOTICE], "call tsg_hardware_reboot...")
|
|
syslog.syslog(syslog.LOG_NOTICE, msg)
|
|
ret = system_cmd_run("reboot")
|
|
|
|
if ret != 0:
|
|
log_handle = syslog.openlog(G_LOCAL_NODE_NAME)
|
|
msg = "[%s] %s" %(MSG_PREFIX[syslog.LOG_NOTICE], "tsg_hardware_reboot error!")
|
|
syslog.syslog(syslog.LOG_NOTICE, msg)
|
|
print (msg)
|
|
return 1
|
|
|
|
return 0
|
|
if __name__ == '__main__':
|
|
fun_res_code = tsg_hardware_reboot()
|
|
sys.exit(fun_res_code)
|