Get the App
SLTechnology News&Howtos  ›  Servers  › 

Redis monitoring zabbix automatic monitoring ports and memory (shell and

Shulou Source: shulou.com Published: 2022-06-02 09:18:39 12月04日 Update

Step 1: write the port discovery script redis_port.py for redis python

#! / 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= (',',':))

Write a port discovery script check_redis_port.sh for redis shell

#! / bin/sh

REDIS_SERVER_PIDS= "$(ps-ef | egrep-v 'grep | egrep | vi | vim | find | cat | tac | head | 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] [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 / 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 | 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

Percentage of memory usage monitored

#! / 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 | 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 template configuration

Automatic discovery

Trigger

{HOST.NAME}: {# REDIS_PORT} redis ping exception, please check

{Template for redis:redis.liveness [{# REDIS_PORT}] .str (PONG)} = 0

{HOST.NAME}: {# REDIS_PORT} redis memory utilization > 80%, reaching {ITEM.LASTVALUE1}%

{Template for redis:redis_used_memory_per [{# REDIS_PORT}] .last ()} > 80

Restart after zabbix configuration file is added

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

Tags: Port configuration memory monitoring script usage situation file permissions template percentage percentage trigger check selection monitoring automation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi Shulou Tech Info vpn Shulou Information