温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-27 06:02:59 来源:网络 阅读:778 作者:zhhmj 栏目:移动开发

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

 

      使用python写的一个nagios插件,主要实现的功能就是查看redis的内存使用率,写这个插件起初是因为公司服务器的redis一个端口的内存使用完了,导致公司网站访问出现异常,所以写了这个插件來检测redis的内存使用率。

      使用方法见脚本:check_redis_mem

  1. #!/usr/bin/env python 
  2. #encoding=utf8 
  3.  
  4. #需要给python安装redis插件,安装方法:#easy_install redis 
  5. import redis 
  6. import sys 
  7. import getopt 
  8.  
  9. def usage(): 
  10.     print """ 
  11. Usage: 
  12.  
  13. check_redis_mem [-h|--help][-H|--hostname][-P|--port][-w|--warning][-c|--critical] 
  14.  
  15. Options: 
  16.     --help|-h) 
  17.         print check_redis_mem help. 
  18.     --host|-H) 
  19.         Sets connect host. 
  20.     --port|-P) 
  21.         Sets connect port. 
  22.     --warning|-w) 
  23.         Sets a warning level for redis mem userd. Default is: on 
  24.     --critical|-c) 
  25.         Sets a critical level for redis mem userd. Default is: on 
  26. Example: 
  27.     ./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 
  28.     This should output: mem is ok and used 10.50%""" 
  29.     sys.exit(3
  30.  
  31. try
  32.     options,args = getopt.getopt(sys.argv[1:],"hH:P:w:c:",["help","host=","port=","warning=","critical="]) 
  33. except getopt.GetoptError as e: 
  34.     usage() 
  35.  
  36. warning = 75 
  37. critical = 85 
  38. host = '' 
  39. port = 0 
  40.  
  41. for name,value in options: 
  42.     if name in ("-h","--help"): 
  43.         usage() 
  44.     if name in ("-H","--host"): 
  45.         host = value 
  46.     if name in ("-P","--port"): 
  47.         port = int(value) 
  48.     if name in ("-w","--warning"): 
  49.         warning = value 
  50.     if name in ("-c","--critical"): 
  51.         critical = value 
  52.  
  53. if host == '' or port == 0
  54.     usage() 
  55.  
  56. try
  57.     r = redis.Redis(host=host,port=port) 
  58.     if r.ping() == True
  59.         maxmem = r.config_get(pattern='maxmemory').get('maxmemory'
  60.         usedmem = r.info().get('used_memory'
  61.         temp=float(usedmem) / float(maxmem) 
  62.         tmp = temp*100 
  63.  
  64.         if tmp >= warning and tmp < critical: 
  65.             print "mem is used %.2f%%" % (tmp) 
  66.             sys.exit(1
  67.         elif tmp >= critical: 
  68.             print "mem is used %.2f%%" % (tmp) 
  69.             sys.exit(2
  70.         else
  71.             print "It's ok and mem is used %.2f%%" % (tmp) 
  72.             sys.exit(0
  73.     else
  74.         print "can't connect." 
  75.         sys.exit(2
  76. except Exception as e: 
  77.     print e.message 
  78.     usage() 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI