forked from OneIdentity/safeguard-ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStart-SafeguardMonitor.ps1
117 lines (114 loc) · 4.63 KB
/
Start-SafeguardMonitor.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
[CmdletBinding(DefaultParameterSetName="Text")]
Param (
[Parameter(Mandatory=$true,Position=0)]
[string]$Appliance,
[Parameter(Mandatory=$true,Position=1)]
[string]$IdentityProvider,
[Parameter(Mandatory=$true,ParameterSetName="Text",Position=2)]
[string]$Username,
[Parameter(Mandatory=$false,ParameterSetName="Text")]
[SecureString]$Password,
[Parameter(Mandatory=$true,ParameterSetName="Cred",Position=1)]
[PSCredential]$Credential,
[Parameter(Mandatory=$false)]
[switch]$IgnoreSsl,
[Parameter(Mandatory=$false)]
[int]$LongIntervalHours = 12,
[Parameter(Mandatory=$false)]
[switch]$LongIntervalBackup,
[Parameter(Mandatory=$false)]
[switch]$LongIntervalSupportBundle
)
if (-not (Get-Module safeguard-ps)) { Import-Module safeguard-ps }
if (Get-Module safeguard-ps)
{
if ($PSCmdlet.ParameterSetName -eq "Text")
{
Connect-Safeguard -Appliance $Appliance -IdentityProvider $IdentityProvider -Username $Username -Password $Password
}
else
{
Connect-Safeguard -Appliance $Appliance -IdentityProvider $IdentityProvider -Credential $Credential
}
Write-Host -ForegroundColor Green "Connected to Safeguard -- $Appliance"
$script:CurrentState = (Get-SafeguardApplianceAvailability).ApplianceCurrentState
$script:LongIntervalTimestamp = (Get-Date)
Write-Host "Starting state is $($script:CurrentState)"
while ($true)
{
$local:Status = (Get-SafeguardApplianceAvailability)
$local:State = $local:Status.ApplianceCurrentState
Write-Verbose "$($local:Status.CurrentTime) state: $($local:State)"
if ($local:State -ne $script:CurrentState)
{
Write-Host "$($local:Status.CurrentTime) state: $($local:State)"
if ($local:State -eq "Online")
{
Write-Host -ForegroundColor Red "Safeguard is back online"
}
else
{
if ($local:Status.IsMaintenance -and (-not $local:Status.IsQuarantine))
{
Write-Host -ForegroundColor Yellow "Safeguard is going down for maintenance"
}
else
{
Write-Host -ForegroundColor Red "Unexpected state: $($local:State)"
try { Get-SafeguardSupportBundle } catch { Write-Host -ForegroundColor Red "Failed to download support bundle when unexpected state detected" }
# TODO: Send an email or some other form of alert here
}
}
$script:CurrentState = $local:State
}
else
{
if ((((Get-Date) - $script:LongIntervalTimestamp).TotalHours) -ge $LongIntervalHours)
{
Write-Host "Running long interval tasks"
$local:LongIntervalTaskBlock = {
$SafeguardSession
if ($args[1])
{
try
{
$local:BackupInfo = (New-SafeguardBackup -Appliance $args[0].Appliance -AccessToken $args[0].AccessToken -Insecure:$args[0].Insecure)
Export-SafeguardBackup -Appliance $args[0].Appliance -AccessToken $args[0].AccessToken -Insecure:$args[0].Insecure $local:BackupInfo.Id
}
catch
{
Write-Output "Failed to create and download backup for long interval"
$_
}
}
if ($args[2])
{
try
{
Get-SafeguardSupportBundle -Appliance $args[0].Appliance -AccessToken $args[0].AccessToken -Insecure:$args[0].Insecure
}
catch
{
Write-Output "Failed to download support bundle for long interval"
$_
}
}
}
Start-Job -ScriptBlock $local:LongIntervalTaskBlock -ArgumentList $SafeguardSession,[bool]$LongIntervalBackup,[bool]$LongIntervalSupportBundle
$script:LongIntervalTimestamp = (Get-Date)
}
}
Start-Sleep -Seconds 10
$local:Jobs = (Get-Job -State "Completed")
if ($local:Jobs.Count -gt 0)
{
Write-Host "$($local:Jobs.Count) job(s) completed long interval tasks"
$local:Jobs | Receive-Job
$local:Jobs | Remove-Job
}
}
}
else
{
throw "safeguard-ps is not installed"
}