Memcached和Redis监控脚本分享

Memcached:
#!/usr/bin/env python
#coding=utf8

import sys
import os

class GetMemStatus():
def __init__(self):
self.val = {}
def check(self):
try:
import memcache
self.mc = memcache.Client(['127.0.0.1:11211'], debug=0)
except:
raise Exception, 'Plugin needs the memcache module'

def extract(self, key):
stats = self.mc.get_stats()
try:
if key in stats[0][1]:
self.val[key] = stats[0][1][key]
return self.val[key]
except:
raise Exception, 'ERROR: key is not in stats!!!'

def main():
if len(sys.argv) == 1:
print "ERROR! Please enter a key"
elif len(sys.argv) == 2:
key = sys.argv[1]
a = GetMemStatus()
a.check()
print a.extract(key)

if __name__ == "__main__":
main()

Redis:
#!/usr/bin/env python
#coding=utf8

import sys
import os

class GetRedisStatus():
def __init__(self):
self.val = {}
def check(self):
try:
import redis
self.redis = redis.Redis('127.0.0.1', port=6379, password=None)
except:
raise Exception, 'Plugin needs the redis module'

def extract(self, key):
info = self.redis.info()
try:
if key in info:
self.val[key] = info[key]
return self.val[key]
except:
raise Exception, 'ERROR info not include this key!'

def main():
if len(sys.argv) == 1:
print "ERROR! Please enter a key"
elif len(sys.argv) == 2:
key = sys.argv[1]
a = GetRedisStatus()
a.check()
print a.extract(key)

if __name__ == "__main__":
main()

0 个评论

要回复文章请先登录注册