Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import cx_Oracle
  3. import ConfigParser
  4. import re
  5. import time
  6. from datetime import datetime
  7.  
  8. # To fix trouble with codepage
  9. import os
  10. os.environ["NLS_LANG"] = "Russian.AL32UTF8"
  11.  
  12. i = 0
  13.  
  14. # Read file with credential
  15. def read_oracle_cred(db_instance):
  16.     if os.path.exists('./connect.ini'):
  17.         send_to_log(' > Config file exists. Begin reading ini file')
  18.         time.sleep(1)
  19.         parser = ConfigParser.ConfigParser()
  20.         parser.read('./connect.ini')
  21.         db_host = parser.get(db_instance, 'db_host')
  22.         db_port = parser.get(db_instance, 'db_port')
  23.         db_sid = parser.get(db_instance, 'db_sid')
  24.         db_user =  parser.get(db_instance, 'db_user')
  25.         db_pass =  parser.get(db_instance, 'db_pass')
  26.     else:
  27.         send_to_log(' > Config file not exists. Can`t continue work')
  28.         send_to_log(' > End work')
  29.         raise SystemExit(1)
  30.     oracle_array = (db_host,db_port,db_sid,db_user,db_pass)
  31.     send_to_log(' > Config file read success')
  32.     return oracle_array
  33.  
  34. # Work with message
  35. def send_to_log(msg):
  36.     cur_time = str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
  37.     print (cur_time+msg)
  38.     f.write(cur_time+msg+'\n')
  39.  
  40. # Check connection to database
  41. def check_connection(db_instance):
  42.     send_to_log(' > Selected SID: '+db_instance.upper())
  43.     sql = 'select 1 from dual'
  44.     c.execute(sql)
  45.     for row in c:
  46.         res = str(row[0])
  47.     if res == '1':
  48.         send_to_log(' > Connection established')
  49.         time.sleep(1)
  50.     else:
  51.         send_to_log(' > Connection lost. Breake script')
  52.         time.sleep(1)
  53.         raise SystemExit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement