博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用python监控memcached基本信息
阅读量:6871 次
发布时间:2019-06-26

本文共 4030 字,大约阅读时间需要 13 分钟。

使用python监控memcached基本信息

使用python监控memcached的基本信息,例如:connections、hitRate、freeMemory、memoryUsage、evictions等等。然后自定义zabbix keys值实现自定义监控模版!

首先安装需要的环境:

pip install python-memcached

话不多说,直接上脚本:

#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'chenmingle'import sysimport subprocessimport jsontry:    import memcacheexcept Exception, e:    print 'pip install python-memcached'    sys.exit(1)class Memcached(object):    def __init__(self, host_list):        self.mc = memcache.Client(host_list)        try:            self.stats = self.mc.get_stats()[0][1]        except Exception, e:            pass    def get_curr_connections(self):        """        Get current connections for Memcached        UserParameter: connections        """        try:            return int(self.stats['curr_connections'])        except Exception, e:            return 0    def get_cache_hit_rate(self):        """        Get the hit rate for Memcached        UserParameter: hitRate        """        try:            rate = float(self.stats['get_hits']) / float(self.stats['cmd_get'])            return "%.2f" % (rate * 100)        except Exception, e:            return 0.0    def get_free_memory(self):        """        Get the free memory in Memcached Byte        UserParameter: freeMemory        """        try:            free = int(self.stats['limit_maxbytes']) - int(self.stats['bytes'])            return free        except Exception, e:            return 0    def get_memory_usage_rate(self):        """        Get the memory usage rate in Memcached        UserParameter: memoryRate        """        try:            rate = float(self.stats['bytes']) / float(self.stats['limit_maxbytes'])            return "%.2f" % (rate * 100)        except Exception, e:            return 0.0    def get_evictions(self):        """        Get evictd items in Memcached one minute avg        UserParameter: evictions        """        try:            # minutes = int(self.stats['uptime']) / 60            # return int(self.stats['evictions']) / int(minutes)            return int(self.stats['evictions'])        except Exception, e:            return 0    def test(self):        # print json.dumps(self.stats, indent=4)        print 'connections: %s' % self.get_curr_connections()        print 'hitRate: %s %%' % self.get_cache_hit_rate()        print 'freeMemory: %s Byte' % self.get_free_memory()        print 'memoryUsage: %s %%' % self.get_memory_usage_rate()        print 'evictions: %s' % self.get_evictions()        print 'alive: %s' % check_alive(host_list)def check_alive(host_list):    host = host_list.split(':')[0]    port = host_list.split(':')[1]    cmd = 'nc -z %s %s > /dev/null 2>&1' % (host, port)    return subprocess.call(cmd, shell=True)def parse(type, host_list):    mc = Memcached([host_list])    if type == 'connections':        print mc.get_curr_connections()    elif type == 'hitRate':        print mc.get_cache_hit_rate()    elif type == 'freeMemory':        print mc.get_free_memory()    elif type == 'memoryUsage':        print mc.get_memory_usage_rate()    elif type == 'evictions':        print mc.get_evictions()    elif type == 'alive':        print check_alive(host_list)    else:        mc.test()if __name__ == '__main__':    try:        host_list = sys.argv[1]        type = sys.argv[2]    except Exception, e:        print "Usage: python %s 127.0.0.1:11211 connections" % sys.argv[0]        sys.exit(1)    parse(type, host_list)

例行测试一下脚本的效果:

[root@Memcached zabbix_agentd.d]# /usr/local/bin/python /home/python/check_memcached.py 10.0.0.90:11211 testconnections: 1593hitRate: 98.15 %freeMemory: 849551027 BytememoryUsage: 80.22 %evictions: 7477932alive: 0

现在已经完成一个简单的监控memcached基本信息的脚本了,接下来为了以后方便查看做个自定义监控项加入到zabbix上去。

  • 首先定义监控项配置:
cd /etc/zabbix/zabbix_agentd.dcat Memcached.conf# MemcachedUserParameter=memcached.stats[*],/usr/local/bin/python /home/python/check_memcached.py 10.0.0.90:11211 $1
  • 在zabbix上配置自定义模版自定义监控项:

使用python监控memcached基本信息

使用python监控memcached基本信息

这样就完成一个简单的监控memcached的自定义模版,如果觉得这文章不错的,请在本人上点个star,感谢!!

转载于:https://blog.51cto.com/legehappy/2145967

你可能感兴趣的文章
简单的文字转语音方法,一分钟学会
查看>>
Python 数据库操作 SQLAlchemy
查看>>
UI交互设计的网站
查看>>
Python 工匠:编写地道循环的两个建议
查看>>
微信域名网址检测
查看>>
电话机器人,什么价格才是好的机器人
查看>>
我的友情链接
查看>>
Android中自定义样式与View的构造函数中的第三个参数defStyle的意义
查看>>
Eclipse中提高Android SDK Manager下载速度方法
查看>>
五、Storm入门之Bolt
查看>>
web开发插入数据时控制台没报错,可能是数据库表被锁了
查看>>
数据库中间件__mycat
查看>>
三、NGINX的配置与优化
查看>>
python_day11のPython操作 pymysql && SQLAchemy
查看>>
openstack运维实战系列(十六)之ceph存储安装
查看>>
远程桌面RDP报错CredSSP encryption oracle remediatation
查看>>
javascript中arguments关键字的用法
查看>>
idea中开发swing的时候使用generate生成form没有Form Main选项
查看>>
格式化输出
查看>>
42幅非常有创意的食品广告欣赏(上篇)
查看>>