Skip to content

Commit 3872bc6

Browse files
committed
set variables for grafana queries
1 parent 0187c68 commit 3872bc6

5 files changed

Lines changed: 180 additions & 51 deletions

File tree

grafana/provisioning/dashboards/aligned/aggregation_mode_gateway.json

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,56 +3269,7 @@
32693269
"schemaVersion": 42,
32703270
"tags": [],
32713271
"templating": {
3272-
"list": [
3273-
{
3274-
"current": {
3275-
"text": "0xe6C9D0cf87cdaA8B2093c4b3830dde7267843F64",
3276-
"value": "0xe6C9D0cf87cdaA8B2093c4b3830dde7267843F64"
3277-
},
3278-
"name": "payments_contract",
3279-
"options": [
3280-
{
3281-
"selected": true,
3282-
"text": "0xe6C9D0cf87cdaA8B2093c4b3830dde7267843F64",
3283-
"value": "0xe6C9D0cf87cdaA8B2093c4b3830dde7267843F64"
3284-
}
3285-
],
3286-
"query": "0xe6C9D0cf87cdaA8B2093c4b3830dde7267843F64",
3287-
"type": "custom"
3288-
},
3289-
{
3290-
"current": {
3291-
"text": "0x6B34AAaE780A5EAB4c91AB8F54f2a421E9c2FB59",
3292-
"value": "0x6B34AAaE780A5EAB4c91AB8F54f2a421E9c2FB59"
3293-
},
3294-
"name": "proof_aggregator_contract",
3295-
"options": [
3296-
{
3297-
"selected": true,
3298-
"text": "0x6B34AAaE780A5EAB4c91AB8F54f2a421E9c2FB59",
3299-
"value": "0x6B34AAaE780A5EAB4c91AB8F54f2a421E9c2FB59"
3300-
}
3301-
],
3302-
"query": "0x6B34AAaE780A5EAB4c91AB8F54f2a421E9c2FB59",
3303-
"type": "custom"
3304-
},
3305-
{
3306-
"current": {
3307-
"text": "0x7EB3B63A4F3e7810Cc9bbc346749E2491Da4D7Cc",
3308-
"value": "0x7EB3B63A4F3e7810Cc9bbc346749E2491Da4D7Cc"
3309-
},
3310-
"name": "proof_aggregator_wallet",
3311-
"options": [
3312-
{
3313-
"selected": true,
3314-
"text": "0x7EB3B63A4F3e7810Cc9bbc346749E2491Da4D7Cc",
3315-
"value": "0x7EB3B63A4F3e7810Cc9bbc346749E2491Da4D7Cc"
3316-
}
3317-
],
3318-
"query": "0x7EB3B63A4F3e7810Cc9bbc346749E2491Da4D7Cc",
3319-
"type": "custom"
3320-
}
3321-
]
3272+
"list": []
33223273
},
33233274
"time": {
33243275
"from": "now-15m",
@@ -3328,5 +3279,5 @@
33283279
"timezone": "",
33293280
"title": "Aggregation Mode",
33303281
"uid": "a66a5480-6a60-4b87-9d29-4f0f446edafd",
3331-
"version": 3
3282+
"version": 4
33323283
}

infra/aggregation_mode/ansible/playbooks/grafana_agg_mode.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
gateway_1_hostname: "{{ lookup('ini', 'gateway_1_hostname', file=config_file) }}"
2929
gateway_2_hostname: "{{ lookup('ini', 'gateway_2_hostname', file=config_file) }}"
3030
grafana_gateway_url: "{{ lookup('ini', 'grafana_gateway_url', file=config_file) }}"
31+
grafana_var_payments_contract: "{{ lookup('ini', 'grafana_var_payments_contract', file=config_file) }}"
32+
grafana_var_proof_aggregator_contract: "{{ lookup('ini', 'grafana_var_proof_aggregator_contract', file=config_file) }}"
33+
grafana_var_proof_aggregator_wallet: "{{ lookup('ini', 'grafana_var_proof_aggregator_wallet', file=config_file) }}"
3134
no_log: true
3235

