在python脚本中尝试提取日志时出现“ KeyError:'pagination'”

时间:2019-11-28 13:36:56

标签: python python-3.x

我正在尝试使用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

0 个答案:

没有答案