Created
July 16, 2021 07:12
-
-
Save justinTM/c8f39ce0708bf44b36d57b751c89ed3b to your computer and use it in GitHub Desktop.
given an environment variable for the grafana api token and any number of dashboard slug arguments, generate prometheus yaml rules config files from legacy grafana dashboard alerts. makes a lot of assumptions like there only being one condition in each alert (one query reference). requires hand-editing of results rule ".expr" attributes to pass …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if [ -z "${GRAFANA_API_TOKEN}" ]; then | |
echo Error: missing environment variable: GRAFANA_API_TOKEN | |
exit 1 | |
fi | |
DASHBOARD_SLUGS="${*}" | |
if [ -z "${DASHBOARD_SLUGS}" ]; then | |
echo Error: missing argument1...n: DASHBOARD_SLUGS | |
exit 1 | |
fi | |
alerts=$( curl -s X GET https://grafana.net/api/alerts/ \ | |
-H "Authorization: Bearer $GRAFANA_API_TOKEN" ) | |
# for each dashboard in list of dashboard slugs | |
dashboard_groups=$( jq -n '[]' ) | |
for slug in $DASHBOARD_SLUGS; do | |
echo "slug=${slug}" | |
namespace="${slug}-rules" | |
filename="${namespace}.yml" | |
this_uid=$( .gitlab/ci/get-dashboard-uid-from-alerts-by-slug.sh "$alerts" "$slug") | |
# echo "this_uid=${this_uid}" | |
this_alerts=$( .gitlab/ci/get-alerts-from-all-alerts-by-dashboard-slug.sh "$alerts" "$slug") | |
# echo "this_alerts=${this_alerts}" | |
this_dashboard=$( .gitlab/ci/get-dashboard-by-uid.sh "$this_uid" ) | |
this_dashboard_name="$( jq -r '.title' <<< $this_dashboard )" | |
# echo "this_dashboard=${this_dashboard}" | |
# for each panel from dashboard alerts | |
this_panel_ids=$( jq 'map(.panelId)' <<< $this_alerts ) | |
dashboard_alert_jsons=$( jq -n '[]' ) | |
# echo "this_panel_ids=${this_panel_ids}" | |
for p in $( .gitlab/ci/get-indexes-from-json.sh "$this_panel_ids" ); do | |
this_panel_id=$( .gitlab/ci/get-element-from-json-by-index.sh "$this_panel_ids" "$p" ) | |
echo "this_panel_id=${this_panel_id}" | |
this_panel=$( .gitlab/ci/get-panel-from-dashboard-json-by-id.sh "$this_dashboard" "$this_panel_id" ) | |
# echo "this_panel=${this_panel}" | |
this_panel_alert=$( jq '.alert' <<< $this_panel ) | |
# echo "this_panel_alert=${this_panel_alert}" | |
# get the query expression from the alert's conditions.refId | |
targets=$( jq '.targets' <<< $this_panel ) | |
conditions=$( jq '.conditions' <<< $this_panel_alert ) | |
# echo "targets=${targets}" | |
# echo "conditions=${conditions}" | |
for m in $( .gitlab/ci/get-indexes-from-json.sh "$conditions" ); do | |
this_condition=$( .gitlab/ci/get-element-from-json-by-index.sh "$conditions" "$m" ) | |
ref_id=$( jq -r '.query.params[0]' <<< $this_condition ) | |
# echo "ref_id=${ref_id}" | |
this_expr=$( .gitlab/ci/get-expr-from-targets-by-ref-id.sh "$targets" "$ref_id" ) | |
# echo "this_expr=${this_expr}" | |
this_condition=$( jq --arg this_expr "$this_expr" '{evaluator: .evaluator, operator: .operator, query: $this_expr}' <<< $this_condition ) | |
# echo "this_condition=${this_condition}" | |
done | |
# form a new json object representing one alert rule | |
this_alert=$( .gitlab/ci/get-element-from-json-by-index.sh "$this_alerts" "$p" ) | |
# echo "this_alert=${this_alert}" | |
this_alert_json=$( jq \ | |
--argjson panel_id "$this_panel_id" \ | |
--arg dashboard "$this_dashboard_name" \ | |
--argjson query "$this_condition" \ | |
--arg folder "$( .gitlab/ci/get-folder-by-dashboard-uid.sh "$this_uid" )" \ | |
--arg panel "$( jq -r '.title' <<< $this_panel )" \ | |
'{for: .for, alert: .name, labels: .alertRuleTags, expr: $query, annotations: {message: .message, panel: $panel, panel_id: $panel_id, dashboard: $dashboard, folder: $folder}}' \ | |
<<< $this_panel_alert ) | |
# echo "this_alert_json=${this_alert_json}" | |
dashboard_alert_jsons=$( jq --argjson this_json "[$this_alert_json]" '. += $this_json' <<< $dashboard_alert_jsons ) | |
done | |
# echo "dashboard_alert_jsons=${dashboard_alert_jsons}" | |
dashboard_groups=$( jq \ | |
--arg dashboard "$this_dashboard_name" \ | |
--argjson this_json_array "$dashboard_alert_jsons" \ | |
'. += [{name: $dashboard, rules: $this_json_array}]' \ | |
<<< $dashboard_groups ) | |
rules_json=$( jq -n \ | |
--argjson dashboard_groups "$dashboard_groups" \ | |
--arg namespace "$namespace" \ | |
'{namespace: $namespace, groups: $dashboard_groups}' \ | |
) | |
yq e -P - <<< $rules_json > "$filename" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires jq and yq. probably only works with bash; that's the only shell i tested with.