nagios监控mysql tps---check_mysql_tps.py

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

parser = OptionParser(usage="%prog -w <warning threshold> -c <critical threshold> [ -h ]\n\nBefore use the script,please execute ‘grant usage on *.* to [email protected]‘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()

try:
    f=open(‘/tmp/Com_commit.txt‘)
except IOError:
    f=open(‘/tmp/Com_commit.txt‘,‘w‘)
    print ‘wait next check,initialize the date time.txt‘
finally:    
    f.close()

try:
    f=open(‘/tmp/Com_rollback.txt‘)
except IOError:
    f=open(‘/tmp/Com_rollback.txt‘,‘w‘)
    print ‘wait next check,initialize the date time.txt‘
finally:    
    f.close()

try:
    f=open(‘/tmp/tpstime.txt‘)
except IOError:
    f=open(‘/tmp/tpstime.txt‘,‘w‘)
    print ‘wait next check,initialize the date time.txt‘
finally:    
    f.close()

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 __Com_commit(self):

        now=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_commit‘" | grep -Po "\d+"  ‘‘‘ %(self.username,self.password,self.hostname,self.port))
        now=re.findall(r‘\d+‘,now)[0]
        f=open(‘/tmp/Com_commit.txt‘,‘r‘)
        before=f.readlines()
        f.close()
        if before == []:
            ff=open(‘/tmp/Com_commit.txt‘,‘w‘)
            ff.write(now)
            ff.close()
        else:
            before=before[-1]
            N = int(now)-int(before)
            ff=open(‘/tmp/Com_commit.txt‘,‘w‘)
            ff.write(now)
            ff.close()
            return N

    def __Com_rollback(self):

        now=commands.getoutput(‘‘‘ mysql -u%s -p%s -h%s -P%s -e "show global status like ‘Com_rollback‘" | grep -Po "\d+" ‘‘‘  %(self.username,self.password,self.hostname,self.port))
        now=re.findall(r‘\d+‘,now)[0]
        f=open(‘/tmp/Com_rollback.txt‘,‘r‘)
        before=f.readlines()
        f.close()
        if before == []:
            ff=open(‘/tmp/Com_rollback.txt‘,‘w‘)
            ff.write(now)
            ff.close()
        else:
            before=before[-1]
            ff=open(‘/tmp/Com_rollback.txt‘,‘w‘)
            ff.write(now)
            ff.close()
            N = int(now)-int(before)
            return N

    def __Uptime(self):
        time_now=datetime.now()
        ff=open(‘/tmp/tpstime.txt‘,‘r‘)
        time_before_str=ff.read()
        ff.close()
        if time_before_str==‘‘:
            ffw=open(‘/tmp/tpstime.txt‘,‘w‘)
            ffw.write(str(time_now))
            ffw.close()
        else:
            time_before=datetime.strptime(time_before_str,"%Y-%m-%d %H:%M:%S.%f")
            delay=(time_now-time_before).seconds
            ffw=open(‘/tmp/tpstime.txt‘,‘w‘)
            ffw.write(str(time_now))
            ffw.close()
            return delay

    def doit(self):
        _Com_commit = int(self.__Com_commit())
        _Com_rollback = int(self.__Com_rollback())
        _Uptime = int(self.__Uptime())
        if (_Com_rollback+_Com_commit) < _Uptime:
        _Uptime = int(self.__Uptime())
        R = ( _Com_rollback + _Com_commit) / _Uptime
        print "TPS is %.1f | TPS=%.1f" %(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).doit()

mysql   TPS  监控脚本

时间: 2024-08-11 05:46:06

nagios监控mysql tps---check_mysql_tps.py的相关文章

Nagios监控MySQL报错:NRPE: Unable to read output的详细解决过程

前言:nagios界面上,看到监控mysql服务报错如下: Warning:NRPE: Unable to read output 1,去nagios监控服务器上check下 1.1,执行check_nrpe命令远程调用 在监控端nagios服务器上执行check_nrpe检查mysql状态报错如下: [[email protected] ~]# /usr/lib/nagios/plugins/check_nrpe  -H192.xx.180.xx -c check_mysql_status N

Nagios 监控mysql服务器详细实现过程

1.NRPE介绍 NRPE是Nagios的一个功能扩展,它可在远程Linux/Unix主机上执行插件程序.通过在远程服务器上安装NRPE插件及Nagios插件程序来向Nagios监控平台提供该服务器的本地情况,如CPU负载,内存使用,磁盘使用等.这里将Nagios监控端称为Nagios服务器端,而将远程被监控的主机称为Nagios客户端. Nagios监控远程主机的方法有多种,其方式包括SNMP,NRPE,SSH,NCSA等.这里介绍其通过NRPE监控远程Linux主机的方式. NRPE(Nag

nagios监控mysql

在nagios上部署check_mysql_health 监控mysql 博客分类: 架构 本监控为基于nagios服务器主动监控方法,利用check_mysql_health实现多种监控模式:       connection-time          (Time to connect to the server)       uptime                   (Time the server is running)       threads-connected      

zabbix监控mysql-如何使用zabbix自带的模板监控-nagios监控mysql主从

用zabbix监控mysql(源码安装)安装位置在/usr/local/zabbix/ 监控端(server): 在被监控端主机上关联该模板Template App MySQL 中文:组态->主机->名称一栏点击进去->模板,点击链接新模板->添加->Template App MySQL->存档 英文:Configuration->Hosts->Name->Templates,Link new templates->Add->Templat

nagios监控mysql主从,并且邮件告警!

客户端操作: yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe#安装 vim /etc/nagios/nrpe.cfg    #更改客户端的nrpe配置文件 allowed_hosts=192.168.20.168    #IP更改成监控端的地址,如果是多个使用,分割开 #漏了这个后期会出现CHECK_NRPE: Error - Could not complete SSL handshake. 的

Nagios 监控 Mysql

被监控机的Mysql监控创建专用数据库.专用数据库账户mysql -uroot -pcreate database nagios_monitor;grant select on nagios_monitor.* to [email protected]'%' identified by 'nagiosMonitor';flush privileges;exit;测试check_mysql脚本运行是否正常cd /usr/lib64/nagios/plugins./check_mysql -unag

Nagios监控Mysql数据库

实验拓扑:地址分配:Nagios 192.168.1.1Mysql 192.168.1.2Nagios上的操作:1)安装nagios#make install //安装主程序,CGI和HTML文件 make install-init //在/etc/rc.d/init.d安装启动脚本 make install-commandmode //配置目录权限 make install-config //安装示例配置文件 make install-webconf //安装nagios的web接口,会在/e

nagios 监控 mysql 读写比 R/W

#!/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 = Opti

第25章 MySQL replication(主从)配置及加入nagios监控

环境介绍: master:23.247.76.253 [[email protected]_client1 tool]# mysql -V mysql  Ver 14.14 Distrib 5.6.32, for linux-glibc2.5 (x86_64) using  EditLine wrapper [[email protected]_client1 tool]# cat /etc/redhat-release CentOS release 6.7 (Final) slave:23.2