zabbix stand-alone multi-instance monitoring scripts redis
The company save costs and make full use of resources, deployed single instance multi-line situation.
As a python novice, decisively I decided to write a very low script, and then implement monitoring.
The following script implements, discovery automatic discovery, then realized, automatic reporting and other monitoring functions of information through a status monitor.
Which is very low place that script on each host must be defined redis local information, such as ports, passwords and the like; in fact, this can be obtained by configuring the center, but because sharing it, so I wrote the example of a defined ; there are generally a little big God to write their own socket to send data, I have here is very simple, through the shell, using a simple easy way.
#!/usr/bin/env python3.6
"""
@author:zmoke
@file: redis_monitor.py
@version:
@time: 2019/04/24
@email:[email protected]
@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): # 建立连接
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])