前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python写的监控mysql主从的ap

python写的监控mysql主从的ap

作者头像
py3study
发布2020-01-08 10:48:34
7460
发布2020-01-08 10:48:34
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
 
#-*-编码:utf-8-*- 
#File:repl_wx.py 
#creater:wangwei 
导入 MySQLdb 
导入 队列,操作系统,base64,时间,sys,wx,线程 
导入 ConfigParser,记录 
从 wx.lib.mixins.listctrl  导入 ListCtrlAutoWidthMixin 
 
 
类 Repl: 
    def  __init __(self ,user,passwd,host,hostname): 
        自我.user =用户 
        自我.passwd = passwd 
        self .host =主机 
        自我.hostname =主机名 
    def  Slave(self ,user,passwd,host,hostname): 
        尝试: 
            conn = MySQLdb.connect(host,user =  self .user,passwd =  self .passwd,connect_timeout =  2 ) 
            cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)  
            sql =  “像“版本”一样显示变量” 
            cursor.execute(sql) 
            alldata = cursor.fetchall() 
            版本= alldata [ 0 ] [ “ Value” ] .split('。' )[ 0 ] 
            sql =  “显示从属状态” 
            cursor.execute(sql) 
            alldata = cursor.fetchall() 
            IO = alldata [ 0 ] [ “ Slave_IO_Running” ] 
            SQL = alldata [ 0 ] [ “ Slave_SQL_Running” ] 
            如果 版本==  “ 4” : 
                Errno = alldata [ 0 ] [ “ Last_errno” ] 
                错误= alldata [ 0 ] [ “ Last_error” ] 
            其他: 
                Errno = alldata [ 0 ] [ “ Last_Errno” ] 
                错误= alldata [ 0 ] [ “ Last_Error” ] 
            cursor.close() 
            conn.close() 
            返回 IO,SQL,Errno,错误 
        除了: 
            返回0 ,0 ,0 ,0  
 
班级 检查: 
    def  __init __(self ,app,user,passwd,host,hostname): 
        自我.app = app 
        自我.user =用户 
        自我.passwd = passwd 
        self .host =主机 
        自我.hostname =主机名 
        老板= Repl(用户,密码,主机,主机名) 
        IO,SQL,Errno,Error = boss.Slave(self .user,self .passwd,self .host,self .hostname) 
        自我.IO = IO 
        自我.SQL = SQL 
        自我.Errno = Errno 
        自我。错误=错误 
    def  状态(自我): 
        现在= time.strftime('%Y-%m-%d%H:%M:%S' ,time.localtime(time.time())) 
        如果不是self .IO  而不是self .SQL:     
            errlist = [ self .host,self .hostname,现在,“错误:无法连接mysql!” ] 
            wx.CallAfter(self .app.ErrorMessage,errlist) 
            “ = =  self .hostname +   ”“  +  self .host +  ”无法连接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' ) 
            记录错误(a) 
            返回0  
        如果self .IO ==  “是” :  
            如果self .SQL ==  “是” :  
                #print now +“” + self.hostname +“ OK-从属正在运行” + self.host 
                #errlist = [self.host,self.hostname,现在,'好-从属正在运行!'] 
                #wx.CallAfter(self.app.ErrorMessage,errlist) 
                返回0  
            其他: 
                #print now +“” + self.hostname +“严重-从属SQL错误! 
                errlist = [ self .host,self .hostname,现在,“严重-从属SQL错误!” ] 
                wx.CallAfter(self .app.ErrorMessage,errlist) 
                a =  自我.hostname +   “”  +  自我.host +  “严重-从属SQL错误! 
                logging.basicConfig(level = logging.DEBUG,format = '%{asctime)s%{levelname)s%{message)s' ,datefmt = '%Y-%m-%d%H:%M:%S' ,filename = 'slave.log' ,filemode = 'a' ) 
                记录错误(a) 
                返回1  
        其他: 
            如果self .SQL ==  “是” :  
                #print now +“” + self.hostname +“严重-从属IO错误!” 
                errlist = [ self .host,self .hostname,现在,“严重-从属IO错误!” ] 
                wx.CallAfter(self .app.ErrorMessage,errlist) 
                a =  self .hostname +   “”  +  self .host +  “严重-从属IO错误! 
                logging.basicConfig(level = logging.DEBUG,format = '%{asctime)s%{levelname)s%{message)s' ,datefmt = '%Y-%m-%d%H:%M:%S' ,filename = 'slave.log' ,filemode = 'a' ) 
                记录错误(a) 
                返回2  
            其他: 
                #print now +“” + self.hostname +“严重-从属IO和SQL错误!” 
                errlist = [ self .host,self .hostname,现在,“严重-从属IO和SQL错误!” ] 
                wx.CallAfter(self .app.ErrorMessage,errlist) 
                “ =  自我.hostname +   ”“  +  自我.host +  ”严重-从IO和SQL错误! 
                logging.basicConfig(level = logging.DEBUG,format = '%{asctime)s%{levelname)s%{message)s' ,datefmt = '%Y-%m-%d%H:%M:%S' ,filename = 'slave.log' ,filemode = 'a' ) 
                记录错误(a) 
                返回3  
 
 StartScan(threading.Thread)类: 
    def  __init __(self ,app): 
        threading.Thread .__ init __(self ) 
        自我.app =应用 
        自我.timeToQuit = threading.Event() 
        自我.timeToQuit.clear() 
    def  stop(self ): 
        自我.timeToQuit.set() 
    def  run(self ): 
        自我.user,自我.passwd,自我.hostlist,自我.a,自我.time =  自我.conf() 
        自我时间=  1 
        而True :  
            线程= [] 
            如果self .timeToQuit.isSet():  
                打破 
            对于 我  在 范围内(自我.a): 
                主持人=  自我.hostlist [i] [ 0 ] 
                主机名=  自我.hostlist [i] [ 1 ] 
                老板=检查(自我,自我。用户,自我.passwd,主机,主机名) 
                t = threading.Thread(target = boss.Status,args =()) 
                thread.append(t) 
            #print'总%s线程正在工作...'%self.a +'\ n' 
            msg = u “第”  +  “%s” %(self .times)+ u “次扫描.....” 
            自我.app.CurrentScan(msg) 
            对于 我  在 范围内(自我.a): 
                线程[i] .start() 
                睡眠时间(0.05 ) 
            对于 我  在 范围内(自我.a): 
                线程[i] .join() 
            自我.times + =  1 
            时间。睡眠(自我。时间) 
    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” ) 
        用户= fp.get(“全局” ,  “用户” ) 
        passwd = fp.get(“ global” ,  “ passwd” ) 
        时间= fp.getint(“全局” ,“时间” ) 
        用户= base64.decodestring(用户) 
        passwd = base64.decodestring(passwd) 
        主机列表= [] 
        对于 我  在 iplist.split(“;” ): 
            hostlist.append(i.split(“,” )) 
        a = len(主持人名单) 
        返回 用户,密码,主机列表,时间 
    def  ErrorMessage(self ,errlist): 
        wx.CallAfter(self .app.ErrorMessage,errlist) 
 
类 ScanUnit(wx.Panel): 
    def  __init __(self ,parent,id = -1 ,title = '' ,port = `` ,): 
        wx.Panel .__ init __(self ,parent,id) 
        自我。父母=父母 
        自我.id = id 
        自我.title =标题 
        self .scaning = wx.StaticText(self ,-1 ,label = u “准备扫描……” ,style = wx.ALIGN_LEFT | wx.ST_NO_AUTORESIZE) 
        self .openBtn = wx.Button(self ,-1 ,u '打开日志' ) 
        自我.openBtn.SetForegroundColour('red' ) 
        #self.openBtn.SetBackgroundColour('紫色') 
        self .startBtn = wx.Button(self ,-1 ,u '扫描' ) 
        self .stopBtn = wx.Button(self ,-1 ,u '停止' ) 
        自我.stopBtn.Disable() 
        自我.list = AutoWidthListCtrl(自我) 
        自我.list.SetTextColour(“ red” ) 
        自我.list.InsertColumn(0 ,  'IP' ,width = 120 ) 
        self .list.InsertColumn(1 ,u '区组名称' ,width = 100 ) 
        self .list.InsertColumn(3 ,u '发生时间' ,width = 140 ) 
        self .list.InsertColumn(4 ,u '返回信息' ,width = 200 ) 
        自我.list.DeleteAllItems() 
        自我.Bind(wx.EVT_BUTTON,自我.OnStart,自我.startBtn) 
        自.Bind(wx.EVT_BUTTON,自.OnStop,自.stopBtn) 
        self .bind(wx.EVT_BUTTON,self .Open,self .openBtn) 
        自我._layout() 
    def  _layout(self ):   #布局 
        动作= wx.BoxSizer(wx.HORIZONTAL) 
action.Add         (self .scaning,3 ,wx.ALIGN_CENTER_HORIZONTAL | wx.EXPAND)#对齐没有解决 
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.StaticBoxSizer(self .box,wx.VERTICAL) 
        自我.ScanUnit.Add(action,  0 ,wx.ALL | wx.EXPAND,  2 ) 
        自我.ScanUnit.Add(listbox,  5 ,wx.EXPAND,  2 ) 
        自我.SetSizer(自我.ScanUnit) 
        self .parent.sizer.Add(self ,1 ,wx.EXPAND) 
    def  OnStart(self ,event): 
        自我.startBtn.Disable() 
        自我.stopBtn.Enable() 
        #self.DeleteItems() 
        自我.thread = StartScan(自我) 
        自我.thread.setDaemon(True ) 
        自我.thread.start() 
        self .scaning.SetLabel(u '正在扫描……' ) 
    def  OnStop(self ,event): 
        自我.stopBtn.Disable() 
        自我.startBtn.Enable() 
        自我.thread.stop() 
        self .scaning.SetLabel(u '停止扫描……' ) 
    def  Open(self ,event): 
        wx.Execute(“ notepad slave.log” ) 
        #os.system('notepad slave.log') 
    def  CurrentScan(self ,msg):  #当前扫描动作 
        自我.scaning.SetLabel(msg) 
    def  ErrorMessage(self ,errlist):#错误信息 
        索引=  自我.list.InsertStringItem(sys.maxint,errlist [ 0 ]) 
        自我.list.SetStringItem(index,  1 ,errlist [ 1 ]) 
        自我.list.SetStringItem(index,  2 ,errlist [ 2 ]) 
        自我.list.SetStringItem(index,  3 ,errlist [ 3 ]) 
        自我.list.RefreshItem(index) 
    def  DeleteItems(self ): 
        自我.list.DeleteAllItems() 
 
类 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 ) 
 
 MainPanel(wx.Panel)类: 
    def  __init __(self ,parent): 
        wx.Panel .__ init __(self ,parent) 
        self .sizer = wx.GridSizer(行= 1 ,cols = 1 ,hgap =  20 ,vgap =  15 ) 
        self .aaa = ScanUnit(self ,-1 ,u '<Mysql主从>' ,'8001' ) 
        self .SetSizer(self .sizer) 
 
class  CreateMenu():#创建菜单 
    def  __init __(self ,parent): 
        自我.menuBar = wx.MenuBar() 
        自我.file = wx.Menu() 
        self .close =  self .file.Append(-1 ,u '退出(&X)' ) 
        自我.menuBar.Append(自我.file,u '文件(&F)' ) 
        自我.help = wx.Menu() 
        self .about =  self .help.Append(-1 ,u '关于(&A)' ) 
        self .menuBar.Append(self .help,u '帮助(&H)' ) 
        parent.SetMenuBar(self .menuBar) 
 
类 MyFrame(wx.App): 
    u '' '''Mysql主从监控\ n电子邮件:wangwei03@gyyx.cn \ nQQ:83521260''' 
    def  OnInit(self ): 
        自.frame = wx.Frame(父= 无,ID = - 1 ,标题= U 'Mysql的主从监控程序' ,大小=(650 ,450 )) 
        自我.frame.SetIcon(wx.Icon('kankan.ico' ,wx.BITMAP_TYPE_ICO)) 
        self .panel = MainPanel(self .frame ) 
        自我.frame.Center(direction = wx.BOTH) 
        self .menu = CreateMenu(self .frame) 
        自我.frame.statusBar =  自我.frame.CreateStatusBar(3 ) 
        #print目录(self.frame.statusBar) 
        自我.frame.statusBar.SetStatusWidths([- 12 ,-12 ,-13 ])  
        自我.frame.statusBar.SetForegroundColour('紫色' ) 
        自我.frame.statusBar.SetBackgroundColour('pink' ) 
        自我.frame.StatusBar.Font.Bold =  True 
        #self.frame.StatusBar.Font.Size = 13 
        自我.frame.StatusBar.SetStatusText(u “好好学习” ,0 ) 
        self .frame.StatusBar.SetStatusText(u “天天向上” ,1 ) 
        自我.timer = wx.PyTimer(自我.Notify) 
        自我.timer.Start(1000 ) 
        自我.Notify() 
        self .Bind(wx.EVT_MENU,self .OnClose,self .menu.close) 
        self .Bind(wx.EVT_MENU,self .OnAbout,self .menu.about) 
        自我.SetTopWindow(自我.frame ) 
        自我.frame.Show() 
        返回True  
    def  通知(self ): 
        t = time.localtime(time.time()) 
        st = time.strftime(“%Y-%m-%d%H:%M:%S” ,t) 
        self .frame.StatusBar.SetStatusText(u “当前系统时间” + st,2 ) 
    def  OnClose(self ,event): 
        自我.frame.Destroy() 
    def  OnAbout(self ,event): 
        wx.MessageBox(self .__ doc__ ,'Mysql Replication Status' ,wx.OK) 
 
如果 __name__ ==  “ __main__” :  
    app = MyFrame(无) 
    app.MainLoop() 

配置文件的名字为:repl.ini

格式为:

[global] user = cmVwbsdfsdfA== passwd = SGMxNzVBcEdEZ0ZRTGsfdfV6aA== time = 10 iplist = 192.168.8.11,烟雨江南;192.168.8.12,开天辟地; #说明:用户名和密码#base64.encodestring(),base64.decodestring()加密和解密,time为设置的超时时间(单位为秒),iplist为IP和名字列表

主要用于批量的mysql丛库服务器状态监控

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档