Command-line interface for querying and managing Elasticsearch data. Designed for operators and developers who need fast access to logs, metrics, and cluster information from the terminal.
- Log querying with smart filtering (host, service, severity, time range, text search)
- Document operations (lift, delete, list, cleanup test data)
- Cluster management (health checks, shard status, index listing)
- Multiple output formats (table, JSON, raw)
- Pipeline-friendly (clean stdout/stderr separation)
- Type-safe Python implementation with comprehensive error handling
Requirements: Python 3.11+, uv
# Clone repository or navigate to elk-tool directory
cd elk-tool
# Install dependencies
uv sync
# Verify installation
uv run elk-tool --helpelk-tool uses layered configuration with the following precedence (highest to lowest):
- CLI flags (
--profile) — override everything - Environment variables (
ELK_PROFILE,ELK_URL, etc.) — per-session overrides - Config file (
~/.config/elk-tool/config.toml) — persistent settings - Built-in defaults — fallback values
Create ~/.config/elk-tool/config.toml from the included example:
mkdir -p ~/.config/elk-tool
cp config.example.toml ~/.config/elk-tool/config.tomlThe config file supports multiple named profiles:
default_profile = "default"
[profiles.default]
url = "https://elasticsearch.example.com:9200"
api_key = "your-api-key-here" # pragma: allowlist secret
timeout = 30
verify_tls = true
[profiles.production]
url = "https://elk-prod.example.com:9200"
api_key = "prod-api-key" # pragma: allowlist secret
timeout = 60
[profiles.local]
url = "http://localhost:9200"
verify_tls = falseSee config.example.toml for all available settings.
Switch between profiles using --profile or the ELK_PROFILE environment variable:
# Use a named profile
uv run elk-tool --profile production logs -t 1h
# Set default profile via environment variable
export ELK_PROFILE="production"
uv run elk-tool logs -t 1h
# CLI flag takes precedence over environment variable
ELK_PROFILE=production uv run elk-tool --profile local logsProfile resolution order: --profile flag > ELK_PROFILE env var > default_profile from config > built-in default.
Environment variables override config file and profile values:
# Elasticsearch endpoint (overrides profile URL)
export ELK_URL="https://elasticsearch.example.com:9200"
# API Key authentication (preferred, overrides profile)
export ELASTIC_API_KEY="your-api-key-here" # pragma: allowlist secret
# Basic auth (alternative to API key)
export ELK_USERNAME="elastic"
export ELK_PASSWORD="changeme" # pragma: allowlist secret
# Profile selection
export ELK_PROFILE="production"
# Optional settings
export ENVIRONMENT="production" # Environment tag for Sentry (default: local)export ELK_URL="https://elasticsearch.example.com:9200"
export ELASTIC_API_KEY="your-api-key" # pragma: allowlist secret
export ELK_PROFILE="default"
export ENVIRONMENT="local"# Recent logs (last 20)
uv run elk-tool logs
# Logs from specific time range
uv run elk-tool logs -t 15m # Last 15 minutes
uv run elk-tool logs -t 1h # Last hour
uv run elk-tool logs -t 24h # Last 24 hours
uv run elk-tool logs -t 7d # Last week
# Filter by host, service, or container
uv run elk-tool logs -h firewall7
uv run elk-tool logs -s litellm
uv run elk-tool logs -C sentry-nginx-1
# Combine filters
uv run elk-tool logs -h sentry -C pgbouncer -t 1h
# Filter by severity level
uv run elk-tool logs -l error # ERROR and FATAL only
uv run elk-tool logs -l warn # WARN, ERROR, FATAL
# Search text in log messages
uv run elk-tool logs -S "connection refused" -t 1h
# Custom columns
uv run elk-tool logs -c ts,level,id,msg
# Raw JSON output (for piping)
uv run elk-tool logs --raw | jq '.hits.hits[]._source'# Lift (retrieve) a document
uv run elk-tool lift logs-generic.otel-default DOC_ID
# Lift and save to file
uv run elk-tool lift INDEX DOC_ID -o testdata/
# Lift just the message body
uv run elk-tool lift INDEX DOC_ID -r
# Delete a document
uv run elk-tool delete INDEX DOC_ID
# Delete without confirmation
uv run elk-tool delete INDEX DOC_ID --force
# List recent documents
uv run elk-tool list INDEX
uv run elk-tool list INDEX --size 50
uv run elk-tool list INDEX --full # Show full document source
# Clean up test documents (marked with int-test: true)
uv run elk-tool cleanup INDEX --dry-run
uv run elk-tool cleanup INDEX# Show cluster health
uv run elk-tool cluster-health
# Show shard status and diagnose allocation issues
uv run elk-tool shard-status
uv run elk-tool shard-status --explain
# List indices
uv run elk-tool indices
uv run elk-tool indices 'logs-*'
# List data streams
uv run elk-tool indices --data-streams
# Show field mappings
uv run elk-tool mapping
uv run elk-tool mapping -f attributes.net
uv run elk-tool mapping --raw# List all hosts sending data
uv run elk-tool hosts
uv run elk-tool hosts -t 1h # Last hour
uv run elk-tool hosts -s myservice # Filter by service
# List all services
uv run elk-tool services
uv run elk-tool services -t 24h # Last 24 hours
uv run elk-tool services -h myhost # Filter by host# Execute raw Elasticsearch query
uv run elk-tool query '{"query": {"match_all": {}}}' -n 10
# Aggregation query
uv run elk-tool query '{"size": 0, "aggs": {"hosts": {"terms": {"field": "host.name"}}}}' -i logs-*
# Raw JSON output
uv run elk-tool query '{"query": {"match_all": {}}}' --rawlogs- View logs with smart filtering (host, service, container, severity, time, search)hosts- List unique hostnames in logs or metrics streamservices- List unique services in logs or metrics stream
lift- Retrieve a document by ID (with optional save-to-file and delete-after)delete- Delete a document by IDlist- List recent documents in an indexcleanup- Delete test documents marked withint-test: true
cluster-health- Show cluster health and node statusshard-status- Show unassigned shards and allocation issuesindices- List indices and data streamsmapping- Show field mappings for an index
query- Execute raw Elasticsearch query JSON
test sentry- Send test error and transaction to Sentry
Most commands support multiple output formats:
- Table mode (default): Human-readable tables with Rich formatting
- Full mode (
--full): Expanded view with complete document details - Raw mode (
--raw): JSON output for piping tojqor other tools
elk-tool uses standard exit codes for scripting:
0- Success1- General error2- Usage error (invalid arguments)3- Input error (file not found, bad data)4- Output error (cannot write file)5- Network error (connection failed)6- Timeout
See CONTRIBUTING.md for developer setup, testing, and contribution guidelines.
MIT