【python】couchbase稽核指令碼__python

來源:互聯網
上載者:User
#couchbasescript.py#!/usr/bin/python# -*- coding:gbk -*-#程式功能 驗證couchbase中的鍵是否對應mysql表的值from couchbase.bucket import Bucketimport ConfigParserimport MySQLdbimport timeimport os#couchbase配置cp= ConfigParser.ConfigParser()cp.read('my.conf')couchbaseip=cp.get('couchbase', 'ip')couchbaseport=cp.get('couchbase', 'port')bucketname=cp.get('couchbase', 'buckname')bucketpasswd=cp.get('couchbase', 'buckpasswd')#print couchbaseip, buckname,buckpasswd,couchbaseport#mysql配置mysqlip=cp.get('mysql', 'ip')mysqlport=cp.get('mysql', 'port')mysqlusername=cp.get('mysql', 'username')mysqldbname_tatrade=cp.get('mysql', 'dbname_tatrade')mysqlpasswd=cp.get('mysql', 'passwd')mysqlip1=cp.get('mysql', 'ip1')mysqlport1=cp.get('mysql', 'port1')mysqlusername1=cp.get('mysql', 'username1')mysqldbname_tatrade1=cp.get('mysql', 'dbname_tatrade1')mysqlpasswd1=cp.get('mysql', 'passwd1')#print mysqlip, mysqlport, mysqlusername, mysqldbname_tatrade, mysqlpasswd# 開啟couchbasecouchbaseconnect='couchbase://'+couchbaseip+':' + couchbaseport +'/' + buckname#print couchbaseconnect#c = Bucket("couchbase://10.2.130.78:8091/assign", password='assign')try:    c = Bucket(couchbaseconnect, password=buckpasswd)except:    print "couchbase串連失敗, 串連資訊如下:"    print couchbaseconnect    print "buchket的密碼為:%s" % buckpasswd    exit()# 開啟資料庫連接try:    db1 = MySQLdb.connect(mysqlip,mysqlusername,mysqlpasswd,mysqldbname_tatrade)    db2 = MySQLdb.connect(mysqlip1,mysqlusername1,mysqlpasswd1,mysqldbname_tatrade1)except:    print "資料庫連接失敗!串連資訊如下:"    print "IP=%s, PORT=%s, username=%s, dbname=%s, password=%s" % (mysqlip, mysqlport, mysqlusername, mysqldbname_tatrade, mysqlpasswd)    exit()# 使用cursor()方法擷取操作遊標 cursor1 = db1.cursor()cursor2 = db2.cursor()print "查詢開始!"sql= " select d_sysdate from  ta_ttainfo t where t.c_tacode='87' and t.c_tenantid='*' "cursor1.execute(sql)dcdate=cursor1.fetchone()#目錄是否存在s=os.path.exists("result")if (s == False ):   os.mkdir("result")# 開啟檔案filename= "result/result_%d.txt" % dcdatef=open(filename,'w')f.write("\n")f.write("                                      賬戶類核對資料稽核表                                   \n")f.write("--------------------------------------------------------------------------------------------------")f.write("\n")f.write("資料庫連接資訊如下:\n")f.write("資料庫IP=%s, 連接埠=%s, 使用者名稱=%s, 資料庫=%s, 資料庫密碼=%s \n" % ( mysqlip, mysqlport, mysqlusername, mysqldbname_tatrade, mysqlpasswd))f.write("資料庫IP=%s, 連接埠=%s, 使用者名稱=%s, 資料庫=%s, 資料庫密碼=%s \n" % ( mysqlip1, mysqlport1, mysqlusername1, mysqldbname_tatrade1, mysqlpasswd1))f.write("couchbase的串連資訊:%s\n" % couchbaseconnect )f.write("--------------------------------------------------------------------------------------------------")f.write("\n")f.write("檢查點:檢查確認表中的基金帳號,是否在couchbase中存在\n")############################################################賬戶申請couchbase查詢##################################################33# SQL 查詢語句sql = ''' SELECT c_fundacco from ta_taccoconfirm               where d_cdate= (select d_sysdate from  ta_ttainfo t where t.c_tacode='87' and t.c_tenantid='*' )                   and c_businflag='81'                   and c_tacode= '87'                   and c_tenantid= '*'                   and c_status='1'        '''f.write("\n")f.write("查詢的SQL語句: %s \n" % sql )f.write("--------------------------------------------------------------------------------------------------")f.write("\n")f.write("\n")f.write("開始時間: " + time.strftime("%Y-%m-%d %H:%M:%S") + "\n")f.write("分庫%s的檢查結果:\n" % mysqldbname_tatrade)f.write("\n")try:   # 執行SQL語句   cursor1.execute(sql)   # 擷取所有記錄列表   results = cursor1.fetchall()   #print results   for row in results:    c_fundacco = row[0]        # 列印結果    #print "賬戶申請表中的資料fundacco=%s" % (c_fundacco )    #擷取couchbase中的基金帳號        try:            res = c.get("*_"+c_fundacco)            #print res.value['custkey']            res=c.get(res.value['custkey'])            couchbase_fundacco=res.value['fundacco']            if (couchbase_fundacco <> c_fundacco):                #print "不相同的基金帳號(確認表的帳號=%s, couchbase=%s)" % (couchbase_fundacco, c_fundacco)                f.write("不相同的基金帳號(確認表的帳號=%s, couchbase=%s) \n" % (couchbase_fundacco, c_fundacco))        except:            #print "在couchbase中查不到的帳號帳號%s" %(c_fundacco)            f.write("在couchbase中查不到的帳號帳號: %s \n" %(c_fundacco))except Exception,e:    print Exception, ":", ef.write("\n\n")f.write("分庫%s的檢查結果:\n" % mysqldbname_tatrade1)try:   # 執行SQL語句   cursor2.execute(sql)   # 擷取所有記錄列表   results = cursor2.fetchall()   #print results   for row in results:    c_fundacco = row[0]        # 列印結果    #print "賬戶申請表中的資料fundacco=%s" % (c_fundacco )    #擷取couchbase中的基金帳號        try:            res = c.get("*_"+c_fundacco)            #print res.value['custkey']            res=c.get(res.value['custkey'])            couchbase_fundacco=res.value['fundacco']            if (couchbase_fundacco <> c_fundacco):                #print "不相同的基金帳號(確認表的帳號=%s, couchbase=%s)" % (couchbase_fundacco, c_fundacco)                f.write("不相同的基金帳號(確認表的帳號=%s, couchbase=%s) \n" % (couchbase_fundacco, c_fundacco))        except:            #print "在couchbase中查不到的帳號帳號%s" %(c_fundacco)            f.write("在couchbase中查不到的帳號帳號: %s \n" %(c_fundacco))except Exception,e:    print Exception, ":", edb1.close()db2.close()f.closef.write("\n\n")f.write("結束時間: " + time.strftime("%Y-%m-%d %H:%M:%S") + "\n")print "查詢完成!"os.system("pause")
#my.cfg#目錄設定檔中可以支援兩個分庫的設定[couchbase]ip=10.2.130.78port=8091bucketname=assignbucketpasswd=assign[mysql]ip=10.2.130.78port=3306username=rootdbname_tatrade=hs_tatradepasswd=hstest@1ip1=10.2.130.78port1=3306username1=rootdbname_tatrade1=hs_tatrade1passwd1=hstest@1
需要安裝如下軟體清單:1.pyhton-2.7.122.MySQL-python-1.2.5.win32-py2.73.couchbase-2.1.2.win32-py2.7把python/bin的安裝目錄添加到環境變數中。

註:
所有軟體下載地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.