commit 6d477839ae0a1a91f993ecbcd8a430f8b11ec6a0 Author: lijia Date: Tue Jul 23 11:56:52 2019 +0800 add new files. diff --git a/cp.sh b/cp.sh new file mode 100644 index 0000000..d603c68 --- /dev/null +++ b/cp.sh @@ -0,0 +1 @@ +scp -P 51022 py_bin/* 202.43.148.184:/opt/tsg/bin/ diff --git a/pkg.sh b/pkg.sh new file mode 100644 index 0000000..911b210 --- /dev/null +++ b/pkg.sh @@ -0,0 +1,4 @@ +rm -rf ./py_temp/* +pyinstaller -F -y --clean --noconsole --distpath ./py_bin --workpath ./py_temp --specpath ./py_temp ./py_src/tsg_software_reboot.py +pyinstaller -F -y --clean --noconsole --distpath ./py_bin --workpath ./py_temp --specpath ./py_temp ./py_src/tsg_hardware_reboot.py +pyinstaller -F -y --clean --noconsole --distpath ./py_bin --workpath ./py_temp --specpath ./py_temp ./py_src/tsg_diagnose.py diff --git a/py_bin/.gitignore b/py_bin/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/py_src/__pycache__/tsg_diagnose.cpython-36.pyc b/py_src/__pycache__/tsg_diagnose.cpython-36.pyc new file mode 100644 index 0000000..ddf5987 Binary files /dev/null and b/py_src/__pycache__/tsg_diagnose.cpython-36.pyc differ diff --git a/py_src/__pycache__/tsg_hardware_reboot.cpython-36.pyc b/py_src/__pycache__/tsg_hardware_reboot.cpython-36.pyc new file mode 100644 index 0000000..b7d54d6 Binary files /dev/null and b/py_src/__pycache__/tsg_hardware_reboot.cpython-36.pyc differ diff --git a/py_src/__pycache__/tsg_software_reboot.cpython-36.pyc b/py_src/__pycache__/tsg_software_reboot.cpython-36.pyc new file mode 100644 index 0000000..dab3160 Binary files /dev/null and b/py_src/__pycache__/tsg_software_reboot.cpython-36.pyc differ diff --git a/py_src/tsg_diagnose.py b/py_src/tsg_diagnose.py new file mode 100644 index 0000000..6f1763d --- /dev/null +++ b/py_src/tsg_diagnose.py @@ -0,0 +1,135 @@ +import sys +import syslog +import random + +#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'] + +def tsg_diagnose_for_app(): + app_msg = ['app is not running', 'app is not running', 'app is not running', 'app maybe deadlock', 'app is reboot in 5 minute'] + app_msg.append('app is running normally') + app_msg.append('app is running normally') + app_msg.append('app is running normally') + + num = random.randint(1,7) + if num <= 4: + msg = "[%s] %s" %(MSG_PREFIX[num], app_msg[num]) + syslog.syslog(num, msg) + print(msg) + return num + +def tsg_diagnose_for_cpu(): + cpu_msg = [] + cpu_msg.append('CPU usage over 95%') + cpu_msg.append('CPU usage over 90%') + cpu_msg.append('CPU usage over 85%') + cpu_msg.append('CPU usage over 80%') + cpu_msg.append('CPU usage over 70%') + + num = random.randint(1,7) + if num <= 4: + msg = "[%s] %s" %(MSG_PREFIX[num], cpu_msg[num]) + syslog.syslog(num, msg) + print(msg) + return num + + +def tsg_diagnose_for_mem(): + mem_msg = [] + mem_msg.append('free memory less than 1%') + mem_msg.append('free memory less than 5%') + mem_msg.append('free memory less than 10%') + mem_msg.append('free memory less than 15%') + mem_msg.append('free memory less than 20%') + + num = random.randint(1,7) + if num <= 4: + msg = "[%s] %s" %(MSG_PREFIX[num], mem_msg[num]) + syslog.syslog(num, msg) + print(msg) + return num + + +def tsg_diagnose_for_disk(): + disk_msg = [] + disk_msg.append('disk usage over 99%') + disk_msg.append('disk usage over 95%') + disk_msg.append('disk usage over 90%') + disk_msg.append('disk usage over 85%') + disk_msg.append('disk usage over 80%') + + num = random.randint(1,7) + if num <= 4: + msg = "[%s] %s" %(MSG_PREFIX[num], disk_msg[num]) + syslog.syslog(num, msg) + print(msg) + return num + +def tsg_diagnose_for_network(): + return 7 + +def tsg_diagnose(): + + log_handle = syslog.openlog("TSG-MXN") + + diagnose_level = syslog.LOG_DEBUG + + #check app, not running, dead-lock + check_ret = tsg_diagnose_for_app() + if check_ret < diagnose_level: + diagnose_level = check_ret + + #error, critical return immediately + if diagnose_level <= syslog.LOG_WARNING: + return diagnose_level + + #check app, drop packet, traffic bps + check_ret = tsg_diagnose_for_network() + if check_ret < diagnose_level: + diagnose_level = check_ret + #error, critical return immediately + if diagnose_level <= syslog.LOG_WARNING: + return diagnose_level + + + #check cpu, more than 80%, 90%, 95% + check_ret = tsg_diagnose_for_cpu() + if check_ret < diagnose_level: + diagnose_level = check_ret + #error, critical return immediately + if diagnose_level <= syslog.LOG_WARNING: + return diagnose_level + + #check mem, free memory less than 10% + check_ret = tsg_diagnose_for_mem() + if check_ret < diagnose_level: + diagnose_level = check_ret + #error, critical return immediately + if diagnose_level <= syslog.LOG_WARNING: + return diagnose_level + + + #check disk, free disk space less than 5% + check_ret = tsg_diagnose_for_mem() + if check_ret < diagnose_level: + diagnose_level = check_ret + #error, critical return immediately + if diagnose_level <= syslog.LOG_WARNING: + return diagnose_level + + #if not error, output success + syslog.syslog(syslog.LOG_DEBUG, "TSG diagnose completed, all modules are running normally!") + print ("TSG diagnose completed, all modules are running normally!") + return 0 + +if __name__ == '__main__': + fun_res_code = tsg_diagnose() + sys.exit(fun_res_code) diff --git a/py_src/tsg_hardware_reboot.py b/py_src/tsg_hardware_reboot.py new file mode 100644 index 0000000..3e48c5f --- /dev/null +++ b/py_src/tsg_hardware_reboot.py @@ -0,0 +1,25 @@ +import sys +import syslog + +##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'] + +def tsg_hardware_reboot(): + log_handle = syslog.openlog("TSG-MXN") + msg = "[%s] %s" %(MSG_PREFIX[syslog.LOG_ERR], "hardware reboot error, cant't run this command in test period!") + + syslog.syslog(syslog.LOG_ERR, msg) + print (msg) + return 1 + +if __name__ == '__main__': + fun_res_code = tsg_hardware_reboot() + sys.exit(fun_res_code) diff --git a/py_src/tsg_software_reboot.py b/py_src/tsg_software_reboot.py new file mode 100644 index 0000000..f1c0722 --- /dev/null +++ b/py_src/tsg_software_reboot.py @@ -0,0 +1,24 @@ +import sys +import syslog + +##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'] + +def tsg_software_reboot(): + log_handle = syslog.openlog("TSG-MXN") + msg = "[%s] %s" %(MSG_PREFIX[syslog.LOG_INFO], "software reboot success.") + syslog.syslog(syslog.LOG_INFO, msg) + print (msg) + return 0 + +if __name__ == '__main__': + fun_res_code = tsg_software_reboot() + sys.exit(fun_res_code) diff --git a/py_temp/.gitignore b/py_temp/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..9afd39d --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +TSG CLI Command Source, such as tsg_diagnose,tsg_hardware_reboot,tsg_software_reboot. \ No newline at end of file