| import datetime |
| import sys |
| import os |
| os.chdir('/www/server/panel/') |
| sys.path.insert(0, "class/") |
| import public |
| |
|
|
| |
|
|
| |
| if len(sys.argv) != 2: |
| print("Usage: python log_task_analyzer.py <log_file_path>") |
| sys.exit(1) |
|
|
| |
| log_file = sys.argv[1] |
|
|
| |
| task_log = "/tmp/task_log.log" |
|
|
| |
| MAX_BYTES_TO_READ = 1024 * 1000 |
|
|
| |
| try: |
| with open(log_file, 'rb') as f: |
| f.seek(0, os.SEEK_END) |
| file_size = f.tell() |
| offset = max(file_size - MAX_BYTES_TO_READ, 0) |
| f.seek(offset) |
| data = f.read(MAX_BYTES_TO_READ).decode('utf-8', errors='ignore') |
| lines = data.splitlines()[::-1] |
| except FileNotFoundError: |
| print(f"Log file {log_file} not found.") |
| sys.exit(1) |
|
|
| |
| found_successful = False |
| found_error_before_next_successful = False |
|
|
| |
| error_patterns = ["错误", "失败", "command not found", "Unknown error", "不存在", |
| "请先到软件商店安装日志清理工具", "failed to run command", |
| "No such file or directory", "not supported or disabled in libcurl"] |
|
|
|
|
| |
| for line in lines: |
| if "Successful" in line: |
| if found_successful: |
| |
| break |
| else: |
| |
| found_successful = True |
| elif any(error_pattern in line for error_pattern in error_patterns) and found_successful: |
| |
| found_error_before_next_successful = True |
| break |
|
|
| |
| with open(task_log, 'a') as f: |
| current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') |
| |
| echo_value = log_file.split('/')[-1].split('.')[0] |
| crontab_data_list = public.M('crontab').where('echo=?', (echo_value,)).select() |
| if found_successful and found_error_before_next_successful: |
| if crontab_data_list: |
| public.M('crontab').where('echo=?', (echo_value,)).setField('result', 0) |
| |
| else: |
| if crontab_data_list: |
| public.M('crontab').where('echo=?', (echo_value,)).setField('result', 1) |
| |
|
|
| |
| |
|
|