mdserver-web/plugins/clean/index.py

228 lines
4.8 KiB
Python
Raw Normal View History

2021-11-23 03:22:08 -05:00
# coding:utf-8
import sys
import io
import os
import time
sys.path.append(os.getcwd() + "/class/core")
import mw
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'clean'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
def getConf():
path = getServerDir() + "/clean.conf"
return path
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
2022-10-02 03:05:13 -04:00
def getLockFile():
return getServerDir() + "/installed_lock.pl"
def runLog():
return getServerDir() + "/clean.log"
2021-11-23 03:22:08 -05:00
def status():
2022-10-02 03:05:13 -04:00
initConf()
if os.path.exists(getLockFile()):
2021-11-23 03:22:08 -05:00
return "start"
return 'stop'
2022-10-02 03:05:13 -04:00
def initConf():
conf = getConf()
if not os.path.exists(conf):
content = ""
clog = [
2022-11-11 12:10:40 -05:00
"/var/spool/clientmqueue/*",
2022-10-02 03:05:13 -04:00
"/var/log/cron-*",
"/var/log/maillog-*",
"/var/log/secure-*",
"/var/log/spooler-*",
"/var/log/yum.log-*",
"/var/log/messages-*",
"/var/log/btmp-*",
"/var/log/audit/audit.log.*",
"/var/log/rhsm/rhsm.log-*",
"/var/log/rhsm/rhsmcertd.log-*",
"/tmp/yum_save_*",
"/tmp/tmp.*",
]
for i in clog:
content += i + "\n"
# 常用日志
clogcom = [
"/var/log/messages",
"/var/log/btmp",
"/var/log/wtmp",
"/var/log/secure",
"/var/log/lastlog",
"/var/log/cron",
2022-10-08 14:04:17 -04:00
"/www/server/cron"
2022-10-02 03:05:13 -04:00
]
for i in clogcom:
if os.path.exists(i):
content += i + "\n"
# 清理日志
rootDir = "/var/log"
l = os.listdir(rootDir)
for x in range(len(l)):
abspath = rootDir + "/" + l[x]
content += abspath + "\n"
mw.writeFile(conf, content)
2021-11-23 03:22:08 -05:00
def start():
2022-10-02 03:05:13 -04:00
initConf()
lock_file = getLockFile()
if not os.path.exists(lock_file):
mw.writeFile(lock_file, "")
import tool_task
tool_task.createBgTask()
2021-11-23 03:22:08 -05:00
return 'ok'
2022-10-02 03:05:13 -04:00
2021-11-23 03:22:08 -05:00
return 'fail'
def stop():
2022-10-02 03:05:13 -04:00
initConf()
lock_file = getLockFile()
if os.path.exists(lock_file):
os.remove(lock_file)
import tool_task
tool_task.removeBgTask()
2021-11-23 03:22:08 -05:00
return 'ok'
2022-10-02 03:05:13 -04:00
2021-11-23 03:22:08 -05:00
return 'fail'
def reload():
2022-10-02 03:05:13 -04:00
return 'ok'
2021-11-23 03:22:08 -05:00
2021-11-23 03:41:24 -05:00
def get_filePath_fileName_fileExt(filename):
(filepath, tempfilename) = os.path.split(filename)
(shotname, extension) = os.path.splitext(tempfilename)
return filepath, shotname, extension
2021-11-23 03:49:03 -05:00
def cleanFileLog(path):
2021-11-23 04:05:06 -05:00
filepath, shotname, extension = get_filePath_fileName_fileExt(path)
2021-11-23 03:49:03 -05:00
if extension == ".log":
2021-11-23 04:11:49 -05:00
cmd = "echo \"\" > " + path
2022-10-02 03:05:13 -04:00
tmp = mw.execShell(cmd)
if tmp[1] != "":
cmd += " | error:" + tmp[1].strip()
2021-11-23 03:49:03 -05:00
print(cmd)
2021-12-05 06:08:41 -05:00
def cleanSelfFileLog(path):
filepath, shotname, extension = get_filePath_fileName_fileExt(path)
2022-10-02 03:05:13 -04:00
if extension == ".log":
cmd = "echo \"\" > " + path
tmp = mw.execShell(cmd)
if tmp[1] != "":
cmd += " | error:" + tmp[1].strip()
print(cmd)
2021-12-05 06:08:41 -05:00
2021-11-23 03:49:03 -05:00
def cleanDirLog(path):
l = os.listdir(path)
2021-11-23 03:51:21 -05:00
for x in range(len(l)):
abspath = path + "/" + l[x]
if os.path.isfile(abspath):
cleanFileLog(abspath)
if os.path.isdir(abspath):
cleanDirLog(abspath)
2021-11-23 03:49:03 -05:00
2021-11-23 03:22:08 -05:00
def cleanLog():
2022-10-02 03:05:13 -04:00
conf = getConf()
clist = mw.readFile(conf).strip()
2021-12-05 05:54:11 -05:00
clist = clist.split("\n")
for x in clist:
abspath = x.strip()
2022-10-02 03:05:13 -04:00
if abspath.find('*') > 1:
cmd = 'rm -rf ' + abspath
print(cmd)
data = mw.execShell(cmd)
# print(data)
continue
2021-12-05 05:54:11 -05:00
if os.path.exists(abspath):
if os.path.isfile(abspath):
2021-12-05 06:08:41 -05:00
cleanSelfFileLog(abspath)
2022-10-02 03:05:13 -04:00
continue
2021-12-05 05:54:11 -05:00
if os.path.isdir(abspath):
cleanDirLog(abspath)
2022-10-02 03:05:13 -04:00
continue
2021-11-23 03:22:08 -05:00
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
print(status())
elif func == 'start':
print(start())
elif func == 'stop':
print(stop())
elif func == 'restart':
print(restart())
elif func == 'reload':
print(reload())
elif func == 'conf':
print(getConf())
2022-10-02 03:05:13 -04:00
elif func == 'run_log':
print(runLog())
2021-11-23 03:22:08 -05:00
elif func == 'clean':
cleanLog()
else:
print('error')