Shell脚本如何监控inode使用率

wen 实用脚本 1

本文目录导读:

Shell脚本如何监控inode使用率

  1. 基础监控脚本
  2. 增强版监控脚本
  3. 详细分析脚本
  4. 定期监控脚本(配合cron)
  5. 使用crontab设置定时监控
  6. 单行快速检查命令
  7. 使用建议

我来介绍几种监控inode使用率的方法:

基础监控脚本

#!/bin/bash
# 检查inode使用率
check_inode_usage() {
    # 获取所有挂载点的inode使用情况
    df -i | grep -v "Filesystem" | grep -v "tmpfs" | while read line; do
        # 提取使用率
        usage=$(echo $line | awk '{print $5}' | sed 's/%//')
        mount_point=$(echo $line | awk '{print $6}')
        # 设置阈值(例如80%)
        threshold=80
        if [ "$usage" -gt "$threshold" ]; then
            echo "WARNING: Inode usage on $mount_point is $usage% (threshold: $threshold%)"
        fi
    done
}
check_inode_usage

增强版监控脚本

#!/bin/bash
# 配置
THRESHOLD=80  # 告警阈值
LOG_FILE="/var/log/inode_monitor.log"
EMAIL="admin@example.com"
# 函数:检查inode使用率
check_inodes() {
    local critical_flag=0
    # 获取所有挂载点的inode信息
    df -iP | tail -n+2 | while read -r line; do
        filesystem=$(echo "$line" | awk '{print $1}')
        total_inodes=$(echo "$line" | awk '{print $2}')
        used_inodes=$(echo "$line" | awk '{print $3}')
        free_inodes=$(echo "$line" | awk '{print $4}')
        usage_percent=$(echo "$line" | awk '{print $5}' | sed 's/%//')
        mount_point=$(echo "$line" | awk '{print $6}')
        # 跳过特殊文件系统
        if [[ "$filesystem" == "tmpfs" || "$filesystem" == "devtmpfs" ]]; then
            continue
        fi
        # 检查使用率是否超过阈值
        if [ "$usage_percent" -gt "$THRESHOLD" ]; then
            echo "$(date '+%Y-%m-%d %H:%M:%S') - CRITICAL: $mount_point inode usage at $usage_percent%" | tee -a "$LOG_FILE"
            critical_flag=1
        fi
        # 检查剩余inode数量
        if [ "$free_inodes" -lt "1000" ]; then
            echo "$(date '+%Y-%m-%d %H:%M:%S') - WARNING: Only $free_inodes free inodes on $mount_point" | tee -a "$LOG_FILE"
        fi
    done
    return $critical_flag
}
# 发送告警邮件
send_alert() {
    local subject="Inode Usage Alert - $(hostname)"
    local body="Inode usage exceeded threshold on $(hostname) at $(date)"
    echo "$body" | mail -s "$subject" "$EMAIL"
}
# 主函数
main() {
    echo "=== Inode Monitor Started at $(date) ===" >> "$LOG_FILE"
    if check_inodes; then
        echo "All inode usage is normal" >> "$LOG_FILE"
    else
        send_alert
    fi
    echo "=== Monitor Ended ===" >> "$LOG_FILE"
}
main

详细分析脚本

