监控CPU、内存使用率前十的进程

1.查看CPU使用率最高的前十个进程

ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head

2.查看内存使用率最高的前十个进程

ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head

3. Zabbix监控 进阶脚本

3.1.自动发现前十进程服务脚本 discovery_process.sh

#!/bin/bash
#system process discovery script
top -b -n 1 > /tmp/.top.txt && chown zabbix. /tmp/.top.txt
proc_array=(`tail -n +8 /tmp/.top.txt | awk {a[$NF]+=$10}END{for(k in a)print a[k],k}|sort -gr|head -10|cut -d" " -f2`)
length=${#proc_array[@]}
 
printf "{
"
printf 	""data":["
for ((i=0;i<$length;i++))
do
    printf "
		{"
    printf ""{#PROCESS_NAME}":"${proc_array[$i]}"}"
    if [ $i -lt $[$length-1] ];then
        printf ","
    fi
done
    printf "
	]
"
printf "}
"


### 注意 awk {a[$NF]+=$10}END{for(k in a)print a[k],k} 此处的 $10 是值内存使用率的那一列,前十排序也根据每个进程的此列的总和值来判断

3.2 监控项获取数据脚本 process_check.sh

#!/bin/bash

mode=$1
name=$2
process=$3
mem_total=$(cat /proc/meminfo | grep "MemTotal" | awk {printf "%.f",$2/1024})
cpu_total=$(( $(cat /proc/cpuinfo | grep "processor" | wc -l) * 100 ))
 
function mempre {
    mem_pre=`tail -n +8 /tmp/.top.txt | awk {a[$NF]+=$10}END{for(k in a)print a[k],k} | grep "${process}" | cut -d" " -f1`
    echo "$mem_pre"
}
 
function memuse {
    mem_use=`tail -n +8 /tmp/.top.txt | awk {a[$NF]+=$10}END{for(k in a)print a[k]/100*${mem_total},k} | grep "${process}" | cut -d" " -f1`
    echo "$mem_use" | awk {printf "%.f",$1*1024*1024}
}
 
function cpuuse {
    cpu_use=`tail -n +8 /tmp/.top.txt | awk {a[$NF]+=$9}END{for(k in a)print a[k],k} | grep "${process}" | cut -d" " -f1`
    echo "$cpu_use"
}
 
function cpupre {
    cpu_pre=`tail -n +8 /tmp/.top.txt | awk {a[$NF]+=$9}END{for(k in a)print a[k]/(${cpu_total}),k} | grep "${process}" | cut -d" " -f1`
    echo "$cpu_pre"
}
 
 
case $name in
    mem)
        if [ "$mode" = "pre" ];then
            mempre
        elif [ "$mode" = "avg" ];then
            memuse
        fi
    ;;
    cpu)
        if [ "$mode" = "pre" ];then
            cpupre
        elif [ "$mode" = "avg" ];then
            cpuuse
        fi
    ;;
    *)
        echo -e "Usage: $0 [mode : pre|avg] [mem|cpu] [process]"
esac

3.3 自定义监控项conf文件配置

UserParameter=discovery.process,/usr/local/zabbix_agents/conf/zabbix_agentd/discovery_process.sh
UserParameter=process.check[*],/usr/local/zabbix_agents/conf/zabbix_agentd/process_check.sh $1 $2 $3

3.4 zabbix lld监控项配置

经验分享 程序员 微信小程序 职场和发展