使用python写的一个nagios插件,主要实现的功能就是查看redis的内存使用率,写这个插件起初是因为公司服务器的redis一个端口的内存使用完了,导致公司网站访问出现异常,所以写了这个插件來检测redis的内存使用率。
使用方法见脚本:check_redis_mem
- #!/usr/bin/env python
- #encoding=utf8
- #需要给python安装redis插件,安装方法:#easy_install redis
- import redis
- import sys
- import getopt
- def usage():
- print """
- Usage:
- check_redis_mem [-h|--help][-H|--hostname][-P|--port][-w|--warning][-c|--critical]
- Options:
- --help|-h)
- print check_redis_mem help.
- --host|-H)
- Sets connect host.
- --port|-P)
- Sets connect port.
- --warning|-w)
- Sets a warning level for redis mem userd. Default is: on
- --critical|-c)
- Sets a critical level for redis mem userd. Default is: on
- Example:
- ./check_redis_mem -H 127.0.0.1 -P 6379 -w 80 -c 90 or ./check_redis_mem -H 127.0.0.1 -P 6379
- This should output: mem is ok and used 10.50%"""
- sys.exit(3)
- try:
- options,args = getopt.getopt(sys.argv[1:],"hH:P:w:c:",["help","host=","port=","warning=","critical="])
- except getopt.GetoptError as e:
- usage()
- warning = 75
- critical = 85
- host = ''
- port = 0
- for name,value in options:
- if name in ("-h","--help"):
- usage()
- if name in ("-H","--host"):
- host = value
- if name in ("-P","--port"):
- port = int(value)
- if name in ("-w","--warning"):
- warning = value
- if name in ("-c","--critical"):
- critical = value
- if host == '' or port == 0:
- usage()
- try:
- r = redis.Redis(host=host,port=port)
- if r.ping() == True:
- maxmem = r.config_get(pattern='maxmemory').get('maxmemory')
- usedmem = r.info().get('used_memory')
- temp=float(usedmem) / float(maxmem)
- tmp = temp*100
- if tmp >= warning and tmp < critical:
- print "mem is used %.2f%%" % (tmp)
- sys.exit(1)
- elif tmp >= critical:
- print "mem is used %.2f%%" % (tmp)
- sys.exit(2)
- else:
- print "It's ok and mem is used %.2f%%" % (tmp)
- sys.exit(0)
- else:
- print "can't connect."
- sys.exit(2)
- except Exception as e:
- print e.message
- usage()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。