码迷,mamicode.com
首页 > 系统相关 > 详细

memcache的监控脚本简单实现

时间:2015-10-13 12:17:49      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:

"""
Server Density Agent Plugin for memcached
This script requires a config entry in the agent config
[Memcached]
host = ‘127.0.0.1‘
port = 12000
"""

import sys
import telnetlib
import re
import socket
import logging
import json
import time


class Memcached:
    def __init__(self, agentConfig, checksLogger, rawConfig):
        self.agentConfig = agentConfig
        self.checksLogger = checksLogger
        self.rawConfig = rawConfig

    def run(self):
        stats = {}

        if hasattr(self.rawConfig[‘Memcached‘], ‘host‘):
            host = self.rawConfig[‘Memcached‘][‘host‘]
        else:
            host = ‘127.0.0.1‘

        if hasattr(self.rawConfig[‘Memcached‘], ‘port‘):
            port = self.rawConfig[‘Memcached‘][‘port‘]
        else:
            port = 12000

        try:
            telnet = telnetlib.Telnet()
            telnet.open(host, port)
            telnet.write(‘stats\r\n‘)

            out = telnet.read_until("END")

            telnet.write(‘quit\r\n‘)
            telnet.close()
        except socket.error, reason:
            sys.stderr.write("%s\n" % reason)
            sys.stderr.write("Is memcached running?\n")
            sys.stderr.write("Host: %s Port: %s\n" % (host, port))
            return stats

        # Current / Total
        stats[‘curr_items‘] = int(re.search("curr_items (\d+)", out).group(1))
        stats[‘total_items‘] = int(re.search("total_items (\d+)", out).group(1))

        # Memory Usgae
        stats[‘limit_maxbytes‘] = int(re.search("limit_maxbytes (\d+)", out).group(1))
        stats[‘bytes‘] = int(re.search("bytes (\d+)", out).group(1))

        # Network Traffic
        stats[‘bytes_read‘] = int(re.search("bytes_read (\d+)", out).group(1))
        stats[‘bytes_written‘] = int(re.search("bytes_written (\d+)", out).group(1))

        # Connections
        stats[‘curr_connections‘] = int(re.search("curr_connections (\d+)", out).group(1))
        stats[‘total_connections‘] = int(re.search("total_connections (\d+)", out).group(1))

        # Hits / Misses
        stats[‘cmd_get‘] = int(re.search("cmd_get (\d+)", out).group(1))
        stats[‘cmd_set‘] = int(re.search("cmd_set (\d+)", out).group(1))
        stats[‘get_hits‘] = int(re.search("get_hits (\d+)", out).group(1))
        stats[‘get_misses‘] = int(re.search("get_misses (\d+)", out).group(1))
        stats[‘delete_hits‘] = int(re.search("delete_hits (\d+)", out).group(1))
        stats[‘delete_misses‘] = int(re.search("delete_misses (\d+)", out).group(1))
        stats[‘incr_hits‘] = int(re.search("incr_hits (\d+)", out).group(1))
        stats[‘decr_hits‘] = int(re.search("decr_hits (\d+)", out).group(1))
        stats[‘incr_misses‘] = int(re.search("incr_misses (\d+)", out).group(1))
        stats[‘decr_misses‘] = int(re.search("decr_misses (\d+)", out).group(1))

        # Evictions
        stats[‘evictions‘] = int(re.search("evictions (\d+)", out).group(1))
        stats[‘reclaimed‘] = int(re.search("reclaimed (\d+)", out).group(1))

        return stats


if __name__ == ‘__main__‘:
    """
    Standalone test configuration
    """
    raw_agent_config = {
        ‘Memcached‘: {
            ‘host‘: ‘127.0.0.1‘,
            ‘port‘: 12000,
        }
    }

    main_checks_logger = logging.getLogger(‘Memcached‘)
    main_checks_logger.setLevel(logging.DEBUG)
    main_checks_logger.addHandler(logging.StreamHandler(sys.stdout))
    host_check = Memcached({}, main_checks_logger, raw_agent_config)

    while True:
        try:
            print json.dumps(host_check.run(), indent=4, sort_keys=True)
        except:
            main_checks_logger.exception("Unhandled exception")
        finally:
            time.sleep(60)

 用python写的,可以把记录写入日志文件,用gnuplot或者rrdtool绘成图片会更好

memcache的监控脚本简单实现

标签:

原文地址:http://www.cnblogs.com/chenpingzhao/p/4874003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!