#!/usr/bin/env python #encoding=utf8 import os,sys,MySQLdb import time,paramiko,threading #from multiprocessing import Pool user_name='dev1' passwd='xxx' def log_w(text,filename): goal="/tmp/data" if os.path.exists(goal): print "/tmp/data is exists" else: os.mkdir(goal) logfile = "/tmp/data/%s" % filename f = open(logfile,'a+') #text=text+"\n" f.write(text) f.close() def mysql_connect_query(ip,u,p,dbname,dbport,sql,dbchar=''): try: conn=MySQLdb.connect(host=ip,user=u,passwd=p,charset=dbchar,db=dbname,port=dbport,) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute(sql) data=cursor.fetchall() cursor.close() conn.close() return data except MySQLdb.Error,e: def mysql_connect_noquery(ip,u,p,dbname,dbport,sql,dbchar=''): try: conn=MySQLdb.connect(host=ip,user=u,passwd=p,charset=dbchar,db=dbname,port=dbport,) cursor=conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) ret=cursor.execute(sql) conn.commit() cursor.close() conn.close() return ret def ssh_connect(ip,password,cmd): ssh=paramiko.SSHClient() ssh.close() return data except Exception,e: print e def get_data(main_id,ip,dbport,dist_id,app_name): if app_name == 'adb' and int(dbport)<30000: pwd = "xxxx" elif app_name == 'ldb' and int(dbport)<30000: pwd="xxxx" else: pwd="xxxx" filename="%s_%s_%s.txt" % (main_id,dist_id,app_name) result = mysql_connect_query(ip,user_name,passwd,'ldb',dbport,sql) for a in result: res=a["update_time"]+'\t'+a["dist"].decode("gbk").encode("utf8")+'\t'+a["account"]+'\t'+str(a["level"])+'\t'+a["server"]+'\n' #print a log_w(res,filename) def get_dist_list(): ip="115.182.1.228" dbuser="read_only" dbpwd="xxx" #派K设U��~Dadb ��o纾D #派K设U��~Dadb+ldb ��o纾D #sql+=" union SELECT a.id,c.dist_id,c.ip,c.port,b.sub_name,c.app_name,CONCAT(a.main_name,'_',b.sub_name,'_',c.app_name) 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 b.flag='1' AND c.del_info!=0 AND c.app_name='ldb' AND c.dist_id<'10000' and c.dist_id in(14,181,36,249,23,280,144,91,273,286,287)" return mysql_connect_query(ip,dbuser,dbpwd,"center_app",3306,sql,"UTF8") def main(): #print os.getpid() threads = [] dist_list=get_dist_list() for x in dist_list: main_id=x["id"] ip=x["ip"] app_name=x["app_name"] dist_id=x["dist_id"] dbport=x["port"] sub_name=x["sub_name"].encode("GBK") t = threading.Thread(target=get_data,args=(main_id,ip,dbport,dist_id,app_name)) t.setDaemon(True) threads.append(t) for i in range(len(threads)): threads[i].start() for i in range(len(threads)): threads[i].join() if __name__ == "__main__": start=time.strftime("%Y%m%d%H%M%S") s_time=time.time() #print "start:",start main() end=time.strftime("%Y%m%d%H%M%S") e_time=time.time() #print "end:",end total=e_time-s_time