def post(self, *args, **kwargs): data = json.loads(self.request.body.decode("utf-8")) db_obj = {} db_obj['host'] = data['db_host'] db_obj['port'] = int(data['db_port']) db_obj['user'] = data['db_user'] db_obj['passwd'] = data['db_pwd'] db_obj['db'] = data['db_instance'] if len(data['db_pwd']) == 32: db_obj['passwd'] = decrypt(data['db_pwd']) try: if data['db_type'] == 'mysql': mysql_conn = MysqlBase(**db_obj) res = mysql_conn.test() elif data['db_type'] == 'oracle': oracle_conn = OracleBase(**db_obj) res = oracle_conn.test() except Exception as e: return self.write(dict(code=-1, msg='%s' % e)) return self.write(dict(code=0, msg='连接成功'))
def run(): DB_INFO = getDBList() for db in DB_INFO: try: # 解密密码 db_pwd = decrypt(db[5]) db_conf = { 'host': db[2], 'port': int(db[3]), 'user': db[4], 'passwd': db_pwd, 'db': db[6] } if not db[6]: if db[1] == 'mysql': db_conf['db'] = 'mysql' if db[1] == 'oracle': db_conf['db'] = 'orcl' if db[1] == 'mysql': db_conn = MysqlBase(**db_conf) if db[1] == 'oracle': db_conn = OracleBase(**db_conf) if db_conn.test(): state = 'Running' else: state = 'failed' CUSTOM_DB_INFO['db'] = 'codo_cmdb' mysql_conn = MysqlBase(**CUSTOM_DB_INFO) up_sql = '''update codo_cmdb.asset_db set state = '%s' where id = %s''' % ( state, db[0]) mysql_conn.change(up_sql) except: traceback.print_exc() return 'failed' print(state) return 'ok'
def mysqlConnTest(data_dict): ############# 监测数据库连接情况,后续放着定时任务里面# ############ try: # 解密密码 db_pwd = decrypt(data_dict['db_pwd']) db_conf = { 'host': data_dict['db_host'], 'port': int(data_dict['db_port']), 'user': data_dict['db_user'], 'passwd': db_pwd, 'db': READONLY_DB_DBNAME } mysql_conn = MysqlBase(**db_conf) if mysql_conn.test(): data_dict['state'] = 'Running' else: data_dict['state'] = 'failed' del mysql_conn except: data_dict['state'] = 'failed'