Skip to main content

Python client library for MongoDB Ops Manager API

Project description

A production-quality Python client library for the MongoDB Ops Manager API, designed to enable automated health checks, metrics collection, and fleet management for MongoDB deployments.

Status: Beta - Core functionality implemented and tested against live Ops Manager

Features

  • Production-ready - Built-in rate limiting, error handling, retry logic with exponential backoff
  • Type-safe - Full type hints throughout with dataclass models
  • Pythonic - Clean, idiomatic Python API design
  • Safe by default - Conservative rate limiting to protect production Ops Manager instances
  • Tested - Validated against live Ops Manager and cross-checked with mongocli (Go SDK)

Installation

pip install opsmanager

Or install from source:

git clone https://github.com/fsnow/python-mongodb-ops-manager.git
cd python-mongodb-ops-manager
pip install -e .

Quick Start

from opsmanager import OpsManagerClient

# Create client
client = OpsManagerClient(
    base_url="https://ops-manager.example.com",
    public_key="your-public-key",
    private_key="your-private-key",
)

# List all projects
projects = client.projects.list()
for project in projects:
    print(f"Project: {project.name} ({project.id})")

# Get hosts in a project
hosts = client.deployments.list_hosts(project_id="your-project-id")
for host in hosts:
    print(f"Host: {host.hostname}:{host.port} - {host.replica_state_name}")

# Get metrics for a host (last 24 hours, 1-minute granularity)
metrics = client.measurements.host(
    project_id="your-project-id",
    host_id="host-id",
    granularity="PT1M",
    period="P1D",
    metrics=["OPCOUNTER_QUERY", "OPCOUNTER_INSERT", "CONNECTIONS"],
)

# Get Performance Advisor suggestions
suggestions = client.performance_advisor.get_suggested_indexes(
    project_id="your-project-id",
    host_id="hostname:27017",
    duration=86400000,  # 24 hours in milliseconds
)

# Clean up
client.close()

Using as Context Manager

with OpsManagerClient(
    base_url="https://ops-manager.example.com",
    public_key="your-public-key",
    private_key="your-private-key",
) as client:
    projects = client.projects.list()

API Coverage

Currently Implemented

Service Description
organizations List organizations, get org details, list projects in org
projects List projects, get project by ID or name
clusters List clusters, get cluster details
deployments List hosts, databases, disks; get by ID or name
measurements Host, database, and disk metrics with time-series data
performance_advisor Namespaces, slow queries, suggested indexes
alerts List alerts, acknowledge alerts

Configuration Options

client = OpsManagerClient(
    base_url="https://ops-manager.example.com",
    public_key="your-public-key",
    private_key="your-private-key",
    timeout=30.0,           # Request timeout in seconds
    rate_limit=2.0,         # Max requests per second (conservative default)
    retry_count=3,          # Number of retries for failed requests
    retry_backoff=1.0,      # Base backoff time between retries
    verify_ssl=True,        # Verify SSL certificates
)

Rate Limiting

Rate limiting is built-in and enabled by default to protect production Ops Manager instances. The default is 2 requests per second, which is conservative but safe.

# Adjust rate limit if needed (be careful with production systems!)
client.set_rate_limit(5.0)  # 5 requests per second

Pagination

All list methods support pagination automatically:

# Fetch all results (handles pagination internally)
all_hosts = client.deployments.list_hosts(project_id="abc123")

# Or iterate with automatic pagination
for host in client.deployments.list_hosts_iter(project_id="abc123"):
    print(host.hostname)

Return Types

All methods support returning either typed objects or raw dictionaries:

# Return typed objects (default)
hosts = client.deployments.list_hosts(project_id="abc123", as_obj=True)
print(hosts[0].hostname)  # IDE autocomplete works

# Return raw dictionaries
hosts = client.deployments.list_hosts(project_id="abc123", as_obj=False)
print(hosts[0]["hostname"])

Testing

Live Integration Tests

Run the integration test suite against a live Ops Manager instance:

export OM_BASE_URL="http://ops-manager.example.com:8081"
export OM_PUBLIC_KEY="your-public-key"
export OM_PRIVATE_KEY="your-private-key"

python tests/test_live.py --verbose

Validation Against mongocli

Compare output against the official MongoDB CLI (uses the Go SDK):

export OM_ORG_ID="your-org-id"
export OM_PROJECT_ID="your-project-id"

python tests/validate_against_mongocli.py

Design Principles

This library is modeled after the official MongoDB Go SDK with Pythonic adaptations:

  1. Service-oriented architecture - Logical grouping of API endpoints
  2. Explicit over implicit - Project ID passed per-call, not stored globally
  3. Safe defaults - Rate limiting and SSL verification enabled by default
  4. Flexible output - Choose between typed objects or raw dictionaries

Use Cases

This library has been tested primarily with read-only API keys for monitoring and reporting use cases:

  • Automated health check reporting for large MongoDB fleets
  • Metrics collection and statistical analysis
  • Performance advisor recommendations aggregation
  • Cluster topology documentation

Write operations (automation config, backup management, etc.) are not yet fully implemented or tested.

Inspiration

Requirements

  • Python 3.9+
  • requests library

License

Apache License 2.0 - See LICENSE for details.

Contributing

Contributions welcome! Please open an issue or pull request.

Disclaimer

This is an independent project and is not officially affiliated with or endorsed by MongoDB, Inc.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opsmanager-0.2.1.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opsmanager-0.2.1-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

Details for the file opsmanager-0.2.1.tar.gz.

File metadata

  • Download URL: opsmanager-0.2.1.tar.gz
  • Upload date:
  • Size: 33.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for opsmanager-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3ec99791987ac5e17bcf91186474650163eddc1d4dd17a282594f3ff7fd280d9
MD5 90bba4ab13c62a738edbcd1da7d4e1d9
BLAKE2b-256 1423eae7d701bf4b9b3c12da073e77884ed1b93437e7d4f289c68da68c4963bf

See more details on using hashes here.

File details

Details for the file opsmanager-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: opsmanager-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 39.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for opsmanager-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 61bb3ef03735dd9427de19032b8d932fe07c54b65fdd6439c94da94b0f95b701
MD5 9003de3ed8cd90423ac929a68f698c8f
BLAKE2b-256 85ca162f0d91365dc3258cf68bffb1fdab7c07d63b848481627cea28fb917a66

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page