写一个Python监控Redis内存使用的脚本,用来练习getopt相关模块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#!/usr/bin/env python # -*- coding:utf-8 -*- import redis import sys import getopt import json import requests def Usage(): print """ --help -h, --host [string] -p, --port [int] -a, --password [string] -i, --item [mup|mu] -w, --warn [int] ./check_redis.py -h 127.0.0.1 -p 6379 -a '123456' -i mup|mu -w 80 -P 12100232222 """ def SendSMS(Message): for i in args: Phone = i url = "http://www.ywnds.com/smstemplate/service/SendMessage?" #Message = "Redis instance %s:%s use memory >" %(Host,Port) + " " + "%" + "%s, Current use memory:%s" %(Warn,used_memory) payload = {'account':'admin','password':'123456','mobile':Phone,'message':Message} headers = {'content-type': 'application/json'} requests.post(url,data=json.dumps(payload),headers=headers) def Redis_Conn(Host,Port,Password): try: pool = redis.ConnectionPool(host=Host, port=Port, password=Password) r = redis.Redis(connection_pool=pool) return r except: print "Redis can not connected or command is error." def Redis_Memuse_Percent(Warn,Phone): maxmemory = r.config_get()['maxmemory'] if maxmemory == '0': print "Redis not set maxmemory." sys.exit(1) else: used_memory = r.info()['used_memory'] result = ("%.0f" % (float(used_memory) / float(maxmemory) * 100)) print "result(M): %s" % result print "used_memory(Byte): %s" % used_memory print "maxmemory(Byte): %s" % maxmemory if (int(result) > int(Warn)): Message = "Redis instance %s:%s use memory percent >" %(Host,Port) + " " + "%" + "%s, Current use memory:%s bytes" %(Warn,used_memory) SendSMS(Message) else: print "Redis memroy use no except" def Redis_Memuse(Warn,Phone): used_memory = r.info()['used_memory'] maxmemory = r.config_get()['maxmemory'] result = ("%.0f" % float(used_memory/1024/1024)) print "result(M): %s" % result print "used_memory(Byte): %s" % used_memory print "maxmemory(Byte): %s" % maxmemory if int(result) > int(Warn): Message = "Redis instance %s:%s use memory > %s" %(Host,Port,Warn) + "M" + ", Current use memory:%s bytes" %(used_memory) SendSMS(Message) else: print "Redis memroy use no except" if "__main__" == __name__: shortargs = 'h:p:a:i:w:P' longargs = ['help','host=','port=','password=','item=','warn=','phone'] try: opts, args = getopt.getopt(sys.argv[1:], shortargs, longargs) except getopt.GetoptError: usage() sys.exit(1) for o, a in opts: if o == "--help": Usage() elif o in ("-h","--host"): Host = a elif o in ("-p","--port"): Port = int(a) elif o in ("-a","--password"): Password = a elif o in ("-i","--item"): Item = a elif o in ("-w","--warn"): Warn = int(a) elif o in ("-P","--phone"): Phone = a else: print "please check the host or opts" Usage() sys.exit(1) r = Redis_Conn(Host,Port,Password) if Item == "mup": Redis_Memuse_Percent(Warn,Phone) elif Item == "mu": Redis_Memuse(Warn,Phone) else: Usage() sys.exit(1) |
短信模块部分给的是伪路径,自行定义成短信或邮件即可。
使用方式:
1 |
$ python test.py -h 127.0.0.1 -p 6379 -a 'password' -w 3 -i mup -P 13688888888 13611111111 |
完结。。。