Check Orphaned CIM Subscriptions Writeaction

Fujitsu.Servers.PRIMERGY.ESXi.CheckOrphanedCIMSubscriptions (WriteActionModuleType)

Writeaction module to check Fujitsu PRIMERGY ESXi Server CIM subscriptions

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
PS WriteAction Microsoft.Windows.PowerShellWriteAction Default

Source Code:

<WriteActionModuleType ID="Fujitsu.Servers.PRIMERGY.ESXi.CheckOrphanedCIMSubscriptions" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TargetName" type="xsd:string"/>
</Configuration>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="PS" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>CheckCIMSubscriptions.ps1</ScriptName>
<ScriptBody><Script>
#-------------------------------------------------------------------
# Fujitsu
# Copyright 2015-2019 FUJITSU LIMITED
#
# CheckCIMSubscriptions.ps1
#
# Summary:
# Checks for orphaned subscriptions to CIM Indications on Host Server
#
#-------------------------------------------------------------------

param (
[string] $TargetName,
[string] $CIMUser,
[string] $CIMPass,
[switch] $OutsideManagementPack
)

Import-Module ("$FileResource[Name='Fujitsu.Servers.PRIMERGY.PS.CommonModule']/Path$", "$PSScriptRoot\Modules\CommonModule.psm1" )[[bool]$OutsideManagementPack]
Import-Module ("$FileResource[Name='Fujitsu.Servers.PRIMERGY.PS.CIMClient']/Path$", "$PSScriptRoot\Modules\CIMClient.psm1" )[[bool]$OutsideManagementPack]
Import-Module ("$FileResource[Name='Fujitsu.Servers.PRIMERGY.PS.CIMSubscribe']/Path$", "$PSScriptRoot\Modules\CIMSubscribe.psm1" )[[bool]$OutsideManagementPack]
Import-Module ("$FileResource[Name='Fujitsu.Servers.PRIMERGY.PS.LoggerModule']/Path$", "$PSScriptRoot\Modules\LoggerModule.psm1" )[[bool]$OutsideManagementPack]
Import-Module ("$FileResource[Name='Fujitsu.Servers.PRIMERGY.PS.CIMIndicationsListener']/Path$", "$PSScriptRoot\Modules\CIMIndicationsListener.psm1")[[bool]$OutsideManagementPack]

$InvokerDllPath = ("$FileResource[Name='Fujitsu.Servers.PRIMERGY.AssemblyInvoker']/Path$", "$PSScriptRoot\..\..\Fujitsu.Servers.PRIMERGY.AssemblyInvoker\bin\Debug\Fujitsu.Servers.PRIMERGY.AssemblyInvoker.dll")[[bool]$OutsideManagementPack]
$ClientDllPath = ("$FileResource[Name='Fujitsu.Servers.PRIMERGY.CIMClient']/Path$", "$PSScriptRoot\..\..\Fujitsu.Servers.PRIMERGY.CIMClient\bin\Debug\Fujitsu.Servers.PRIMERGY.CIMClient.dll" )[[bool]$OutsideManagementPack]

# Heartbeats data path
$AppDataPath = "$($Env:LOCALAPPDATA)\Fujitsu\SVISCOM\SVISCOM-ESXi"

function Main {
$script:Logger = New-Logger -Section $SectionHostServer -HostTag $TagHostsMonitoring -ServerName $TargetName -ScriptName "CheckCIMSubscriptions.ps1" -OutsideManagementPack:$OutsideManagementPack

$Logger.Debug("TargetName = $TargetName")
$Logger.Debug("CIMUser = $CIMUser")
$Logger.Debug("AppDataPath = $AppDataPath")

if ($CIMUser -eq "") {
$Logger.Debug("Please add an account to Fujitsu PRIMERGY ESXi Servers CIM Account to allow this MP query CIMOM.")
exit 0
}

$status = Get-RegValue -SubKey $RegKeyCIMListener -Property $RegValueDeployStatus
$Logger.Debug("DeployStatus = $status")

if ($status -ne $CIMListenerDeployStatus[$StatusRemoved]) {
try {
$script:Cim = New-CIMClient -InvokerDllPath $InvokerDllPath -ClientDllPath $ClientDllPath -ClientPort $CIMPort -ClientUser $CIMUser -ClientPass $CIMPass -Logger $Logger
} catch {
$Logger.Error(107, "Error: $($_.Exception.Message)")
exit 107
}

try {
Invoke-SubscriptionChecking
} finally {
$script:Cim.Dispose()
}
}

$Logger.Debug("***** Normal end of script. *****")
}

