监控与日志

有效的监控和日志管理是保障服务稳定运行的关键。本章介绍 Ollama 的监控和日志管理。

服务状态监控

基本状态检查

# 查看运行中的模型
ollama ps

# 查看已安装模型
ollama list

# 检查服务版本
curl http://localhost:11434/api/version

API 健康检查

# 简单检查
curl -s http://localhost:11434/api/tags | jq .

# 检查服务是否响应
curl -s -o /dev/null -w "%{http_code}" http://localhost:11434/api/version

资源监控

GPU 监控

# 实时监控
watch -n 1 nvidia-smi

# 查看显存使用
nvidia-smi --query-gpu=memory.used,memory.total --format=csv

# 查看GPU利用率
nvidia-smi --query-gpu=utilization.gpu --format=csv

内存监控

# 查看内存使用
free -h

# 实时监控
watch -n 1 free -h

# 详细信息
cat /proc/meminfo | grep -E "Mem|Swap"

CPU 监控

# 实时监控
top

# 或使用 htop
htop

# 查看进程
ps aux | grep ollama

磁盘监控

# 查看磁盘使用
df -h

# 查看模型目录大小
du -sh ~/.ollama/models
du -sh /usr/share/ollama/.ollama/models

日志管理

查看日志

Linux(systemd):

# 查看全部日志
journalctl -u ollama

# 实时查看
journalctl -u ollama -f

# 查看最近 100 行
journalctl -u ollama -n 100

# 查看今天的日志
journalctl -u ollama --since today

# 查看最近 1 小时
journalctl -u ollama --since "1 hour ago"

Docker:

# 查看日志
docker logs ollama

# 实时查看
docker logs -f ollama

# 查看最近 100 行
docker logs --tail 100 ollama

# 查看带时间戳的日志
docker logs -t ollama

macOS:

# 查看应用日志
log show --predicate 'process == "ollama"' --last 1h

日志级别

启用调试模式获取更详细的日志:

OLLAMA_DEBUG=1 ollama serve

日志分析

# 搜索错误
journalctl -u ollama | grep -i error

# 搜索警告
journalctl -u ollama | grep -i warn

# 统计错误数量
journalctl -u ollama | grep -c -i error

性能指标

响应时间

# 测量 API 响应时间
curl -w "Time: %{time_total}s\n" -o /dev/null -s http://localhost:11434/api/version

生成速度

在模型运行时观察 tokens/s(每秒生成的 token 数)。

并发性能

# 使用 ab 测试
ab -n 100 -c 10 http://localhost:11434/api/version

# 使用 wrk 测试
wrk -t4 -c10 -d30s http://localhost:11434/api/version

监控脚本

简单监控脚本

#!/bin/bash
# monitor-ollama.sh

echo "=== Ollama Status ==="
echo "Time: $(date)"
echo ""

# 检查服务
echo "Service Status:"
if curl -s http://localhost:11434/api/version > /dev/null; then
    echo "  Status: Running"
else
    echo "  Status: Not Running"
fi
echo ""

# GPU 状态
echo "GPU Status:"
nvidia-smi --query-gpu=name,memory.used,memory.total,utilization.gpu --format=csv,noheader
echo ""

# 内存状态
echo "Memory Status:"
free -h | grep -E "Mem|Swap"
echo ""

# 运行中的模型
echo "Running Models:"
ollama ps

定时监控

# 添加到 crontab,每 5 分钟检查一次
*/5 * * * * /path/to/monitor-ollama.sh >> /var/log/ollama-monitor.log

告警配置

简单告警脚本

#!/bin/bash
# alert-ollama.sh

# 检查服务
if ! curl -s http://localhost:11434/api/version > /dev/null; then
    echo "ALERT: Ollama service is down!" | mail -s "Ollama Alert" admin@example.com
    exit 1
fi

# 检查内存
memory_used=$(free | grep Mem | awk '{print int($3/$2 * 100)}')
if [ $memory_used -gt 90 ]; then
    echo "ALERT: Memory usage is ${memory_used}%" | mail -s "Ollama Alert" admin@example.com
fi

# 检查 GPU 显存
if command -v nvidia-smi &> /dev/null; then
    gpu_memory=$(nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits | head -1)
    gpu_total=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | head -1)
    gpu_percent=$((gpu_memory * 100 / gpu_total))
    if [ $gpu_percent -gt 90 ]; then
        echo "ALERT: GPU memory usage is ${gpu_percent}%" | mail -s "Ollama Alert" admin@example.com
    fi
fi

日志轮转

systemd 日志轮转

# 编辑 journald 配置
sudo nano /etc/systemd/journald.conf

配置示例:

[Journal]
SystemMaxUse=500M
SystemMaxFileSize=100M
MaxRetentionSec=7day

重启服务:

sudo systemctl restart systemd-journald

Docker 日志轮转

services:
  ollama:
    image: ollama/ollama
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "3"

故障排查

常见问题排查步骤

  1. 检查服务状态
ollama ps
curl http://localhost:11434/api/version
  1. 查看日志
journalctl -u ollama -n 50
docker logs --tail 50 ollama
  1. 检查资源
free -h
nvidia-smi
df -h
  1. 检查网络
netstat -tlnp | grep 11434
curl -v http://localhost:11434/api/version

常见错误

错误可能原因解决方法
Connection refused服务未启动启动服务
Out of memory内存不足释放内存或使用更小模型
CUDA out of memory显存不足使用更小模型或 CPU
Model not found模型未下载下载模型