Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/plugins/clean/index.py

176 lines
3.6 KiB

4 years ago
# 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
def status():
if os.path.exists(getConf()):
return "start"
return 'stop'
def start():
file = initDreplace()
data = mw.execShell(file + ' start')
if data[1] == '':
return 'ok'
return 'fail'
def stop():
file = initDreplace()
data = mw.execShell(file + ' stop')
if data[1] == '':
return 'ok'
return 'fail'
def reload():
file = initDreplace()
data = mw.execShell(file + ' reload')
if data[1] == '':
return 'ok'
return 'fail'
4 years ago
def get_filePath_fileName_fileExt(filename):
(filepath, tempfilename) = os.path.split(filename)
(shotname, extension) = os.path.splitext(tempfilename)
return filepath, shotname, extension
4 years ago
def cleanFileLog(path):
4 years ago
filepath, shotname, extension = get_filePath_fileName_fileExt(path)
4 years ago
if extension == ".log":
4 years ago
cmd = "echo \"\" > " + path
4 years ago
print(cmd)
4 years ago
print(mw.execShell(cmd))
4 years ago
def cleanDirLog(path):
l = os.listdir(path)
4 years ago
for x in range(len(l)):
abspath = path + "/" + l[x]
if os.path.isfile(abspath):
cleanFileLog(abspath)
if os.path.isdir(abspath):
cleanDirLog(abspath)
4 years ago
4 years ago
def cleanLog():
# 清理日志
rootDir = "/var/log"
print("clean start")
4 years ago
clog = [
"rm -rf /var/log/cron-*",
"rm -rf /var/log/maillog-*",
"rm -rf /var/log/secure-*",
"rm -rf /var/log/spooler-*",
"rm -rf /var/log/yum.log-*",
4 years ago
"rm -rf /var/log/messages-*",
4 years ago
"rm -rf /var/log/btmp-*",
4 years ago
"rm -rf /var/log/audit/audit.log.*",
"rm -rf /var/log/rhsm/rhsm.log-*",
"rm -rf /var/log/rhsm/rhsmcertd.log-*",
4 years ago
"rm -rf /tmp/yum_save_*",
4 years ago
"rm -rf /tmp/tmp.*",
4 years ago
]
for i in clog:
print(i)
4 years ago
mw.execShell(i)
4 years ago
4 years ago
# 常用日志
4 years ago
clogcom = [
"/var/log/messages",
4 years ago
"/var/log/btmp",
"/var/log/wtmp",
4 years ago
"/var/log/secure",
"/var/log/lastlog",
"/var/log/cron",
4 years ago
]
for i in clogcom:
4 years ago
if os.path.exists(i):
4 years ago
mw.execShell("echo \"\" > " + i)
4 years ago
4 years ago
l = os.listdir(rootDir)
for x in range(len(l)):
abspath = rootDir + "/" + l[x]
4 years ago
if os.path.isfile(abspath):
cleanFileLog(abspath)
if os.path.isdir(abspath):
cleanDirLog(abspath)
4 years ago
4 years ago
print("clean end")
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())
elif func == 'clean':
cleanLog()
else:
print('error')