This one was fun to put together.
I wanted to write code to query the status of an Azure Load Balancer. I couldn’t find much out there. This code query’s the Azure Load Balancer’s Metrics for VipAvailability – through the REST API. If it returns 100 then are good to go. Anyting else, then there may be a issue. You can query any metric, and you can set a time range, I am just looking at the last min.
Note: This is for a Standard Load Balancer, not Basic.
Some of the Metrics Available:
VipAvailability : Average count of availability of VIP endpoints, based on probe results. DipAvailability : Average count of availability of DIP endpoints, based on probe results. ByteCount : Total number of bytes processed per front-end. PacketCount : Total number of packets processed per front-end. SynCount : Total number of SYN packets received. SnatConnectionCount : Total number of new SNAT connections, that is, outbound connections that are masqueraded to the Public IP address front-end.
And the same metrics are often referred to by different names (this was confusing to me):
value localizedValue ----- -------------- VipAvailability Data Path Availability DipAvailability Health Probe Status ByteCount Byte Count PacketCount Packet Count SYNCount SYN Count SnatConnectionCount SNAT Connection Count AllocatedSnatPorts Allocated SNAT Ports (Preview) UsedSnatPorts Used SNAT Ports (Preview)
Here is the code (bouns: BASH/cURL too) to find the VipAvaiablity of Azure Load Balancers:
$SubscriptionId = "$($env:SubscriptionId)" $TenantId = "$($env:TenantId)" $ClientID = "$($env:ClientID)" $ClientSecret = "$($env:ClientSecret)" $TenantDomain = "$($env:TenantDomain)" $loginURL = "https://login.microsoftonline.com/$TenantId/oauth2/token" $resource = "https://management.core.windows.net/" $resourceGroupName = "eastUS-01" $body = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret} $oauth = Invoke-RestMethod -Method Post -Uri $loginURL -Body $body $headerParams = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"} $start=((Get-Date).AddMinutes(-1)).ToUniversalTime().ToString("yyy-MM-ddTHH:mm:00Z") $end=(Get-Date).ToUniversalTime().ToString("yyy-MM-ddTHH:mm:00Z") $filter = "(name.value eq 'VipAvailability') and aggregationType eq 'Average' and startTime eq $start and endTime eq $end and timeGrain eq duration'PT1M'" $url = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Network/loadBalancers/jemurphyLB01/providers/microsoft.insights/metrics?`$filter=${filter}&api-version=2016-09-01" $results=Invoke-RestMethod -Uri $url -Headers $headerParams -Method Get $results.value | select -ExpandProperty data | select timestamp,average
SUBSCRIPTIONID="" RESOURCEGROUPNAME="" CLIENTID="" CLIENTSECRET="" TENANTID="" RESOURCEGROUPNAME="" LBNAME="" LOGINURL="https://login.microsoftonline.com/$TENANTID/oauth2/token" RESOURCE="https://management.core.windows.net/" TOKEN=$(curl --silent --request POST $LOGINURL --data-urlencode "resource=https://management.core.windows.net" --data-urlencode "client_id=$CLIENTID" --data-urlencode "grant_type=client_credentials" --data-urlencode "client_secret=$CLIENTSECRET" | jq -r '.access_token') STARTTIME=$(date -u +'%Y-%m-%dT%H:%M:00' --date='-1 min') ENDTIME=$(date -u +'%Y-%m-%dT%H:%M:00') FILTER="(name.value eq 'VipAvailability') and aggregationType eq 'Average' and startTime eq $STARTTIME and endTime eq $ENDTIME and timeGrain eq duration'PT1M'" URL="https://management.azure.com/subscriptions/$SUBSCRIPTIONID/resourceGroups/$RESOURCEGROUPNAME/providers/Microsoft.Network/loadBalancers/$LBNAME/providers/microsoft.insights/metrics" RESULTS=$(curl -s -G --header "authorization: Bearer $TOKEN" --data-urlencode "\$filter=$FILTER" --data-urlencode "api-version=2016-09-01" $URL | jq .value[].data[].average) echo "$RESULTS"
I think the hardest part was trying to get the date and time in the right format. Why is that so hard?
This HAS to be helpful to some one!
No comments yet.