判断Redis运行状况是否正常的Python监控脚本

import commands
 
'''
    execute system command
'''
def execSysCmd(cmd):
    (status,output) = commands.getstatusoutput(cmd)
    return (status,output)
 
CHECK_REDIS_OK_CMD = "(echo -en \"PING\r\nset pivot damnit\r\nget pivot\r\n\"; sleep 1) | nc 10.81.31.95 16381"
(status,output) = execSysCmd(CHECK_REDIS_OK_CMD)
if status != 0:
    print "Redis not ok!!!"
    print output
    exit(1)
else:
    res = output.split('\n')
    print res
    if len(res) != 4:
        print "Redis server not work"
        exit(1)
print "Redis is working!!!"
exit(0)

 

如果正常输出如下:

[‘+PONG\r’, ‘+OK\r’, ‘$6\r’, ‘damnit\r’]

Redis is working!!!

Leave a Comment.