记录一次 nas docker 运行出错 使用Python脚本检查错误并重启对应容器 npc 运行出错 导致无法连接


import os
import time
import datetime
import subprocess
import mysql.connector

# Docker容器名称
container_name = "containerName"

# MySQL数据库连接配置
db_host = "localhost"
db_user = "*********"
db_password = "*********"
db_database = "***********"

# 连接到MySQL数据库
db_connection = mysql.connector.connect(
    host=db_host,
    user=db_user,
    password=db_password,
    database=db_database
)
db_cursor = db_connection.cursor()

# 创建错误日志表格
db_cursor.execute("CREATE TABLE IF NOT EXISTS error_logs (id INT AUTO_INCREMENT PRIMARY KEY, container_name VARCHAR(255), error_time DATETIME)")
db_connection.commit()

# 日志文件路径
#log_file = "/var/log/docker.log"

# 定时检查间隔(秒)
check_interval = 60

# 定义错误关键词
error_keywords = ["\x1b[1;31m[E]\x1b[0m"]

# 定义容器重启次数
restart_count = 0


while True:
    # 获取当前时间
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    # 获取容器日志
    logs_command = f"docker logs --tail 100 {container_name}"
    logs_process = subprocess.Popen(logs_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    logs_output, _ = logs_process.communicate()
    logs = logs_output.decode("utf-8")

    # 检查日志中是否包含错误关键词
    if 1:#any(keyword in logs.lower() for keyword in error_keywords):
        # 提取时间字符串
        #log_lines = logs.split("\n")
        for line in logs.splitlines():
            if error_keywords[0] in line:
                # 获取时间字符串
                time_string = line[:19]

                # 解析时间字符串为datetime对象
                error_time = datetime.datetime.strptime(time_string, "%Y/%m/%d %H:%M:%S")

                # 查询是否存在记录
                db_cursor.execute("SELECT COUNT(*) FROM error_logs WHERE error_time = %s", (error_time,))
                result = db_cursor.fetchone()
                count = result[0]

                # 判断是否存在记录
                if count > 0:
                    break
                    print("存在记录,不重启docker")
                else:
                    print("不存在记录,执行重启docker操作")
                    # 记录错误时间到数据库
                    db_cursor.execute("INSERT INTO error_logs (container_name, error_time) VALUES (%s, %s)",
                                      (container_name, error_time))
                    db_connection.commit()

                    # 重启容器
                    restart_command = f"docker restart {container_name}"
                    subprocess.run(restart_command, shell=True)
                    restart_count += 1

                    print(f"Detected error in logs. Restarted container. Restart count: {restart_count}")
                    # 休眠一段时间,等待容器重启完成
                    break

    break
    # 休眠一段时间后继续检查
    #time.sleep(check_interval)

posted @ 2023-06-09 10:22  simp00  阅读(83)  评论(0编辑  收藏  举报