3336
- name: Install required packages
@@ -38,6 +41,7 @@
3841
- software-properties-common
3942
- wget
4043
- gnupg
44+
- jq
4145
state: latest
4246
update_cache: true
4347
vars:
@@ -142,3 +146,32 @@
142146
daemon_reload: true
143147
vars:
144148
ansible_ssh_user: "{{ admin_user }}"
149+
150+
- name: Wait for Grafana to be ready
151+
uri:
152+
url: "http://localhost:3000/api/health"
153+
status_code: 200
154+
register: grafana_health
155+
until: grafana_health.status == 200
156+
retries: 10
157+
delay: 5
158+
159+
- name: Copy dashboard variables script
160+
become: true
161+
copy:
162+
src: scripts/grafana_set_variables.sh
163+
dest: /tmp/grafana_set_variables.sh
164+
mode: '0755'
165+
vars:
166+
ansible_ssh_user: "{{ admin_user }}"
167+
168+
- name: Set dashboard variables via API
169+
shell: |
170+
/tmp/grafana_set_variables.sh \
171+
"http://localhost:3000" \
172+
"{{ grafana_admin_password }}" \
173+
"Aggregation Mode" \
174+
"payments_contract={{ grafana_var_payments_contract }}" \
175+
"proof_aggregator_contract={{ grafana_var_proof_aggregator_contract }}" \
176+
"proof_aggregator_wallet={{ grafana_var_proof_aggregator_wallet }}"
177+
no_log: false

infra/aggregation_mode/ansible/playbooks/ini/config-hoodi.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ grafana_monitor_port=5432
9090
grafana_monitor_db=pg_auto_failover
9191
# Public gateway URL
9292
grafana_gateway_url=https://hoodi.gateway.alignedlayer.com
93+
# Dashboard variables (contract addresses for dashboards)
94+
grafana_var_payments_contract=0x7222E0183cE1A96619d0c883e9bfc6b76D4e780e
95+
grafana_var_proof_aggregator_contract=0x6B34AAaE780A5EAB4c91AB8F54f2a421E9c2FB59
96+
grafana_var_proof_aggregator_wallet=0x7eb3b63a4f3e7810cc9bbc346749e2491da4d7cc
9397

9498
# ============================================
9599
# Task Sender Configuration

infra/aggregation_mode/ansible/playbooks/ini/config-mainnet.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ grafana_monitor_port=5432
9090
grafana_monitor_db=pg_auto_failover
9191
# Public gateway URL
9292
grafana_gateway_url=https://mainnet.gateway.alignedlayer.com
93+
# Dashboard variables (contract addresses for dashboards)
94+
grafana_var_payments_contract=0xc8631Bc1E60c20db40e474F791126212fA8255F4
95+
grafana_var_proof_aggregator_contract=0xD0696d3eEebffcAB2D1b358805efAA005A9A8BC0
96+
grafana_var_proof_aggregator_wallet=0x57628fe929016C30463473349DcDd592ea27C8b3
9397

9498
# ============================================
9599
# Task Sender Configuration
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/bin/bash
2+
# grafana_set_variables.sh
3+
# Sets dashboard variables in a Grafana dashboard via the Grafana API.
4+
#
5+
# Usage:
6+
# ./grafana_set_variables.sh <grafana_url> <admin_password> <dashboard_uid_or_title> [key=value ...]
7+
#
8+
# The third argument can be either:
9+
# - A dashboard UID (e.g., "a66a5480-6a60-4b87-9d29-4f0f446edafd")
10+
# - A dashboard title to search for (e.g., "Aggregation Mode")
11+
#
12+
# Example:
13+
# ./grafana_set_variables.sh "http://localhost:3000" "admin123" "a66a5480-6a60-4b87-9d29-4f0f446edafd" \
14+
# "payments_contract=0x1234..." \
15+
# "proof_aggregator_contract=0x5678..." \
16+
# "proof_aggregator_wallet=0xabcd..."
17+
18+
set -e
19+
20+
GRAFANA_URL="${1}"
21+
ADMIN_PASSWORD="${2}"
22+
DASHBOARD_ID="${3}"
23+
shift 3
24+
25+
if [ -z "$GRAFANA_URL" ] || [ -z "$ADMIN_PASSWORD" ] || [ -z "$DASHBOARD_ID" ]; then
26+
echo "Usage: $0 <grafana_url> <admin_password> <dashboard_uid_or_title> [key=value ...]"
27+
exit 1
28+
fi
29+
30+
AUTH="admin:${ADMIN_PASSWORD}"
31+
32+
# Check if DASHBOARD_ID looks like a UID (contains dashes in UUID format)
33+
if [[ "$DASHBOARD_ID" =~ ^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$ ]]; then
34+
# It's a UID, use it directly
35+
DASHBOARD_UID="$DASHBOARD_ID"
36+
echo "Using dashboard UID: ${DASHBOARD_UID}"
37+
else
38+
# List all dashboards and find by title
39+
DASHBOARD_TITLE="$DASHBOARD_ID"
40+
SEARCH_RESULT=$(curl -s -u "${AUTH}" "${GRAFANA_URL}/api/search?type=dash-db")
41+
42+
# Find dashboard UID by matching title
43+
DASHBOARD_UID=$(echo "${SEARCH_RESULT}" | jq -r --arg title "$DASHBOARD_TITLE" '.[] | select(.title == $title) | .uid' | head -1)
44+
45+
if [ -z "$DASHBOARD_UID" ]; then
46+
echo "Error: Dashboard '${DASHBOARD_ID}' not found"
47+
exit 1
48+
fi
49+
50+
echo "Found dashboard '${DASHBOARD_ID}' with UID: ${DASHBOARD_UID}"
51+
fi
52+
53+
# Get full dashboard definition
54+
DASHBOARD_RESPONSE=$(curl -s -u "${AUTH}" "${GRAFANA_URL}/api/dashboards/uid/${DASHBOARD_UID}")
55+
56+
# Extract dashboard and meta from response
57+
DASHBOARD=$(echo "${DASHBOARD_RESPONSE}" | jq '.dashboard')
58+
FOLDER_ID=$(echo "${DASHBOARD_RESPONSE}" | jq '.meta.folderId')
59+
60+
if [ "$DASHBOARD" = "null" ]; then
61+
echo "Error: Could not retrieve dashboard definition"
62+
exit 1
63+
fi
64+
65+
# Process each key=value pair
66+
for ARG in "$@"; do
67+
KEY="${ARG%%=*}"
68+
VALUE="${ARG#*=}"
69+
70+
if [ -z "$KEY" ] || [ -z "$VALUE" ]; then
71+
echo "Warning: Skipping invalid argument '${ARG}'"
72+
continue
73+
fi
74+
75+
echo "Setting variable '${KEY}' = '${VALUE}'"
76+
77+
# Check if variable already exists in templating.list
78+
EXISTING_VAR=$(echo "${DASHBOARD}" | jq --arg key "$KEY" '.templating.list // [] | map(select(.name == $key)) | length')
79+
80+
if [ "$EXISTING_VAR" -gt 0 ]; then
81+
# Update existing variable
82+
DASHBOARD=$(echo "${DASHBOARD}" | jq --arg key "$KEY" --arg val "$VALUE" '
83+
.templating.list = [
84+
.templating.list[] |
85+
if .name == $key then
86+
.query = $val | .current = {"text": $val, "value": $val}
87+
else
88+
.
89+
end
90+
]
91+
')
92+
else
93+
# Add new variable
94+
NEW_VAR=$(jq -n --arg key "$KEY" --arg val "$VALUE" '{
95+
"name": $key,
96+
"type": "constant",
97+
"hide": 2,
98+
"query": $val,
99+
"current": {"text": $val, "value": $val},
100+
"skipUrlSync": false
101+
}')
102+
103+
DASHBOARD=$(echo "${DASHBOARD}" | jq --argjson var "$NEW_VAR" '
104+
.templating.list = (.templating.list // []) + [$var]
105+
')
106+
fi
107+
done
108+
109+
# Remove id and version to allow update (Grafana uses uid for identification)
110+
DASHBOARD=$(echo "${DASHBOARD}" | jq 'del(.id) | del(.version)')
111+
112+
# Create the save payload
113+
PAYLOAD=$(jq -n \
114+
--argjson dashboard "$DASHBOARD" \
115+
--argjson folderId "$FOLDER_ID" \
116+
'{
117+
"dashboard": $dashboard,
118+
"folderId": $folderId,
119+
"overwrite": true
120+
}')
121+
122+
# Save the dashboard
123+
SAVE_RESPONSE=$(curl -s -u "${AUTH}" \
124+
-H "Content-Type: application/json" \
125+
-X POST \
126+
"${GRAFANA_URL}/api/dashboards/db" \
127+
-d "${PAYLOAD}")
128+
129+
# Check for errors
130+
STATUS=$(echo "${SAVE_RESPONSE}" | jq -r '.status // empty')
131+
if [ "$STATUS" = "success" ]; then
132+
echo "Dashboard updated successfully"
133+
else
134+
ERROR_MSG=$(echo "${SAVE_RESPONSE}" | jq -r '.message // "Unknown error"')
135+
echo "Error updating dashboard: ${ERROR_MSG}"
136+
exit 1
137+
fi

0 commit comments

Comments
 (0)