#!/bin/bash # Script: get_grafana_logs.sh # Description: Finds the Grafana pod and displays its logs in real-time. # Usage: ./get_grafana_logs.sh [namespace] # --- Configuration (can be overridden by command-line argument) --- GRAFANA_NAMESPACE="${1:-monitoring}" # Default to 'monitoring' if no argument provided # --- Main Logic --- echo "Searching for Grafana pod in namespace '$GRAFANA_NAMESPACE'..." # Find the Grafana pod name. Use `head -n 1` in case multiple pods match, # we'll take the first one. `2>/dev/null` suppresses kubectl errors if no pod is found. GRAFANA_POD=$(kubectl get pods -n "$GRAFANA_NAMESPACE" -l app.kubernetes.io/name=grafana -o jsonpath='{.items[0].metadata.name}' 2>/dev/null) if [ -z "$GRAFANA_POD" ]; then echo "Error: Grafana pod not found in namespace '$GRAFANA_NAMESPACE'." echo "Please ensure Grafana is deployed and running, and the namespace is correct." exit 1 fi echo "Found Grafana pod: $GRAFANA_POD" echo "--- Displaying real-time logs (Ctrl+C to stop) ---" # Display logs. `--follow` streams real-time logs. kubectl logs "$GRAFANA_POD" -n "$GRAFANA_NAMESPACE" --follow if [ $? -ne 0 ]; then echo "Error: Could not retrieve logs for pod '$GRAFANA_POD'." echo "Check kubectl permissions or if the pod is actually running and healthy." exit 1 fi echo "Logs stream ended."