码迷,mamicode.com
首页 > 移动开发 > 详细

nagios 监控 mysql 读写比 R/W

时间:2015-08-25 17:10:28      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-
from optparse import OptionParser
import commands,sys,jastme,re
from datetime import datetime
"""
    Nagios plugin to report the mysql R/W Ratio
    author jastme
"""

parser = OptionParser(usage="%prog -w <warning threshold> -c <critical threshold> [ -h ]\n\nBefore use the script,please execute ‘grant usage on *.* to monitor@‘127.0.0.1‘ identified by ‘monitor‘;\nflush privileges",version="%prog ")
        
parser.add_option("-w", "--warning",action="store", type="string", dest="warn_threshold", help="Warning threshold in percentage")
        
parser.add_option("-c", "--critical",action="store", type="string", dest="crit_threshold", help="Critical threshold in percentage")
        
(options, args) = parser.parse_args()

class Monitor:

    def __init__(self,username,password,hostname,port):
        """ you can call sam var here """
        self.username = username
        self.password = password
        self.port = port
        self.hostname = hostname

    def __Qcache_hits(self,):

        R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Qcache_hits‘" | grep -Po "\d+"  ‘‘‘ %(self.username,self.password,self.hostname,self.port))
        return R

    def __Com_select(self):

        R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_select‘" | grep -Po "\d+" ‘‘‘  %(self.username,self.password,self.hostname,self.port))
        return R

    def __Com_insert(self):

        R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_insert‘" | grep -Po "\d+" ‘‘‘  %(self.username,self.password,self.hostname,self.port))
        return R

    def __Com_update(self):

        R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_update‘" | grep -Po "\d+" ‘‘‘  %(self.username,self.password,self.hostname,self.port))
        return R

    def __Com_delete(self):

        R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_delete‘" | grep -Po "\d+" ‘‘‘  %(self.username,self.password,self.hostname,self.port))
        return R

    def __Com_replace(self):

        R=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_replace‘" | grep -Po "\d+" ‘‘‘  %(self.username,self.password,self.hostname,self.port))
        return R

    def __doit(self):
        _Qcache_hits = int(re.findall(r"\d+",self.__Qcache_hits())[0])
        _Com_select = int(re.findall(r"\d+",self.__Com_select())[0])
        _Com_insert = int(re.findall(r"\d+",self.__Com_insert())[0])
        _Com_update = int(re.findall(r"\d+",self.__Com_update())[0])
        _Com_delete = int(re.findall(r"\d+",self.__Com_delete())[0])
        _Com_replace = int(re.findall(r"\d+",self.__Com_replace())[0])
        R = ( _Com_select + _Qcache_hits) / ( _Com_insert + _Com_update + _Com_delete + _Com_replace ) * 100
        return R

    def Result(self):
        R = self.__doit()
        print "R/W Ratio is %d | R/W Ratio=%d" %(R,R) 

if __name__ == "__main__":
    username = "monitor"
    password = str(jastme.decrypt(119,u‘NALBOBJBCBHAGBOA‘))
    hostname = "127.0.0.1"
    port = "3369"
    Monitor(username,password,hostname,port).Result()


nagios 监控 mysql 读写比 R/W

标签:

原文地址:http://my.oschina.net/jastme/blog/496892

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