一、编写自动发现脚本

[root@test-db zabbix_agentd.d]# cat /etc/zabbix/monitor_sh/redis_discovery.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-


import re
import json
import os

CONF_DIR = '/data/redis/conf'

pattern = re.compile(r'redis\S+.conf')

def filterlist():
    #获取dir列表
    conf_list = os.listdir(CONF_DIR)
    new_filet_list = []
    for i in conf_list:
        data = {}
        if os.path.isfile(os.path.join(CONF_DIR,i)) and pattern.search(i):
            with open(os.path.join(CONF_DIR,i),'r') as f:
                for line in f:
                    bind_ip = re.findall(r'^bind\s+(\d+\.\d+\.\d+\.\d+)',line)
                    bind_port = re.findall(r'^port\s+(.*)', line)
                    bind_pass = re.findall(r'^requirepass\s+(.*)', line)
                    if bind_ip:
                        ip = ''.join(bind_ip)
                        if ip == '0.0.0.0':
                            ip = '127.0.0.1'
                        data['{#IP}'] = ip
                    if bind_port:
                        data['{#PORT}'] = ''.join(bind_port)
                    if bind_pass:
                        #print bind_pass
                        str_pass = ''.join(bind_pass)
                        #print str_pass
                        password = str_pass.strip('"')
                        #print password
                        data['{#PASSWORD}'] = password
                        #print password
                        #print 'data:',data
                    #else:
                    # password = 'null'
                    # data['{#PASSWORD}'] = password


            new_filet_list.append(data)

    #print 'data',data
    return json.dumps({'data':new_filet_list},sort_keys=True,indent=4,separators=(',',':'))

if __name__ == "__main__":
    result = filterlist()
    print result

二、编写redis监控脚本

[root@test-db zabbix_agentd.d]# cat /etc/zabbix/monitor_sh/redis.sh
#!/bin/bash
# Redis状态及info信息监控脚本
# Author yousong.xiang 250919938@qq.com
# Date 2019.03.13,2020.11.12
# v1.0.2
# 添加百分比监控(现redis服务做成自动发现目标,之前通过模板取值方式不兼容)

[ -f /etc/profile ] && . /etc/profile

REDIS="redis-cli"

if [ # -ne 4 ]; then
    echo "input error"
    exit 9
fi

HOST=1
PORT=2
PASS=3
CURRENT=4

#以下为兼容redis无密码状态,传参值为null则赋值为''
if [ "{PASS}" == "" ]; then
    PASS=
fi

function RedisPing() {
    RESULT=`{REDIS} -h{HOST} -p {PORT} -a "{PASS}" ping |grep -c PONG `
    if [ {RESULT} -ne 1 ]; then
        ping_result=1
        echoping_result
        exit 7
    else
        ping_result=0
        echo ping_result
    fi
}


function RedisStatus() {
    REDIS_CLI="{REDIS} -h {HOST} -p{PORT} -a "{PASS}" info"
    result=`{REDIS_CLI}|grep -w {CURRENT}| awk -F':' '{print2}'`
    echo {result}

}


function used_memory_human() {
    code=`RedisPing`
    result=`RedisStatus`
    # 以下分别写入三种条件判断,因为结果唯一,可以这么写,之后再优化
    M_CODE=`echo{result}|grep -i m |wc -l`
    G_CODE=`echo {result}|grep -i g |wc -l`
    K_CODE=`echo{result}|grep -i k |wc -l`

   if [ "{M_CODE}" == "1" ]; then
       code=`echo{result}|awk -F '.' '{print 1}'`
   fi

   if [ "{G_CODE}" == "1" ]; then
       code=`echo {result}|awk -F 'G' '{print1}'`
       code=`echo "{code}*1024"|bc`
   fi


   if [ "{K_CODE}" == "1" ]; then
       code='1'
   fi

    echo {code%.*}
}

function maxmemory_human() {
    code=`RedisPing`
    result=`RedisStatus`

    # 以下分别写入三种条件判断,因为结果唯一,可以这么写,之后再优化
    M_CODE=`echo{result}|grep -i m |wc -l`
    G_CODE=`echo {result}|grep -i g |wc -l`
    K_CODE=`echo{result}|grep -i k |wc -l`

   if [ "{M_CODE}" == "1" ]; then
       code=`echo{result}|awk -F '.' '{print 1}'`
   fi

   if [ "{G_CODE}" == "1" ]; then
       code=`echo {result}|awk -F 'G' '{print1}'`
       code=`echo "{code}*1024"|bc`
   fi


   if [ "{K_CODE}" == "1" ]; then
       code='1'
   fi

   echo {code%.*}
}
case4 in
  used_memory_human)
    code=`used_memory_human`
    echo code
  ;;
  maxmemory_human)
    code=`maxmemory_human`
    echocode
  ;;
  connected_clients)
    code=`RedisPing`
    code=`RedisStatus *`
    echo{code}
  ;;

  role)
    code=`RedisPing`
    result=`RedisStatus *|awk -F "\r" '{print1}'`
    #echo "{result}...."
    if [ "{result}" == "master" ]; then
        code='1'
    else
        code='0'
    fi
    echo {code}
  ;;

  ping)
    code=`RedisPing`
    echo{code}
  ;;
  prop)
    CURRENT='used_memory_human'
    use_code=`used_memory_human`
    use_code={use_code:-0}
    CURRENT='maxmemory_human'
    max_code=`maxmemory_human`
    max_code={max_code:-2000}

    code=`echo "100*use_code/max_code"|bc`
    echo $code
  ;;
  *)
    echo "USAGE: used_memory_human|maxmemory_human|connected_clients|role|ping|prop"
  ;;
esac

三、zabbix-agent配置文件

[root@test-db zabbix_agentd.d]# cat ../zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=30
HostMetadataItem=system.uname
Server=192.168.0.42
ServerActive=192.168.0.42
Include=/etc/zabbix/zabbix_agentd.d/*.conf
Hostname=192.168.0.146
UnsafeUserParameters=1

四、后台添加模板

4.1监控项原型

4.2触发器原型

4.3图形原型

五、查看图形信息

最后修改日期: 2024年1月6日

作者

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。