@@ -263,12 +263,16 @@ NOTIF_UID=$(curl -s -u "$GRAFANA_USER:$GRAFANA_PASSWORD" \
263263if [[ -z " $NOTIF_UID " ]]; then
264264 log " 🔧 Contact point not found, creating..."
265265
266+ # Use fixed UID for idempotency
267+ CONTACT_POINT_UID=" lambda-webhook-contact"
266268 CONTACT_POINT_JSON=$( jq -n \
267269 --arg name " lambda-webhook" \
270+ --arg uid " $CONTACT_POINT_UID " \
268271 --arg url " $LAMBDA_URL " \
269272 --arg user " $WEBHOOK_USER " \
270273 --arg pass " $WEBHOOK_PASSWORD " \
271274 ' {
275+ uid: $uid,
272276 name: $name,
273277 type: "webhook",
274278 settings: {
@@ -281,10 +285,11 @@ if [[ -z "$NOTIF_UID" ]]; then
281285 }
282286 }' )
283287
284- curl -s -X POST -H " Content-Type: application/json" \
288+ # Use PUT for idempotent creation/update
289+ curl -s -X PUT -H " Content-Type: application/json" \
285290 -u " $GRAFANA_USER :$GRAFANA_PASSWORD " \
286291 -d " $CONTACT_POINT_JSON " \
287- " $GRAFANA_URL /api/v1/provisioning/contact-points"
292+ " $GRAFANA_URL /api/v1/provisioning/contact-points/ $CONTACT_POINT_UID "
288293
289294 sleep 2
290295
326331DASHBOARD_UID=$( curl -s -u " $GRAFANA_USER :$GRAFANA_PASSWORD " " $GRAFANA_URL /api/search?query=JVM" | jq -r ' .[0].uid' )
327332PANEL_ID=1
328333
329- # Build alert rule JSON
334+ # Build alert rule JSON with fixed UID for idempotency
330335log " 🛠️ Generating alert rule JSON..."
331336
337+ RULE_UID=" jvm-thread-dump-alert"
332338ALERT_RULE_JSON=$( jq -n \
333339 --arg url " $LAMBDA_URL " \
334340 --arg uid " $DASHBOARD_UID " \
335341 --argjson pid " $PANEL_ID " \
336342 --arg notifUid " $NOTIF_UID " \
337- --arg folderUid " $FOLDER_UID " '
343+ --arg folderUid " $FOLDER_UID " \
344+ --arg ruleUid " $RULE_UID " '
338345{
346+ uid: $ruleUid,
339347 dashboardUID: $uid,
340348 panelId: $pid,
341349 folderUID: $folderUid,
@@ -404,17 +412,18 @@ ALERT_RULE_JSON=$(jq -n \
404412
405413echo " $ALERT_RULE_JSON " > " $LAMBDA_ALERT_RULE_FILE "
406414
407- log " 📤 Creating alert rule..."
415+ log " 📤 Creating/updating alert rule (idempotent) ..."
408416
409- RESPONSE=$( curl -s -X POST -H " Content-Type: application/json" \
417+ RESPONSE=$( curl -s -X PUT -H " Content-Type: application/json" \
410418 -u " $GRAFANA_USER :$GRAFANA_PASSWORD " \
411419 -d " $ALERT_RULE_JSON " \
412- " $GRAFANA_URL /api/v1/provisioning/alert-rules" )
420+ " $GRAFANA_URL /api/v1/provisioning/alert-rules/ $RULE_UID " )
413421
414422if echo " $RESPONSE " | jq -e ' .uid' > /dev/null; then
415- log " ✅ Lambda alert rule created: RULE_UID $( echo " $RESPONSE " | jq -r ' .uid' ) "
423+ RETURNED_UID=$( echo " $RESPONSE " | jq -r ' .uid' )
424+ log " ✅ Alert rule created/updated with UID: $RETURNED_UID "
416425else
417- log " ❌ Failed to create Lambda alert rule"
426+ log " ❌ Failed to create/update alert rule"
418427 echo " $RESPONSE "
419428 exit 1
420429fi
@@ -454,8 +463,20 @@ for i in {1..30}; do
454463 sleep 5
455464done
456465
457- # Create notification policy via API
458- POLICY_JSON=$( cat << EOF
466+ # Check current notification policy
467+ log " 🔍 Checking current notification policy..."
468+ CURRENT_POLICY=$( curl -s -u " $GRAFANA_USER :$GRAFANA_PASSWORD " \
469+ " http://$GRAFANA_LB /api/v1/provisioning/policies" 2> /dev/null || echo " {}" )
470+
471+ CURRENT_RECEIVER=$( echo " $CURRENT_POLICY " | jq -r ' .receiver // "default"' )
472+
473+ if [[ " $CURRENT_RECEIVER " == " lambda-webhook" ]]; then
474+ log " ✅ Notification policy already configured for lambda-webhook"
475+ else
476+ log " 🔧 Updating notification policy to use lambda-webhook..."
477+
478+ # Create notification policy via API
479+ POLICY_JSON=$( cat << EOF
459480{
460481 "receiver": "lambda-webhook",
461482 "group_by": ["alertname"],
@@ -477,23 +498,23 @@ POLICY_JSON=$(cat <<EOF
477498 "repeat_interval": "1h"
478499}
479500EOF
480- )
501+ )
481502
482- # Apply the notification policy
483- log " 📤 Applying notification policy via API..."
484- POLICY_RESULT=$( curl -s -X PUT -H " Content-Type: application/json" \
485- -u " $GRAFANA_USER :$GRAFANA_PASSWORD " \
486- -d " $POLICY_JSON " \
487- " http://$GRAFANA_LB /api/v1/provisioning/policies" )
503+ # Apply the notification policy
504+ log " 📤 Applying notification policy via API..."
505+ POLICY_RESULT=$( curl -s -X PUT -H " Content-Type: application/json" \
506+ -u " $GRAFANA_USER :$GRAFANA_PASSWORD " \
507+ -d " $POLICY_JSON " \
508+ " http://$GRAFANA_LB /api/v1/provisioning/policies" )
488509
489- if echo " $POLICY_RESULT " | grep -q " policies updated" ; then
490- log " ✅ Notification policy successfully applied"
491- else
492- log " ⚠️ Warning: Notification policy application returned: $POLICY_RESULT "
510+ if echo " $POLICY_RESULT " | grep -q " policies updated" ; then
511+ log " ✅ Notification policy successfully applied"
512+ else
513+ log " ⚠️ Warning: Notification policy application returned: $POLICY_RESULT "
493514
494- # Fallback to ConfigMap method if API fails
495- log " 🔄 Trying fallback method with ConfigMap..."
496- cat > " $NOTIFICATION_POLICY_CONFIGMAP_FILE " << EOF
515+ # Fallback to ConfigMap method if API fails
516+ log " 🔄 Trying fallback method with ConfigMap..."
517+ cat > " $NOTIFICATION_POLICY_CONFIGMAP_FILE " << EOF
497518apiVersion: v1
498519kind: ConfigMap
499520metadata:
@@ -533,10 +554,11 @@ data:
533554 mute_timings: []
534555EOF
535556
536- kubectl apply -f " $NOTIFICATION_POLICY_CONFIGMAP_FILE "
537- log " 🔄 Restarting Grafana to apply ConfigMap policy..."
538- kubectl rollout restart deployment grafana -n " $NAMESPACE "
539- kubectl rollout status deployment grafana -n " $NAMESPACE " --timeout=60s
557+ kubectl apply -f " $NOTIFICATION_POLICY_CONFIGMAP_FILE "
558+ log " 🔄 Restarting Grafana to apply ConfigMap policy..."
559+ kubectl rollout restart deployment grafana -n " $NAMESPACE "
560+ kubectl rollout status deployment grafana -n " $NAMESPACE " --timeout=60s
561+ fi
540562fi
541563
542564# Verify policy was applied
0 commit comments