112 lines
3.2 KiB
Bash
Executable File
112 lines
3.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Script to import Grafana dashboards
|
|
|
|
GRAFANA_URL="http://localhost:3000"
|
|
GRAFANA_USER="admin"
|
|
GRAFANA_PASS="admin123"
|
|
GRAFANA_API_KEY=""
|
|
NAMESPACE="monitoring"
|
|
|
|
echo "Starting Grafana dashboard import..."
|
|
|
|
# Function to get Grafana API key
|
|
get_api_key() {
|
|
echo "Getting Grafana API key..."
|
|
# Check if we can authenticate with default credentials
|
|
if curl -s -o /dev/null -w "%{http_code}" -u $GRAFANA_USER:$GRAFANA_PASS $GRAFANA_URL/api/auth/keys; then
|
|
# Create an API key
|
|
API_KEY_JSON=$(curl -s -X POST -H "Content-Type: application/json" \
|
|
-u $GRAFANA_USER:$GRAFANA_PASS \
|
|
$GRAFANA_URL/api/auth/keys \
|
|
-d '{"name":"dashboard-importer","role":"Admin"}')
|
|
|
|
GRAFANA_API_KEY=$(echo $API_KEY_JSON | grep -o '"key":"[^"]*"' | cut -d'"' -f4)
|
|
if [ ! -z "$GRAFANA_API_KEY" ]; then
|
|
echo "Created new API key"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Function to import dashboard
|
|
import_dashboard() {
|
|
local dashboard_file=$1
|
|
local dashboard_name=$2
|
|
|
|
echo "Importing dashboard: $dashboard_name from $dashboard_file"
|
|
|
|
if [ -f "$dashboard_file" ]; then
|
|
# Create payload for dashboard import
|
|
local payload=$(cat <<EOF
|
|
{
|
|
"dashboard": $(cat $dashboard_file | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g'),
|
|
"overwrite": true,
|
|
"inputs": [
|
|
{
|
|
"name": "DS_PROMETHEUS",
|
|
"type": "datasource",
|
|
"pluginId": "prometheus",
|
|
"value": "Prometheus"
|
|
}
|
|
]
|
|
}
|
|
EOF
|
|
)
|
|
# Import the dashboard
|
|
if [ ! -z "$GRAFANA_API_KEY" ]; then
|
|
curl -s -X POST \
|
|
-H "Content-Type: application/json" \
|
|
-H "Authorization: Bearer $GRAFANA_API_KEY" \
|
|
$GRAFANA_URL/api/dashboards/import \
|
|
-d "$payload"
|
|
else
|
|
curl -s -X POST \
|
|
-H "Content-Type: application/json" \
|
|
-u $GRAFANA_USER:$GRAFANA_PASS \
|
|
$GRAFANA_URL/api/dashboards/import \
|
|
-d "$payload"
|
|
fi
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo "✓ Successfully imported: $dashboard_name"
|
|
else
|
|
echo "✗ Failed to import: $dashboard_name"
|
|
fi
|
|
else
|
|
echo "✗ Dashboard file not found: $dashboard_file"
|
|
fi
|
|
}
|
|
|
|
# Get port-forward PID if exists
|
|
PF_PID=$(lsof -i :3000 -t 2>/dev/null)
|
|
|
|
if [ -z "$PF_PID" ]; then
|
|
echo "Setting up port-forward for Grafana..."
|
|
kubectl port-forward -n monitoring svc/grafana 3000:3000 &
|
|
PORT_FORWARD_PID=$!
|
|
echo $PORT_FORWARD_PID > /tmp/grafana-pf.pid
|
|
|
|
# Wait for port-forward to be ready
|
|
echo "Waiting for port-forward to be ready..."
|
|
sleep 10
|
|
else
|
|
echo "Port-forward for Grafana seems to be already active"
|
|
fi
|
|
|
|
# Wait a bit for Grafana to be ready
|
|
sleep 5
|
|
|
|
# Get API key
|
|
get_api_key
|
|
|
|
# Import dashboards
|
|
import_dashboard "cluster-monitoring-dashboard.json" "Cluster Monitoring Dashboard"
|
|
import_dashboard "kubernetes-overview-dashboard.json" "Kubernetes Cluster Overview"
|
|
|
|
# Clean up if we started the port-forward
|
|
if [ ! -z "$PORT_FORWARD_PID" ]; then
|
|
echo "Cleaning up port-forward..."
|
|
kill $PORT_FORWARD_PID 2>/dev/null
|
|
fi
|
|
|
|
echo "Dashboard import process completed!" |