python监控单台多实例数据库服务器端口的实例代码

发布时间:2020-12-22编辑:脚本学堂
本文介绍下,使用python代码实现监控单台多实例数据库服务器端口的方法,有需要的朋友参考学习下。

mysql/ target=_blank class=infotextkey>mysql数据库在单台服务器上实现多实例,需要对各个数据库的进程进行监控。

一种方法是根据"ps auxww|grep mysqld|grep -v root|grep -v grep"这个命令抓取本地运行的数据库端口,也可以根据linuxjishu/14092.html target=_blank class=infotextkey>netstat命令来获取本地数据库实例的所有端口;
还有一种方法,从资产中得到这个服务器应该运行的所有mysql端口,用python的socket模块来检测端口是否存活,这种方法比较简单一些。

下面对这二种方法,用python分别加以实现。

方法一的实现代码:
 

复制代码 代码示例:
#!/bin/env python
# -*- coding: utf-8 -*-
#edit: www.jb200.com
#
import os,sys,MySQLdb
 
def center(sql):#连接数据库
    try:
        center_ip = '192.168.1.100'
        center_user = 'root'
        center_passwd = 'xxxxxx'
        conn = MySQLdb.connect(host = center_ip,user = center_user,passwd = center_passwd,charset='utf8',connect_timeout=20)
        cursor = conn.cursor() 
        cursor.execute(sql)
        alldata = cursor.fetchall()
        cursor.close()
        conn.close()
        return alldata
    except:
        return 0
 
class check_port():#走资产中获取本地IP中应该有多少个mysql实例端口
 
    def __init__(self):
        conn = "ip a|grep glob|grep -v '192.168'|awk '{print $2}'"
        self.host = os.popen(conn).readlines()[0].split("/")[0]
 
    def remot(self):
        sql = "SELECT PORT FROM center.host_info WHERE flag='1' AND del_info!=0 AND ip='%s' AND host_name IN ('a','b','c')" % self.host
        alldata = center(sql)
        cent_port = []
        if alldata != 0:
            for i in alldata:
                cent_port.append(str(i[0]))
            return cent_port
        else:
            return cent_port
 
    def local(self):#获取本地mysql有多少个实例运行
        psinfo = os.popen("ps auxww|grep mysqld|grep -v root|grep -v grep").readlines()
        local_port = []
        if not psinfo:
            return local_port
        for i in psinfo:
            for j in i.split("--"):
                if j.find("port") != -1:
                    port = j.split("=")[1].strip()
                    local_port.append(port)
        return local_port
 
    def main(self):
        local_port = self.local()
        cent_port = self.remot()
                cent_port.sort()
                local_port.sort()
        if local_port == cent_port and len(local_port) != 0 and len(cent_port) != 0:
            print 0
        else:
            error = ""
            diff_list = list(set(local_port) ^ set(cent_port))
            for port in diff_list:
                sql = "SELECT CONCAT(a.main_name,'_',b.sub_name,'_',c.app_name,'_',c.port) AS used FROM center_app.main_category a, center_app.sub_category b, center_app.app_info c WHERE a.id = b.main_id AND b.dist_id = c.dist_id AND b.main_id = c.main_id AND b.main_id='2' AND c.flag='1' AND c.del_info!=0 AND c.ip='%s' and c.port='%s'" % (self.host,port)
                alldata = center(sql)
                if error == "":
                    error = error + alldata[0][0]
                else:
                    error = error + ";" + alldata[0][0]
            print error
 
if __name__ == "__main__":
    boss = check_port()
    boss.main()

如果采用第二种方法,可以使用下面的函数实现端口测试:
 

复制代码 代码示例:
#!/bin/python
#site: www.jb200.com
#
ipmort socket
 
def test_port()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)
    address = '127.0.0.1'
    port = 80
    try:
        s.connect((address,port))
        return True
    except Exception,e:
        return False        
    s.close()