Script for ESXi CIM Indications Data Source
Fujitsu.Servers.PRIMERGY.ESXi.CIMIndications.DataSource.Script (DataSourceModuleType)
Element properties:
Member Modules:
Overrideable Parameters:
Source Code:
<DataSourceModuleType ID="Fujitsu.Servers.PRIMERGY.ESXi.CIMIndications.DataSource.Script" Accessibility="Public" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="IntervalSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="HostServer" type="xsd:string"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" Selector="$Config/IntervalSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.SimpleScheduler">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<SyncTime/>
</DataSource>
<ProbeAction ID="Script" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagProbe">
<ScriptName>CIMIndications.ps1</ScriptName>
<ScriptBody><Script>
#-------------------------------------------------------------------
# Fujitsu
# Copyright 2015-2019 FUJITSU LIMITED
#
# CIMIndications.ps1
#
# Summary:
# Checks Windows Event Log for new CIM Indications
#
#-------------------------------------------------------------------
param (
[string] $HostServer,
[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.LoggerModule']/Path$", "$PSScriptRoot\Modules\LoggerModule.psm1")[[bool]$OutsideManagementPack]
$AppDataPath = "$($Env:LOCALAPPDATA)\Fujitsu\SVISCOM\SVISCOM-ESXi"
if (! (Test-Path -Path $AppDataPath)) {
New-Item -ItemType directory -Path $AppDataPath | Out-Null
}
function Main {
$script:Logger = New-Logger -Section $SectionHostServer -HostTag $TagHostsMonitoring -ServerName $HostServer -ScriptName "CIMIndications.ps1" -OutsideManagementPack:$OutsideManagementPack
$Logger.Debug("HostServer = $HostServer")
$Logger.Debug("AppDataPath = $AppDataPath")
try {
$script:oAPI = New-SCOMApiObject -OutsideManagementPack:$OutsideManagementPack
$ticks = 0
if (Test-Path -Path "$($AppDataPath)\LastEventLogRead") {
$ticks = Get-Content -Path "$($AppDataPath)\LastEventLogRead"
try {
$ticks = [int64] $ticks
} catch {
$ticks = [int64] 0
}
}
$LastRead = Get-Date $ticks
try {
$LogName = (Get-ItemProperty "$RootRegistryKey\$RegKeyCIMListener" -Name "EventLogName" -Ea Stop).EventLogName
$Source = (Get-ItemProperty "$RootRegistryKey\$RegKeyCIMListener" -Name "EventLogSource" -Ea Stop).EventLogSource
} catch {
$LogName = "SVISCOM-ESXi"
$Source = "CIM Indication"
}
$Logger.Debug("LastRead = $LastRead")
$Logger.Debug("LogName = $LogName")
$Logger.Debug("Source = $Source")
try {
if ($ticks -gt 0) {
$items = Get-EventLog "$LogName" -Source "$Source" -After $LastRead -Ea Stop | Sort-Object Index
} else {
$items = Get-EventLog "$LogName" -Source "$Source" -Ea Stop | Sort-Object Index
}
} catch {
$items = $Null # We can do Count on null object in PoSh
}
$LastRead = (Get-Date).Ticks.ToString()
if ($items.Count -gt 0) {
Out-ItemBags $items
} else {
$Logger.Debug("No new Event Log entries found.")
Out-EmptyBag
}
$LastRead | Out-File -FilePath "$($AppDataPath)\LastEventLogRead"
$Logger.Debug("***** Normal end of script. *****")
} catch {
$Logger.Error(107, "Error: $($_.Exception.Message)")
}
}
function Out-ItemBags {
param (
$items
)
foreach ($item in $items) {
$line = $item.Message.Split(([string[]]"`r`n"), 4, ([StringSplitOptions]"None"))
$info = $line[1].Split("\")
$Logger.Debug("New Event Log entry from $($line[0]): ($([string]$item.TimeGenerated)) ($($line[1])) $($line[2])")
$oPropertyBag = $oAPI.CreatePropertyBag()
$oPropertyBag.AddValue("IPAddress", $line[0])
switch ($info[0]) {
"0" { $oPropertyBag.AddValue("Severity", "INFORMATIONAL") } # INFORMATIONAL
"1" { $oPropertyBag.AddValue("Severity", "WARNING") } # MINOR
"2" { $oPropertyBag.AddValue("Severity", "WARNING") } # MAJOR
"3" { $oPropertyBag.AddValue("Severity", "CRITICAL") } # CRITICAL
}
$oPropertyBag.AddValue("EventId", ($info[1..($info.Length-2)] -join "\"))
$oPropertyBag.AddValue("ErrorCode", $info[$info.Length-1])
$oPropertyBag.AddValue("Date", ([string]$item.TimeGenerated))
$oPropertyBag.AddValue("Name", $line[2])
$oPropertyBag.AddValue("Details", $line[3])
if ($line[2] -imatch "(.*):\s[0-9.]+\s.*") {
$oPropertyBag.AddValue("ID", $matches[1])
} else {
$oPropertyBag.AddValue("ID", $line[2])
}
$oPropertyBag
}
}
function Out-EmptyBag {
$oPropertyBag = $oAPI.CreatePropertyBag()
$oPropertyBag.AddValue("IPAddress", "N/A")
$oPropertyBag.AddValue("Severity", "N/A")
$oPropertyBag.AddValue("EventId", "0")
$oPropertyBag.AddValue("ErrorCode", "0")
$oPropertyBag.AddValue("Date", "N/A")
$oPropertyBag.AddValue("Name", "N/A")
$oPropertyBag.AddValue("Details", "N/A")
$oPropertyBag.AddValue("ID", "N/A")
$oPropertyBag
}
try {
Main
} finally {
$Logger.OutputBuffer()
}
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>HostServer</Name>
<Value>$Config/HostServer$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="Script">
<Node ID="DS"/>
</Node>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>