-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmss_monitor.sh
More file actions
executable file
·45 lines (37 loc) · 1.54 KB
/
Copy pathvmss_monitor.sh
File metadata and controls
executable file
·45 lines (37 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
set -e
# Azure VMSS Scheduled Events Monitor
# Monitors Azure VM Scale Set scheduled events for termination signals
# Reference: https://learn.microsoft.com/en-us/azure/virtual-machines/linux/scheduled-events
METADATA_ENDPOINT='http://169.254.169.254/metadata/scheduledevents?api-version=2020-07-01'
EVENTS_FILE='/tmp/scheduledevents.json'
STOP_SCRIPT='/opt/stop.sh'
echo "Checking for VMSS scheduled events..."
# Query Azure Instance Metadata Service for scheduled events
curl -sf "$METADATA_ENDPOINT" -H 'Metadata: true' > "$EVENTS_FILE"
# Check if termination event is scheduled
if grep -q "Terminate" "$EVENTS_FILE"; then
echo "Terminate event detected! Initiating graceful shutdown..."
# Start agent shutdown, if script exists and is executable
if [ -x "$STOP_SCRIPT" ]; then
echo "Executing stop script: $STOP_SCRIPT"
"$STOP_SCRIPT"
else
echo "Warning: Stop script not found or not executable: $STOP_SCRIPT"
fi
# Acknowledge the event to Azure
EventId=$(jq -r '.Events[] | select(.EventType == "Terminate") | .EventId' "$EVENTS_FILE")
if [ -n "$EventId" ]; then
echo "Acknowledging event: $EventId"
# Use jq to safely construct JSON payload to handle special characters
JSON_PAYLOAD=$(jq -n --arg eventId "$EventId" '{"StartRequests": [{"EventId": $eventId}]}')
curl -sf -X POST "$METADATA_ENDPOINT" \
-H 'Metadata: true' \
-H 'Content-Type: application/json' \
-d "$JSON_PAYLOAD"
echo "Event acknowledged successfully"
fi
else
echo "No termination events scheduled"
fi
exit 0