Skip to content

Instantly share code, notes, and snippets.

@scolton99
Created August 30, 2023 18:36
Show Gist options
  • Save scolton99/71bb4a65d93946e7a85efaa17afcfd17 to your computer and use it in GitHub Desktop.
Save scolton99/71bb4a65d93946e7a85efaa17afcfd17 to your computer and use it in GitHub Desktop.
SolarWinds Rainmeter Widget
[Rainmeter]
Update=1000
Background=#@#Background.png
BackgroundMode=3
BackgroundMargins=0,34,0,14
[Metadata]
Name=Solarwinds
Author=scolton
Information=Keeps track of SW servers.
License=Creative Commons BY-NC-SA 3.0
Version=1.0.0
[Variables]
fontName=Trebuchet MS
textSize=8
colorBar=235,170,0,255
colorText=255,255,255,205
hostName=
username=
password=
nodeName=
nodeDisplayName=
status=Refreshing...
muteText=Mute
unmuteText=Unmute
; ----------------------------------
; MEASURES return some kind of value
; ----------------------------------
[MeasureSWQL1]
; This measure returns the total disk space
Measure=Plugin
Plugin=RunCommand
Parameter="pwsh -c $Password = $(ConvertTo-SecureString \"#password#\" -AsPlainText -Force); #CURRENTPATH#getAlertSuppressionStatus.ps1 -OrionHost \"#hostName#\" -Username \"#username#\" -Password $Password -NodeName \"#nodeName#\""
State=Hide
FinishAction=[!SetVariable "status" "[MeasureSWQL1]"][!UpdateMeter meterOutput]
OutputType=UTF8
UpdateDivider=60
OnUpdateAction=[!SetVariable "status" "Refreshing..."][!CommandMeasure MeasureSWQL1 "Run"]
IfMatch=Muted
IfMatchAction=!Log "Muted!"
IfNotMatchAction=!Log "Not muted!"
[MeasureMute]
Measure=Plugin
Plugin=RunCommand
Parameter="pwsh -c $Password = $(ConvertTo-SecureString \"#password#\" -AsPlainText -Force); #CURRENTPATH#setMuteAlerts.ps1 -OrionHost \"#hostName#\" -Username \"#username#\" -Password $Password -NodeName \"#nodeName#\" -Mute"
State=Hide
OutputType=UTF8
FinishAction=[!UpdateMeasure MeasureSWQL1][!SetVariable "muteText" "Mute"]
[MeasureUnmute]
Measure=Plugin
Plugin=RunCommand
Parameter="pwsh -c $Password = $(ConvertTo-SecureString \"#password#\" -AsPlainText -Force); #CURRENTPATH#setMuteAlerts.ps1 -OrionHost \"#hostName#\" -Username \"#username#\" -Password $Password -NodeName \"#nodeName#\""
State=Hide
OutputType=UTF8
FinishAction=[!UpdateMeasure MeasureSWQL1][!SetVariable "unmuteText" "Unmute"]
; ----------------------------------
; STYLES are used to "centralize" options
; ----------------------------------
[styleTitle]
StringAlign=Center
StringCase=Upper
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,50
FontColor=#colorText#
FontFace=#fontName#
FontSize=10
AntiAlias=1
ClipString=1
[styleLeftText]
StringAlign=Left
; Meters using styleLeftText will be left-aligned.
StringCase=None
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1
[styleRightText]
StringAlign=Right
StringCase=None
StringStyle=Bold
StringEffect=Shadow
FontEffectColor=0,0,0,20
FontColor=#colorText#
FontFace=#fontName#
FontSize=#textSize#
AntiAlias=1
ClipString=1
[styleBar]
BarColor=#colorBar#
BarOrientation=HORIZONTAL
SolidColor=255,255,255,15
; ----------------------------------
; METERS display images, text, bars, etc.
; ----------------------------------
[meterTitle]
MeterStyle=styleTitle
Meter=String
Text=Solarwinds
X=100
Y=12
[meterOutput]
Meter=String
MeasureName=MeasureSWQL1
Text=#nodeDisplayName#: #status#
X=10
Y=40
MeterStyle=styleLeftText
DynamicVariables=1
[meterMute]
Meter=String
LeftMouseUpAction=[!CommandMeasure MeasureMute "Run"][!SetVariable "muteText" "Muting..."]
W=105
X=52.5
Y=60
StringAlign=Center
Text=#muteText#
FontColor=#colorText#
FontFace=#fontName#
StringStyle=Bold
DynamicVariables=1
[meterUnmute]
Meter=String
Text=#unmuteText#
LeftMouseUpAction=[!CommandMeasure MeasureUnmute "Run"][!SetVariable "unmuteText" "Unmuting..."]
X=157.5
W=105
Y=60
Text=Unmute
StringAlign=Center
FontColor=#colorText#
FontFace=#fontName#
StringStyle=Bold
DynamicVariables=1
param ([string] $OrionHost, [string] $Username, [SecureString] $Password, [string] $NodeName, [switch] $Debug)
Import-Module ".\SWCommon.psm1" -Force
$Credential = New-Object System.Management.Automation.PSCredential ($Username, $Password)
$Connection = New-SwisConnection -HostName $OrionHost -Credential $Credential -UseSSL $True -ValidateSSL $False -Port 17778
$Query = "SELECT CASE WHEN AZ.ID IS NOT NULL THEN 'Muted' ELSE 'Unmuted' END AS AlertStatus FROM Orion.Nodes N LEFT JOIN Orion.AlertSuppression AZ on AZ.EntityURI = N.URI WHERE N.NodeName = '${NodeName}'"
try {
$SwisResponse = Search-Swis -Connection $Connection -SWQL $Query
Write-Host $SwisResponse[0].AlertStatus
} catch {
if ($Debug) {
Write-Host $_
} else {
Write-Host "Network error"
}
}
param ([string] $OrionHost, [string] $Username, [SecureString] $Password, [string] $NodeName, [switch] $Mute, [switch] $Debug)
Import-Module '.\SWCommon.psm1' -Force
$Credential = New-Object System.Management.Automation.PSCredential ($Username, $Password)
$Connection = New-SwisConnection -HostName $OrionHost -Credential $Credential -UseSSL $True -ValidateSSL $False -Port 17778
$Query = "SELECT Uri FROM Orion.Nodes WHERE NodeName = '$NodeName'"
$NodeUriResponse = Search-Swis -Connection $Connection -SWQL $Query
$NodeUri = $NodeUriResponse[0].Uri
$Verb = if ($Mute) { "SuppressAlerts" } else { "ResumeAlerts" }
$Entity = "Orion.AlertSuppression"
$Payload = @{
entityUris = @($NodeUri)
}
try {
Invoke-SwisVerb -Connection $Connection -Entity $Entity -Verb $Verb -Payload $Payload
Write-Host success
} catch {
if ($Debug) {
Write-Host $_
} else {
Write-Host failure
}
}
class SwisConnection {
[String] $HostName
[Bool] $UseSSL
[Bool] $ValidateSSL
[Int] $Port
[PSCredential] $Credential
SwisConnection(
[String] $HostName,
[Bool] $UseSSL,
[Bool] $ValidateSSL,
[Int] $Port,
[PSCredential] $Credential
) {
$this.HostName = $HostName
$this.UseSSL = $UseSSL
$this.Credential = $Credential
$this.ValidateSSL = $ValidateSSL
$this.Port = $Port
}
}
function Get-BaseWebRequestParams([SwisConnection] $Connection) {
return @{
Authentication = "Basic"
SkipCertificateCheck = !$Connection.ValidateSSL
Credential = $Connection.Credential
}
}
function Get-BaseURL([SwisConnection] $Connection) {
$Proto = $(if ($Connection.UseSSL) {
"https"
} else {
"http"
})
return "${Proto}://$($Connection.HostName):$($Connection.Port)/SolarWinds/InformationService/v3/Json"
}
function Complete-SwisInvocation([hashtable] $WebRequestParams) {
try {
$SWISResponse = Invoke-WebRequest @WebRequestParams -SkipHttpErrorCheck
} catch {
throw "Network error: $($_)"
}
if ($SWISResponse.StatusCode -ge 400) {
throw "Request error: $($SWISResponse.Content)"
}
$Content = $SWISResponse.Content
if (!$Content -or ($Content -eq "null")) {
return $null
}
try {
$ResponseObj = ConvertFrom-Json -InputObject $Content
return $ResponseObj.results
} catch {
throw "Error decoding JSON response: $($Content)"
}
}
function Search-Swis([SwisConnection] $Connection, [string] $SWQL) {
$WebRequestParams = Get-BaseWebRequestParams -Connection $Connection
$WebRequestParams['Uri'] = "$(Get-BaseURL -Connection $Connection)/Query"
$WebRequestParams['Body'] = @{ query = $SWQL }
return Complete-SwisInvocation $WebRequestParams
}
function Invoke-SwisVerb (
[SwisConnection] $Connection,
[string] $Entity,
[string] $Verb,
[hashtable] $Payload
) {
$WebRequestParams = Get-BaseWebRequestParams -Connection $Connection
$WebRequestParams['Uri'] = "$(Get-BaseURL -Connection $Connection)/Invoke/$Entity/$Verb"
$WebRequestParams['Method'] = 'POST'
$WebRequestParams['Body'] = ConvertTo-Json -InputObject $Payload
$WebRequestParams['Headers'] = @{
'Content-Type' = 'application/json'
}
return Complete-SwisInvocation $WebRequestParams
}
function New-SwisConnection (
[String] $HostName,
[Bool] $UseSSL,
[Bool] $ValidateSSL,
[Int] $Port,
[PSCredential] $Credential
) {
return [SwisConnection]::new($HostName, $UseSSL, $ValidateSSL, $Port, $Credential)
}
Export-ModuleMember -Function Invoke-SwisVerb, Search-Swis, New-SwisConnection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment