redis监控zabbix自动化监测端口和内存(shell和
第一步:编写redis python 的端口发现脚本 redis_port.py
#!/usr/bin/env python
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的揭西网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
import os
import json
t=os.popen("""ps aux |grep -v grep |grep redis-server|awk -F '*:' '{print $2}'|awk '{print $1}' """)
ports = []
for port in t.readlines():
r = os.path.basename(port.strip())
ports += [{'{#REDIS_PORT}':r}]
print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':'))
编写redis shell 的端口发现脚本 check_redis_port.sh
#!/bin/sh
REDIS_SERVER_PIDS="$(ps -ef|egrep -v 'grep|egrep|vi|vim|find|cat|tac|head|tail|more|less'|grep 'redis-server'|awk '{print $2}')"
REDIS_PORTS=""
for pid in ${REDIS_SERVER_PIDS};do
PORTS=$(sudo netstat -anlp|grep -w ${pid}|awk '{print $4}'|awk -F: '{print $2}'|grep -v "[0-9][0-9][0-9][0-9][0-9]"|grep -v "^$" |head -1)
REDIS_PORTS="${PORTS} ${REDIS_PORTS}"
done
port=(${REDIS_PORTS})
function node_port {length=${#port[@]}
br/>length=${#port[@]}
printf '\t'"\"data\":["
for ((i=0;i<$length;i++));do
printf '\n\t\t{'
printf "\"{#REDIS_PORT}\":\"${port[$i]}\"}"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "\n\t]\n"
printf "}\n"
}
node_port
修改权限: chmod 755 redis_port.py check_redis_port.sh
shell和python选择一种即可。
第二步:监测端口存活
#!/bin/bash
function redis_cli_command () {
redis_cli_tmp="$(whereis redis-cli|awk -F: '{print $2}')"
${redis_cli_tmp} -v > /dev/null 2>&1
if [ $? -eq 0 ];then
redis_cli="${redis_cli_tmp}"
else
REDIS_USERS="$(ps -ef|egrep -v 'grep|egrep|vi|vim|cat|tac|head|tail|more|less'|grep 'redis-server'|awk '{print $1}'|sort|uniq)"
for user in ${REDIS_USERS};do
USER_HOME="$(cat /etc/passwd|grep ${user}|awk -F: '{print $(NF-1)}')"
if [ -d ${USER_HOME} ];then
redis_cli_tmp="$(sudo find ${USER_HOME}/redis* -type f -name redis-cli|head -1)"
${redis_cli_tmp} -v > /dev/null 2>&1
[ $? -eq 0 ] && redis_cli="${redis_cli_tmp}" && break
fi
done
fi
}
hostip=/sbin/ifconfig|grep 10.169.42| awk '{print $2}'
redis_cli_command
liveness=$(${redis_cli} -h ${hostip} -p $1 ping)
if [ ${liveness} == "PONG" ];then
echo "PONG"
else
echo "error"
fi
监测内存使用情况百分比
#!/bin/bash
function redis_cli_command () {
redis_cli_tmp="$(whereis redis-cli|awk -F: '{print $2}')"
${redis_cli_tmp} -v > /dev/null 2>&1
if [ $? -eq 0 ];then
redis_cli="${redis_cli_tmp}"
else
REDIS_USERS="$(ps -ef|egrep -v 'grep|egrep|vi|vim|cat|tac|head|tail|more|less'|grep 'redis-server'|awk '{print $1}'|sort|uniq)"
for user in ${REDIS_USERS};do
USER_HOME="$(cat /etc/passwd|grep ${user}|awk -F: '{print $(NF-1)}')"
if [ -d ${USER_HOME} ];then
redis_cli_tmp="$(sudo find ${USER_HOME}/redis* -type f -name redis-cli|head -1)"
${redis_cli_tmp} -v > /dev/null 2>&1
[ $? -eq 0 ] && redis_cli="${redis_cli_tmp}" && break
fi
done
fi
}
hostip=/sbin/ifconfig|grep 10.169.42| awk '{print $2}'
redis_cli_command
used_memory=$(${redis_cli} -h ${hostip} -p $1 info|grep 'used_memory:'|awk -F: '{print $2}'|tr -d '\r')
max_memory=$(${redis_cli} -h ${hostip} -p $1 config get maxmemory|tail -1|awk '{print $NF}'|tr -d '"')
if [ ${max_memory} -eq 0 ];then
echo "0"
elif [ ${max_memory} -gt 0 ];then
num_per=echo "scale=0;$used_memory*100"/${max_memory}|bc
echo $num_per
fi
zabbix 模板配置
触发器
{HOST.NAME}:{#REDIS_PORT} redis ping异常,请检查
{Template for redis:redis.liveness[{#REDIS_PORT}].str(PONG)}=0
{HOST.NAME}:{#REDIS_PORT} redis 内存使用率>80%,达到{ITEM.LASTVALUE1}%
{Template for redis:redis_used_memory_per[{#REDIS_PORT}].last()}>80
zabbix 配置文件添加 配置完重启
UserParameter=redis_port.discovery,/app/sinova/zabbix/scripts/check_redis_port.sh
UserParameter=redis_used_memory_per[*],/app/sinova/zabbix/scripts/redis_used_memory.sh $1
当前题目:redis监控zabbix自动化监测端口和内存(shell和
转载来源:http://ybzwz.com/article/gojgos.html