zabbix添加top10内存和cpu资源占用情况
系统并发量大了以后容易出现性能瓶颈,快速找出占用内存和cpu资源较多的进程,可以为快速定位问题提供参考
找出top10内存和cpu的进程可以作为参考
1.编写监控脚本
自动发现进程脚本
# vim /usr/local/zabbix_agents_3.2.0/scripts/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 "{\n" printf '\t'"\"data\":[" for ((i=0;i<$length;i++)) do printf "\n\t\t{" printf "\"{#PROCESS_NAME}\":\"${proc_array[$i]}\"}" if [ $i -lt $[$length-1] ];then printf "," fi done printf "\n\t]\n" printf "}\n"
# 具体的cpu,内存等资源使用情况取值脚本
# cat /usr/local/zabbix_agents_3.2.0/scripts/process_check.sh
#!/bin/bash #system process CPU&MEM use information #mail: mail@huangming.org 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 "\b${process}\b" | 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 "\b${process}\b" | 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 "\b${process}\b" | 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 "\b${process}\b" | 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
# 创建监控项
# vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd.conf
UserParameter=discovery.process,/usr/local/zabbix_agents_3.2.0/scripts/discovery_process.sh
UserParameter=process.check[*],/usr/local/zabbix_agents_3.2.0/scripts/process_check.sh $1 $2 $3
chown zabbix.zabbix /tmp/.top.txt
2.创建模板
Template linux_discovery_process_status
应用,方便后面观察结果使用:
process_cpu_status
process_mem_status
创建完模版之后,添加自动发现规则
Discovery rules -->Create discovesy rule
name :process cpu and mem status discovery
key : discovery.process
添加具体的Item prototypes
cpu:
process cpu and mem status discovery
name: cpu_percent{#PROCESS_NAME}
key: process.check[avg,cpu,{#PROCESS_NAME}]
内存:
Item prototypes --> Create item prototype
name: mem_percent{#PROCESS_NAME}
key: process.check[pre,mem,{#PROCESS_NAME}]
注意 unit 是 % ,数据类型是 float
查看监控数据