ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 12.6px;text-align: left;line-height: 1.75;color: rgb(221, 17, 68);background: rgba(27, 31, 35, 0.05);padding: 3px 5px;border-radius: 4px;">ollama_monitor.py文件。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 14px;margin: 10px 8px;color: rgb(201, 209, 217);background: rgb(13, 17, 23);text-align: left;line-height: 1.5;overflow-x: auto;border-radius: 8px;padding: 0px !important;">importrequests
importtime
importsubprocess
importpsutil
importlogging
importos
fromdatetimeimportdatetime
OLLAMA_HOST = os.environ.get("OLLAMA_HOST","localhost:11434")
try:
port = OLLAMA_HOST.split(":")[1] # 提取端口号
OLLAMA_API_URL =f"http://localhost:{port}/api/tags"
exceptIndexError:
logging.error("OLLAMA_HOST 环境变量格式错误,应为 '主机:端口'")
port ="11434"#默认端口
OLLAMA_API_URL =f"http://localhost:{port}/api/tags"
TIMEOUT_SECONDS =10
RESTART_COMMAND ="ollama ps"
defsetup_logging():
"""配置日志记录器,同时输出到文件和控制台。"""
log_dir ="logs"
ifnotos.path.exists(log_dir):
os.makedirs(log_dir)
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
log_file_path = os.path.join(log_dir,f"ollama_monitor_{current_time}.log")
# 创建文件处理器
file_handler = logging.FileHandler(log_file_path)
file_handler.setLevel(logging.INFO)
file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_formatter)
# 创建控制台处理器
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
# 获取根日志记录器并添加处理器
logging.basicConfig(level=logging.INFO, handlers=[file_handler, console_handler])
defcheck_ollama_status():
"""检查 Ollama 状态,如果卡住则返回 True,否则返回 False。"""
try:
response = requests.get(OLLAMA_API_URL, timeout=TIMEOUT_SECONDS)
response.raise_for_status()
returnFalse
exceptrequests.exceptions.RequestExceptionase:
logging.error(f"Ollama 可能卡住:{e}")
returnTrue
defrestart_ollama():
"""重启 Ollama 服务。"""
logging.info("重启 Ollama 服务...")
try:
forprocinpsutil.process_iter(['pid','name']):
ifproc.info['name'] =='ollama.exe':
p = psutil.Process(proc.info['pid'])
p.terminate()
subprocess.Popen(RESTART_COMMAND, shell=True)
logging.info("Ollama 服务已重启。")
exceptExceptionase:
logging.error(f"重启 Ollama 服务失败:{e}")
if__name__ =="__main__":
setup_logging()
whileTrue:
ifcheck_ollama_status():
restart_ollama()
sleep_time =int(os.environ.get("OLLAMA_MONITOR_INTERVAL",60))
time.sleep(sleep_time)ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 0.1em;color: rgb(63, 63, 63);">这边检测的是ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 12.6px;text-align: left;line-height: 1.75;color: rgb(221, 17, 68);background: rgba(27, 31, 35, 0.05);padding: 3px 5px;border-radius: 4px;">http://localhost:{port}/api/tags获取模型列表接口,用使用ps接口,没效果。 这边读取了环境变量ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-feature-settings: normal;font-variation-settings: normal;font-size: 12.6px;text-align: left;line-height: 1.75;color: rgb(221, 17, 68);background: rgba(27, 31, 35, 0.05);padding: 3px 5px;border-radius: 4px;">OLLAMA_MONITOR_INTERVAL监控间隔,默认为60秒ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;display: table;padding: 0px 0.2em;color: rgb(255, 255, 255);background: rgb(15, 76, 129);">安装依赖包requirements.txt
requests
psutil
pyinstaller运行pip install -r requirements.txt
在程序目录下执行:pyinstaller --onefile ollama_monitor.py之后在根目录的dist/ollama_monitor.exe文件。 将该文件拷贝到目标机器上运行即可。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |