温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

zabbix监控如何用脚本监控redis服务

发布时间:2020-06-08 10:05:11 来源:亿速云 阅读:507 作者:Leah 栏目:系统运维

zabbix监控如何用脚本监控redis服务?这个问题可能是我们日常学习或工作经常见到的。通过这个问题,希望你能收获更多。今天跟随小编一起来看解决方案吧。

第一步:编写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/" rel="nofollow">br/>length=${#port[@]}<br/printf "{\n"
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 模板配置

zabbix监控如何用脚本监控redis服务

zabbix监控如何用脚本监控redis服务

zabbix监控如何用脚本监控redis服务

zabbix监控如何用脚本监控redis服务

触发器

zabbix监控如何用脚本监控redis服务
{HOST.NAME}:{#REDIS_PORT} redis ping异常,请检查
{Template for redis:redis.liveness[{#REDIS_PORT}].str(PONG)}=0

zabbix监控如何用脚本监控redis服务

{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

看完上述内容,你们掌握在zabbix中用脚本监控redis服务的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI