Rockwell SOS服务监控

from datetime import datetime, timedelta
import time
import subprocess
import logging
import os
import shutil

# 配置日志记录器
service_log_file_path = r'C:\SOS_MONITOR\service_log.log'
logging.basicConfig(filename=service_log_file_path, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def parse_log_time(log_line):
    # 假设日志中时间戳的格式为 "2023/11/30 15:10:16"
    timestamp_str = log_line.split('|')[2].strip()
    timestamp = datetime.strptime(timestamp_str, '%Y/%m/%d %H:%M:%S')
    return timestamp

def check_log(log_directory, service_directory):
    five_minutes_ago = datetime.now() - timedelta(minutes=5)
    has_error = False
    log_file_path = os.path.join(log_directory, 'wrapper.log')
    
    # 备份日志文件
    if os.path.getsize(service_log_file_path) > 2 * 1024 * 1024:  # 如果日志文件大小超过2M
        backup_log(service_log_file_path)
        
    with open(log_file_path, 'r') as file:
        for line in file:
            timestamp = parse_log_time(line)
            if timestamp >= five_minutes_ago:
                has_error = True
                if 'SQL Exception message: Closed Connection' in line:
                    logging.warning(log_directory+": "+line)  # 记录日志信息
                    restart_service(service_directory)
                    break
    # 如果五分钟之内没有log 重启服务
    if not has_error and datetime.now() - five_minutes_ago >= timedelta(minutes=5):
        logging.warning(log_directory+"Within the last 5 minutes, there are no logs in the wrapper.log file")  # 记录日志信息
        restart_service(service_directory)

def restart_service(service_directory):
    # 重启服务的代码
    logging.info('Restarting service in directory: ' + service_directory)  # 记录日志信息
    subprocess.run([os.path.join(service_directory, 'StopApp-NT.bat')])
    logging.info('Service in directory ' + service_directory + ' stopped')  # 记录日志信息
    time.sleep(10)
    subprocess.run([os.path.join(service_directory, 'StartApp-NT.bat')])
    logging.info('Service in directory ' + service_directory + ' started')  # 记录日志信息
    
def backup_log(log_file_path):
    backup_dir = os.path.join(os.path.dirname(log_file_path), 'log_backup')
    os.makedirs(backup_dir, exist_ok=True)
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    backup_file_path = os.path.join(backup_dir, f'service_log_{timestamp}.log')
    shutil.copy(log_file_path, backup_file_path)
    open(log_file_path, 'w').close()  # 清空原始日志文件
    
# # 定义需要检查的日志目录和服务目录列表
# log_and_service_directories = [
#     (r'C:\SOS\SOS_PM_Lineation_9011\logs', r'C:\SOS\SOS_PM_Lineation_9011\bin'),
#     (r'C:\SOS\SOS_ESJJ_Station_Sync_ESJJ01\logs', r'C:\SOS\SOS_ESJJ_Station_Sync_ESJJ01\bin')
# ]
    
# 从文件中读取目录路径对
def read_directory_pairs(file_path):
    directory_pairs = []
    with open(file_path, 'r') as file:
        for line in file:
            log_directory, service_directory = line.strip().split(',')
            directory_pairs.append((log_directory, service_directory))
    return directory_pairs

# 读取目录路径对列表
directory_pairs = read_directory_pairs('directories.txt')

# 循环检查日志并重启服务
for log_directory, service_directory in directory_pairs:
    logging.info(log_directory+'------------------------Application monitor Start........----------------------')
    check_log(log_directory, service_directory)
    logging.info(log_directory+'------------------------Application monitor End--------------------------------')

start.bat 脚本:start /d "C:\SOS_MONITOR" SOSMonitor.exe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值