@gilangjavier/grafctl
v0.1.0
Published
CLI tool for Grafana operations
Downloads
101
Maintainers
Readme
@gilangjavier/grafctl
CLI tool for Grafana with token optimization for AI agents.
Token Savings
| Operation | Standard API | grafctl | Savings | |-----------|--------------|---------|---------| | Dashboards list | ~4,500 tokens | ~800 tokens | 82% | | Dashboard export | ~8,000 tokens | ~1,200 tokens | 85% | | Datasources list | ~2,800 tokens | ~500 tokens | 82% | | Datasource health | ~1,500 tokens | ~300 tokens | 80% | | Alerts list | ~3,500 tokens | ~650 tokens | 81% | | Alerts pause/unpause | ~1,200 tokens | ~250 tokens | 79% | | Folders list | ~2,200 tokens | ~400 tokens | 82% | | Average | | | 82% |
For AI Agents
Installation (Ephemeral)
npx @gilangjavier/grafctl <command>No installation required. Runs directly via npx.
Quick Commands
# List all dashboards
npx @gilangjavier/grafctl dashboards list
# List dashboards in a specific folder
npx @gilangjavier/grafctl dashboards list --folder <folder-id>
# Filter dashboards by tags
npx @gilangjavier/grafctl dashboards list --tags "prod,monitoring"
# Export a dashboard by UID
npx @gilangjavier/grafctl dashboards export <uid>
# Export dashboard to file
npx @gilangjavier/grafctl dashboards export <uid> --file dashboard.json
# Import a dashboard from file
npx @gilangjavier/grafctl dashboards import dashboard.json
# List all datasources
npx @gilangjavier/grafctl datasources list
# Check datasource health
npx @gilangjavier/grafctl datasources health <datasource-id>
# List all alert rules
npx @gilangjavier/grafctl alerts list
# Filter alerts by state
npx @gilangjavier/grafctl alerts list --state alerting
# Pause an alert rule
npx @gilangjavier/grafctl alerts pause <alert-uid>
# Unpause an alert rule
npx @gilangjavier/grafctl alerts unpause <alert-uid>
# List all folders
npx @gilangjavier/grafctl folders list
# Create a new folder
npx @gilangjavier/grafctl folders create "My Folder"
# Check Grafana health
npx @gilangjavier/grafctl health
# Show current config
npx @gilangjavier/grafctl configExpected Behavior
Exit Codes:
0- Success1- Error (connection failed, invalid input, config missing)
Output Format:
- Default: Human-readable tables
--format jsonor-o json: Structured JSON for programmatic use
Profile Selection:
-p, --profile <name>- Use specific profile from config
Cache Control:
--no-cache- Disable ETag caching for fresh data
Before/After Token Comparison
Before (direct Grafana API calls):
Raw JSON response with metadata, nested objects, extensive dashboard JSON
~4,500 tokens for a typical dashboards list response
~8,000+ tokens for dashboard export with full panel definitionsAfter (grafctl):
Clean table output with essential data only (uid, title, folder, url)
~800 tokens for the same dashboards list
~1,200 tokens for dashboard export with curated outputFor Humans
Installation
npm install -g @gilangjavier/grafctlSetup Profile
Initialize configuration:
grafctl initThis creates ~/.config/grafctl/profiles.yaml with a sample profile. Edit it:
profiles:
default:
url: http://localhost:3000
token_env: GRAFANA_TOKEN # Use env var
# OR
token_file: /path/to/token # Read from file
# OR
token: "api-key-here" # Inline (not recommended)Token priority (highest first):
token_env- Environment variable nametoken_file- Path to file containing tokentoken- Inline token string
All Available Commands
| Command | Description | Options |
|---------|-------------|---------|
| grafctl init | Create sample config | --force to overwrite |
| grafctl config | Show current config | -p, --profile |
| grafctl dashboards list | List dashboards | --folder <id>, --tags <tags> |
| grafctl dashboards export <uid> | Export dashboard JSON | --file <path> to save |
| grafctl dashboards import <file> | Import dashboard from file | |
| grafctl datasources list | List datasources | -p, --profile |
| grafctl datasources health <id> | Check datasource health | -p, --profile |
| grafctl alerts list | List alert rules | --state <state> |
| grafctl alerts pause <uid> | Pause an alert rule | -p, --profile |
| grafctl alerts unpause <uid> | Unpause an alert rule | -p, --profile |
| grafctl folders list | List folders | -p, --profile |
| grafctl folders create <title> | Create a folder | -p, --profile |
| grafctl health | Check Grafana health | -p, --profile |
Global flags available for all commands:
-p, --profile <name>- Use specific profile-o, --format <format>- Output format:tableorjson--no-cache- Disable ETag caching
Common Workflows
List and filter dashboards:
# All dashboards
grafctl dashboards list
# Dashboards in a specific folder
grafctl dashboards list --folder 123
# Dashboards with specific tags
grafctl dashboards list --tags "production,infra"Export and backup dashboards:
# Export single dashboard
grafctl dashboards export abc123xyz --file my-dashboard.json
# Export all dashboards (script)
for uid in $(grafctl dashboards list --format json | jq -r '.[].uid'); do
grafctl dashboards export "$uid" --file "backup/${uid}.json"
doneMigrate dashboards between instances:
# Export from source
grafctl -p source dashboards export <uid> --file dash.json
# Import to target
grafctl -p target dashboards import dash.jsonCheck datasource health:
# List datasources to get IDs
grafctl datasources list
# Check specific datasource health
grafctl datasources health 5Manage alert rules during maintenance:
# List active alerts
grafctl alerts list --state alerting
# Pause an alert before maintenance
grafctl alerts pause <alert-uid>
# Perform maintenance...
# Unpause after maintenance
grafctl alerts unpause <alert-uid>Organize with folders:
# List existing folders
grafctl folders list
# Create new folder
grafctl folders create "Team Metrics"Token Optimization Details
Filtering
grafctl implements intelligent filtering to reduce data volume:
| Filter | Description |
|--------|-------------|
| --folder | Filter dashboards by folder ID |
| --tags | Filter dashboards by comma-separated tags |
| --state | Filter alerts by state (normal, alerting, pending, no_data) |
| Column selection | Only essential columns displayed in table mode |
Caching (ETag)
grafctl implements HTTP ETag caching to reduce redundant API calls:
- Conditional requests - Uses
If-None-Matchheader with stored ETags - 304 responses - Returns cached data when content has not changed
- Per-profile cache - Cache keys include profile name for isolation
Benefits:
- Reduced network latency on repeated calls
- Lower token consumption for unchanged data
- Automatic cache invalidation when Grafana state changes
To bypass cache for fresh data:
grafctl dashboards list --no-cacheDeterministic Output
grafctl produces consistent, predictable output:
| Component | Standard Response | grafctl Output |
|-----------|-------------------|----------------|
| HTTP headers | Full headers | Stripped |
| JSON metadata | status, data, nested objects | Direct values |
| Dashboard JSON | Full panel definitions, templating | Essential fields only |
| Timestamps | Unix milliseconds, mixed formats | Consistent ISO8601 |
| Alert rules | Full query definitions | Status, summary, labels |
| Datasource config | Credentials, secure JSON | Basic info only |
| Empty arrays | Included | Omitted from tables |
| Error messages | Verbose stack traces | Clean error messages |
Output is sorted and formatted consistently across runs for reliable parsing.
Contributing
For AI Agents
To suggest improvements or test changes:
- Review the codebase structure in
src/ - Identify optimization opportunities
- Document findings in GitHub issues:
https://github.com/gilangjavier/grafctl/issues
For Humans
Development Setup:
# Clone the repository
git clone https://github.com/gilangjavier/grafctl.git
cd grafctl
# Install dependencies
npm install
# Build TypeScript
npm run build
# Run tests
npm test
# Run in development mode
npm run dev -- dashboards listSubmitting Changes:
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-change - Make changes and add tests
- Run the test suite:
npm test - Commit with clear messages
- Push and open a Pull Request
Reporting Issues:
Include:
- grafctl version (
grafctl --version) - Node.js version (
node --version) - Grafana version
- Steps to reproduce
- Expected vs actual output
License
MIT - See LICENSE file for details.
