Python client library for MongoDB Ops Manager API
Project description
python-mongodb-ops-manager
Python client library for MongoDB Ops Manager API
Status
Beta - Core functionality implemented and tested against live Ops Manager
Overview
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.
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:
- Service-oriented architecture - Logical grouping of API endpoints
- Explicit over implicit - Project ID passed per-call, not stored globally
- Safe defaults - Rate limiting and SSL verification enabled by default
- Flexible output - Choose between typed objects or raw dictionaries
Use Cases
- Automated health check reporting for large MongoDB fleets
- Metrics collection and statistical analysis
- Performance advisor recommendations aggregation
- Cluster topology documentation
- Operational automation and scripting
Inspiration
- Go SDK: go-client-mongodb-ops-manager - Official MongoDB Go client
- Python: atlasapi - Community Atlas API library
Requirements
- Python 3.9+
requestslibrary
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opsmanager-0.1.0.tar.gz.
File metadata
- Download URL: opsmanager-0.1.0.tar.gz
- Upload date:
- Size: 32.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00747014540775fc1805e3de4b9c361bb7ea3774870f14320097be6f30a22542
|
|
| MD5 |
5e277bf02d0e060feab422a96a10427c
|
|
| BLAKE2b-256 |
6eb55b392218194da1bb2354e9913ce7288cbb9111b556af5ade4c0caa83ad0b
|
File details
Details for the file opsmanager-0.1.0-py3-none-any.whl.
File metadata
- Download URL: opsmanager-0.1.0-py3-none-any.whl
- Upload date:
- Size: 39.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ee9e6f9119274781b0fcb7a7781964cf92ff7ec6fd339248dd7498ce6f2b8c1
|
|
| MD5 |
63f1f2ad83983bfcc753ab7f104bfc18
|
|
| BLAKE2b-256 |
818b908af76fe95ca0e6eb11a862e215131f391c917a27dc5c74b5347ce5fc54
|