#!/bin/bash
# 详细inode分析
detailed_inode_analysis() {
    local mount_point="$1"
    echo "=== Inode Analysis for $mount_point ==="
    echo ""
    # 1. 总体使用情况
    echo "1. Overall inode usage:"
    df -i "$mount_point" | column -t
    echo ""
    # 2. 找到占用inode最多的目录
    echo "2. Top 10 directories by inode count:"
    find "$mount_point" -xdev -type d 2>/dev/null | while read dir; do
        count=$(find "$dir" -xdev -type f 2>/dev/null | wc -l)
        echo "$count $dir"
    done | sort -rn | head -10 | awk '{print NR".", $2, "-", $1, "files"}'
    echo ""
    # 3. 统计文件类型
    echo "3. File type distribution:"
    find "$mount_point" -xdev -type f 2>/dev/null | while read file; do
        extension="${file##*.}"
        if [ "$extension" == "$file" ]; then
            echo "no_extension"
        else
            echo "$extension"
        fi
    done | sort | uniq -c | sort -rn | head -10
    echo ""
    # 4. 查找大量小文件
    echo "4. Directories with potentially small files (>1000 files):"
    find "$mount_point" -xdev -type d 2>/dev/null | while read dir; do
        count=$(find "$dir" -maxdepth 1 -type f 2>/dev/null | wc -l)
        if [ "$count" -gt 1000 ]; then
            echo "$dir: $count files"
        fi
    done | head -20
}
# 使用示例
detailed_inode_analysis "/data"

定期监控脚本(配合cron)

#!/bin/bash
# 监控并记录inode使用趋势
MONITOR_DIR="/var/log/inode_monitor"
TREND_FILE="$MONITOR_DIR/trend.csv"
ALERT_LOG="$MONITOR_DIR/alerts.log"
# 创建目录
mkdir -p "$MONITOR_DIR"
# 收集数据
collect_inode_data() {
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    # 初始化趋势文件
    if [ ! -f "$TREND_FILE" ]; then
        echo "timestamp,mount_point,total,used,free,usage%" > "$TREND_FILE"
    fi
    # 收集每个挂载点的数据
    df -iP | tail -n+2 | while read -r line; do
        filesystem=$(echo "$line" | awk '{print $1}')
        [[ "$filesystem" =~ ^(tmpfs|devtmpfs|overlay)$ ]] && continue
        mount_point=$(echo "$line" | awk '{print $6}')
        total=$(echo "$line" | awk '{print $2}')
        used=$(echo "$line" | awk '{print $3}')
        free=$(echo "$line" | awk '{print $4}')
        usage=$(echo "$line" | awk '{print $5}' | sed 's/%//')
        echo "$timestamp,$mount_point,$total,$used,$free,$usage" >> "$TREND_FILE"
    done
}
# 生成报告
generate_report() {
    echo "=== Inode Usage Report - $(date) ==="
    echo ""
    echo "Current Status:"
    df -i | grep -v tmpfs | grep -v devtmpfs
    echo ""
    # 显示趋势(最近24小时)
    echo "Last 24 hours trend (hourly):"
    awk -F',' 'NR>1{print $1,$2,$6}' "$TREND_FILE" | sort -u | tail -24
}
# 清理旧数据(保留30天)
cleanup_old_data() {
    find "$MONITOR_DIR" -name "*.log" -mtime +30 -delete
}
# 主函数
main() {
    collect_inode_data
    generate_report
    cleanup_old_data
}
main

使用crontab设置定时监控

# 编辑crontab
crontab -e
# 添加以下行(每5分钟检查一次)
*/5 * * * * /path/to/inode_monitor.sh
# 或者每小时检查并记录
0 * * * * /path/to/inode_monitor.sh --record
# 每天发送报告
0 8 * * * /path/to/inode_monitor.sh --report | mail -s "Daily Inode Report" admin@example.com

单行快速检查命令

# 快速查看inode使用率
df -i | awk '{if(NR>1 && $5+0 > 80) print $1, $5, $6}'
# 实时监控(每5秒刷新)
watch -n 5 'df -i | grep -v tmpfs'
# 查找使用inode最多的目录
find / -xdev -type d | while read dir; do echo "$(find "$dir" -maxdepth 1 -type f | wc -l) $dir"; done | sort -rn | head -10

使用建议

  1. 设置合适的阈值:根据系统配置调整告警阈值(通常75-90%)
  2. 关注特定目录:如邮件队列(/var/spool/mqueue)、缓存目录等
  3. 定期清理:对于inode紧张的系统,定期清理临时文件
  4. 日志轮转:确保监控日志不会耗尽inode

这些脚本可以直接使用,根据实际需求调整参数即可。

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