Shell脚本如何查看容器资源使用

wen 实用脚本 1

本文目录导读:

Shell脚本如何查看容器资源使用

  1. 使用 Docker 命令
  2. 使用 cgroups 直接读取
  3. 完整的监控脚本示例
  4. 获取多个容器的资源统计
  5. 检查容器是否超过资源限制
  6. 使用建议

在Shell脚本中查看容器资源使用情况,主要有以下几种常用方法:

使用 Docker 命令

查看单个容器资源

#!/bin/bash
# 查看特定容器的CPU、内存使用
docker stats <container_name_or_id> --no-stream
# 查看所有容器的资源使用
docker stats --no-stream
# 格式化输出
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}"

查看容器详细信息

#!/bin/bash
container_name="your_container"
# 获取内存使用情况
memory_info=$(docker inspect $container_name --format='{{.State.Status}}')
echo "容器状态: $memory_info"
# 查看资源限制
cpu_limit=$(docker inspect $container_name --format='{{.HostConfig.CpuShares}}')
memory_limit=$(docker inspect $container_name --format='{{.HostConfig.Memory}}')
echo "CPU限制: $cpu_limit"
echo "内存限制: $((memory_limit/1024/1024)) MB"

使用 cgroups 直接读取

读取容器cgroup信息

#!/bin/bash
container_id="<container_id>"
# 内存使用情况
memory_usage=$(cat /sys/fs/cgroup/memory/docker/$container_id/memory.usage_in_bytes)
memory_limit=$(cat /sys/fs/cgroup/memory/docker/$container_id/memory.limit_in_bytes)
echo "内存使用: $((memory_usage/1024/1024)) MB"
echo "内存限制: $((memory_limit/1024/1024)) MB"
# CPU使用情况
cpu_usage=$(cat /sys/fs/cgroup/cpu/docker/$container_id/cpuacct.usage)
echo "CPU总使用: $((cpu_usage/1000000)) ms"

完整的监控脚本示例

#!/bin/bash
# 容器资源监控脚本
MONITOR_INTERVAL=5
LOG_FILE="container_monitor.log"
monitor_container() {
    local container_name=$1
    echo "开始监控容器: $container_name"
    echo "时间戳 | CPU% | 内存使用 | 内存限制 | 内存%" >> $LOG_FILE
    while true; do
        # 获取容器stats
        stats=$(docker stats $container_name --no-stream --format "{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" 2>/dev/null)
        if [ $? -ne 0 ]; then
            echo "错误: 容器 $container_name 不存在或无法访问"
            exit 1
        fi
        timestamp=$(date "+%Y-%m-%d %H:%M:%S")
        cpu_percent=$(echo $stats | cut -f1)
        memory_usage=$(echo $stats | cut -f2)
        memory_percent=$(echo $stats | cut -f3)
        # 记录日志
        echo "$timestamp | $cpu_percent | $memory_usage | $memory_percent" >> $LOG_FILE
        # 检查内存使用率是否超过阈值
        mem_percent_value=$(echo $memory_percent | sed 's/%//')
        if (( $(echo "$mem_percent_value > 80" | bc -l) )); then
            echo "警告: 容器 $container_name 内存使用率超过80%!"
        fi
        sleep $MONITOR_INTERVAL
    done
}
# 检查参数
if [ $# -eq 0 ]; then
    echo "用法: $0 <container_name>"
    exit 1
fi
# 检查Docker是否运行
if ! docker info >/dev/null 2>&1; then
    echo "错误: Docker未运行"
    exit 1
fi
# 启动监控
monitor_container $1

获取多个容器的资源统计

#!/bin/bash
get_all_containers_stats() {
    echo "容器资源使用统计"
    echo "=================="
    printf "%-25s %-10s %-15s %-15s %-8s\n" "容器名" "CPU%" "内存使用" "内存限制" "内存%"
    echo "---------------------------------------------------------------"
    # 获取所有运行中的容器stats
    docker stats --no-stream --format "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}" | \
    while IFS=$'\t' read -r name cpu mem mem_perc; do
        printf "%-25s %-10s %-15s %-8s\n" "$name" "$cpu" "$mem" "$mem_perc"
    done
}
# 执行
get_all_containers_stats

检查容器是否超过资源限制

#!/bin/bash
check_resource_limits() {
    local container=$1
    local cpu_threshold=80
    local mem_threshold=80
    # 获取当前使用率
    cpu_usage=$(docker stats $container --no-stream --format "{{.CPUPerc}}" | sed 's/%//')
    mem_usage=$(docker stats $container --no-stream --format "{{.MemPerc}}" | sed 's/%//')
    # 比较阈值
    if (( $(echo "$cpu_usage > $cpu_threshold" | bc -l) )); then
        echo "CPU使用率警告: $cpu_usage% (阈值: $cpu_threshold%)"
    fi
    if (( $(echo "$mem_usage > $mem_threshold" | bc -l) )); then
        echo "内存使用率警告: $mem_usage% (阈值: $mem_threshold%)"
    fi
}
# 对每个容器进行检查
for container in $(docker ps --format "{{.Names}}"); do
    echo "检查容器: $container"
    check_resource_limits $container
    echo "---"
done

使用建议

  1. 性能考虑:频繁调用 docker stats 可能影响性能,建议适当增加间隔时间
  2. 错误处理:添加容器存在性检查和Docker运行状态检查
  3. 日志记录:对重要监控数据记录日志,便于后续分析
  4. 阈值告警:设置合理的资源使用阈值,及时发现问题

这些脚本可以根据实际需求进行修改和扩展,比如添加邮件告警、连接监控系统等。

抱歉,评论功能暂时关闭!