#!/usr/bin/python import json import os import subprocess import time import syslog def ping_redis(cmd): output = '' while True: try: output = subprocess.check_output(cmd, shell=True) except subprocess.CalledProcessError as e: syslog.syslog(syslog.LOG_ERR, 'ping redis failed, cmd : {}'.format(cmd)) if 'PONG' in output: break syslog.syslog(syslog.LOG_ERR, 'ping response : {}'.format(output)) time.sleep(1) database_config_file = "/var/run/redis/sonic-db/database_config.json" data = {} while True: if os.path.isfile(database_config_file): with open(database_config_file, "r") as read_file: data = json.load(read_file) break time.sleep(1) syslog.syslog(syslog.LOG_ERR, 'config file {} does not exist right now'.format(database_config_file)) while True: if 'INSTANCES' in data: for inst in data["INSTANCES"]: port = data["INSTANCES"][inst]["port"] cmd = "redis-cli -p " + str(port) + " ping" ping_redis(cmd) break time.sleep(1) syslog.syslog(syslog.LOG_ERR, 'config file {} does not have INSTANCES'.format(database_config_file))