WCOMAB / PRTGSlackNotification

PowerShell script for sending PRTG notifications to Slack channel
MIT License
40 stars 13 forks source link

differentiate ok,warning and Error Messages with Slack Attachments #2

Closed Dirk23 closed 7 years ago

Dirk23 commented 8 years ago

It would be very nice to have an optical way to differentiate between error, warning and ok messages from prtg. I used message attachments on an Linux Script for OTRS Ticketing to differentiate between open and closed Tickets.

Maybee it is posible to use that for your script too?

devlead commented 8 years ago

@Dirk23 yes that's fully possible.

Dirk23 commented 8 years ago

If i where into PowerShell i could do it, but i dont know how to check the status.

devlead commented 8 years ago

@Dirk23 don't have time to look at it just now, but actually looks like someone done the work already if you check the answer Paessler KB https://kb.paessler.com/en/topic/66113-how-can-prtg-send-notifications-to-slack

great idea so would happily take it in as an pull request.

Dirk23 commented 8 years ago

I dont know how to make an pull request, but i took your PRTGSlackNotification.ps1 and changed it to the one with attachments. Well, i works, but i don't know where the value for the $ColorofState comes from!? My Messages are all without the attachment stuff.

# ----------------------------------------------------------------------------------------------
# Copyright (c) WCOM AB 2015.
# ----------------------------------------------------------------------------------------------
# This source code is subject to terms and conditions of the The MIT License (MIT)
# copy of the license can be found in the LICENSE file at the root of this distribution.
# ----------------------------------------------------------------------------------------------
# You must not remove this notice, or any other, from this software.
# ----------------------------------------------------------------------------------------------
Param(
    [string]$SlackToken,
    [string]$SlackChannel,
    [string]$SiteName,
    [string]$Device,
    [string]$Name,
    [string]$Status,
    [string]$Down,
    [string]$DateTime,
    [string]$LinkDevice,
    [string]$Message,
    [string]$ColorofState,
    [string]$LinkSensor 

)
$postSlackMessage = @{
    token        = $SlackToken;
    channel      = $SlackChannel;
    unfurl_links = "true";
    username     = $sitename;
    text        = "> *New notification* for <$($LinkDevice)|$($Device)>" 
    attachments  = @( @{
        ts     = "Timestamp: $DateTime"
        fallback    = "New notification for $($Device) - $($Name) is $($Status) $($Down). $($LinkDevice)"
        title       = "${Name}: Status $($Status) $($Down)"
        title_link  = $LinkSensor
        color       = $ColorofState
        text        = $Message
    } )
}
$postSlackMessage | Out-File -FilePath slack.log
$postSlackMessage.text  | Out-File -FilePath slack.log -Append
Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage | Out-File -FilePath slack.log -Append

As Webhook it is working much better:

# ----------------------------------------------------------------------------------------------
# Copyright (c) WCOM AB 2015.
# ----------------------------------------------------------------------------------------------
# This source code is subject to terms and conditions of the The MIT License (MIT)
# copy of the license can be found in the LICENSE file at the root of this distribution.
# ----------------------------------------------------------------------------------------------
# You must not remove this notice, or any other, from this software.
# ----------------------------------------------------------------------------------------------
Param(
    [string]$SlackWebHook,
    [string]$SlackChannel,
    [string]$SiteName,
    [string]$Device,
    [string]$Name,
    [string]$Status,
    [string]$Down,
    [string]$DateTime,
    [string]$LinkDevice,
    [string]$Message,
    [string]$ColorofState,
    [string]$LinkSensor 
)
$postSlackMessage = @{
    channel      = $SlackChannel
    unfurl_links = "true"
    username     = $SiteName
    text        = "> *New notification* for <$($LinkDevice)|$($Device)>" 
    attachments  = @( @{
        ts     = "${[System.Math]::Truncate((Get-Date -Date (Get-Date).ToUniversalTime() -UFormat %s))}"
        #pretext     = "Timestamp: $DateTime"
        fallback    = "New notification for $($Device) - $($Name) is $($Status) $($Down). $($LinkDevice)"
        title       = "${Name}: Status $($Status) $($Down)"
        title_link  = $LinkSensor
        color       = $ColorofState
        text        = $Message
    } )
} | ConvertTo-Json -Depth 2
$postSlackMessage | Out-File -FilePath slack.log
$postSlackMessage.text  | Out-File -FilePath slack.log -Append
Invoke-RestMethod -Method Post -ContentType 'application/json' -Uri $SlackWebHook -Body $postSlackMessage  | Out-File -FilePath slack.log -Append

i needed to modify my parameters to get the color for the Webhook to:

-SlackWebHook 'URL' -SlackChannel '#general' -SiteName '%sitename' -Device '%device' -Name '%name' -Status '%status' -Down '%down' -DateTime '%datetime' -LinkDevice '%linkdevice' -Message '%message' -ColorofState '%colorofstate' -LinkSensor '%linksensor'

devlead commented 8 years ago

issue is that the chat.postMessage doesn't take json per default. only attachments are added as an json string to an form post key called attachments

try something like

$attachments =  ConvertTo-Json -Depth 2 @(@{
        ts          = "${[System.Math]::Truncate((Get-Date -Date (Get-Date).ToUniversalTime() -UFormat %s))}"
        fallback    = "New notification for $($Device) - $($Name) is $($Status) $($Down). $($LinkDevice)"
        title       = "${Name}: Status $($Status) $($Down)"
        title_link  = $LinkSensor
        color       = $ColorofState
        text        = $Message
    })

$postSlackMessage = @{
    token        = $SlackToken;
    channel      = $SlackChannel;
    unfurl_links = "true";
    username     = $sitename;
    text         = "> *New notification* for <$($LinkDevice)|$($Device)>" 
    attachments  = $attachments
}