Export logs with Python
💡 3 min read
Learn how to use Python to export logs as a JSON file
This article explains how to export the Onna platform activity logs as a JSON file using Python on Linux or macOS.
Info
You can run the script with your user account or as service account.
A service account is recommended,
if you plan to run it recurrently or unattended.
Please contact support[at]onna.com for assistance with setting up a service account.
# Requirements
- The same Python version used by Onna Enterprise. Currently 3.8 (opens new window)
- Python Virtual Environment (venv) (opens new window), included with Python 3.8
- Python Package Installer (pip) (opens new window)
# Setup the virtual environment
- Create a directory for your virtual environment, start the virtual environment, and then activate it.
mkdir ~/onna-export-logs
cd ~/onna-export-logs
python3 -m venv venv
source venv/bin/activate
1
2
3
4
2
3
4
- Download the requirements file (opens new window).
- Install the dependencies.
pip install -r requirements.txt
1
- Download the sample script (opens new window).
- To see the script's arguments, run
./venv/bin/python auditlogs.py --help
.
./venv/bin/python auditlogs.py --help
usage: auditlogs.py [-h] --username USERNAME --password PASSWORD --account ACCOUNT --account_url ACCOUNT_URL --from_date FROM_DATE --to_date TO_DATE --fname FNAME [--container CONTAINER] --size SIZE
Download Onna audit logs
optional arguments:
-h, --help show this help message and exit
--username USERNAME Service Account username
--password PASSWORD password
--account ACCOUNT the Onna account name
--account_url ACCOUNT_URL
the URL of your account, e.g https://enterprise.onna.com/ACMECORP
--from_date FROM_DATE
start date. most date formats are accepted
--to_date TO_DATE end date. most date formats are accepted
--fname FNAME name of the file
--container CONTAINER
name of the account container
--size SIZE result size
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Where:
USERNAME
is your account emailPASSWORD
is your account passwordACCOUNT
is your account nameACCOUNT_URL
is your account URL. For example, https://enterprise.onna.com/ACMECORPFROM_DATE
is the start date of the logs to export. Use the format "MM/DD/YYYY"TO_DATE
is the end date of the logs to export. Use the format "MM/DD/YYYY"FNAME
is the name of the file. It will be saved as a JSON file in your/tmp
folderCONTAINER
is the name of your account. For more info, see the glossary.SIZE
is the number of items downloaded for each batch
- Run the script.
For example:
./venv/bin/python auditlogs.py --username ACMEUSER@ACMECORP.COM \
--password Y0urP#ssWoRD!* --account ACMECORP \
--account_url https://enterprise.onna.com \
--from_date "01/01/2020" --to_date "01/20/2020" \
--fname example-audit --container ACMECORP \
--size 100
1
2
3
4
5
6
2
3
4
5
6
Info
After running the script, remember to deactivate your virtual environment by typing deactivate
in your shell.
The result will look similar to the output below:
Expand to see the example output
[
{
"_index": "user-activity-account-2019-07-11",
"_type": "doc",
"_id": "AWvhkN8cdgM3tma3FpC6",
"_score": null,
"_source": {
"type": "log",
"action": "nav-menu-opened",
"payload": {
"session-timestamp": 1562857496133,
"route": "/dashboard/workspace/list",
"element": "nav-menu-opened",
"x": 42,
"y": 42,
"width": 1280,
"height": 1024,
"user_hash": "[sha256]30b52a374c4a20b643114e69dca3901283647233fe91916e1dcdd2e1d76784a9",
"user": "robot.integration.test@gmail.com",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/76.0.3809.62 Safari/537.36",
"origin": "https://enterprise.onna.com/account",
"session-id": "78c2147335d64dc0b48e5667f684cd91",
"account": "account"
},
"date": "2019-07-11",
"@timestamp": "1562857496000"
},
"sort": [
1562857496000
]
},
{
"_index": "user-activity-account-2019-07-11",
"_type": "doc",
"_id": "AWvhkNsQdgM3tma3FpB8",
"_score": null,
"_source": {
"type": "log",
"action": "tracking-session",
"payload": {
"screen-width": 1440,
"screen-height": 900,
"os": "MacIntel",
"browser": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/76.0.3809.62 Safari/537.36",
"user_hash": "[sha256]30b52a374c4a20b643114e69dca3901283647233fe91916e1dcd34re1d5b784a9",
"user": "robot.integration.test@gmail.com",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/76.0.3809.62 Safari/537.36",
"origin": "https://enterprise.onna.com/account",
"session-id": "78c2147335d64dc0b48e5667f684cd91",
"account": "account"
},
"date": "2019-07-11",
"@timestamp": "1562857495000"
},
"sort": [
1562857495000
]
},
{
"_index": "user-activity-account-2019-07-11",
"_type": "doc",
"_id": "AWvhkMu-dgM3tma3Foru",
"_score": null,
"_source": {
"type": "log",
"action": "tracking-session",
"payload": {
"screen-width": 1440,
"screen-height": 900,
"os": "MacIntel",
"browser": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/76.0.3809.62 Safari/537.36",
"user_hash": "[sha256]30b52a374c4a20b643114e69dca39012836474r3fe91916e1dcdd2e1d5b784a9",
"user": "robot.integration.test@gmail.com",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/76.0.3809.62 Safari/537.36",
"origin": ""https://enterprise.onna.com/account",
"session-id": "37c8358bd3174e2da82f3341faa2bd60",
"account": "account"
},
"date": "2019-07-11",
"@timestamp": "1562857491000"
},
"sort": [
1562857491000
]
}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85