#!/usr/bin/python
#-*- coding: UTF-8 -*-
# ########################################################################
# This program
# Version: 2.0.0 (2012-10-10)
# Authors: lianjie.ning@qunar.com
# History:
# ########################################################################
import os
import socket
import subprocess
import sys
import traceback
from ConfigParser import ConfigParser
class Finger(object):
'finger.py'
def __init__ (self):
print '---- %s, %s' % (socket.gethostname(), self.__doc__)
def load_config (self, file="finger.ini"):
if not os.path.exists(file):
print file,"is not exists, but is created, please fix it"
temp_ini = '''[conn_db]
login_pwd =
exec_sql =
'''
open(file, 'w').write(temp_ini)
os.chmod(file, 0600)
sys.exit()
config = ConfigParser()
config.read(file)
if config.has_section('conn_db') is True:
if config.has_option('conn_db', 'login_pwd') is True:
login_pwd = config.get('conn_db', 'login_pwd')
if config.has_option('conn_db', 'exec_sql') is True:
exec_sql = config.get('conn_db', 'exec_sql')
return (login_pwd, exec_sql)
def grant_user(self, login_pwd, exec_sql):
if os.path.exists('/usr/local/bin/mysql'):
mysql = '/usr/local/bin/mysql'
elif os.path.exists('/usr/bin/mysql'):
mysql = '/usr/bin/mysql'
elif os.path.exists('/bin/mysql'):
mysql = '/bin/mysql'
else:
print "command not fount of mysql"
sys.exit()
user = 'xxxx'
conn_port = [3306,3307,3308,3309,3310]
for i in conn_port:
ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
address = ('127.0.0.1', int(i))
status = ss.connect_ex(address)
ss.settimeout(3)
ss.close()
if status == 0:
conn_mysql = '%s -u%s -p%s -h127.0.0.1 -P%d -N -s -e"%s"' % (mysql, user, login_pwd, i, exec_sql)
p = subprocess.call(conn_mysql, shell=True, stdout=open("/dev/null"))
if p == 0:
print "---- checking port: %s is NORMAL" % i
else:
print "---- checking prot: %s is ERROR" % i
if __name__ == '__main__':
try:
process = Finger()
(login_pwd, exec_sql) = process.load_config()
process.grant_user(login_pwd, exec_sql)
except Exception, e:
print str(e)
traceback.print_exc()
sys.exit()