1
This commit is contained in:
69
crond/upcrond-192.168.8.125.py
Normal file
69
crond/upcrond-192.168.8.125.py
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import sys # sys 模块用于 sys.exit(),虽然这里没有直接使用,但通常在脚本中需要
|
||||
|
||||
# --- 配置日志 ---
|
||||
# 确保日志文件路径正确
|
||||
LOG_FILE = '/opt/upc_resent/log/upc-192.168.8.125.log'
|
||||
# 定义时间格式,将被 logging.basicConfig 的 datefmt 使用
|
||||
ISOTIMEFORMAT = '%Y-%m-%d %X'
|
||||
|
||||
# 配置日志,确保只执行一次
|
||||
# format 中包含了时间戳 %(asctime)s,所以不需要手动在日志字符串中添加 datetime
|
||||
logging.basicConfig(filename=LOG_FILE, filemode="a", level=logging.DEBUG,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s', datefmt=ISOTIMEFORMAT)
|
||||
# --- 日志配置结束 ---
|
||||
|
||||
|
||||
def IsOpen(ip, port):
|
||||
"""
|
||||
检查指定IP和端口是否可达。
|
||||
如果端口不可达,则记录警告并尝试重启另一个Python脚本。
|
||||
"""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(1) # 设置连接超时为1秒,避免无限期阻塞
|
||||
try:
|
||||
logging.debug(f"尝试连接到 {ip}:{port}...")
|
||||
s.connect((ip, int(port)))
|
||||
s.shutdown(socket.SHUT_RDWR) # 使用 SHUT_RDWR 代替数字 2,更具可读性
|
||||
|
||||
print(f"端口 {port} is open") # Python 3 print 函数
|
||||
logging.info(f"端口 {port} is open")
|
||||
return True
|
||||
except (OSError, socket.timeout) as e: # 捕获具体的网络错误和超时
|
||||
print(f"端口 {port} is down") # Python 3 print 函数
|
||||
logging.warning(f"端口 {port} 检查失败: {e},尝试重启程序")
|
||||
|
||||
# --- 重启命令 ---
|
||||
# 确保这里的 python3 路径和脚本路径正确
|
||||
# 这里的脚本名 "192.168.8.125.py" 看起来是硬编码的,请确认是否正确
|
||||
restart_command = "/usr/bin/python3 /opt/upc_resent/bin/192.168.8.125.py &"
|
||||
logging.info(f"执行重启命令: {restart_command}")
|
||||
os.system(restart_command)
|
||||
# --- 重启命令结束 ---
|
||||
|
||||
return False
|
||||
except ValueError: # 捕获 int(port) 转换失败的情况
|
||||
print(f"错误: 端口 '{port}' 不是有效的数字。")
|
||||
logging.error(f"端口 '{port}' 不是有效的数字。")
|
||||
return False
|
||||
finally:
|
||||
s.close() # 确保无论成功或失败,socket 都会被关闭
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# --- 目标设备IP和端口 ---
|
||||
# 这些值在原脚本中是硬编码的,如果需要从命令行参数获取,请修改
|
||||
target_ip = '192.168.8.9'
|
||||
target_port = 10129
|
||||
# --- 目标设备IP和端口结束 ---
|
||||
|
||||
logging.info(f"启动端口检查程序,检查 {target_ip}:{target_port}")
|
||||
IsOpen(target_ip, target_port)
|
||||
|
||||
|
||||
69
crond/upcrond-192.168.8.73.py
Normal file
69
crond/upcrond-192.168.8.73.py
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import sys # sys 模块用于 sys.exit(),虽然这里没有直接使用,但通常在脚本中需要
|
||||
|
||||
# --- 配置日志 ---
|
||||
# 确保日志文件路径正确
|
||||
LOG_FILE = '/opt/upc_resent/log/upc-192.168.8.73.log'
|
||||
# 定义时间格式,将被 logging.basicConfig 的 datefmt 使用
|
||||
ISOTIMEFORMAT = '%Y-%m-%d %X'
|
||||
|
||||
# 配置日志,确保只执行一次
|
||||
# format 中包含了时间戳 %(asctime)s,所以不需要手动在日志字符串中添加 datetime
|
||||
logging.basicConfig(filename=LOG_FILE, filemode="a", level=logging.DEBUG,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s', datefmt=ISOTIMEFORMAT)
|
||||
# --- 日志配置结束 ---
|
||||
|
||||
|
||||
def IsOpen(ip, port):
|
||||
"""
|
||||
检查指定IP和端口是否可达。
|
||||
如果端口不可达,则记录警告并尝试重启另一个Python脚本。
|
||||
"""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(1) # 设置连接超时为1秒,避免无限期阻塞
|
||||
try:
|
||||
logging.debug(f"尝试连接到 {ip}:{port}...")
|
||||
s.connect((ip, int(port)))
|
||||
s.shutdown(socket.SHUT_RDWR) # 使用 SHUT_RDWR 代替数字 2,更具可读性
|
||||
|
||||
print(f"端口 {port} is open") # Python 3 print 函数
|
||||
logging.info(f"端口 {port} is open")
|
||||
return True
|
||||
except (OSError, socket.timeout) as e: # 捕获具体的网络错误和超时
|
||||
print(f"端口 {port} is down") # Python 3 print 函数
|
||||
logging.warning(f"端口 {port} 检查失败: {e},尝试重启程序")
|
||||
|
||||
# --- 重启命令 ---
|
||||
# 确保这里的 python3 路径和脚本路径正确
|
||||
# 这里的脚本名 "192.168.8.125.py" 看起来是硬编码的,请确认是否正确
|
||||
restart_command = "/usr/bin/python3 /opt/upc_resent/bin/192.168.8.73.py &"
|
||||
logging.info(f"执行重启命令: {restart_command}")
|
||||
os.system(restart_command)
|
||||
# --- 重启命令结束 ---
|
||||
|
||||
return False
|
||||
except ValueError: # 捕获 int(port) 转换失败的情况
|
||||
print(f"错误: 端口 '{port}' 不是有效的数字。")
|
||||
logging.error(f"端口 '{port}' 不是有效的数字。")
|
||||
return False
|
||||
finally:
|
||||
s.close() # 确保无论成功或失败,socket 都会被关闭
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# --- 目标设备IP和端口 ---
|
||||
# 这些值在原脚本中是硬编码的,如果需要从命令行参数获取,请修改
|
||||
target_ip = '192.168.8.9'
|
||||
target_port = 10079
|
||||
# --- 目标设备IP和端口结束 ---
|
||||
|
||||
logging.info(f"启动端口检查程序,检查 {target_ip}:{target_port}")
|
||||
IsOpen(target_ip, target_port)
|
||||
|
||||
|
||||
54
crond/upcrond-dev1.py
Executable file
54
crond/upcrond-dev1.py
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
# Auto-generated from template. DO NOT EDIT DIRECTLY!
|
||||
# Device: 设备73-TCP (dev1)
|
||||
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
|
||||
# ====== 配置参数(从配置文件读取)======
|
||||
TARGET_IP = '192.168.8.9'
|
||||
TARGET_PORT = 10079
|
||||
LOG_FILE = '/home/smart/pythonPJ/upc_resent/log/crond-dev1.log'
|
||||
RESTART_COMMAND = "/usr/bin/python3 /home/smart/pythonPJ/upc_resent/bin/dev1.py &"
|
||||
DEVICE_ID = 'dev1'
|
||||
# =========================
|
||||
|
||||
ISOTIMEFORMAT = '%Y-%m-%d %X'
|
||||
|
||||
logging.basicConfig(filename=LOG_FILE, filemode="a", level=logging.DEBUG,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s', datefmt=ISOTIMEFORMAT)
|
||||
|
||||
|
||||
def is_port_open(ip, port):
|
||||
"""检查指定IP和端口是否可达"""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(1)
|
||||
try:
|
||||
logging.debug(f"尝试连接到 {ip}:{port}...")
|
||||
s.connect((ip, int(port)))
|
||||
s.shutdown(socket.SHUT_RDWR)
|
||||
print(f"[{DEVICE_ID}] 端口 {port} is open")
|
||||
logging.info(f"端口 {port} is open")
|
||||
return True
|
||||
except (OSError, socket.timeout) as e:
|
||||
print(f"[{DEVICE_ID}] 端口 {port} is down")
|
||||
logging.warning(f"端口 {port} 检查失败: {e},尝试重启程序")
|
||||
logging.info(f"执行重启命令: {RESTART_COMMAND}")
|
||||
os.system(RESTART_COMMAND)
|
||||
return False
|
||||
except ValueError:
|
||||
print(f"错误: 端口 '{port}' 不是有效的数字。")
|
||||
logging.error(f"端口 '{port}' 不是有效的数字。")
|
||||
return False
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.info(f"[{DEVICE_ID}] 启动端口检查程序,检查 {TARGET_IP}:{TARGET_PORT}")
|
||||
print(f"[{DEVICE_ID}] 启动端口检查程序,检查 {TARGET_IP}:{TARGET_PORT}")
|
||||
is_port_open(TARGET_IP, TARGET_PORT)
|
||||
54
crond/upcrond-dev2.py
Executable file
54
crond/upcrond-dev2.py
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
# Auto-generated from template. DO NOT EDIT DIRECTLY!
|
||||
# Device: 设备125-TCP (dev2)
|
||||
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
|
||||
# ====== 配置参数(从配置文件读取)======
|
||||
TARGET_IP = '192.168.8.9'
|
||||
TARGET_PORT = 10129
|
||||
LOG_FILE = '/home/smart/pythonPJ/upc_resent/log/crond-dev2.log'
|
||||
RESTART_COMMAND = "/usr/bin/python3 /home/smart/pythonPJ/upc_resent/bin/dev2.py &"
|
||||
DEVICE_ID = 'dev2'
|
||||
# =========================
|
||||
|
||||
ISOTIMEFORMAT = '%Y-%m-%d %X'
|
||||
|
||||
logging.basicConfig(filename=LOG_FILE, filemode="a", level=logging.DEBUG,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s', datefmt=ISOTIMEFORMAT)
|
||||
|
||||
|
||||
def is_port_open(ip, port):
|
||||
"""检查指定IP和端口是否可达"""
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(1)
|
||||
try:
|
||||
logging.debug(f"尝试连接到 {ip}:{port}...")
|
||||
s.connect((ip, int(port)))
|
||||
s.shutdown(socket.SHUT_RDWR)
|
||||
print(f"[{DEVICE_ID}] 端口 {port} is open")
|
||||
logging.info(f"端口 {port} is open")
|
||||
return True
|
||||
except (OSError, socket.timeout) as e:
|
||||
print(f"[{DEVICE_ID}] 端口 {port} is down")
|
||||
logging.warning(f"端口 {port} 检查失败: {e},尝试重启程序")
|
||||
logging.info(f"执行重启命令: {RESTART_COMMAND}")
|
||||
os.system(RESTART_COMMAND)
|
||||
return False
|
||||
except ValueError:
|
||||
print(f"错误: 端口 '{port}' 不是有效的数字。")
|
||||
logging.error(f"端口 '{port}' 不是有效的数字。")
|
||||
return False
|
||||
finally:
|
||||
s.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.info(f"[{DEVICE_ID}] 启动端口检查程序,检查 {TARGET_IP}:{TARGET_PORT}")
|
||||
print(f"[{DEVICE_ID}] 启动端口检查程序,检查 {TARGET_IP}:{TARGET_PORT}")
|
||||
is_port_open(TARGET_IP, TARGET_PORT)
|
||||
85
crond/upcrond-dev3.py
Executable file
85
crond/upcrond-dev3.py
Executable file
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
# Auto-generated from template. DO NOT EDIT DIRECTLY!
|
||||
# Device: 设备UDP示例 (dev3) - UDP Protocol
|
||||
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
# ====== 配置参数(从配置文件读取)======
|
||||
TARGET_IP = '192.168.8.9'
|
||||
TARGET_PORT = 10080
|
||||
LOG_FILE = '/home/smart/pythonPJ/upc_resent/log/crond-dev3.log'
|
||||
RESTART_COMMAND = "/usr/bin/python3 /home/smart/pythonPJ/upc_resent/bin/dev3.py &"
|
||||
DEVICE_ID = 'dev3'
|
||||
# =========================
|
||||
|
||||
ISOTIMEFORMAT = '%Y-%m-%d %X'
|
||||
|
||||
logging.basicConfig(filename=LOG_FILE, filemode="a", level=logging.DEBUG,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s', datefmt=ISOTIMEFORMAT)
|
||||
|
||||
|
||||
def is_udp_port_open(ip, port):
|
||||
"""
|
||||
检查UDP端口是否可用。
|
||||
UDP是无连接的,这里通过检查端口是否被占用或发送测试数据来判断。
|
||||
"""
|
||||
try:
|
||||
# 尝试绑定到该端口,如果成功说明端口未被占用(服务未启动)
|
||||
# 如果失败说明端口已被占用(服务正在运行)
|
||||
test_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
test_sock.settimeout(1)
|
||||
test_sock.bind((ip, int(port)))
|
||||
test_sock.close()
|
||||
|
||||
# 如果绑定成功,说明服务未启动
|
||||
print(f"[{DEVICE_ID}] UDP端口 {port} 未被占用,服务可能未运行")
|
||||
logging.warning(f"UDP端口 {port} 未被占用,服务可能未运行,尝试重启程序")
|
||||
logging.info(f"执行重启命令: {RESTART_COMMAND}")
|
||||
os.system(RESTART_COMMAND)
|
||||
return False
|
||||
except OSError as e:
|
||||
# 绑定失败,说明端口已被占用,服务正在运行
|
||||
if "Address already in use" in str(e) or "98" in str(e):
|
||||
print(f"[{DEVICE_ID}] UDP端口 {port} is open (已被占用,服务运行中)")
|
||||
logging.info(f"UDP端口 {port} is open (服务运行中)")
|
||||
return True
|
||||
else:
|
||||
print(f"[{DEVICE_ID}] 检查UDP端口 {port} 时发生错误: {e}")
|
||||
logging.error(f"检查UDP端口 {port} 时发生错误: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def check_process_running():
|
||||
"""通过进程名检查服务是否正在运行"""
|
||||
try:
|
||||
# 检查是否有对应设备的Python进程在运行
|
||||
cmd = f"ps aux | grep '{DEVICE_ID}.py' | grep -v grep"
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
print(f"[{DEVICE_ID}] 进程检查: 服务正在运行")
|
||||
logging.info(f"进程检查: 服务正在运行")
|
||||
return True
|
||||
else:
|
||||
print(f"[{DEVICE_ID}] 进程检查: 服务未运行")
|
||||
logging.warning(f"进程检查: 服务未运行,尝试重启")
|
||||
logging.info(f"执行重启命令: {RESTART_COMMAND}")
|
||||
os.system(RESTART_COMMAND)
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[{DEVICE_ID}] 进程检查失败: {e}")
|
||||
logging.error(f"进程检查失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
logging.info(f"[{DEVICE_ID}] 启动UDP端口检查程序,检查 {TARGET_IP}:{TARGET_PORT}")
|
||||
print(f"[{DEVICE_ID}] 启动UDP端口检查程序,检查 {TARGET_IP}:{TARGET_PORT}")
|
||||
|
||||
# 对于UDP,优先使用进程检查方式
|
||||
check_process_running()
|
||||
Reference in New Issue
Block a user