check_memcache with python

简介:

 在 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  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
}



本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/554745,如需转载请自行联系原作者

相关文章
|
机器学习/深度学习 算法 Serverless
【李宏毅机器学习CP4】(task2)回归+Python Basics with Numpy
第一部分:回归栗子 ps:CP3的部分在上一篇笔记中【李宏毅机器学习】CP1-3笔记了。 1.问题描述 现在假设有10个x_data和y
155 0
【李宏毅机器学习CP4】(task2)回归+Python Basics with Numpy
|
Java 关系型数据库 MySQL
Python高级语法5:私有属性、魔法属性、with与上下文管理器
Python高级语法5:私有属性、魔法属性、with与上下文管理器
173 0
|
存储 数据库 Python
Python - with 语句
Python - with 语句
89 0
|
数据库连接 数据库 Python
Python中的With语句
在Python中,您需要通过打开文件来访问文件。您可以使用 open()函数来实现。Open 返回一个文件对象,该文件对象具有用于获取有关已打开文件的信息和对其进行操作的方法和属性。
133 0
|
缓存 Python
Python - with open()、os.open()、open()的详细使用
Python - with open()、os.open()、open()的详细使用
487 0
|
程序员 Python
说说Python中with的用法?
公众号新增加了一个栏目,就是每天给大家解答一道Python常见的面试题,反正每天不贪多,一天一题,正好合适,只希望这个面试栏目,给那些正在准备面试的同学,提供一点点帮助!
96 0
|
Python
python with as有什么好处?
python with as有什么好处?
84 0
|
关系型数据库 MySQL 数据库连接
With关键字的使用 | 手把手教你入门Python之七十八
with语句实质上是⼀个上下⽂管理器,很多需要手动关闭的连接,比如说,文件连接,socket连接,数据库的连接都能使用with关键字来自动关闭连接。
|
C++ Python 编译器
为什么Python没有属性赋值的“with”语句?
Python有一个 'with' 语句,它封装了块的执行,在块的入口和出口调用代码。有些语言的结构是这样的: a = 1 # equivalent to obj.a = 1 total = total + 1 # obj.total = obj.total + 1 在Python中,这样的结构是不明确的。
|
Python
python:好用的 with 语法
python:好用的 with 语法
1379 0