Linux自定义监控项
一、监控CPU和内存前10
1、编写自动发现脚本
mkdir /script/ -p
cat > /script/process_check.sh <<'EOF'
#!/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"
EOF
2、CPU和内存资源使用情况取值脚本
cat > /script/process_check.sh << 'EOF'
#!/bin/bash
#system process CPU&MEM use information
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
EOF
3、创建监控项
cat > /etc/zabbix/zabbix_agentd.d/discover.conf << EOF
UserParameter=discovery.process,/script/discovery_process.sh
UserParameter=process.check[*],/script/process_check.sh $1 $2 $3
EOF
chmod +x /script/*.sh
二、创建模板
1、新建模板
Template linux_discovery_process_status
此模板下新建应用集
process_cpu_status
process_mem_status
2、添加自动发现规则
名称 :process cpu and mem status discovery
键值: discovery.process
3、添加监控原型
1、CPU
名称:cpu_percent:{#PROCESS_NAME}
键值:process.check[avg,cpu,{#PROCESS_NAME}]
信息类型:浮点数
单位:%
应用集:process_cpu_status (可新创建)
2、内存
名称: mem_percent{#PROCESS_NAME}
键值: process.check[pre,mem,{#PROCESS_NAME}]
信息类型:浮点说
单位:%
应用集:process_mem_status
三、获取最新数据
1、CPU
2、内存