示例#1
0
 def get_file(self, src_path, dest_path):
     '''
     :param src_path: 远端目录
     :param dest_path: 本地目录
     :return:
     '''
     if src_path[-1] == '/':
         src_path = src_path[:-1]
     if dest_path[-1] == '/':
         dest_path = dest_path[:-1]
     src_path = src_path.replace('\\', '/')
     dest_path = dest_path.replace('\\', '/')
     self.__makedirs_if_not_exist(dest_path)
     try:
         transport = paramiko.Transport((self.server_ip_address, self.server_port))
         transport.connect(username=self.server_username, password=self.server_password)
         sftp = paramiko.SFTPClient.from_transport(transport)
         all_files = self.__get_all_files_in_remote_dir(sftp, src_path)
         for full_file_path in all_files:
             # filename = os.path.split(full_file_path)[1]
             # filepath = os.path.split(full_file_path)[0]
             prefix_dir_path = os.path.dirname(src_path)
             relative_path = self.__trunc_prefix_path(prefix_dir_path, full_file_path)
             dest_full_path = dest_path + '/' + relative_path
             dest_dir_path = os.path.dirname(dest_full_path)
             self.__makedirs_if_not_exist(dest_dir_path)
             # dest_full_path = dest_full_path.replace('\\', '/')
             logger.info('[{}] get: {} to [Local]: {}'.format(self.server_ip_address, src_path, dest_full_path))
             # print(
             #     '[{}] get: {} to [Local]: {}'.format(self.server_ip_address,src_path,dest_full_path)
             #     )
             sftp.get(full_file_path, dest_full_path)
     except Exception as error:
         logger.error("Get File ERR: [{}] [{}]".format(self.server_ip_address, error))
示例#2
0
    def make_single_awr(self, db, dbid, inst_num, bid, eid, filename):
        one_awr_report_html_sql = self.awr_sql['awr_report_html']
        awr_report_html_sql = one_awr_report_html_sql.format(
            dbid, inst_num, bid, eid)
        filepath = os.path.abspath(
            os.path.join(os.path.dirname(os.path.dirname(__file__)), 'logs',
                         'awr', filename))
        dirpath = os.path.dirname(filepath)
        if not os.path.exists(dirpath):
            os.makedirs(dirpath)
        logger.info('make awr report: {}'.format(filepath))
        res = db.select_db(awr_report_html_sql)
        for oneline in res:
            newline = oneline[0]
            if newline is None:
                newline = '\n'

            try:
                fh = open(filepath, 'ab+')
                newline = newline.encode('utf-8')
                fh.write(newline)
            except Exception as err:
                print("err:", oneline)
                print(err)
            finally:
                fh.close()
示例#3
0
    def exec_multi_commands(self, cmd_list, recv_bytes=1024):
        shell_cmd = self.ssh.invoke_shell()

        for one_cmd in cmd_list:
            logger.info('[{}] run: {}'.format(self.server_ip_address, one_cmd))
            # print ('[{}] run: {}'.format(self.server_ip_address,one_cmd))
            shell_cmd.send(one_cmd + '\n')
            receive_buf = ''
            while True:
                sleep(0.5)
                # print (shell_cmd.recv(recv_bytes))
                result = shell_cmd.recv(recv_bytes).decode('utf-8')
                receive_buf += result
                if len(result) != recv_bytes:
                    receive_buf = self.format_output(receive_buf, one_cmd)
                    logger.debug(receive_buf)
                    logger.info('[{}] status: {}'.format(self.server_ip_address, 'cmd executed'))
                    # print (receive_buf)
                    break
示例#4
0
 def connect_server(self):
     while True:
         try:
             self.ssh = paramiko.SSHClient()
             self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
             self.ssh.connect(hostname=self.server_ip_address, port=self.server_port, username=self.server_username,
                              password=self.server_password)
             logger.info('Connect server :{} Success'.format(self.server_ip_address))
             # print('Connect server :{} Success'.format(self.server_ip_address))
             return
         except Exception as err:
             if self.try_times != 0:
                 logger.warn('Connect failed, Begin to retry [{}]...'.format(4 - self.try_times))
                 # print('Connect failed, Begin to retry ...')
                 self.try_times -= 1
             else:
                 logger.error(
                     'Retry {0} times, Catch an error {1}, Skip connect to {2} !'.format(self.try_times, err,
                                                                                         self.server_ip_address))
                 # print('Retry {0} times, Catch an error {1}, Skip connect to {2} !'.format(self.try_times, err,
                 #                                                                           self.server_ip_address))
                 break
