bobbai 发表于 2018-11-7 11:14:08

nagios --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
  


[*]#!/bin/env python
[*]#-*-encoding=utf8-*-
[*]__author__ = 'songtao'
[*]
[*]
[*]
[*]import redis
[*]import sys
[*]import getopt
[*]
[*]
[*]def usage():
[*]    print """
[*]    -H127.0.0.1
[*]    -p6379
[*]    -C
[*]    -w 50
[*]    -c 80
[*]    ./check_redis.py -H 127.0.0.1 -p 6379 -C memuse -c 80 -w 90
[*]    """
[*]    sys.exit(3)
[*]#def conn_redis(host,port):
[*]#    r = redis.Redis(hosthost=host,portport=port)
[*]#    if r.ping():
[*]#      r = redis.Redis(hosthost=host,portport=port)
[*]#      return r
[*]#    else:
[*]#      print "can not connect!!"
[*]#      sys.exit(0)
[*]#r = redis.Redis(hosthost=host,portport=port)
[*]
[*]warning = 80
[*]critical = 90
[*]
[*]
[*]def memused():
[*]    maxmem = r.config_get()['maxmemory']
[*]    usedmem = r.info()['used_memory']
[*]    result = float(usedmem) / float(maxmem) * 100
[*]    if result >=warning and result < critical:
[*]      print &quot;Warning!;mem_used:%.2f%%|mem_used:%.2f%%&quot; % (result,result)
[*]      sys.exit(1)
[*]    elif result > critical:
[*]      print &quot;Critical!;mem_used:%.2f%%|mem_used:%.2f%%&quot; % (result,result)
[*]      sys.exit(2)
[*]    else:
[*]      print &quot;OK!;mem_used:%.2f%%|mem_used:%.2f%%&quot; % (result,result)
[*]      sys.exit(0)
[*]
[*]
[*]def redis_fork():
[*]    fork_used = r.info()['latest_fork_usec'] / 1000
[*]    result = float(fork_used) / 1000
[*]    if result >=warning and result < critical:
[*]      print &quot;Warning!;latest_fork:%.2f%%|latest_fork:%.2f%%&quot; % (result,result)
[*]      sys.exit(1)
[*]    elif result > critical:
[*]      print &quot;Critical!;latest_fork:%.2f%%|latest_fork:%.2f%%&quot; % (result,result)
[*]      sys.exit(2)
[*]    else:
[*]      print &quot;OK!;latest_fork:%.2f%%|latest_fork:%.2f%%&quot; % (result,result)
[*]      sys.exit(0)
[*]
[*]
[*]if &quot;__main__&quot; == __name__:
[*]    try:
[*]      opts,args = getopt.getopt(sys.argv,&quot;h:H:p:C:w:c:&quot;)
[*]      for opt,arg in opts:
[*]            if opt in (&quot;-h&quot;,&quot;--help&quot;):
[*]                usage()
[*]            if opt in (&quot;-H&quot;,&quot;--host&quot;):
[*]                host = arg
[*]            if opt in (&quot;-p&quot;,&quot;--port&quot;):
[*]                port = int(arg)
[*]            if opt in (&quot;-C&quot;,&quot;--command&quot;):
[*]                cmd = arg
[*]            if opt in (&quot;-w&quot;,&quot;--warning&quot;):
[*]                warning = float(arg)
[*]            if opt in (&quot;-c&quot;,&quot;--critical&quot;):
[*]               critical = float(arg)
[*]    except:
[*]      print &quot;please check the host or opts&quot;
[*]      usage()
[*]      sys.exit(3)
[*]
[*]#   print opts
[*]#   print args
[*]#      print &quot;host is %s ,port is %s,cmd is %s,warning is %s,critical is %s&quot; % (host,port,cmd,warning,critical)
[*]    try:
[*]      r = redis.Redis(hosthost=host,portport=port)
[*]      r.ping()
[*]    except:
[*]      print &quot;redis can not connected or command is error&quot;
[*]      usage()
[*]      sys.exit(3)
[*]    if cmd == &quot;memuse&quot;:
[*]      memused()
[*]    if cmd == &quot;fork&quot;:
[*]      redis_fork()
  



页: [1]
查看完整版本: nagios --redis 监控脚本