Functions/Send-DataDogEvent.ps1

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

<#
.SYNOPSIS
    Sends an event to DataDog
.DESCRIPTION
    PowerShell cmdlet to send events to DataDog via DogStatsD
    UDP datagram. Format as per described at:
    - http://docs.datadoghq.com/guides/dogstatsd/#events-1

.PARAMETER Title
    Mandatory Title of the Event
.PARAMETER Text
    Optional Event body text
.PARAMETER AlertType
    Optional type of the event:
        'info','success','warning','error'
    Default is info
.PARAMETER Priority
    Priority of the event: normal or low
.PARAMETER Tag
    List of tag definitions for the metric in "tagname:value" format
.PARAMETER ComputerName
    Optional - ComputerName (Hostname or IPaddress) of the
    target statsd service
        Default is 127.0.0.1 for localhost
.PARAMETER Port
    Optional - Port for the target statsd service
        Default is 8125
.EXAMPLE
    Send-DataDogEvent -Title 'My event' -Text 'Add any details' -Tag @("subsytem:my_test_tag1")
#>

function Send-DataDogEvent {
    [CmdletBinding(SupportsShouldProcess=$true)]
    param(
        [Parameter(Mandatory)]
        [string]$Title,

        [Parameter()]
        [string]$Text='',

        [Parameter()]
        [string]$ComputerName,

        [Parameter()]
        [ValidateRange(1,65535)]
        [int]$Port,

        [Parameter()]
        [ValidateSet('normal','low')]
        [string]$Priority='normal',

        [Parameter()]
        [ValidateSet('info','success','warning','error')]
        [string]$AlertType='info',

        [Parameter()]
        [string[]]$Tag=@()
    )

    $data = "_e{$($Title.Length),$($Text.Length)}:$Title|$Text|h:$ComputerName|p:$Priority|t:$AlertType|#$([string]::Join(',',$Tag))"
    $statsdParams = @{
        Data = $data
    }
    if ($ComputerName) {
        $statsdParams.ComputerName = $ComputerName
    }
    if ($Port) {
        $statsdParams.Port = $Port
    }

    if ($PSCmdlet.ShouldProcess("Sending DataDog event: $data")) {
        Send-StatsD @statsdParams
    }
}