在试图提取日志的python脚本中获取“KeyError:'分页'”

2024-05-21 14:43:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用API和python获取日志。但是,在运行脚本文件时,出现以下错误:

Unexpected error processing audit event data. Exception: 'pagination'
Traceback (most recent call last):
File "audit_collection.py", line 88, in get_audit_logs
if 'next' in response_body['meta']['pagination']['next_link']:
KeyError: 'pagination'

我使用的代码是:

if 'next' in response_body['meta']['pagination']['next_link']:
                logging.debug('Saving pagination checkpoint')
                with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
                                                    'checkpoint_audit_next')), 'w') as ca:
                    ca.write(response_body['meta']['pagination']['next'])
                return True

我对python非常陌生,还在学习:-)

谢谢

编辑

if os.path.exists(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint', 'checkpoint_audit_next')):
    with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
                                        'checkpoint_audit_next')), 'r') as c:
        payload['meta']['pagination']['pageToken'] = c.read()

try:
    logging.debug('Calling Mimecast: ' + url + ' Request ID: ' + request_id + ' Request Body: ' + str(payload))
    response = requests.post(url=url, data=str(payload), headers=headers, timeout=120)
    logging.debug('Response code: ' + str(response.status_code) + ' Headers: ' + str(response.headers))
    if response.status_code == 429:
        logging.warn('Mimecast API rate limit reached, sleeping for 30 seconds')
        time.sleep(30)
        logging.debug('Calling Mimecast: ' + url + ' Request ID: ' + request_id)
        response = requests.post(url=url, data=str(payload), headers=headers, timeout=120)
    elif response.status_code != 200:
        logging.error('Request to ' + url + ' with , request id: ' + request_id + ' returned with status code: '
                      + str(response.status_code) + ', response body: ' + response.text)
        return False
    else:
        try:
            response_body = json.loads(response.text)
            logging.debug('Setting log file name...')
            data_file_name = datetime.datetime.utcnow().strftime('%d%m%Y')
            data_file_name = 'audit_log_' + str(data_file_name) + '.log'
            with open(os.path.join(data_dir, 'audit', data_file_name), 'a') as al:
                for event in response_body['data']:
                    al.write("date=" + event["eventTime"] + "|mcType=auditLog|user=" + event["user"] +
                             "|auditType=\"" + event["auditType"] + "\"|eventInfo=\"" + event["eventInfo"] + "\n")

            if 'next' in response_body['meta']['pagination']['next_link']:
                logging.debug('Saving pagination checkpoint')
                with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
                                                    'checkpoint_audit_next')), 'w') as ca:
                    ca.write(response_body['meta']['pagination']['next'])
                return True
            else:
                logging.debug('No more pages to collect. Saving end date as start date checkpoint for next run')
                with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
                                                    'checkpoint_audit_start')), 'w') as csd:
                    csd.write(end)
                if os.path.exists(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
                                               'checkpoint_audit_next')):
                    logging.debug('Cleaning up page token')
                    os.remove(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
                                           'checkpoint_audit_next'))
                return False
        except Exception, e:
            logging.exception('Unexpected error processing audit event data. Exception: ' + str(e))
            return False
except Exception, e:
    logging.error('Unexpected error calling API. Exception: ' + str(e))
    return False

我使用的代码来自https://github.com/SumoLogic/sumologic-content


Tags: pathdebugdataosresponseloggingbodypagination