首页 > nagios > check_memcache with python

check_memcache with python

2011年4月27日 eric 发表评论 阅读评论

在 http://exchange.nagios.org/ 上有shell 写的脚本,nagios可以监控到命中率,他是基于 telnet 的,使用之后看到 进程里有个telnet 进程,很不爽,而且脚本休要修改一下,不然会报错,不知道大家是不是同样的问题,而我,想要的是监控命中率,还有内存使用率以及,当前连接数·所以只能自己动手来写个脚本了·在python 的官网上看到有 python-memcache包,就下来看了一下源码,使用还是很方便的·。

shell $> cat check_memcache.py
#!/usr/bin/python
import memcache
import getopt
import sys

def usage():
   print """check_memcache is a Nagios to monitor memcached memory Plugin
   Usage:

   check_memcache [-h|--help][-w|warning][-c|critical]

   Options:
       --help|-h)
            print check_memcache help
       --warning|-w)
            Sets a warning level for memcache use memory.Default is :o ff
       --critical|-c)
            Sets a critical level for memcache use memory.Default is:off. """
   sys.exit(0)

try:
    options,args = getopt.getopt(sys.argv[1:],"hw:c:",["help","warning=","critical="])

except getopt.GetoptError:
   usage()
   sys.exit(3)

for name,value in options:
  if name in ("-h","--hlep"):
     usage()
  if name in ("-w","--warning"):
     warning = int(value)
  if name in ("-c","--critical"):
     critical = int(value)
try:
   mc = memcache.Client(['127.0.0.1:11211'], debug=0)
   conn = mc.get_stats()
   memstat = conn[0][1]

except:
   print 'please check your memcached host and port'
   sys.exit(3)

#for key in a:
#  print '%s : %s' % (key,a[key])

tobal = float(memstat['limit_maxbytes'])
memory = float(memstat['bytes'])
free = (tobal - memory)/1024
free = '%d%s' % (free,'kb')
connection = memstat['accepting_conns']
memory = memory / 1024
if memory < 0.01:
   memory = 0.01
else:
   memory = int(memory)

get_miss = float(memstat['get_misses'])
get_hits = float(memstat['get_hits'])
if get_miss == 0 and get_hits == 0:
   hitrate = 100
else:
   hitrate = get_hits/(get_hits + get_miss)*100

hitrate = '%d%s' % (hitrate,'%')
output = 'use:%skb,free:%s,hitrate:%s,connection:%s' % (memory,free,hitrate,connection)
perfdata = "'use'=%skb 'free'=%s 'hitrate'=%s 'connection'=%s" % (memory,free,hitrate,connection)

if 'warning' in dir() and 'critical' in dir():
  if memory >= warning:
     print 'WARNING - %s|%s' % (output,perfdata)
     sys.exit(1)
  elif memory >=critical:
     print 'CRITICAL - %s|%s' % (output,perfdata)
     sys.exit(2)
  else:
     print 'OK - %s|%s' % (output,perfdata)
     sys.exit(0)
else:
  print 'OK - %s|%s' % (output,perfdata)
  sys.exit(0)

安装步骤:
1.

shell $> wget http://www.nginxs.com/download/check_memcache

2.测试 ./check_memcache

3.修改客户端 nrpe
shell $> vim /etc/nagios/nrpe.cfg
command[check_memcache]=/usr/lib/nagios/plugins/check_memcache -w $ARG1$ -c $ARG2$

4.服务端添加监控服务

define service{
use local-service
host_name eric_1,eric_2,eric_3
service_description memcached_memory
check_command check_nrpe!check_memcache!4000!5000
}

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/464.html

分类: nagios 标签: , , ,
  1. 2017年12月16日08:43 | #1

    payday loans
    payday loans
    payday loans online
    payday loans
    payday loans

  2. 2017年12月19日18:21 | #2

    casino games
    casino online usa
    casino games
    casino online
    casino online usa

  3. 2018年1月3日21:49 | #3

    payday loans no credit check
    payday loans no credit
    loans no credit check
    loans no credit check
    payday loans online no credit check

  4. 2018年1月5日20:28 | #4

    I used to be recommended this web site by means of my cousin.
    I am not certain whether or not this post is
    written by means of him as nobody else realize such distinct approximately my problem.
    You’re incredible! Thank you!

  5. 2018年1月6日03:02 | #5

    Hi there, constantly i used to check webpage posts here in the
    early hours in the morning, since i love to learn more and more.

  6. 2018年1月8日01:42 | #6

    casino games slots free
    free casino games
    online casinos
    casino slots
    free slots online

  7. 2018年1月11日18:37 | #7

    Hurrah! Finally I got a web site from where I can really take valuable data regarding my study and knowledge.

  8. 2018年1月14日12:37 | #8

    payday loans no credit check
    loans for bad credit
    loans for bad credit
    loans for bad credit
    loans for bad credit

  9. 2018年1月15日07:22 | #9

    Howdy! I know this is kind of off topic but I was wondering if
    you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having trouble finding one?
    Thanks a lot!

  10. 2018年1月19日17:19 | #10

    online casino play for free
    best us casinos online
    casinos in michigan with hotels
    download free casino slot games
    harrah’s casino locations by state

  11. 2018年1月20日21:01 | #11

    Hi to all, it’s truly a pleasant for me to go to see this web page,
    it consists of useful Information.

评论分页
1 2 3 4 464
订阅评论
2+7= (必填)