|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | +# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
| 4 | + |
| 5 | +# This script retrieves all audit logs across an OCI Tenancy. |
| 6 | +# for a timespan defined by start_time and end_time. |
| 7 | +# This sample script retrieves Audit events for last 5 days. |
| 8 | +# This script will work at a tenancy level only. |
| 9 | + |
| 10 | +import datetime |
| 11 | +import oci |
| 12 | + |
| 13 | + |
| 14 | +def get_regions(identity): |
| 15 | + ''' |
| 16 | + To retrieve the list of all available regions. |
| 17 | + ''' |
| 18 | + list_of_regions = [] |
| 19 | + list_regions_response = identity.list_regions() |
| 20 | + for r in list_regions_response.data: |
| 21 | + list_of_regions.append(r.name) |
| 22 | + return list_of_regions |
| 23 | + |
| 24 | + |
| 25 | +def get_compartments(identity, tenancy_id): |
| 26 | + ''' |
| 27 | + Retrieve the list of compartments under the tenancy. |
| 28 | + ''' |
| 29 | + compartment_ocids = [] |
| 30 | + # Store tenancy id as the first compartment |
| 31 | + compartment_ocids.append(tenancy_id) |
| 32 | + list_compartments_response = oci.pagination.list_call_get_all_results( |
| 33 | + identity.list_compartments, |
| 34 | + compartment_id=tenancy_id).data |
| 35 | + for c in list_compartments_response: |
| 36 | + compartment_ocids.append(c.id) |
| 37 | + return compartment_ocids |
| 38 | + |
| 39 | + |
| 40 | +def get_audit_events(audit, compartment_ocids, start_time, end_time): |
| 41 | + ''' |
| 42 | + Get events iteratively for each compartment defined in 'compartments_ocids' |
| 43 | + for the region defined in 'audit'. |
| 44 | + This method eagerly loads all audit records in the time range and it does |
| 45 | + have performance implications of lot of audit records. |
| 46 | + Ideally, the generator method in oci.pagination should be used to lazily |
| 47 | + load results. |
| 48 | + ''' |
| 49 | + list_of_audit_events = [] |
| 50 | + for c in compartment_ocids: |
| 51 | + list_events_response = oci.pagination.list_call_get_all_results( |
| 52 | + audit.list_events, |
| 53 | + compartment_id=c, |
| 54 | + start_time=start_time, |
| 55 | + end_time=end_time).data |
| 56 | + |
| 57 | + # Results for a compartment 'c' for a region defined |
| 58 | + # in 'audit' object. |
| 59 | + list_of_audit_events.extend(list_events_response) |
| 60 | + return list_of_audit_events |
| 61 | + |
| 62 | + |
| 63 | +# Setting configuration |
| 64 | +# Default path for configuration file is "~/.oci/config" |
| 65 | +config = oci.config.from_file() |
| 66 | +tenancy_id = config["tenancy"] |
| 67 | + |
| 68 | +# Initiate the client with the locally available config. |
| 69 | +identity = oci.identity.IdentityClient(config) |
| 70 | + |
| 71 | +# Timespan defined by variables start_time and end_time(today). |
| 72 | +# ListEvents expects timestamps into RFC3339 format. |
| 73 | +# For the purposes of sample script, logs of last 5 days. |
| 74 | +end_time = datetime.datetime.utcnow() |
| 75 | +start_time = end_time + datetime.timedelta(days=-5) |
| 76 | + |
| 77 | +# This array will be used to store the list of available regions. |
| 78 | +regions = get_regions(identity) |
| 79 | + |
| 80 | +# This array will be used to store the list of compartments in the tenancy. |
| 81 | +compartments = get_compartments(identity, tenancy_id) |
| 82 | + |
| 83 | +audit = oci.audit.audit_client.AuditClient(config) |
| 84 | + |
| 85 | +# For each region get the logs for each compartment. |
| 86 | +for r in regions: |
| 87 | + # Intialize with a region value. |
| 88 | + audit.base_client.set_region(r) |
| 89 | + # To separate results by region use print here. |
| 90 | + audit_events = get_audit_events( |
| 91 | + audit, |
| 92 | + compartments, |
| 93 | + start_time, |
| 94 | + end_time) |
| 95 | + |
| 96 | + # Results for a region 'r' for each compartment. |
| 97 | + if audit_events: |
| 98 | + print audit_events |
0 commit comments