cheng029 发表于 2012-8-19 23:01:41

nagios插件-查看redis的内存使用率

nagios插件-查看redis的内存使用率
      使用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,"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()

aa0660 发表于 2013-3-13 15:26:22

谢谢楼主,共同发展

llcong 发表于 2013-5-15 19:41:00

不要在一棵树上吊死,在附近几棵树上多试试死几次~

olga 发表于 2013-5-16 19:13:54

自从我变成了狗屎,就再也没有人踩在我头上了。

hti 发表于 2013-5-17 23:51:43

不要在一棵树上吊死,在附近几棵树上多试试死几次~

华风 发表于 2013-5-19 04:52:24

小手一抖,钱钱到手!

zzbb 发表于 2013-5-20 09:15:09

内练一口气,外练一口屁。
页: [1]
查看完整版本: nagios插件-查看redis的内存使用率