monicazhang 发表于 2016-1-25 14:00:02

nagios监控mysql tps---check_mysql_tps.py*


来自:网络

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python2.7                               nagios安装
# -*- 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 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")
                         nagios配置
(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                   nagios实施
      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)
      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)                  nagios培训
      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())
      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()





想做Nagios, Zabbix,Cacti,iTop各种交流的,可以进入开源监控工具Nagios交流QQ群号 :476809427

页: [1]
查看完整版本: nagios监控mysql tps---check_mysql_tps.py*