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://pwwzsj.com/article/ipcsj.html