Monitor mysql Master/Slave apps written in python

Source: Internet
Author: User
Tags sql error

#! /Usr/bin/env python #-*-coding: UTF-8-*-# File: repl_wx.py # creater: wangwei import MySQLdb import Queue, OS, base64, time, sys, wx, threading import ConfigParser, logging from wx. lib. mixins. listctrl import ListCtrlAutoWidthMixin class Repl: def _ init _ (self, user, passwd, host, hostname): self. user = user self. passwd = passwd self. host = host self. hostname = hostname def Slave (self, user, passwd, host, hostnam E): try: conn = MySQLdb. connect (host, user = self. user, passwd = self. passwd, connect_timeout = 2) cursor = conn. cursor (cursorclass = MySQLdb. cursors. dictCursor) SQL = "show variables like 'version'" cursor.exe cute (SQL) alldata = cursor. fetchall () version = alldata [0] ["Value"]. split ('. ') [0] SQL = "show slave status" cursor.exe cute (SQL) alldata = cursor. fetchall () IO = alldata [0] ["Slave_IO_Running"] S QL = alldata [0] ["Slave_ SQL _Running"] if version = "4 ": errno = alldata [0] ["Last_errno"] Error = alldata [0] ["Last_error"] else: errno = alldata [0] ["Last_Errno"] Error = alldata [0] ["Last_Error"] cursor. close () conn. close () return IO, SQL, Errno, Error failed T: return, class Check: def _ init _ (self, app, user, passwd, host, hostname ): self. app = app self. user = user self. passwd = passwd self. host = hos T self. hostname = hostname boss = Repl (user, passwd, host, hostname) IO, SQL, Errno, Error = boss. slave (self. user, self. passwd, self. host, self. hostname) self. IO = IO self. SQL = SQL self. errno = Errno self. error = Error def Status (self): now = time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime (time. time () if not self. IO and not self. SQL: errlist = [self. host, self. hostname, now, 'error: Can not Connect mysql! '] Wx. CallAfter (self. app. ErrorMessage, errlist) a = self. hostname + "" + self. host + "Can not Connect mysql DB! "Logging. basicConfig (level = logging. DEBUG, format = '% (asctime) s % (levelname) s % (message) s', datefmt =' % Y-% m-% d % H: % M: % s', filename = 'slave. log', filemode = 'A') logging. error (a) return 0 if self. IO = "Yes": if self. SQL = "Yes": # print now + "" + self. hostname + "OK-slave is running" + self. host # errlist = [self. host, self. hostname, now, 'OK-slave is running! '] # Wx. CallAfter (self. app. ErrorMessage, errlist) return 0 else: # print now + "" + self. hostname + "Critical-slave SQL Error! "Errlist = [self. host, self. hostname, now, 'critical-slave SQL Error! '] Wx. CallAfter (self. app. ErrorMessage, errlist) a = self. hostname + "" + self. host + "Critical-slave SQL Error! "Logging. basicConfig (level = logging. DEBUG, format = '% (asctime) s % (levelname) s % (message) s', datefmt =' % Y-% m-% d % H: % M: % s', filename = 'slave. log', filemode = 'A') logging. error (a) return 1 else: if self. SQL = "Yes": # print now + "" + self. hostname + "Critical-slave IO Error! "Errlist = [self. host, self. hostname, now, 'critical-slave IO Error! '] Wx. CallAfter (self. app. ErrorMessage, errlist) a = self. hostname + "" + self. host + "Critical-slave IO Error! "Logging. basicConfig (level = logging. DEBUG, format = '% (asctime) s % (levelname) s % (message) s', datefmt =' % Y-% m-% d % H: % M: % s', filename = 'slave. log', filemode = 'A') logging. error (a) return 2 else: # print now + "" + self. hostname + "Critical-slave IO and SQL Error! "Errlist = [self. host, self. hostname, now, 'critical-slave IO and SQL Error! '] Wx. CallAfter (self. app. ErrorMessage, errlist) a = self. hostname + "" + self. host + "Critical-slave IO and SQL Error! "Logging. basicConfig (level = logging. DEBUG, format = '% (asctime) s % (levelname) s % (message) s', datefmt =' % Y-% m-% d % H: % M: % s', filename = 'slave. log', filemode = 'A') logging. error (a) return 3 class StartScan (threading. thread): def _ init _ (self, app): threading. thread. _ init _ (self) self. app = app self. timeToQuit = threading. event () self. timeToQuit. clear () def stop (self): self. timeToQuit. set () def run (self): self. User, self. passwd, self. hostlist, self. a, self. time = self. conf () self. times = 1 while True: threads = [] if self. timeToQuit. isSet (): break for I in range (self. a): host = self. hostlist [I] [0] hostname = self. hostlist [I] [1] boss = Check (self, self. user, self. passwd, host, hostname) t = threading. thread (target = boss. status, args = () threads. append (t) # print 'total % s Threads is working... '% self. a + '\ n' msg = U "no." + "% s" % (self. times) + u "scans ..... "self. app. currentScan (msg) for I in range (self. a): threads [I]. start () time. sleep (0.05) for I in range (self. a): threads [I]. join () self. times + = 1 time. sleep (self. time) def CurrentScan (self, msg): wx. callAfter (self. app. currentScan, msg) def conf (self): fp = ConfigParser. configParser () fp. readfp (open ('repl. ini ') iplist = fp. get ("global", "iplist") user = fp. get ("Global", "user") passwd = fp. get ("global", "passwd") time = fp. getint ("global", "time") user = base64.decodestring (user) passwd = base64.decodestring (passwd) hostlist = [] for I in iplist. split (";"): hostlist. append (I. split (",") a = len (hostlist) return user, passwd, hostlist, a, time def ErrorMessage (self, errlist): wx. callAfter (self. app. errorMessage, errlist) class ScanUnit (wx. panel): def _ init __( Self, parent, id =-1, title = '', port ='',): wx. panel. _ init _ (self, parent, id) self. parent = parent self. id = id self. title = title self. scaning = wx. staticText (self,-1, label = u "to be scanned ...... ", Style = wx. ALIGN_LEFT | wx. ST_NO_AUTORESIZE) self. openBtn = wx. button (self,-1, u'open log') self. openBtn. setForegroundColour ('red') # self. openBtn. setBackgroundColour ('purple ') self. startBtn = wx. button (self,-1, u'scan') self. stopBtn = wx. button (self,-1, u'stop') self. stopBtn. disable () self. list = AutoWidthListCtrl (self) self. list. setTextColour ("red") self. list. insertColumn (0, 'IP', width = 120) self. list. insertColumn (1, u'block name', width = 100) self. list. insertColumn (3, u'occurrence time', width = 140) self. list. insertColumn (4, u'return information', width = 200) self. list. deleteAllItems () self. bind (wx. EVT_BUTTON, self. onStart, self. startBtn) self. bind (wx. EVT_BUTTON, self. onStop, self. stopBtn) self. bind (wx. EVT_BUTTON, self. open, self. openBtn) self. _ layout () def _ layout (self): # layout action = wx. boxSizer (wx. HORIZONTAL) action. add (self. scaning, 3, wx. ALIGN _ CENTER_HORIZONTAL | wx. EXPAND) # The alignment does not solve the action. add (self. startBtn, 1, wx. ALIGN_CENTER_VERTICAL | wx. EXPAND) action. add (self. stopBtn, 1, wx. ALIGN_CENTER_VERTICAL | wx. EXPAND) action. add (self. openBtn, 1, wx. ALIGN_CENTER_VERTICAL | wx. EXPAND) listbox = wx. boxSizer (wx. HORIZONTAL) listbox. add (self. list, 1, wx. EXPAND) self. box = wx. staticBox (self, self. id, self. title, style = wx. SUNKEN_BORDER) self. scanUnit = wx. stat IcBoxSizer (self. box, wx. VERTICAL) self. scanUnit. add (action, 0, wx. ALL | wx. EXPAND, 2) self. scanUnit. add (listbox, 5, wx. EXPAND, 2) self. setSizer (self. scanUnit) self. parent. sizer. add (self, 1, wx. EXPAND) def OnStart (self, event): self. startBtn. disable () self. stopBtn. enable () # self. deleteItems () self. thread = StartScan (self) self. thread. setDaemon (True) self. thread. start () self. scaning. setLabel (U' is scanning ...... ') Def OnStop (self, event): self. stopBtn. disable () self. startBtn. enable () self. thread. stop () self. scaning. setLabel (U' stop scanning ...... ') Def Open (self, event): wx. execute ("notepad slave. log ") # OS. system ('notepad slave. log') def CurrentScan (self, msg): # current scan action self. scaning. setLabel (msg) def ErrorMessage (self, errlist): # error message index = self. list. insertStringItem (sys. maxint, errlist [0]) self. list. setStringItem (index, 1, errlist [1]) self. list. setStringItem (index, 2, errlist [2]) self. list. setStringItem (index, 3, errlist [3]) self. list. refreshItem (index) def DeleteItems (self): self. list. deleteAllItems () class AutoWidthListCtrl (wx. listCtrl, ListCtrlAutoWidthMixin): def _ init _ (self, parent): wx. listCtrl. _ init _ (self, parent,-1, style = wx. LC_REPORT | wx. LC_HRULES | wx. LC_VRULES) ListCtrlAutoWidthMixin. _ init _ (self) class MainPanel (wx. panel): def _ init _ (self, parent): wx. panel. _ init _ (self, parent) self. sizer = wx. gridSizer (rows = 1, cols = 1, hgap = 20, vgap = 15) self. aaa = ScanUnit (self,-1, U' <Mysql master-slave> ', '123') self. setSizer (self. sizer) class CreateMenu (): # create menu def _ init _ (self, parent): self. menuBar = wx. menuBar () self. file = wx. menu () self. close = self. file. append (-1, U' exit (& X) ') self. menuBar. append (self. file, U' file (& F) ') self. help = wx. menu () self. about = self. help. append (-1, U' about (& A) ') self. menuBar. append (self. help, u'help (& H) ') parent. setMenuBar (self. menuBar) class MyFrame (wx. app): u''' Mysql Master/Slave monitoring \ nE-mail: wangwei03@gyyx.cn \ nQQ: 83521260 ''' def OnInit (self): self. frame = wx. frame (parent = None, id =-1, title = u'mysql Master/Slave programme ', size = (650,450) self. frame. setIcon (wx. icon ('kankan. ico ', wx. BITMAP_TYPE_ICO) self. panel = MainPanel (self. frame) self. frame. center (direction = wx. BOTH) self. menu = CreateMenu (self. frame) self. frame. statusBar = self. frame. createStatusBar (3) # print dir (self. frame. statusBar) self. frame. statusBar. setStatusWidths ([-12,-12,-13]) self. frame. statusBar. setForegroundColour ('purple ') self. frame. statusBar. setBackgroundColour ('pink') self. frame. statusBar. font. bold = True # self. frame. statusBar. font. size = 13 self. frame. statusBar. setStatusText (u "Learning", 0) self. frame. statusBar. setStatusText (u "Daily", 1) self. timer = wx. pyTimer (self. permission y) self. timer. start (1000) self. policy () self. bind (wx. EVT_MENU, self. onClose, self. menu. close) self. bind (wx. EVT_MENU, self. onAbout, self. menu. about) self. setTopWindow (self. frame) self. frame. show () return True def every Y (self): t = time. localtime (time. time () st = time. strftime ("% Y-% m-% d % H: % M: % S", t) self. frame. statusBar. setStatusText (u "Current System Time" + st, 2) def OnClose (self, event): self. frame. destroy () def OnAbout (self, event): wx. messageBox (self. _ doc __, 'mysql Replication status', wx. OK) if _ name _ = "_ main _": app = MyFrame (None) app. mainLoop ()

The configuration file name is repl. ini.

Format:

[Global] user = cmVwbsdfsdfA = passwd = Hangzhou = time = 10 iplist = 192.168.8.11, Yanyu Jiangnan; 192.168.8.12, available on a daily basis; # Description: username and password # base64.encodestring (), base64.decodestring () encryption and decryption. time is the set timeout time (in seconds), and iplist is the IP address and name list.

It is mainly used to monitor the status of mysql Cluster Server in batches.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.