Skip to content

Instantly share code, notes, and snippets.

@0ryant
Last active June 1, 2020 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0ryant/5229734a9183e8b3eaeb6f94cceb2ed5 to your computer and use it in GitHub Desktop.
Save 0ryant/5229734a9183e8b3eaeb6f94cceb2ed5 to your computer and use it in GitHub Desktop.
ps module to use the opsgenie alerts api
#region notes
<#
Ryan Tilcock 29-5-2020
v1 - added basic functionality for each alert api method on https://docs.opsgenie.com/docs/alert-api
#>
#endregion
#region functions
function New-OpsGenieAlert {
[cmdletbinding()]
param (
[parameter(Mandatory)]
[string]$Message,
[parameter()]
[string]$Description,
[parameter()]
[string][ValidateSet('P1','P2','P3','P4','P5')]$Priority='P3',
[parameter()]
[string]$Alias=$(((New-Guid).toString())+'-'+(Get-Date -Format 'yyyyMMddHHmm')),
[parameter(Mandatory)]
[string]$APIKey
)
#make it look nice in slack
$Description = '```'+"`n"+$Description+"`n"+'```'
#build the json req body
$Body = @{
message = $Message
description = $Description
priority = $Priority
alias = $Alias
} | ConvertTo-Json
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = 'https://api.opsgenie.com/v2/alerts'
'ContentType' = 'application/json'
'Body' = $Body
'Method' = 'POST'
}
#invoke the req, add the alias to the return
$req = (Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json
$req | Add-Member Alias $Alias
return $req
}
function Get-OpsGenieAlert {
[cmdletbinding()]
param (
[parameter(Mandatory)]
[string]$SearchIdentifier,
[parameter()]
[string][ValidateSet('alias','id','tiny')]$IdentifierType = 'alias',
[parameter(Mandatory)]
[string]$APIKey
)
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = "https://api.opsgenie.com/v2/alerts/$($SearchIdentifier)?identifierType=$($IdentifierType)"
'ContentType' = 'application/json'
'Method' = 'GET'
}
#invoke the req
return $(((Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json).data)
}
function Get-OpsGenieAlertList {
[cmdletbinding()]
param(
[Parameter(Mandatory=$true)]
[string]$APIKey,
[Parameter()]
[int]$Limit = 100,
[Parameter()]
[string][ValidateSet("open", "acked", "unacked", "seen", "notseen", "closed")]$Status = 'open',
[Parameter()]
[string][ValidateSet("createdAt", "updatedAt")]$Sort = "createdAt",
[Parameter()]
[string][ValidateSet("asc", "desc")]$Order = "asc"
)
#build the url
$URI = 'https://api.opsgenie.com/v2/alerts?query=status%3A'+$Status+'&limit='+$Limit+'&order='+$Order+'&sort='+$SortBy
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = $URI
'ContentType' = 'application/json'
'Method' = 'GET'
}
#Send the request off to the OpsGenie Alerts API
return (Invoke-RestMethod @InvokeParams).data
}
function Get-OpsGenieAlertIDFromAlias {
[cmdletbinding()]
param (
[parameter(Mandatory)]
[string]$SearchIdentifier,
[parameter(Mandatory)]
[string]$APIKey
)
#build the splat$
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = "https://api.opsgenie.com/v2/alerts/$($SearchIdentifier)?identifierType=alias"
'ContentType' = 'application/json'
'Method' = 'GET'
}
#invoke the req
return $(((Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json).data).id
}
function Close-OpsGenieAlert {
[cmdletbinding()]
param(
[parameter(mandatory,position=0,valuefrompipeline)]
[string]$SearchIdentifier,
[parameter()]
[string]$Note = '',
[parameter()]
[string][ValidateSet('alias','id','tiny')]$identifierType = 'alias',
[parameter(Mandatory)]
[string]$APIKey
)
#wait - trying to close immediately after a change orphans slack alert
Start-Sleep -Seconds 1
#build the req body
$Body = @{
user = $env:USERNAME
source = $env:COMPUTERNAME
note = $note
} | ConvertTo-Json
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = "https://api.opsgenie.com/v2/alerts/$($SearchIdentifier)/close?identifierType=$($identifierType)"
'ContentType' = 'application/json'
'Body' = $Body
'Method' = 'POST'
}
return $((Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json | Format-List)
}
function Add-OpsGenieAlertNote {
[cmdletbinding()]
param(
[parameter(mandatory,position=0,valuefrompipeline)]
[string]$SearchIdentifier,
[parameter()]
[string]$Note = '',
[parameter()]
[string][ValidateSet('alias','id','tiny')]$identifierType = 'alias',
[parameter(Mandatory)]
[string]$APIKey
)
#build the req body
$Body = @{
user = $env:USERNAME
source = $env:COMPUTERNAME
note = $note
} | ConvertTo-Json
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = "https://api.opsgenie.com/v2/alerts/$($SearchIdentifier)/notes?identifierType=$($identifierType)"
'ContentType' = 'application/json'
'Body' = $Body
'Method' = 'POST'
}
return $((Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json)
}
function Set-OpsGenieAlertAcknowledged {
[cmdletbinding()]
param(
[parameter(mandatory,position=0,valuefrompipeline)]
[string]$SearchIdentifier,
[parameter()]
[string]$Note = '',
[parameter()]
[string][ValidateSet('alias','id','tiny')]$identifierType = 'alias',
[parameter(Mandatory)]
[string]$APIKey
)
#build the req body
$Body = @{
user = $env:USERNAME
source = $env:COMPUTERNAME
note = $note
} | ConvertTo-Json
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = "https://api.opsgenie.com/v2/alerts/$($SearchIdentifier)/acknowledge?identifierType=$($identifierType)"
'ContentType' = 'application/json'
'Body' = $Body
'Method' = 'POST'
}
return $((Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json)
}
function Set-OpsGenieAlertUnacknowledged {
[cmdletbinding()]
param(
[parameter(mandatory,position=0,valuefrompipeline)]
[string]$SearchIdentifier,
[parameter()]
[string]$Note = '',
[parameter()]
[string][ValidateSet('alias','id','tiny')]$identifierType = 'alias',
[parameter(Mandatory)]
[string]$APIKey
)
#build the req body
$Body = @{
user = $env:USERNAME
source = $env:COMPUTERNAME
note = $note
} | ConvertTo-Json
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = "https://api.opsgenie.com/v2/alerts/$($SearchIdentifier)/unacknowledge?identifierType=$($identifierType)"
'ContentType' = 'application/json'
'Body' = $Body
'Method' = 'POST'
}
return $((Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json)
}
function Set-OpsGenieAlertSnooze {
[cmdletbinding()]
param(
[parameter(mandatory,position=0)]
[string]$SearchIdentifier,
[parameter()]
[int][ValidateSet('15','30','60','120','360')]$MinutestoSnooze=15,
[parameter()]
[string]$Note = '',
[parameter()]
[string][ValidateSet('alias','id','tiny')]$identifierType = 'alias',
[parameter(Mandatory)]
[string]$APIKey
)
[string]$EndTime = $((Get-Date).AddMinutes($MinutestoSnooze).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.000Z'))
#build the req body
$Body = @{
endTime = $EndTime
user = $env:USERNAME
source = $env:COMPUTERNAME
note = $note
} | ConvertTo-Json
#build the splat
$InvokeParams = @{
'Headers' = @{
"Authorization" = "GenieKey $APIKey"
}
'Uri' = "https://api.opsgenie.com/v2/alerts/$($SearchIdentifier)/snooze?identifierType=$($identifierType)"
'ContentType' = 'application/json'
'Body' = $Body
'Method' = 'POST'
}
return $((Invoke-WebRequest @InvokeParams).content | ConvertFrom-Json | Format-List)
}
#endregion
#region demo
<#
$alert = New-OpsGenieAlert -Message 'oh no everything borked' -Description "it all v borked`n`n$(get-date)" -Priority P2 -APIKey $testkey
$ID = Get-OpsGenieAlertIDFromAlias -SearchIdentifier $alert.Alias -APIKey $testkey
Get-OpsGenieAlert -SearchIdentifier $alert.Alias -IdentifierType alias -APIKey $testkey
Get-OpsGenieAlert -SearchIdentifier $ID -IdentifierType id -APIKey $testkey
Set-OpsGenieAlertAcknowledged -SearchIdentifier $alert.Alias -identifierType alias -Note 'ack'-APIKey $testkey
Set-OpsGenieAlertUnacknowledged -SearchIdentifier $alert.Alias -identifierType alias -Note 'un-ack'-APIKey $testkey
Set-OpsGenieAlertSnooze -SearchIdentifier $ID -MinutestoSnooze 30 -Note 'snoozing' -identifierType id -APIKey $testkey
Set-OpsGenieAlertAcknowledged -SearchIdentifier $alert.Alias -identifierType alias -Note 'ack once again with the renegade master'-APIKey $testkey
Add-OpsGenieAlertNote -SearchIdentifier $alert.Alias -Note 'adding some note' -APIKey $testkey
Close-OpsGenieAlert -SearchIdentifier $alert.Alias -Note "closed" -APIKey $testkey
Get-OpsGenieAlert -SearchIdentifier $ID -IdentifierType id -APIKey $testkey
#>
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment