zabbix监控常用脚本

1、磁盘发现脚本:

磁盘自动发现脚本:vim  /etc/zabbix/zabbix_scripts/disk_discovery.sh

#!/bin/bash
disk_array=(`grep -E "(vd[a-z]$|sd[a-z]$)" /proc/partitions | awk '{print $4}'`)
length=${#disk_array[@]}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
printf '\n\t\t{'
printf "\"{#DISK_NAME}\":\"${disk_array[$i]}\"}"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "\n\t]\n"
printf "}\n"

2、磁盘监控脚本:

磁盘状态监控脚本:vim  /etc/zabbix/zabbix_scripts/disk_status.sh

#!/bin/bash
#UserParameter=custom.vfs.dev.read.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$4}' //磁盘读的次数
#UserParameter=custom.vfs.dev.read.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}' //磁盘读的毫秒数
#UserParameter=custom.vfs.dev.write.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}' //磁盘写的次数
#UserParameter=custom.vfs.dev.write.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}' //磁盘写的毫秒数
#UserParameter=custom.vfs.dev.io.active[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}'
#UserParameter=custom.vfs.dev.io.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}' //花费在IO操作上的毫秒数
#UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}' //读扇区的次数(一个扇区的等于512B)
#UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}' //写扇区的次数(一个扇区的等于512B)
Device=$1
DISK=$2
case $DISK in
rops)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $4}'
;;
readms)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $7}'
;;
wops)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $8}'
;;
writems)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $11}'
;;
ioactive)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $12}'
;;
ioms)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $13}'
;;
readsectors)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $6}'
;;
writesectors)
cat /proc/diskstats | grep $1 | head -1 | awk '{print $10}'
;;
DiskAvaliable)
df -k | head -2 | grep "\b$Device\b" | awk '{print $4}'
;;
*)
echo -e "\e[033mUsage: sh $0 [rrqm|wrqm|rps|wps|rKBps|wKBps|avgqu-sz|avgrq-sz|await|svctm|util]\e[0m"
esac

3、进程发现脚本: 

vim  /etc/zabbix/zabbix_scripts/process_discovery.sh

#!/bin/bash
 
File="/etc/zabbix/zabbix_scripts/search_proess_name.list"
IFS=$'\n'
discovery () {
Proess_Name_Port=($(cat $File|grep -v "^#"))
printf '{\n'
printf '\t"data":[\n'
num=${#Proess_Name_Port[@]}
for site in ${Proess_Name_Port[@]}
do
num=$(( $num - 1 ))
name=$(echo $site|awk '{print $1}')
port=$(echo $site|awk '{print $2}')
if [ $num -ne 0 ] ; then
printf "\t\t{\"{#PROCESS_NAME}\":\""%s"\",\"{#PORCESS_PORT}\":\""${port}"\"},\n" ${name}
else
printf "\t\t{\"{#PROCESS_NAME}\":\""%s"\",\"{#PORCESS_PORT}\":\""${port}"\"}\n" ${name}
printf '\t]\n'
printf '}\n'
fi
done
}
discovery

进程发现列表文件:

vim /etc/zabbix/zabbix_scripts/search_proess_name.list

## 程序名称 端口(端口也是可以使用名称代替的)
zookeeper 2181
#rocketmq 9876
#supervisord 9001
#rocketmq-console-ng 9877
#xxl-job-executor-sample-springboot 999999
#dubbo-admin-server 8099
#xxl-job-admin 8888
#bc-admin-server 999999
redis集群监控脚本:
vim  /etc/zabbix/zabbix_scripts/redis-cluster.sh
#!/bin/bash
 
REDISCLI="/usr/local/src/app/redis-5.0.5/src/redis-cli"
HOST=$1
PORT=$2
 
if [[ $# == 3 ]];then
case $3 in
 
cluster_info)
$REDISCLI -h $HOST -p $PORT -c cluster info
;;
 
cluster_nodes)
$REDISCLI -h $HOST -p $PORT -c cluster nodes
;;
cluster_state)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_state" | awk -F':' '{print $2}'| grep -c ok)
echo $result
;;
cluster_slots_assigned)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_slots_assigned" | awk -F':' '{print $2}')
echo $result
;;
cluster_slots_ok)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_slots_ok" | awk -F':' '{print $2}')
echo $result
;;
cluster_slots_pfail)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_slots_pfail" | awk -F':' '{print $2}')
echo $result
;;
cluster_slots_fail)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_slots_fail" | awk -F':' '{print $2}')
echo $result
;;
cluster_known_nodes)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_known_nodes" | awk -F':' '{print $2}')
echo $result
;;
cluster_size)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_size" | awk -F':' '{print $2}')
echo $result
;;
cluster_current_epoch)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_current_epoch" | awk -F':' '{print $2}')
echo $result
;;
cluster_my_epoch)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_my_epoch" | awk -F':' '{print $2}')
echo $result
;;
cluster_stats_messages_ping_sent)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_stats_messages_ping_sent" | awk -F':' '{print $2}')
echo $result
;;
cluster_stats_messages_pong_sent)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_stats_messages_pong_sent" | awk -F':' '{print $2}')
echo $result
;;
cluster_stats_messages_sent)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_stats_messages_sent" | awk -F':' '{print $2}')
echo $result
;;
cluster_stats_messages_ping_received)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_stats_messages_ping_received" | awk -F':' '{print $2}')
echo $result
;;
cluster_stats_messages_pong_received)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_stats_messages_pong_received" | awk -F':' '{print $2}')
echo $result
;;
cluster_stats_messages_received)
result=$($REDISCLI -h $HOST -p $PORT -c cluster info | grep -w "cluster_stats_messages_received" | awk -F':' '{print $2}')
echo $result
;;
*)
echo -e "\033[33mUsage: $0 {cluster_state|cluster_slots_assigned|cluster_slots_ok|cluster_slots_pfail|cluster_slots_fail|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_known_nodes|cluster_size|cluster_current_epoch|cluster_my_epoch|cluster_stats_messages_ping_sent|cluster_stats_messages_pong_sent|cluster_stats_messages_sent|cluster_stats_messages_ping_received|cluster_stats_messages_pong_received|cluster_stats_messages_received}\033[0m"
;;
esac
fi

zookeeper 集群监控脚本:vim  /etc/zabbix/zabbix_scripts/zookeeper_status.sh

#!/bin/bash
zk=$1
case $zk in
 ## 注册节点数
 zk_znode_count)
 echo "mntr" | /usr/bin/nc localhost 2181 | grep zk_znode_count | awk '{print $2}'
 ;;
 ## 使用内存近视值
 zk_approximate_data_size)
 echo "mntr" | /usr/bin/nc localhost 2181 | grep zk_approximate_data_size | awk '{print $2}'
 ;;
 ## zk_watch 数量
 zk_watch_count)
 echo "mntr" | /usr/bin/nc localhost 2181 | grep zk_watch_count | awk '{print $2}'
 ;;
 ## 等待列队的长度
 zk_outstanding_requests)
 echo "mntr" | /usr/bin/nc localhost 2181 | grep zk_outstanding_requests | awk '{print $2}'
 ;;
 ## 活动连接数
 zk_num_alive_connections)
 echo "mntr" | /usr/bin/nc localhost 2181 | grep zk_num_alive_connections | awk '{print $2}'
 ;;
 *)
 echo -e "\e[033mUsage: sh $0 [rrqm|wrqm|rps|wps|rKBps|wKBps|avgqu-sz|avgrq-sz|await|svctm|util]\e[0m"
esac

zabbix配置监控的键值:vim /etc/zabbix/zabbix_agentd.d/userparameter_yunwei.conf

# redis
UserParameter=redis.status[*],/etc/zabbix/zabbix_scripts/redis_status.sh $1 $2 $3
 
# disk
UserParameter=disk.discovery[*],/etc/zabbix/zabbix_scripts/disk_discovery.sh
 
UserParameter=disk.io[*],/etc/zabbix/zabbix_scripts/disk_stat.sh $1 $2
UserParameter=disk.io.status[*],/etc/zabbix/zabbix_scripts/disk_status.sh $1 $2
 
# proess
UserParameter=proess.discovery[*],/etc/zabbix/zabbix_scripts/process_discovery.sh
UserParameter=proess.status[*],/etc/zabbix/zabbix_scripts/process_status.sh $1 $2 $3
 
# zk status
UserParameter=zookeeper.status[*],/etc/zabbix/zabbix_scripts/zookeeper_status.sh $1

 

 

 

 

posted @ 2020-06-17 02:29  小毅哥博客  阅读(1710)  评论(0编辑  收藏  举报