215 lines
6.5 KiB
Python
215 lines
6.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
应用入口模块
|
|
统一的应用程序入口类(门面模式)
|
|
"""
|
|
|
|
import sys
|
|
from typing import Optional
|
|
|
|
from config_manager import ConfigManager, MonitorConfig
|
|
from network_monitor import NetworkMonitor
|
|
from advanced_monitor import AdvancedNetworkMonitor
|
|
from utils import NetworkUtils, ValidatorFactory
|
|
|
|
|
|
class Application:
|
|
"""应用程序类(门面模式)"""
|
|
|
|
def __init__(self):
|
|
"""初始化应用程序"""
|
|
self.config_manager = ConfigManager()
|
|
|
|
def print_banner(self) -> None:
|
|
"""打印欢迎横幅"""
|
|
print("=" * 60)
|
|
print(" 网络监控器 (Network Monitor)")
|
|
print("=" * 60)
|
|
print()
|
|
|
|
def print_menu(self) -> None:
|
|
"""打印菜单"""
|
|
print("请选择运行模式:")
|
|
print(" 1. 基础监控模式")
|
|
print(" 2. 高级监控模式 (带日志和通知)")
|
|
print(" 3. 自定义参数")
|
|
print(" 0. 退出")
|
|
print()
|
|
|
|
def get_user_choice(self) -> str:
|
|
"""
|
|
获取用户选择
|
|
|
|
Returns:
|
|
用户选择的选项
|
|
"""
|
|
return input("请选择 (0-3): ").strip()
|
|
|
|
def get_custom_config(self) -> MonitorConfig:
|
|
"""
|
|
获取自定义配置
|
|
|
|
Returns:
|
|
配置对象
|
|
"""
|
|
print("\n=== 自定义配置 ===")
|
|
|
|
# 获取目标IP
|
|
target_ip = input("目标IP (默认: 8.8.8.8): ").strip()
|
|
if not target_ip:
|
|
target_ip = "8.8.8.8"
|
|
|
|
# 验证IP
|
|
ip_validator = ValidatorFactory.create_ip_validator()
|
|
while not ip_validator.validate(target_ip):
|
|
print(f"错误: {ip_validator.get_error_message(target_ip)}")
|
|
target_ip = input("目标IP (默认: 8.8.8.8): ").strip()
|
|
if not target_ip:
|
|
target_ip = "8.8.8.8"
|
|
|
|
# 获取延迟阈值
|
|
delay_input = input("延迟阈值秒数 (1-3600, 默认: 30): ").strip()
|
|
if delay_input:
|
|
delay_validator = ValidatorFactory.create_range_validator(1, 3600)
|
|
while not delay_validator.validate(delay_input):
|
|
print(f"错误: {delay_validator.get_error_message(delay_input)}")
|
|
delay_input = input("延迟阈值秒数 (1-3600, 默认: 30): ").strip()
|
|
if not delay_input:
|
|
break
|
|
else:
|
|
delay_input = "30"
|
|
|
|
delay_threshold = float(delay_input) if delay_input else 30.0
|
|
|
|
# 获取ping间隔
|
|
interval_input = input("Ping间隔秒数 (1-300, 默认: 5): ").strip()
|
|
if interval_input:
|
|
interval_validator = ValidatorFactory.create_range_validator(1, 300)
|
|
while not interval_validator.validate(interval_input):
|
|
print(f"错误: {interval_validator.get_error_message(interval_input)}")
|
|
interval_input = input("Ping间隔秒数 (1-300, 默认: 5): ").strip()
|
|
if not interval_input:
|
|
break
|
|
else:
|
|
interval_input = "5"
|
|
|
|
ping_interval = float(interval_input) if interval_input else 5.0
|
|
|
|
# 创建配置
|
|
config = MonitorConfig(
|
|
target_ip=target_ip,
|
|
delay_threshold=delay_threshold,
|
|
ping_interval=ping_interval
|
|
)
|
|
|
|
print(f"\n配置: IP={target_ip}, 延迟={delay_threshold}s, 间隔={ping_interval}s\n")
|
|
return config
|
|
|
|
def run_basic_mode(self, config: Optional[MonitorConfig] = None) -> None:
|
|
"""
|
|
运行基础模式
|
|
|
|
Args:
|
|
config: 配置对象,如果为None则使用默认配置
|
|
"""
|
|
if config is None:
|
|
config = self.config_manager.load_from_args()
|
|
|
|
print("\n启动基础监控模式...\n")
|
|
monitor = NetworkMonitor(config)
|
|
monitor.monitor()
|
|
|
|
def run_advanced_mode(self, config: Optional[MonitorConfig] = None) -> None:
|
|
"""
|
|
运行高级模式
|
|
|
|
Args:
|
|
config: 配置对象,如果为None则使用默认配置
|
|
"""
|
|
if config is None:
|
|
config = self.config_manager.load_from_args()
|
|
|
|
print("\n启动高级监控模式...\n")
|
|
monitor = AdvancedNetworkMonitor(
|
|
config=config,
|
|
enable_logging=True,
|
|
enable_notifications=True
|
|
)
|
|
monitor.monitor()
|
|
|
|
def run_custom_mode(self) -> None:
|
|
"""运行自定义模式"""
|
|
config = self.get_custom_config()
|
|
self.run_basic_mode(config)
|
|
|
|
def run_with_args(self, args: list) -> None:
|
|
"""
|
|
使用命令行参数运行
|
|
|
|
Args:
|
|
args: 命令行参数列表
|
|
"""
|
|
# 模拟命令行参数
|
|
original_argv = sys.argv
|
|
sys.argv = [''] + args
|
|
|
|
try:
|
|
config = self.config_manager.load_from_args()
|
|
self.run_basic_mode(config)
|
|
finally:
|
|
sys.argv = original_argv
|
|
|
|
def run_interactive(self) -> None:
|
|
"""运行交互式模式"""
|
|
self.print_banner()
|
|
|
|
while True:
|
|
self.print_menu()
|
|
choice = self.get_user_choice()
|
|
|
|
if choice == "0":
|
|
print("\n退出程序")
|
|
sys.exit(0)
|
|
elif choice == "1":
|
|
self.run_basic_mode()
|
|
elif choice == "2":
|
|
self.run_advanced_mode()
|
|
elif choice == "3":
|
|
self.run_custom_mode()
|
|
else:
|
|
print("\n无效选项,请重新选择\n")
|
|
import time
|
|
time.sleep(1)
|
|
continue
|
|
|
|
# 监控结束后询问是否继续
|
|
print("\n监控已停止")
|
|
input("\n按Enter继续...")
|
|
print()
|
|
|
|
def run(self) -> None:
|
|
"""运行应用程序"""
|
|
# 检查是否有命令行参数
|
|
if len(sys.argv) > 1:
|
|
self.config_manager.load_from_args()
|
|
config = self.config_manager.get_config()
|
|
|
|
# 判断是否启用高级功能
|
|
if config.enable_logging or config.enable_notifications:
|
|
self.run_advanced_mode(config)
|
|
else:
|
|
self.run_basic_mode(config)
|
|
else:
|
|
self.run_interactive()
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
app = Application()
|
|
app.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|