示例#5
0
 def exec_commands(self, cmd_str):
     '''
     :param exec_cmd: 需要执行的命令(单个命令)
     :return:
     '''
     if not isinstance(cmd_str, str):
         logger.error("Only suppoort commands in one string")
         # print("Only suppoort commands in one string")
         exit()
     # ssh = paramiko.SSHClient()
     # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     # ssh.connect(hostname=self.server_ip_address, port=self.server_port, username=self.server_username,
     #             password=self.server_password)
     exec_cmd = 'source .bash_profile || source .profile;' + cmd_str
     logger.info('run cmd: {}'.format(cmd_str))
     stdin, stdout, stderr = self.ssh.exec_command(exec_cmd)
     res, err = stdout.read(), stderr.read()
     result = res if res else err
     logger.debug("[%s]".center(50, "-") % self.server_ip_address)
     # print("[%s]".center(50, "-") % self.server_ip_address)
     logger.info('[{}] status: {}'.format(self.server_ip_address, 'cmd executed'))
     # print('run cmd: {}'.format(cmd_str))
     logger.debug(result.decode())
示例#6
0
    def search(self):
        all_list = self.list_all_files(self.src_path)
        for one_file in all_list:
            fh = open(one_file, mode='r+t', encoding='utf-8')
            try:
                text_lines = fh.readlines()
                row = 1
                for one_line in text_lines:
                    for match_key in self.match_keys:
                        if re.search(match_key, one_line):
                            logger.info(
                                'file: [{}], lines: [{}] match the key: {} '.
                                format(one_file, row, match_key))
                            logger.debug(
                                'match row content: [{}]'.format(one_line))
                            # print('file: [{}], lines: [{}] match the key: {} '.format(match_key, one_file, row))

                    row += 1
            except Exception as err:
                logger.error('skip file: {}, \n error: {}'.format(
                    one_file, err))
                # print('[Warning]skip file: {}, error: {}'.format(err, one_file))
            finally:
                fh.close()
示例#7
0
 def main_process(self, collect_date):
     rescode = self.isVaildDate(collect_date)
     if not rescode:
         logger.error('invaild date: {}'.format(collect_date))
         exit()
     for onekey in self.db_config:
         onerow = self.db_config[onekey]
         logger.info(onerow)
         (host, listen_port, service_name, db_username,
          db_password) = self.get_single_db_info(onerow)
         ora = OracleExecFactory(host, listen_port, service_name,
                                 db_username, db_password)
         col_date = self.format_date(collect_date)
         start_date = col_date
         end_date = col_date
         flag = True
         while start_date.strftime("%Y-%m-%d") == end_date.strftime(
                 "%Y-%m-%d") and flag:
             cur_date = datetime.datetime.now() + datetime.timedelta(
                 hours=-1)
             start_date = end_date
             end_date = end_date + datetime.timedelta(
                 hours=self.awr_interval_time)
             start_str = start_date.strftime("%Y%m%d%H")
             end_str = end_date.strftime("%Y%m%d%H")
             if end_date < cur_date:
                 (db_id, inst_num, bid,
                  eid) = self.get_awr_element(ora, start_str, end_str)
                 filename = 'awrrpt_{}_{}_{}_{}-{}.html'.format(
                     host, service_name, start_date.strftime("%Y%m%d"),
                     start_str, end_str)
                 logger.info(
                     'db_id: {}, inst_num: {}, bid: {}, eid: {}'.format(
                         db_id, inst_num, bid, eid))
                 self.make_single_awr(ora, db_id, inst_num, bid, eid,
                                      filename)
             else:
                 end_str = cur_date.strftime("%Y%m%d%H")
                 flag = False
                 if start_str != end_str:
                     (db_id, inst_num, bid,
                      eid) = self.get_awr_element(ora, start_str, end_str)
                     filename = 'awrrpt_{}_{}_{}_{}-{}.html'.format(
                         host, service_name, start_date.strftime("%Y%m%d"),
                         start_str, end_str)
                     logger.info(
                         'db_id: {}, inst_num: {}, bid: {}, eid: {}'.format(
                             db_id, inst_num, bid, eid))
                     self.make_single_awr(ora, db_id, inst_num, bid, eid,
                                          filename)
                 else:
                     break
示例#8
0
 def close_server(self):
     logger.info('disconnect from [{}]'.format(self.server_ip_address))
     self.ssh.close()