zabbix monitoring single machine multi instance redis script

zabbix monitoring single machine multi instance redis script

The company saves cost, makes full use of resources and deploys the online situation of single machine and multi instance.
As a new python User, I decided to write a very low script and then implement the monitoring function.

The following script realizes the automatic discovery, and then realizes the automatic reporting function of other monitoring information through a status monitoring.
The low point is that the script on each host must define the local redis information, such as port, password and so on. In fact, this can be obtained through the configuration center, but because it is shared, an instance of the definition is written. There is also a point that the gods usually write their own socket to send data. I'm very simple here, using a simple and easy way through shell Law.

#!/usr/bin/env python3.6

"""
@author:zmoke
@file: redis_monitor.py
@version:
@time: 2019/04/24
@email:zmoke1792@gmail.com
@function: 
"""

import redis
import subprocess
import sys
import json

class RedisApi(object):
    def __init__(self, name, redis_message):
        self.__redis_addr = redis_message[name][0]
        self.__redis_port = redis_message[name][1]
        self.__redis_password = redis_message[name][2]
        self.__zabbix_sender = '/usr/local/zabbix/bin/zabbix_sender'
        self.__zabbix_conf = '/usr/local/zabbix/conf/zabbix_agentd.conf'

    def connect(self):  # Establish connection
        try:
            pool = redis.ConnectionPool(host=self.__redis_addr, port=self.__redis_port, db=None, password=self.__redis_password)
            connection = redis.Redis(connection_pool=pool)
            return connection
        except Exception as e:
            print('redis connect err :%s' % e)

    def send_message(self, key, msg):
        zabbix_sender = self.__zabbix_sender, zabbix_conf = self.__zabbix_conf
        try:
            subprocess.Popen([zabbix_sender, "-c", zabbix_conf, "-k", key, "-o", msg],
                            shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            # print "send zabbix success"
        except Exception as e:
            print(f"Something went wrong while exectuting zabbix_sender : {e}")

if __name__ == "__main__":

    redis_message = {'test1': ['127.0.0.1', 6379, None], 'test2': ['127.0.0.1', 6382, None]}
    name = sys.argv[1]
    if name == 'discovery':
        data = {
            "data": [
            ]
        }

        for message in redis_message.keys():
            # print(redis_message[message][1])
            data["data"].append({"{#REDISPORT}": redis_message[message][1]})
        print(json.dumps(data, sort_keys=True, indent=4))
    else:
        api = RedisApi(name, redis_message=redis_message)
        con = api.connect()
        if con.ping():
            print(0)
        else:
            print(1)
        messages = con.info()

        for key in messages.keys():
            if key.startswith('db'):
                pass
            else:
                monitor_key = f'Redis.Info[{key}, {redis_message[name][1]}]'

                print(monitor_key, ":", messages[key])
                # api.send_message(key=key, msg=messages[key])

Tags: Operation & Maintenance Redis Zabbix shell JSON

Posted on Wed, 08 Jan 2020 06:39:00 -0800 by carrotcake1029