您好,登錄后才能下訂單哦!
使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。