oglog/script/import_grafana.sh

54 lines
1.6 KiB
Bash

#!/bin/bash
# Variables
GRAFANA_URL="https://oglog-graf.mytld:3000"
RESOURCE_DIR="../etc/grafana/resources"
TOKEN_FILE="./grafana_token.txt"
# Comprobar que el token existe
if [ ! -f "$TOKEN_FILE" ]; then
echo "Error: No se encontró el archivo $TOKEN_FILE con el token de Grafana."
echo "Ejecuta primero setup_grafana_token.sh"
exit 1
fi
API_TOKEN=$(cat "$TOKEN_FILE")
# Importar datasources
if [ -f "$RESOURCE_DIR/datasources/datasources.json" ]; then
echo "Importando datasources..."
jq -c '.[]' "$RESOURCE_DIR/datasources/datasources.json" | while read -r datasource; do
curl -s -X POST "$GRAFANA_URL/api/datasources" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d "$datasource" > /dev/null
done
echo "Datasources importados."
fi
# Importar dashboards
echo "Importando dashboards..."
for f in "$RESOURCE_DIR/dashboards"/*.json; do
echo "Importando $(basename "$f")"
jq 'del(.dashboard.id) | {dashboard: .dashboard, overwrite: true}' "$f" | \
curl -s -X POST "$GRAFANA_URL/api/dashboards/db" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
--data-binary @-
done
echo "Dashboards importados."
# Importar alertas
if [ -f "$RESOURCE_DIR/alerts/alert-rules.json" ]; then
echo "Importando alert rules..."
jq -c '.[]' "$RESOURCE_DIR/alerts/alert-rules.json" | while read -r alert; do
curl -s -X POST "$GRAFANA_URL/api/v1/provisioning/alert-rules" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
-d "$alert"
done
echo "Alertas importadas."
fi
echo "Importación completa."