A Python-based application to backup Grafana settings using the Grafana API
—
Comprehensive API validation and feature detection system that ensures compatibility with different Grafana versions and configurations. The health checking system validates connectivity, authentication, and feature availability before performing operations.
The primary API validation function that orchestrates comprehensive health checking and feature detection.
def main(settings):
"""
Perform comprehensive API health checks and feature detection
Args:
settings (dict): Configuration settings including API endpoints and credentials
Returns:
tuple: (status_code, response_data, dashboard_uid_support,
datasource_uid_support, paging_support, contact_point_support)
"""Located in the grafana_backup.dashboardApi module, these functions provide low-level API validation.
def health_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
"""
Check Grafana server health and availability
Args:
grafana_url (str): Base URL of Grafana instance
http_get_headers (dict): HTTP headers for authentication
verify_ssl (bool): Whether to verify SSL certificates
client_cert (str): Path to client certificate (optional)
debug (bool): Enable debug logging
Returns:
tuple: (status_code, json_response)
"""def auth_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
"""
Verify API token authentication and permissions
Args:
grafana_url (str): Base URL of Grafana instance
http_get_headers (dict): HTTP headers with API token
verify_ssl (bool): Whether to verify SSL certificates
client_cert (str): Path to client certificate (optional)
debug (bool): Enable debug logging
Returns:
tuple: (status_code, json_response)
"""def uid_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
"""
Detect if Grafana instance supports UID-based operations
Args:
grafana_url (str): Base URL of Grafana instance
http_get_headers (dict): HTTP headers for authentication
verify_ssl (bool): Whether to verify SSL certificates
client_cert (str): Path to client certificate (optional)
debug (bool): Enable debug logging
Returns:
tuple: (dashboard_uid_support, datasource_uid_support)
"""def paging_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
"""
Detect if Grafana instance supports API paging (v6.2+)
Args:
grafana_url (str): Base URL of Grafana instance
http_get_headers (dict): HTTP headers for authentication
verify_ssl (bool): Whether to verify SSL certificates
client_cert (str): Path to client certificate (optional)
debug (bool): Enable debug logging
Returns:
bool: True if paging is supported, False otherwise
"""def contact_point_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
"""
Detect if Grafana instance supports unified alerting contact points
Args:
grafana_url (str): Base URL of Grafana instance
http_get_headers (dict): HTTP headers for authentication
verify_ssl (bool): Whether to verify SSL certificates
client_cert (str): Path to client certificate (optional)
debug (bool): Enable debug logging
Returns:
bool: True if contact points are supported, False otherwise
"""The system detects whether the Grafana instance supports UID-based operations:
Detects support for API result paging introduced in Grafana 6.2:
Detects unified alerting contact point support:
Health checking behavior can be controlled through configuration:
# Configuration settings that control health checking
API_HEALTH_CHECK: bool # Enable/disable basic health checks
API_AUTH_CHECK: bool # Enable/disable authentication verification
DEBUG: bool # Enable debug logging for health checks# SSL and certificate configuration
VERIFY_SSL: bool # Verify SSL certificates
CLIENT_CERT: str # Path to client certificate file for mutual TLSfrom grafana_backup.api_checks import main as check_api
from grafana_backup.grafanaSettings import main as load_config
# Load configuration
settings = load_config('/path/to/grafanaSettings.json')
# Perform comprehensive health check
(status, response, dashboard_uid, datasource_uid,
paging, contact_points) = check_api(settings)
if status == 200:
print("API is healthy and authenticated")
print(f"Dashboard UID support: {dashboard_uid}")
print(f"Datasource UID support: {datasource_uid}")
print(f"Paging support: {paging}")
print(f"Contact point support: {contact_points}")
else:
print(f"API check failed: {response}")from grafana_backup.dashboardApi import (
health_check, auth_check, uid_feature_check,
paging_feature_check, contact_point_check
)
# Extract settings
grafana_url = settings['GRAFANA_URL']
headers = settings['HTTP_GET_HEADERS']
verify_ssl = settings['VERIFY_SSL']
client_cert = settings['CLIENT_CERT']
debug = settings['DEBUG']
# Individual health checks
health_status, health_response = health_check(
grafana_url, headers, verify_ssl, client_cert, debug
)
auth_status, auth_response = auth_check(
grafana_url, headers, verify_ssl, client_cert, debug
)
dashboard_uid, datasource_uid = uid_feature_check(
grafana_url, headers, verify_ssl, client_cert, debug
)
paging_supported = paging_feature_check(
grafana_url, headers, verify_ssl, client_cert, debug
)
contact_points_supported = contact_point_check(
grafana_url, headers, verify_ssl, client_cert, debug
)Health checking is automatically integrated into all major operations:
# Example from backup operation
from grafana_backup.save import main as save_backup
# Health checks are performed automatically before backup
save_args = {
'save': True,
'--components': None,
'--no-archive': False,
'--config': None
}
# This call will automatically:
# 1. Perform health checks
# 2. Detect feature support
# 3. Adapt backup behavior based on detected features
# 4. Exit gracefully if health checks fail
save_backup(save_args, settings)When all health checks pass, the system configures feature flags:
# Features are added to settings for use by other modules
settings.update({
'DASHBOARD_UID_SUPPORT': True,
'DATASOURCE_UID_SUPPORT': True,
'PAGING_SUPPORT': True,
'CONTACT_POINT_SUPPORT': False
})When health checks fail, operations are prevented:
When debug mode is enabled, health checks provide detailed diagnostic information:
# Debug output includes:
# - HTTP request/response details
# - Authentication header validation
# - SSL certificate information
# - API endpoint availability
# - Feature detection results{
"general": {
"api_health_check": true,
"api_auth_check": true,
"debug": false,
"verify_ssl": true
}
}DEBUG=true for detailed diagnostic informationThe API health checking system provides robust validation and feature detection to ensure reliable operation across different Grafana versions and configurations.
Install with Tessl CLI
npx tessl i tessl/pypi-grafana-backup