资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用python监控redis-创新互联

无聊的时候,写的一个小监控脚本,适用于nagios使用python监控redis

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

文章标题:使用python监控redis-创新互联
URL链接:http://cdkjz.cn/article/shecj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220