|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +DATASOURCES=$(curl -s http://localhost:3000/api/datasources) |
| 4 | + |
| 5 | +if [ -z "$DATASOURCES" ] || [[ "$DATASOURCES" == *"message"*"Unauthorized"* ]]; then |
| 6 | + echo '[{"error": "Could not reach Grafana or authentication is required."}]' |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | + |
| 10 | +# ========================================== |
| 11 | +# 1. DISCOVER ALL SERVICES |
| 12 | +# ========================================== |
| 13 | +ALL_SERVICES="" |
| 14 | + |
| 15 | +while read -r ds; do |
| 16 | + TYPE=$(echo "$ds" | jq -r '.type') |
| 17 | + DS_UID=$(echo "$ds" | jq -r '.uid') |
| 18 | + |
| 19 | + if [ "$TYPE" == "loki" ]; then |
| 20 | + RES=$(curl -s "http://localhost:3000/api/datasources/proxy/uid/$DS_UID/loki/api/v1/label/service_name/values") |
| 21 | + FOUND=$(echo "$RES" | jq -r '.data[]?' 2>/dev/null) |
| 22 | + ALL_SERVICES="$ALL_SERVICES\n$FOUND" |
| 23 | + |
| 24 | + elif [ "$TYPE" == "prometheus" ]; then |
| 25 | + RES=$(curl -s "http://localhost:3000/api/datasources/proxy/uid/$DS_UID/api/v1/label/service_name/values") |
| 26 | + FOUND=$(echo "$RES" | jq -r '.data[]?' 2>/dev/null) |
| 27 | + ALL_SERVICES="$ALL_SERVICES\n$FOUND" |
| 28 | + |
| 29 | + elif [ "$TYPE" == "tempo" ]; then |
| 30 | + RES=$(curl -s "http://localhost:3000/api/datasources/proxy/uid/$DS_UID/api/search/tag/service.name/values") |
| 31 | + FOUND=$(echo "$RES" | jq -r '.tagValues[]?' 2>/dev/null) |
| 32 | + ALL_SERVICES="$ALL_SERVICES\n$FOUND" |
| 33 | + fi |
| 34 | +done < <(echo "$DATASOURCES" | jq -c '.[]') |
| 35 | + |
| 36 | +# Clean up: remove empty lines, sort alphabetically, and remove duplicates |
| 37 | +UNIQUE_SERVICES=$(echo -e "$ALL_SERVICES" | sed '/^\s*$/d' | sort -u) |
| 38 | + |
| 39 | +# If no services were found, output empty array and exit gracefully |
| 40 | +if [ -z "$UNIQUE_SERVICES" ]; then |
| 41 | + echo '[]' |
| 42 | + exit 0 |
| 43 | +fi |
| 44 | + |
| 45 | +# ========================================== |
| 46 | +# 2. CHECK DATA SOURCES FOR EACH SERVICE |
| 47 | +# ========================================== |
| 48 | +FINAL_RESULTS=() |
| 49 | + |
| 50 | +# Loop through each discovered service dynamically |
| 51 | +while read -r SERVICE_NAME; do |
| 52 | + |
| 53 | + # Hold the datasource results for this specific service |
| 54 | + SERVICE_DS_RESULTS=() |
| 55 | + |
| 56 | + while read -r ds; do |
| 57 | + TYPE=$(echo "$ds" | jq -r '.type') |
| 58 | + DS_UID=$(echo "$ds" | jq -r '.uid') |
| 59 | + NAME=$(echo "$ds" | jq -r '.name') |
| 60 | + STATUS="" |
| 61 | + |
| 62 | + if [ "$TYPE" == "loki" ]; then |
| 63 | + QUERY="{service_name=\"$SERVICE_NAME\"}" |
| 64 | + ENCODED=$(jq -nr --arg q "$QUERY" '$q|@uri') |
| 65 | + RES=$(curl -s "http://localhost:3000/api/datasources/proxy/uid/$DS_UID/loki/api/v1/query_range?query=$ENCODED") |
| 66 | + |
| 67 | + if [[ "$RES" != *'"result":[]'* ]] && [[ -n "$RES" ]]; then |
| 68 | + STATUS="data_found" |
| 69 | + else |
| 70 | + STATUS="no_data_found" |
| 71 | + fi |
| 72 | + |
| 73 | + elif [ "$TYPE" == "prometheus" ]; then |
| 74 | + QUERY="{service_name=\"$SERVICE_NAME\"}" |
| 75 | + ENCODED=$(jq -nr --arg q "$QUERY" '$q|@uri') |
| 76 | + RES=$(curl -s "http://localhost:3000/api/datasources/proxy/uid/$DS_UID/api/v1/query?query=$ENCODED") |
| 77 | + |
| 78 | + if [[ "$RES" != *'"result":[]'* ]] && [[ -n "$RES" ]]; then |
| 79 | + STATUS="data_found" |
| 80 | + else |
| 81 | + STATUS="no_data_found" |
| 82 | + fi |
| 83 | + |
| 84 | + elif [ "$TYPE" == "tempo" ]; then |
| 85 | + RES=$(curl -s "http://localhost:3000/api/datasources/proxy/uid/$DS_UID/api/search?tags=service.name=$SERVICE_NAME") |
| 86 | + |
| 87 | + if [[ "$RES" != *'"traces":[]'* ]] && [[ -n "$RES" ]]; then |
| 88 | + STATUS="data_found" |
| 89 | + else |
| 90 | + STATUS="no_data_found" |
| 91 | + fi |
| 92 | + |
| 93 | + else |
| 94 | + STATUS="not_implemented" |
| 95 | + fi |
| 96 | + |
| 97 | + # Create a JSON object for this specific datasource status |
| 98 | + DS_OBJ=$(jq -n \ |
| 99 | + --arg name "$NAME" \ |
| 100 | + --arg status "$STATUS" \ |
| 101 | + '{name: $name, status: $status}') |
| 102 | + |
| 103 | + SERVICE_DS_RESULTS+=("$DS_OBJ") |
| 104 | + |
| 105 | + done < <(echo "$DATASOURCES" | jq -c '.[]') |
| 106 | + |
| 107 | + # Convert the array of datasource statuses into a JSON array |
| 108 | + DS_ARRAY=$(printf '%s\n' "${SERVICE_DS_RESULTS[@]}" | jq -s '.') |
| 109 | + |
| 110 | + # Create the final JSON object mapping the service name to its datasource statuses |
| 111 | + SVC_OBJ=$(jq -n \ |
| 112 | + --arg svc "$SERVICE_NAME" \ |
| 113 | + --argjson ds "$DS_ARRAY" \ |
| 114 | + '{service: $svc, datasources: $ds}') |
| 115 | + |
| 116 | + FINAL_RESULTS+=("$SVC_OBJ") |
| 117 | + |
| 118 | +done <<< "$UNIQUE_SERVICES" |
| 119 | + |
| 120 | +# Output the final nested JSON structure |
| 121 | +printf '%s\n' "${FINAL_RESULTS[@]}" | jq -s '.' |
0 commit comments