Created
June 1, 2017 21:05
-
-
Save johnyardstick/c0288c63b4caf4cc416365a939f5aa9b to your computer and use it in GitHub Desktop.
Datadog uptime gist
This file contains hidden or 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
| #--------- Notify-DataDog of downtime end | |
| $apikey = $OctopusParameters['Api-Key'] | |
| $appkey = $OctopusParameters['App-Key'] | |
| $machine = $OctopusParameters['Octopus.Machine.Name'] | |
| $downtime_id = $OctopusParameters['Octopus.Action[Disable Datadog Monitors].Output.datadog_downtime_id'] | |
| Write-Host "Downtime ID: $downtime_id" | |
| #Do the HTTP POST to DataDog | |
| #$uri = "https://app.datadoghq.com/api/v1/downtime/" + $downtime_id + "?api_key=$apikey&application_key=$appkey" | |
| #Write-Host "Uri: $uri" | |
| #Invoke-WebRequest -Uri $uri -Method Delete -UseBasicParsing | |
| $downtime_end = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-uformat "%s")) + 300 | |
| $message = New-Object PSObject | |
| $message | Add-Member -MemberType NoteProperty -Name scope -Value "host:$machine" | |
| $message | Add-Member -MemberType NoteProperty -Name "end" -Value "$downtime_end" | |
| #Do the HTTP POST to DataDog | |
| $uri = "https://app.datadoghq.com/api/v1/downtime/" + $downtime_id + "?api_key=$apikey&application_key=$appkey" | |
| $postBody = ConvertTo-Json -InputObject $message | |
| $postStr = [System.Text.Encoding]::UTF8.GetBytes($postBody) | |
| $webRequest = [System.Net.WebRequest]::Create($uri) | |
| $webRequest.ContentType = "application/json" | |
| $webrequest.ContentLength = $postStr.Length | |
| $webRequest.Method = "PUT" | |
| $requestStream = $webRequest.GetRequestStream() | |
| $requestStream.Write($postStr, 0,$postStr.length) | |
| $requestStream.Close() | |
| [System.Net.WebResponse] $resp = $webRequest.GetResponse() | |
| $rs = $resp.GetResponseStream() | |
| [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs | |
| $web_response = $sr.ReadToEnd() | |
| $downtime_end_response = $web_response | ConvertFrom-Json | Select -ExpandProperty "end" | |
| if ($downtime_end -eq $downtime_end_response) | |
| { | |
| Write-Host "Downtime successfully set to end at: $downtime_end" | |
| } | |
| else | |
| { | |
| Write-Host "Issue ending downtime" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment