-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlexStatus.ps1
129 lines (110 loc) · 4.66 KB
/
PlexStatus.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
118
119
120
121
122
123
124
125
126
127
128
129
# Enter the path to the config file for Tautulli and Discord
[string]$strPathToConfig = "$PSScriptRoot\config\config.json"
$json = @"
{
"ScriptSettings": {
"PlexStatus": {
"Webhook": "https://discord.com/api/webhooks/<redacted>",
"WebhookAnnounce": "https://discord.com/api/webhooks/<redacted>"
}
}
}
"@
$json | Out-File "$PSScriptRoot\config\config.json.template"
if (test-path $strPathToConfig){
Import-Module PowerHTML
# Script name MUST match what is in config.json under "ScriptSettings"
[string]$strScriptName = 'PlexStatus'
# Parse the config file and assign variables
[object]$objConfig = Get-Content -Path $strPathToConfig -Raw | ConvertFrom-Json
[string]$strDiscordWebhook = $objConfig.ScriptSettings.$strScriptName.Webhook
[string]$strDiscordWebhookAnnounce = $objConfig.ScriptSettings.$strScriptName.WebhookAnnounce
[string]$Messagetext = 'Overall System status'
# Discord Webhook Uri
$Uri = $strDiscordWebhook
$AnnouncementUri = $strDiscordWebhookAnnounce
$StatusPage = 'https://status.plex.tv'
$Loop = $null
# First Status Call
$wc = New-Object System.Net.WebClient
$res = $wc.DownloadString($($StatusPage))
$html = ConvertFrom-Html -Content $res
[string]$Status = ($html.SelectNodes('/html/body/div[1]/div[2]/div[1]/span[1]')).innertext -replace "`n|`r| "
if ($Status -match 'All Systems Operational' ){
$Content = '```DIFF'+"`n"+"! "+$Status+"`n"+'```'
}
Else{
$Content = '```DIFF'+"`n"+"- "+$Status+"`n"+'```'
}
$DCContent = @"
**$Messagetext [plex.tv](https://status.plex.tv):**$Content
"@
#Send to Discord, each time the Script is starting
$UserPayload = [PSCustomObject]@{content = $DCContent}
Invoke-RestMethod -Uri $uri -Body ($UserPayload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'Application/Json'
# Loop for status.plex.tv, only posts to channel when status is 'Bad'
while ($Loop -ne 'Ended'){
$res = $wc.DownloadString($($StatusPage))
$html = ConvertFrom-Html -Content $res
[string]$Status = ($html.SelectNodes('/html/body/div[1]/div[2]/div[1]/span[1]')).innertext -replace "`n|`r| "
if ($Status -match 'All Systems Operational' ){
$Content = '```DIFF'+"`n"+"! "+$Status+"`n"+'```'
$Stat = "Good"
}
Else{
$Content = '```DIFF'+"`n"+"- "+$Status+"`n"+'```'
$Stat = "Bad"
}
$DCContent = @"
**$Messagetext [plex.tv](https://status.plex.tv):**$Content
"@
$ADCContent = @"
**$Messagetext [plex.tv](https://status.plex.tv):**$Content
"@
if ($Stat -eq 'Bad'){
#Send to Discord Announcement Channel
$AUserPayload = [PSCustomObject]@{content = $ADCContent}
Invoke-RestMethod -Uri $AnnouncementUri -Body ($AUserPayload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'Application/Json'
do {
$res = $wc.DownloadString($($StatusPage))
$html = ConvertFrom-Html -Content $res
[string]$Status = ($html.SelectNodes('/html/body/div[1]/div[2]/div[1]/span[1]')).innertext -replace "`n|`r| "
if ($Status -match 'All Systems Operational' ){
$Content = '```DIFF'+"`n"+"! "+$Status+"`n"+'```'
$DCContent = @"
**$Messagetext [plex.tv](https://status.plex.tv):**$Content
"@
$ADCContent = @"
**$Messagetext [plex.tv](https://status.plex.tv):**$Content
"@
#Send to Discord
$UserPayload = [PSCustomObject]@{content = $DCContent}
Invoke-RestMethod -Uri $uri -Body ($UserPayload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'Application/Json'
#Send to Discord Announcement Channel
$AUserPayload = [PSCustomObject]@{content = $ADCContent}
Invoke-RestMethod -Uri $AnnouncementUri -Body ($AUserPayload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'Application/Json'
$Stat = "Good"
}
Else{
$Content = '```DIFF'+"`n"+"- "+$Status+"`n"+'```'
$DCContent = @"
**$Messagetext [plex.tv](https://status.plex.tv):**$Content
"@
#Send to Discord
$UserPayload = [PSCustomObject]@{content = $DCContent}
Invoke-RestMethod -Uri $uri -Body ($UserPayload | ConvertTo-Json -Depth 4) -Method Post -ContentType 'Application/Json'
$Stat = "Bad"
}
sleep 300
}
Until($Stat -eq 'Good')
}
sleep 60
}
}
Else {
write-host "Stopping Container - Config file not Found!" -ForegroundColor Red
Write-Host ""
write-host "Please check config file, and fill out all variables - '/opt/appdata/plexstatus2discord/config/config.json'" -ForegroundColor Yellow
sleep 180
}