nagios --redis 监控脚本

发布时间:2020-06-01 03:01:39 作者:s270987763
来源:网络 阅读:1184

 目前脚本可以监控 redis内存使用率,fork时间

脚本使用方法:

监控内存使用率

./check_redis.py -H 192.168.1.100 -p 6379 -C memuse -w 80 -c 90

监控上次fork消耗时间(通常redis在进行fork时,redis服务的响应会有影响)

./check_redis.py -H 192.168.1.100 -p 6379 -C fork -w 3 -c 5

 

cat check_redis.py

  1. #!/bin/env python 
  2. #-*-encoding=utf8-*- 
  3. __author__ = 'songtao' 
  4.  
  5.            
  6.  
  7. import redis 
  8. import sys 
  9. import getopt 
  10.  
  11.  
  12. def usage(): 
  13.     print """  
  14.     -H  127.0.0.1 
  15.     -p  6379 
  16.     -C  [memuse|fork] 
  17.     -w 50 
  18.     -c 80 
  19.     ./check_redis.py -H 127.0.0.1 -p 6379 -C memuse -c 80 -w 90 
  20.     """ 
  21.     sys.exit(3) 
  22. #def conn_redis(host,port): 
  23. #    r = redis.Redis(hosthost=host,portport=port) 
  24. #    if r.ping(): 
  25. #        r = redis.Redis(hosthost=host,portport=port) 
  26. #        return r 
  27. #    else: 
  28. #        print "can not connect!!" 
  29. #        sys.exit(0) 
  30. #r = redis.Redis(hosthost=host,portport=port) 
  31.  
  32. warning = 80 
  33. critical = 90 
  34.  
  35.  
  36. def memused(): 
  37.     maxmem = r.config_get()['maxmemory'] 
  38.     usedmem = r.info()['used_memory'] 
  39.     result = float(usedmem) / float(maxmem) * 100 
  40.     if result >=warning and result < critical: 
  41.         print "Warning!;mem_used:%.2f%%|mem_used:%.2f%%" % (result,result) 
  42.         sys.exit(1) 
  43.     elif result > critical: 
  44.         print "Critical!;mem_used:%.2f%%|mem_used:%.2f%%" % (result,result) 
  45.         sys.exit(2) 
  46.     else: 
  47.         print "OK!;mem_used:%.2f%%|mem_used:%.2f%%" % (result,result) 
  48.         sys.exit(0) 
  49.  
  50.  
  51. def redis_fork(): 
  52.     fork_used = r.info()['latest_fork_usec'] / 1000 
  53.     result = float(fork_used) / 1000 
  54.     if result >=warning and result < critical: 
  55.         print "Warning!;latest_fork:%.2f%%|latest_fork:%.2f%%" % (result,result) 
  56.         sys.exit(1) 
  57.     elif result > critical: 
  58.         print "Critical!;latest_fork:%.2f%%|latest_fork:%.2f%%" % (result,result) 
  59.         sys.exit(2) 
  60.     else: 
  61.         print "OK!;latest_fork:%.2f%%|latest_fork:%.2f%%" % (result,result) 
  62.         sys.exit(0) 
  63.  
  64.  
  65. if "__main__" == __name__: 
  66.     try: 
  67.         opts,args = getopt.getopt(sys.argv[1:],"h:H:p:C:w:c:") 
  68.         for opt,arg in opts: 
  69.             if opt in ("-h","--help"): 
  70.                 usage() 
  71.             if opt in ("-H","--host"): 
  72.                 host = arg 
  73.             if opt in ("-p","--port"): 
  74.                 port = int(arg) 
  75.             if opt in ("-C","--command"): 
  76.                 cmd = arg 
  77.             if opt in ("-w","--warning"): 
  78.                 warning = float(arg) 
  79.             if opt in ("-c","--critical"): 
  80.                critical = float(arg) 
  81.     except: 
  82.         print "please check the host or opts" 
  83.         usage() 
  84.         sys.exit(3) 
  85.  
  86. #   print opts 
  87. #   print args 
  88. #        print "host is %s ,port is %s,cmd is %s,warning is %s,critical is %s" % (host,port,cmd,warning,critical) 
  89.     try: 
  90.         r = redis.Redis(hosthost=host,portport=port) 
  91.         r.ping() 
  92.     except: 
  93.         print "redis can not connected or command is error" 
  94.         usage() 
  95.         sys.exit(3) 
  96.     if cmd == "memuse": 
  97.         memused() 
  98.     if cmd == "fork": 
  99.         redis_fork() 

 

推荐阅读:
  1. check_oracle_health监控oracle
  2. 生产环境下搭建 nagios+nconf+cacti+npc的整合

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

nagios python redis

上一篇:Someone else is already generating the configuration for nconf

下一篇:iOS appDelegate中的方法

相关阅读

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

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