xjf6b commited on
Commit
a7ec032
·
verified ·
1 Parent(s): 6eeb271

Update vps_monitor.py

Browse files
Files changed (1) hide show
  1. vps_monitor.py +14 -25
vps_monitor.py CHANGED
@@ -6,32 +6,21 @@ import sys
6
  from flask import Flask, jsonify, render_template_string
7
  from threading import Thread
8
  import logging
9
- from colorama import init, Fore, Back, Style
10
-
11
- init(autoreset=True) # 初始化 colorama
12
 
13
  app = Flask(__name__)
14
 
15
  vps_status = {}
16
 
17
  # 设置日志
18
- class ColoredFormatter(logging.Formatter):
19
- def format(self, record):
20
- if record.levelno == logging.INFO:
21
- record.msg = f"{Fore.GREEN}{record.msg}{Style.RESET_ALL}"
22
- elif record.levelno == logging.WARNING:
23
- record.msg = f"{Fore.YELLOW}{record.msg}{Style.RESET_ALL}"
24
- elif record.levelno == logging.ERROR:
25
- record.msg = f"{Fore.RED}{record.msg}{Style.RESET_ALL}"
26
- return super().format(record)
27
-
28
- formatter = ColoredFormatter('%(asctime)s - %(levelname)s - %(message)s')
29
- handler = logging.StreamHandler(sys.stdout)
30
- handler.setFormatter(formatter)
31
-
32
  logger = logging.getLogger()
33
- logger.setLevel(logging.INFO)
34
- logger.addHandler(handler)
35
 
36
  def get_vps_configs():
37
  configs = []
@@ -89,7 +78,7 @@ def check_and_run_script(config):
89
  check_command = f"ps aux | grep {script_name} | grep -v grep"
90
  stdin, stdout, stderr = client.exec_command(check_command)
91
  if stdout.read():
92
- status = f"{Fore.GREEN}Running{Style.RESET_ALL}"
93
  logger.info(f"Script {script_name} is running on {config['hostname']}")
94
  else:
95
  logger.warning(f"Script {script_name} not running on {config['hostname']}. Executing restart script.")
@@ -98,13 +87,13 @@ def check_and_run_script(config):
98
  exit_status = stdout.channel.recv_exit_status()
99
 
100
  if exit_status == 0:
101
- status = f"{Fore.YELLOW}Restarted{Style.RESET_ALL}"
102
  logger.info(f"Restart script {script_name} executed successfully on {config['hostname']}")
103
  output = stdout.read().decode('utf-8')
104
  logger.info(f"Restart output: {output}")
105
  else:
106
  error_output = stderr.read().decode('utf-8')
107
- status = f"{Fore.RED}Error{Style.RESET_ALL}"
108
  logger.error(f"Error executing restart script {script_name} on {config['hostname']}: {error_output}")
109
 
110
  key = f"{config['hostname']}:{script_name}"
@@ -121,7 +110,7 @@ def check_and_run_script(config):
121
  key = f"{config['hostname']}:{script_name}"
122
  vps_status[key] = {
123
  'index': config['index'],
124
- 'status': f"{Fore.RED}Error: {str(e)}{Style.RESET_ALL}",
125
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
126
  'username': config['username'],
127
  'script_name': script_name
@@ -133,11 +122,11 @@ def check_and_run_script(config):
133
  logger.info(f"Finished checking VPS {config['index']}: {config['hostname']} - {script_name}")
134
 
135
  def check_all_vps():
136
- logger.info(f"{Back.BLUE}{Fore.WHITE}Starting VPS check{Style.RESET_ALL}")
137
  vps_configs = get_vps_configs()
138
  for config in vps_configs:
139
  check_and_run_script(config)
140
- logger.info(f"{Back.BLUE}{Fore.WHITE}Finished VPS check{Style.RESET_ALL}")
141
  for key, status in vps_status.items():
142
  logger.info(f"VPS {status['index']} - {key}: Status: {status['status']}, Username: {status['username']}, Script: {status['script_name']}")
143
 
 
6
  from flask import Flask, jsonify, render_template_string
7
  from threading import Thread
8
  import logging
 
 
 
9
 
10
  app = Flask(__name__)
11
 
12
  vps_status = {}
13
 
14
  # 设置日志
15
+ logging.basicConfig(
16
+ level=logging.INFO,
17
+ format='%(asctime)s - %(levelname)s - %(message)s',
18
+ handlers=[
19
+ logging.StreamHandler(sys.stdout),
20
+ logging.StreamHandler(sys.stderr)
21
+ ]
22
+ )
 
 
 
 
 
 
23
  logger = logging.getLogger()
 
 
24
 
25
  def get_vps_configs():
26
  configs = []
 
78
  check_command = f"ps aux | grep {script_name} | grep -v grep"
79
  stdin, stdout, stderr = client.exec_command(check_command)
80
  if stdout.read():
81
+ status = "Running"
82
  logger.info(f"Script {script_name} is running on {config['hostname']}")
83
  else:
84
  logger.warning(f"Script {script_name} not running on {config['hostname']}. Executing restart script.")
 
87
  exit_status = stdout.channel.recv_exit_status()
88
 
89
  if exit_status == 0:
90
+ status = "Restarted"
91
  logger.info(f"Restart script {script_name} executed successfully on {config['hostname']}")
92
  output = stdout.read().decode('utf-8')
93
  logger.info(f"Restart output: {output}")
94
  else:
95
  error_output = stderr.read().decode('utf-8')
96
+ status = "Error"
97
  logger.error(f"Error executing restart script {script_name} on {config['hostname']}: {error_output}")
98
 
99
  key = f"{config['hostname']}:{script_name}"
 
110
  key = f"{config['hostname']}:{script_name}"
111
  vps_status[key] = {
112
  'index': config['index'],
113
+ 'status': f"Error: {str(e)}",
114
  'last_check': time.strftime('%Y-%m-%d %H:%M:%S'),
115
  'username': config['username'],
116
  'script_name': script_name
 
122
  logger.info(f"Finished checking VPS {config['index']}: {config['hostname']} - {script_name}")
123
 
124
  def check_all_vps():
125
+ logger.info("Starting VPS check")
126
  vps_configs = get_vps_configs()
127
  for config in vps_configs:
128
  check_and_run_script(config)
129
+ logger.info("Finished VPS check")
130
  for key, status in vps_status.items():
131
  logger.info(f"VPS {status['index']} - {key}: Status: {status['status']}, Username: {status['username']}, Script: {status['script_name']}")
132