Skip to content

Instantly share code, notes, and snippets.

@justynroberts
Last active June 26, 2024 10:29
Show Gist options
  • Save justynroberts/a85b71456885a7120d39bb0a066e7cca to your computer and use it in GitHub Desktop.
Save justynroberts/a85b71456885a7120d39bb0a066e7cca to your computer and use it in GitHub Desktop.
Rundeck User Reporting

API Data Fetch and CSV Export Script

This script fetches user data from an API, processes it, writes it to a CSV file, and provides a summary of user activities. The API URL and headers have been sanitized for security purposes.

Features

  • Fetches data from an API.
  • Processes and fills missing data fields.
  • Writes data to a CSV file.
  • Parses datetime fields.
  • Counts users who executed jobs and logged in within the last 60 days.
  • Outputs summary statistics.

Installation

  1. Install Dependencies:

    Ensure you have Python installed. Then, install the required libraries using pip:

    pip install requests

Usage

  1. Configure the Script:

    • Update the url and your-auth-token variables with your API endpoint and authentication token.
  2. Run the Script:

    Execute the script using Python:

    python report.py
  3. Output:

    • The script will write the fetched and processed data to output.csv.
    • It will also print the summary statistics in the console.

License

MIT License
-----------

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
import requests
import csv
from datetime import datetime, timedelta, timezone
# URL and headers for API request (Sanitized)
url = "http://your-api-endpoint/api/27/user/list/"
headers = {
"X-Rundeck-Auth-Token": "your-auth-token",
"accept": "application/json"
}
# Fetch data from the API
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
# Determine all unique keys in the JSON data
all_keys = set()
for entry in data:
all_keys.update(entry.keys())
# Fill missing values with 'None' or 'Never'
for entry in data:
for key in all_keys:
if key not in entry:
entry[key] = 'Never' if key == 'lastJob' else None
# Write JSON data to CSV
csv_file = 'output.csv'
with open(csv_file, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=all_keys)
writer.writeheader()
writer.writerows(data)
# Get the current date
current_date = datetime.now(timezone.utc)
# Define the cutoff date for 60 days ago
cutoff_date = current_date - timedelta(days=60)
# Function to parse datetime with known formats
def parse_datetime(date_str):
for fmt in ('%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d'):
try:
return datetime.strptime(date_str, fmt).replace(tzinfo=timezone.utc)
except ValueError:
continue
return None
total_users = len(data)
executed_recently_count = 0
logged_in_recently_count = 0
for entry in data:
last_job = entry.get('lastJob')
updated = entry.get('updated')
if last_job != 'Never' and last_job is not None:
last_job_date = parse_datetime(last_job)
if last_job_date and last_job_date >= cutoff_date:
executed_recently_count += 1
if updated != 'Never' and updated is not None:
updated_date = parse_datetime(updated)
if updated_date and updated_date >= cutoff_date:
logged_in_recently_count += 1
# Print main facts
print("------------------------------")
print("📉 CSV written to output.csv")
print(f"👥 Total users: {total_users}")
print(f"🛠️ Total users who executed jobs in the last 60 days: {executed_recently_count}")
print(f"🔑 Total users who logged in to the console the last 60 days: {logged_in_recently_count}")
print("------------------------------")
else:
print(f"❌ Failed to retrieve data: {response.status_code} - {response.text}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment