For the complete documentation index, see llms.txt. This page is also available as Markdown.

Get active and inactive users

Run a Python script against the Harness Audit API to identify which users logged in over a specified time period.

Identify which users logged in to your Harness account over a specific time period. This topic provides a Python script that queries the Harness Audit API for LOGIN events across a date range, compares the results against every user in your account, and categorizes each user as active, inactive, or deleted.

Login activity supports several account management tasks:

  • Compliance and auditing: Track user access for security and regulatory requirements.

  • License management: Identify active users to optimize license usage.

  • User lifecycle management: Find inactive users who may need to be offboarded.


What you will learn in this topic

By the end of this topic, you will be able to:


Before you begin

Before you run the script, ensure you have the following:

  • Python 3.x: Installed on the system where you run the script.

  • Python requests library: Install it with pip install requests.

  • API token: A token with permission to read audit logs and users. For more information, see Manage API keys.

  • Audit log permission: Permission to view audit logs in your Harness account. For more information, see Permissions reference.

  • Harness account ID: Available in any Harness URL, for example https://app.harness.io/ng/account/<ACCOUNT_ID>/....


How the script works

Understand the output categories before you act on the results, because an empty login record does not always mean the account is safe to delete. The script queries the Harness Audit API for LOGIN events within a date range, compares that data against all users in your account, and writes three files.

  • active_users.ndjson: Users who logged in during the specified time period.

  • inactive_users.ndjson: Users who exist in the account but did not log in during the specified time period.

  • deleted_users.ndjson: Users who logged in during the specified time period but no longer exist in the account.

The output files use NDJSON format (newline-delimited JSON), where each line is a valid JSON object representing one user record.


Run the script

Save the complete script as get_inactive_users.py, then run it from the command line with your environment URL and credentials. By default, the script analyzes the last 30 days of login activity.

Specify a custom date range

Set an explicit window when you report on a fixed audit period, such as a quarter, rather than the trailing 30 days. Pass the --start and --end parameters:

Use environment variables

Set credentials as environment variables to keep tokens out of your shell history and process list. The script reads HARNESS_API_KEY for an API key and HARNESS_BEARER for a Bearer token.


Script parameters

Use these parameters to control the target account, the authentication method, and the reporting window.

Parameter
Required
Description
Default
Example

--env

Yes

Harness environment URL in the format <domain>.harness.io/ng/account/<account_id>/

None

app.harness.io/ng/account/abc123/

--apikey

No*

Harness API key for authentication

HARNESS_API_KEY env var

pat.abc123.xyz...

--bearer

No*

Bearer token for authentication

HARNESS_BEARER env var

eyJhbGc...

--start

No

Start date and time in YYYY-MM-DD HH:MM format

30 days ago

2025-01-01 00:00

--end

No

End date and time in YYYY-MM-DD HH:MM format

Current time

2025-01-31 23:59

* One of --apikey or --bearer is required, or the corresponding environment variable.


Interpret the output

Read the output files to decide which accounts to offboard and which to retain. The script writes all three NDJSON files to the current directory.

active_users.ndjson

Contains audit log entries for users who logged in during the specified time period. Each line includes:

inactive_users.ndjson

Contains user records for users who exist in the account but did not log in during the specified time period. Each line includes:

deleted_users.ndjson

Contains audit log entries for users who logged in during the specified time period but no longer exist in the account.

Analyze the output

Process the NDJSON files with command-line tools when you need a quick count, or with Python when you need to feed the results into another system.

To count the records in each category, use wc:

To extract email addresses, use jq:

To process the records programmatically, read them in Python:

For accounts with many users or extensive audit history, the script can take several minutes to complete. It paginates through the data, fetching up to 1000 audit log entries or 100 users per page, and prints progress as it runs.


Complete script

Save the following as get_inactive_users.py.

get_inactive_users.py

Troubleshooting

Match the error the script prints to the corresponding fix.

401 Unauthorized

Solution: Verify that your API key or Bearer token is valid and has the necessary permissions to access audit logs. For more information, see Manage API keys.

403 Forbidden

Solution: Your API key or Bearer token does not have permission to view audit logs or user information. Confirm you have the necessary permissions to access these resources.

Invalid date format

Solution: Ensure dates use the format YYYY-MM-DD HH:MM, for example 2025-01-01 00:00.

429 Too Many Requests

Solution: The script exceeded the Harness API rate limits. Wait a few minutes and run it again. For more information, see Rate limits.


  • Manage users: Add, edit, and delete users, and act on the inactive accounts this script identifies.

  • Audit trail: Review the audit events that this script queries.

  • Manage API keys: Create the token the script uses to authenticate.

  • Harness API quickstart: Understand how to authenticate and call Harness APIs.

Last updated

Was this helpful?