function Invoke-SubscriptionChecking {
$script:Cim = Add-CIMIndications -CIMClientObject $script:Cim -RegKeyListener "$RootRegistryKey\$RegKeyCIMListener" -RegKeySubscribed "$RootRegistryKey\$RegKeySubscribed"

try {
if (Test-Path -Path "$RootRegistryKey\$RegKeySubscribed") {
$properties = Get-ItemProperty -Path "$RootRegistryKey\$RegKeySubscribed"
$names = Get-Item -Path "$RootRegistryKey\$RegKeySubscribed" | Select-Object -ExpandProperty Property

foreach ($name in $names) {
$targetid = $name
$values = ($properties.$name).Split(";")
$hostname = $values[0]
$IPAddress = $values[1]
$CIMPort = $values[2]
$CheckCert = $values[3]
$CIMListener = $values[4]

$Logger.Debug("Checking $hostname ($($IPAddress):$($CIMPort), CheckCert=$($CheckCert))")

$Cim.IndicationTag = $targetid
$Cim.Address = $IPAddress
$Cim.Port = $CIMPort
$Cim.CheckCert = ($CheckCert -eq "True")

$orphaned = Test-TargetOrphaned -TargetId $targetid
if ($Cim.IsSubscribed()) {
if ($orphaned) {
$Logger.Debug("Host $hostname is subscribed to CIM Indications but is not monitored by SCOM. Unsubscribing...")
$Cim.Unsubscribe()
Remove-ItemProperty -Path "$RootRegistryKey\$RegKeySubscribed" -Name $targetid
} else {
$listener = $Cim.GetListener()
if ($CIMListener -eq $listener) {
$Logger.Debug("Host $hostname is subscribed to CIM Indications")
} elseif ($CIMListener -eq "") {
$Logger.Debug("Skipping $hostname due to GetCIMListenerAddress error")
} else {
$Logger.Debug("Host $hostname is subscribed to CIM Indications with different listener configuration. Resubscribing...")
$Cim.Unsubscribe()
$Cim.Subscribe($hostname, $True)
}
}
} else {
if ($orphaned) {
$Logger.Debug("Host $hostname is not subscribed to CIM Indications and is not monitored by SCOM. Removing invalid registry entry...")
Remove-ItemProperty -Path "$RootRegistryKey\$RegKeySubscribed" -Name $targetid
} else {
$Logger.Debug("Host $hostname is not subscribed to CIM Indications. Subscribing...")
$Cim.Subscribe($hostname, $True)
}
}
}
}
} catch {
$Logger.Error(107, "Error: $($_.Exception.Message)")
}
}

function Test-TargetOrphaned {
param (
[string] $TargetId
)

$ticks = 0

try {
if ((Test-Path -Path "$($AppDataPath)\Heartbeats\$($TargetId)")) {
$ticks = Get-Content -Path "$($AppDataPath)\Heartbeats\$($TargetId)"
$ticks = [int64] $ticks
}

$tspan = New-TimeSpan -Start (Get-Date $ticks)

# Target is orphaned if last heartbeat was more than hour ago
if ($tspan.Hours -gt 0) {
return $True
}
} catch {
$Logger.Debug("IsOrphaned Error: $($_.Exception.Message)")
}

return $False
}

try {
Main
} finally {
$Logger.OutputBuffer()
}
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>TargetName</Name>
<Value>$Config/TargetName$</Value>
</Parameter>
<Parameter>
<Name>CIMUser</Name>
<Value>$RunAs[Name="FTSESXSEED!Fujitsu.Servers.PRIMERGY.ESXiSeed.CIMAccount"]/UserName$</Value>
</Parameter>
<Parameter>
<Name>CIMPass</Name>
<Value>$RunAs[Name="FTSESXSEED!Fujitsu.Servers.PRIMERGY.ESXiSeed.CIMAccount"]/Password$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>1200</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="PS"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>