mdserver-web/plugins/op_waf/tool_task.py

168 lines
4.4 KiB
Python
Raw Permalink Normal View History

2022-10-06 12:47:26 -04:00
# coding:utf-8
import sys
import io
import os
import time
import json
2024-11-23 16:46:05 -05:00
web_dir = os.getcwd() + "/web"
if os.path.exists(web_dir):
sys.path.append(web_dir)
os.chdir(web_dir)
import core.mw as mw
from utils.crontab import crontab as MwCrontab
2022-10-06 12:47:26 -04:00
app_debug = False
if mw.isAppleSystem():
app_debug = True
def getPluginName():
return 'op_waf'
def getPluginDir():
return mw.getPluginDir() + '/' + getPluginName()
def getServerDir():
return mw.getServerDir() + '/' + getPluginName()
def getTaskConf():
conf = getServerDir() + "/task_config.json"
return conf
def getConfigData():
2024-11-23 16:46:05 -05:00
conf = getTaskConf()
if os.path.exists(conf):
2022-10-06 12:47:26 -04:00
return json.loads(mw.readFile(getTaskConf()))
2025-08-22 05:00:16 -04:00
return {
2022-10-06 12:47:26 -04:00
"task_id": -1,
2025-08-22 05:00:16 -04:00
"period": "day-n",
"where1": "7",
"hour": "0",
"minute": "15",
2022-10-06 12:47:26 -04:00
}
def createBgTask():
removeBgTask()
2025-08-22 05:00:16 -04:00
createBgTaskByName(getPluginName())
2022-10-06 12:47:26 -04:00
2025-08-22 05:00:16 -04:00
def createBgTaskByName(name):
cfg = getConfigData()
2022-10-06 12:47:26 -04:00
2025-08-22 04:35:15 -04:00
_name = "[勿删]OP防火墙后台任务"
2022-10-06 12:47:26 -04:00
res = mw.M("crontab").field("id, name").where("name=?", (_name,)).find()
if res:
return True
if "task_id" in cfg.keys() and cfg["task_id"] > 0:
res = mw.M("crontab").field("id, name").where(
"id=?", (cfg["task_id"],)).find()
if res and res["id"] == cfg["task_id"]:
print("计划任务已经存在!")
return True
2024-12-03 07:12:55 -05:00
mw_dir = mw.getPanelDir()
2022-10-06 12:47:26 -04:00
cmd = '''
2022-10-15 11:16:25 -04:00
mw_dir=%s
2022-10-06 12:47:26 -04:00
rname=%s
plugin_path=%s
script_path=%s
logs_file=$plugin_path/${rname}.log
2022-10-15 11:16:25 -04:00
''' % (mw_dir, name, getServerDir(), getPluginDir())
2022-10-06 12:47:26 -04:00
cmd += 'echo "★【`date +"%Y-%m-%d %H:%M:%S"`】 STSRT★" >> $logs_file' + "\n"
cmd += 'echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" >> $logs_file' + "\n"
2023-02-04 23:37:49 -05:00
2025-08-22 05:15:55 -04:00
cmd += 'echo "cd $mw_dir && source bin/activate && python3 $script_path/tool_task.py run >> $logs_file 2>&1"' + "\n"
cmd += 'cd $mw_dir && source bin/activate && python3 $script_path/tool_task.py run >> $logs_file 2>&1' + "\n"
2023-02-04 23:37:49 -05:00
2022-10-06 12:47:26 -04:00
cmd += 'echo "【`date +"%Y-%m-%d %H:%M:%S"`】 END★" >> $logs_file' + "\n"
cmd += 'echo "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" >> $logs_file' + "\n"
params = {
'name': _name,
2025-08-22 05:05:01 -04:00
'type': cfg['period'],
2022-10-06 12:47:26 -04:00
'week': "",
2025-08-22 05:00:16 -04:00
'where1': cfg['where1'],
'hour': cfg['hour'],
'minute': cfg['minute'],
2022-10-06 12:47:26 -04:00
'save': "",
'backup_to': "",
'stype': "toShell",
'sname': '',
'sbody': cmd,
2024-11-23 16:46:05 -05:00
'url_address': '',
2022-10-06 12:47:26 -04:00
}
2024-11-23 16:46:05 -05:00
task_id = MwCrontab.instance().add(params)
2022-10-06 12:47:26 -04:00
if task_id > 0:
2025-08-22 05:00:16 -04:00
cfg["task_id"] = task_id
mw.writeFile(getTaskConf(), json.dumps(cfg))
2022-10-06 12:47:26 -04:00
def removeBgTask():
2025-08-22 05:24:27 -04:00
cfg = getConfigData()
for x in range(len(cfg)):
2022-10-06 12:47:26 -04:00
if "task_id" in cfg.keys() and cfg["task_id"] > 0:
res = mw.M("crontab").field("id, name").where(
"id=?", (cfg["task_id"],)).find()
if res and res["id"] == cfg["task_id"]:
2024-11-23 16:46:05 -05:00
data = MwCrontab.instance().delete(cfg["task_id"])
2025-08-22 05:24:07 -04:00
if data['status']:
2022-10-06 12:47:26 -04:00
cfg["task_id"] = -1
2025-08-22 06:58:01 -04:00
mw.writeFile(getTaskConf(), json.dumps(cfg))
2022-10-06 12:47:26 -04:00
return True
return False
2022-10-15 11:16:25 -04:00
def getCpuUsed():
path = getServerDir() + "/cpu.info"
2023-02-04 14:40:18 -05:00
if mw.isAppleSystem():
import psutil
used = psutil.cpu_percent(interval=1)
mw.writeFile(path, str(int(used)))
else:
2023-02-04 14:48:03 -05:00
cmd = "top -bn 1 | fgrep 'Cpu(s)' | awk '{print 100 -$8}' | awk -F . '{print $1}'"
data = mw.execShell(cmd)
2023-02-04 14:48:22 -05:00
mw.writeFile(path, str(int(data[0].strip())))
2022-10-15 11:16:25 -04:00
2025-08-22 05:14:27 -04:00
def pSqliteDb(dbname='logs'):
db_dir = getServerDir() + '/logs/'
2025-08-22 08:03:36 -04:00
conn = mw.M(dbname).dbPos(db_dir, "waf")
2025-08-22 05:14:27 -04:00
conn.execute("PRAGMA synchronous = 0")
conn.execute("PRAGMA cache_size = 8000")
conn.execute("PRAGMA page_size = 32768")
conn.execute("PRAGMA journal_mode = wal")
conn.execute("PRAGMA journal_size_limit = 1073741824")
return conn
2022-10-06 12:47:26 -04:00
def run():
2025-08-22 08:00:15 -04:00
now_t = int(time.time())
logs_conn = pSqliteDb('logs')
del_hot_log = "delete from logs where time<{}".format(now_t)
print(del_hot_log)
r = logs_conn.execute(del_hot_log)
return 'ok'
2022-10-06 12:47:26 -04:00
if __name__ == "__main__":
if len(sys.argv) > 1:
action = sys.argv[1]
if action == "remove":
removeBgTask()
elif action == "add":
createBgTask()
elif action == "run":
run()