You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在内存告警脚本中添加高内存进程的文件描述符采集功能?

Modified Script with Open File Descriptor Collection

Got it, let's adjust your script to include open file descriptor details for the top memory-hungry processes. Here's the updated version, followed by breakdowns of the key changes:

subject="Check Memory Status"
to="myemail@domain.com"
# Fixed the IP extraction regex (missing a dot in original)
ip=$(ifconfig | grep -oP '(?<=inet addr:)\d+\.\d+\.\d+\.\d+')
free=$(free -mt | grep Total | awk '{print $4}')

if [[ "$free" -le 300 ]]; then
    # Start with clear section headers for readability
    echo "=== High Memory Usage Processes ===" > /tmp/memory.txt
    # Capture top 10 memory processes (adjust head -10 to change count)
    ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -10 >> /tmp/memory.txt

    echo -e "\n=== Open File Descriptors for Top Processes ===" >> /tmp/memory.txt

    # Extract PIDs of top 10 processes (skip the ps header line)
    ps -eo pid --sort=-%mem | head -11 | tail -10 | while read pid; do
        # Validate PID to avoid errors from exited processes
        if [[ -n "$pid" && "$pid" =~ ^[0-9]+$ ]]; then
            # Get the process command name
            cmd=$(ps -p "$pid" -o cmd= | head -1)
            # Count total open FDs using /proc (no lsof dependency)
            fd_count=$(ls /proc/"$pid"/fd 2>/dev/null | wc -l)
            # Optional: Get a sample of open FDs (limit to 5 lines to keep file small)
            fd_sample=$(ls -l /proc/"$pid"/fd 2>/dev/null | head -5)

            # Append process FD details to the file
            echo -e "\nProcess PID: $pid" >> /tmp/memory.txt
            echo "Command: $cmd" >> /tmp/memory.txt
            echo "Total Open File Descriptors: $fd_count" >> /tmp/memory.txt
            if [[ -n "$fd_sample" ]]; then
                echo "Sample File Descriptors:" >> /tmp/memory.txt
                echo "$fd_sample" >> /tmp/memory.txt
            fi
        fi
    done

    file=/tmp/memory.txt
    echo -e "Warning, Memory is running low! on $ip\n Free memory: $free MB" | mailx -a "$file" -s "$subject" "$to"
fi

exit 0

Key Changes Explained:

  • Fixed IP Extraction: The original regex was missing a dot, which would have pulled incorrect IPs. Updated to capture valid IPv4 addresses properly.
  • Structured Output: Added clear section headers to make /tmp/memory.txt easier to parse when you receive the alert email.
  • PID Extraction: We first grab the PIDs of the top 10 memory processes (skipping the header line from ps), then loop through each PID to collect FD data.
  • FD Count Without lsof: Used /proc/$pid/fd (a Linux proc filesystem entry) to count open FDs—this is lightweight and doesn't require installing extra tools like lsof. If you want more detailed FD info (like what each FD points to), replace the ls /proc/$pid/fd lines with lsof -p $pid (just ensure lsof is installed on your system).
  • Error Handling: Added checks for valid PIDs and redirected error output (2>/dev/null) to avoid messy errors if a process exits before we can collect its data.
  • Optional FD Sample: Included a sample of the first 5 open FDs to give context without making the alert file too large—you can remove this section if you only need the count.

Quick Tweaks You Can Make:

  • Adjust head -10 in the ps commands to capture more/fewer top memory processes.
  • If you don't need the FD sample, delete the lines related to fd_sample.
  • Replace the /proc FD count with lsof -p $pid | wc -l if you have lsof and want a more granular breakdown of FD types.

内容的提问来源于stack exchange,提问作者voltas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:12:03