-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlackWebhook.ps1
117 lines (109 loc) · 3.99 KB
/
SlackWebhook.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Param(
[string]$SlackWebHook,
[string]$SlackChannel,
[string]$SiteName,
[string]$Device,
[string]$Name,
[string]$Status,
[string]$Down,
[string]$DateTime,
[string]$LinkDevice,
[string]$SensorID,
[string]$PRTGServer,
[string]$APIToken,
[string]$Message,
[switch]$Debug
)
# Debug logging function
function Write-DebugLog {
param([string]$Message)
if ($Debug) {
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Add-Content -Path "C:\prtgslackdebug.log"
}
}
Write-DebugLog "Script started with parameters:"
Write-DebugLog "SlackWebHook: $SlackWebHook"
Write-DebugLog "SlackChannel: $SlackChannel"
Write-DebugLog "Name: $Name"
Write-DebugLog "Status: $Status"
Write-DebugLog "LinkDevice: $LinkDevice"
Write-DebugLog "SensorID: $SensorID"
Write-DebugLog "PRTGServer: $PRTGServer"
# Can set one of the if statements to your needs by commenting one or the other out.
# Slack can get busy if you monitor a lot of servers.
# Do NOT send notifications if Name contains excluded terms
#if (-not ($Name -match "APT|Backup|Update")) {
# Only send notifications if Name contains the terms
if ($Name -match "HTTP|Load") {
Write-DebugLog "Name matches condition, proceeding with alert"
$acknowledgeUrl = "$($PRTGServer)/api/acknowledgealarm.htm?id=$($SensorID)&ackmsg=Acknowledged+via+Slack&targeturl=/sensor.htm?id=$($SensorID)&apitoken=$($APIToken)"
$pauseUrl = "$($PRTGServer)/api/pauseobject.htm?id=$($SensorID)&action=1&apitoken=$($APIToken)"
$resumeUrl = "$($PRTGServer)/api/pauseobject.htm?id=$($SensorID)&action=0&apitoken=$($APIToken)"
Write-DebugLog "Generated URLs:"
Write-DebugLog "Acknowledge URL: $acknowledgeUrl"
Write-DebugLog "Pause URL: $pauseUrl"
Write-DebugLog "Resume URL: $resumeUrl"
$postSlackMessage = @{
channel = $SlackChannel
unfurl_links = "true"
username = "PRTG"
icon_url = "https://prtgicons.paessler.com/prtgx/led_red_big.png"
blocks = @(
@{
type = "section"
text = @{
type = "mrkdwn"
text = "*Time:* $($DateTime)`n*Device:* <$($LinkDevice)|$($Name)>`n*Status:* $($Status) $($Down)`n*Message:* $($Message)"
}
}
@{
type = "actions"
elements = @(
@{
type = "button"
text = @{
type = "plain_text"
text = "Acknowledge"
}
style = "primary"
url = $acknowledgeUrl
}
@{
type = "button"
text = @{
type = "plain_text"
text = "Pause Monitor"
}
style = "danger"
url = $pauseUrl
}
@{
type = "button"
text = @{
type = "plain_text"
text = "Resume Monitor"
}
url = $resumeUrl
}
)
}
)
}
$jsonMessage = $postSlackMessage | ConvertTo-Json -Depth 10
Write-DebugLog "Prepared Slack message JSON:"
Write-DebugLog $jsonMessage
try {
Write-DebugLog "Attempting to send Slack message..."
Invoke-RestMethod -Method Post -ContentType 'application/json' -Uri $SlackWebHook -Body $jsonMessage
Write-DebugLog "Slack message sent successfully"
}
catch {
Write-DebugLog "Error sending Slack message: $($_.Exception.Message)"
Write-DebugLog "Full error details: $($_)"
}
}
else {
Write-DebugLog "Name does not match condition, skipping alert"
}
Write-DebugLog "Script execution completed"