Python monitors the database port of a single multi-instance Database Server

Source: Internet
Author: User

Recently, the company has a single server with multiple instances of databases and needs to monitor the processes of each database, because our asset database already contains how many instances the server runs and what is the port, so we can compare the port of the asset database with the port of the database running locally on the server, and report the mysql instance that is not running and its purpose. One way is to crawl the local database port according to the "ps auxww | grep mysqld | grep-v root | grep-v grep" command, however, you can also use the netstat command to obtain all the ports of the local database instance. You can also obtain all the mysql ports that the server should run from the assets, it is easier to use the socket module of python to check whether the port is alive. I used the first method because I have already written this code for other purposes, which is equivalent to reuse. The following is the code :#! /Bin/env python #-*-coding: UTF-8-*-import OS, sys, MySQLdb def center (SQL): # connect to the database try: center_ip = '2017. 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.exe cute (SQL) alldata = cursor. fetchall () cursor. close () conn. close () return alldata records t: Return 0 class check_port (): # obtain the number of mysql instance ports in the local IP address from the asset 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 ): # obtain the number of local mysql instances running 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 (. 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. 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 () www.2cto.com if you use the second method, it is very simple. Use the following function to test the port: ipmort socket def test_port () s = socket. socket (socket. AF_INET, socket. SOCK_STREAM) s. settimeout (2) address = '2017. 0.0.1 'port = 80 try: s. connect (address, port) return True except t Exception, e: return False s. close ()

Related Article

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.