Fujitsu.Servers.PRIMERGY.Linux.PS.LoggerModule (DeployableResource)

Element properties:

TypeDeployableResource
File NameLoggerModule.psm1
AccessibilityPublic

Source Code:

<DeployableResource ID="Fujitsu.Servers.PRIMERGY.Linux.PS.LoggerModule" Accessibility="Public" FileName="LoggerModule.psm1" HasNullStream="false"/>

File Content: LoggerModule.psm1

#-------------------------------------------------------------------

# Fujitsu
# Copyright 2014-2019 FUJITSU LIMITED
#
# LoggerModule.psm1
#
# Summary:
# Logger module for Linux MP PowerShell scripts
#
#-------------------------------------------------------------------

# set Script variables:
$script:LoggerConfigFile = "SVISCOM-LinLog"
$script:SectionRoot = "root"
$script:SectionCommentSection = "CommentSection"
$script:SectionServerDiscovery = "ServerDiscovery"
$script:SectionComponentsMonitor = "ComponentsMonitor"
$script:SectionPerformanceMonitor = "PerformanceMonitor"
$script:SectionAlertsMonitor = "AlertsMonitor"
$script:SectionLogParser = "LogParser"
$script:SectionCommentHosts = "CommentHosts"
$script:TagDebugMode = "DebugMode"
$script:TagOverWrite = "OverWrite"
$script:TagHostsDiscovery = "HostsForDiscovery"
$script:TagHostsMonitoring = "HostsForMonitoring"

$script:MPScriptVersion = "8.5.0.0"
$script:LogFilePath = "$($Env:TEMP)\SVISCOM\SVISCOM-Lin"
$script:ErrFilePrefix = "PRIMERGYAlertTrace"
$script:DebugMode = $False
$script:DebugFile = $False
$script:OverWrite = $True
$script:DebugForHost = $False
$script:OutsideMP = $False
$script:LogBuffer = $Null
$script:ScriptName = "Script.ps1"

$script:LogFileName = "$script:LogFilePath\PRIMERGYLogTrace.log"
$script:ErrFileName = "$($script:LogFilePath)\$($script:ErrFilePrefix).log"

function Get-BaseName {
<#
.SYNOPSIS
Returns base name for provided path.
.DESCRIPTION
Function returns base name for provided path.
.PARAMETER Path
Path from which will be extracted file name.
.EXAMPLE
$sFile = Get-BaseName -Path "test.txt"
#>
Param ([string] $Path)

try {
return [IO.Path]::GetFileNameWithoutExtension($Path)
} catch {
return $Path
}
}

function Format-CenterString {
<#
.SYNOPSIS
Returns string centered by surrounding it with spaces.
.DESCRIPTION
Function returns string that is centered to provided length by surrounding it with spaces.
.PARAMETER Text
Text to center.
.PARAMETER Length
Length of new centered text. Lentgth must be > Text.Length.
.EXAMPLE
$sText = Format-CenterString -Text "test" -Length 10
#>
Param (
[string] $Text,
[int] $Length
)

$Left = $Length - $Text.Length
$Right = [int]($Left / 2)
$Left = $Left - $Right

return "$(" " * $Left)$Text$(" " * $Right)"
}

function New-LoggerSampleConfig {
<#
.SYNOPSIS
Creates sample configuration file.
.DESCRIPTION
Function creates sample configuration file for all PoSh scripts in management pack.
.EXAMPLE
New-LoggerSampleConfig
#>
$fileOK = $False
$pathOK = $False
$SampleConfigName = "$($script:LogFilePath)\$($script:LoggerConfigFile).sample.xml"
if (! (Test-Path -Path $script:LogFilePath)) {
New-Item -ItemType directory -Path $script:LogFilePath | Out-Null
}
if (Test-Path -Path $script:LogFilePath) {
$pathOK = $True
}

if (Test-Path -Path $SampleConfigName) {
$txt = Get-Content $SampleConfigName
foreach ($obj in $txt) {
if ($obj.contains($script:MPScriptVersion)) {
$fileOK = $True
break
}
}
}

# we write a new sample file every time the MP is changed
if (($pathOK -eq $True) -and ($fileOK -eq $False)) {
if (Test-Path -Path $SampleConfigName) {
Remove-Item -Path $SampleConfigName -Force | Out-Null
}

New-Item -Path $SampleConfigName -ItemType File | Out-Null
Add-Content -Path $SampleConfigName -Value @"
<$SectionRoot>
<$SectionCommentSection>
SVISCOM-Lin Debug XML file for Linux MP Version $($script:MPScriptVersion)
With this file logging for all PowerShell scripts within the management pack can be enabled.

Rename the file, by removing '.sample' part from name, to enable reading this file.

Each of the sections (before '$SectionCommentHosts') in this file represents a script.

DebugMode enables logging (yes) or disables logging (no)
OverWrite defines continuous logging (no) or single script run logging (yes)
</$SectionCommentSection>
<$SectionServerDiscovery>
<$TagDebugMode>yes</$TagDebugMode>
<$TagOverWrite>yes</$TagOverWrite>
</$SectionServerDiscovery>
<$SectionComponentsMonitor>
<$TagDebugMode>yes</$TagDebugMode>
<$TagOverWrite>yes</$TagOverWrite>
</$SectionComponentsMonitor>
<$SectionPerformanceMonitor>
<$TagDebugMode>no</$TagDebugMode>
<$TagOverWrite>yes</$TagOverWrite>
</$SectionPerformanceMonitor>
<$SectionAlertsMonitor>
<$TagDebugMode>no</$TagDebugMode>
<$TagOverWrite>yes</$TagOverWrite>
</$SectionAlertsMonitor>
<$SectionLogParser>
<$TagDebugMode>no</$TagDebugMode>
<$TagOverWrite>yes</$TagOverWrite>
</$SectionLogParser>
<$SectionCommentHosts>
In the sections below single servers for logging can be selected.
Use '$TagHostsDiscovery' to select for which hosts discovery scripts should log information,
use '$TagHostsMonitoring' to select for which hosts monitoring scripts should log information.
As a value enter 'all' (without quote signs) for all servers or particular comma separated hostnames.
See the examples below.
</$SectionCommentHosts>
<$TagHostsDiscovery>all</$TagHostsDiscovery>
<$TagHostsMonitoring>server1,server2,server3</$TagHostsMonitoring>
</$SectionRoot>
"@
}
}

function Read-XmlLogFile {
<#
.SYNOPSIS
Set up debug variables for provided configuration.
.DESCRIPTION
Function reads xml configuration file and set up debug variables for provided configuration parameters.
.PARAMETER Section
Section for PoSh script in configuration file.
.PARAMETER HostTag
Tag for host names in configuration file.
.PARAMETER Hostname
Hostname to check.
.PARAMETER OutsideMP
Script is run outside of management pack.
.EXAMPLE
Read-XmlLogFile -Section "ServerDiscovery" -HostTag "HostsForDiscovery" -Hostname "localhost"
#>
Param (
[string] $Section,
[string] $HostTag,
[string] $Hostname,
[bool] $OutsideMP = $False
)

$XmlFileName = "$($script:LogFilePath)\$($script:LoggerConfigFile).xml"
$ListOfHosts = ""
$obj = ""

$DebugMode = $OutsideMP
$DebugForHost = $OutsideMP
$OverWrite = $True

if ((Test-Path -Path $script:LogFilePath) -and (Test-Path -Path $XmlFileName)) {
[xml]$xmlfile = Get-Content $XmlFileName

$TempBaseName = Get-BaseName $Hostname
$TargetBaseName = $TempBaseName.ToLower()

if ($xmlfile.$SectionRoot.$Section.$TagDebugMode -ne $null) {
$thisVal = $($xmlfile.$SectionRoot.$Section.$TagDebugMode).ToUpper()
if ($thisVal -eq "YES") {
$DebugMode = $True
}
}

if ($DebugMode) {
if ($xmlfile.$SectionRoot.$Section.$TagOverWrite -ne $null) {
$thisVal = $($xmlfile.$SectionRoot.$Section.$TagOverWrite).ToUpper()
if ($thisVal -eq "NO") {
$OverWrite = $False
}
}

if ($xmlfile.$SectionRoot.$HostTag -ne $null) {
$DebugHosts = $($xmlfile.$SectionRoot.$HostTag).ToLower()
} else {
$DebugHosts = ""
}

# Look, if DEBUG shall run for this server.
# There are two possibilities to check: "all" server DEBUG is on or this server is in the list.
if ($DebugHosts -eq "all") {
$DebugForHost = $True
} else {
# look, if this host is in the list of DebugHosts
$ListOfHosts = $($DebugHosts).split(",")
Write-LogDebug "Searching for host: '$PrincipalName' in list of DebugHosts: $DebugHosts"
foreach ($obj in $ListOfHosts) {
$TestBaseName = Get-BaseName "$obj"
if ($TestBaseName -eq $TargetBaseName) {
$DebugForHost = $True
}
}
}
}
}

return $DebugForHost, $OverWrite
}

function New-LogFile {
<#
.SYNOPSIS
Creates new/overwrites existing log file.
.DESCRIPTION
Function creates new or overwrites existing log file - depending on read configuration parameters.
.PARAMETER DebugMode
Is debug mode enabled.
.PARAMETER OverWrite
Overwrites log file.
.PARAMETER LogFileName
Log file name (full path to file).
.PARAMETER LogFilePath
Log file path to create if not exists.
.EXAMPLE
New-LogFile
#>
Param (
[bool] $DebugMode,
[bool] $OverWrite,
[string] $LogFileName,
[string] $LogFilePath
)

if ($DebugMode) {
if (!(Test-Path -Path $LogFilePath)) {
New-Item -ItemType directory -Path $LogFilePath | Out-Null
}

if (Test-Path -Path $LogFileName) {
if ($OverWrite -eq $True) {
Remove-Item -Path $LogFileName -Force | Out-Null
}
}

if (!(Test-Path -Path $LogFileName)) {
New-Item -Path $LogFileName -ItemType File | Out-Null
}

Add-Content -Path $LogFileName -Value @"

********** $(Format-CenterString (Get-Date -Format F) $LogFileName.Length) **********
********** $($LogFileName) **********
********** $(Format-CenterString ("MP Version: $($script:MPScriptVersion)") $LogFileName.Length) **********

"@
}
}

#-------------------------------------------------------------------
# Exportable functions:
#-------------------------------------------------------------------

function New-Logger {
<#
.SYNOPSIS
Creates new logger object for PoSh script.
.DESCRIPTION
Function set up logger object for PowerShell script.
.PARAMETER Section
Section for PoSh script in configuration file.
.PARAMETER HostTag
Tag for host names in configuration file
.PARAMETER ServerName
Host name of server for which script is running.
.PARAMETER ScriptName
Name of PoSh script for log purposes.
.PARAMETER CreateSampleConfig
Switch for creating sample configuration file.
.PARAMETER OutsideManagementPack
Script is run outside of management pack.
.EXAMPLE
$logger = New-Logger -Section "ServerDiscovery" -HostTag "HostsForDiscovery" -ServerName "localhost" -ScriptName "DiscoveryScript.ps1" -CreateSampleConfig
#>
Param (
[string] $Section = $(Throw New-Object System.ArgumentException "Parameter -Section must be set.", "Section"),
[string] $HostTag = $(Throw New-Object System.ArgumentException "Parameter -HostTag must be set.", "HostTag"),
[string] $ServerName = $(Throw New-Object System.ArgumentException "Parameter -ServerName must be set.", "ServerName"),
[string] $ScriptName = "Script.ps1",
[switch] $CreateSampleConfig,
[switch] $OutsideManagementPack
)

$writeDebug = {
<#
.SYNOPSIS
Writes debug message into log file.
.DESCRIPTION
Function writes debug message into log file.
.PARAMETER Text
Debug message to write.
.EXAMPLE
$logger.Debug("This is debug message")
#>
Param (
[string] $Text
)

if ($this.DebugMode) {
if ($this.OutsideMP) {
$this.LogBuffer += $Text
}

if (($this.LogFileName.Length -gt 0) -and (Test-Path -Path $this.LogFileName)) {
$DateTime = Get-Date -format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $this.LogFileName -Value "$DateTime $Text"
}
}
}

$writeError = {
<#
.SYNOPSIS
Writes error message into log file.
.DESCRIPTION
Function writes error message into log file.
.PARAMETER ErrNo
Error number.
.PARAMETER Text
Error message to write.
.EXAMPLE
$logger.Error(404, "Page not found")
#>
Param (
[int] $ErrNo,
[string] $Text
)

$this.Debug($Text)

$DateTime = Get-Date -format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $this.ErrFileName -Value "[$($ErrNo)] [$($DateTime)] [$($this.ScriptName)] $($Text)"
}

$writeBuffer = {
<#
.SYNOPSIS
Outputs log buffer in the pipeline or console.
.DESCRIPTION
If script is run outside of management pack - function outputs script logs in the pipeline or console.
.EXAMPLE
$logger.OutputBuffer()
#>
if ($this.OutsideMP) {
Write-Output $this.LogBuffer
}
}

$DebugMode, $OverWrite = (Read-XmlLogFile $Section $HostTag $ServerName ([bool]$OutsideManagementPack))
$LogFilePrefix = "PRIMERGY" + (Get-BaseName $ScriptName)
$LogFileName = "$($script:LogFilePath)\$($LogFilePrefix)Trace_$($ServerName).log"

$loggerObject = New-Object -TypeName PSObject -Property @{
Type = "Fujitsu.Servers.PRIMERGY.PS.Logger"
Version = $script:MPScriptVersion
OutsideMP = [bool]$OutsideManagementPack
DebugMode = $DebugMode
OverWrite = $OverWrite
LogFileName = $LogFileName
ErrFileName = "$($script:LogFilePath)\$($script:ErrFilePrefix)_$($ServerName).log"
ScriptName = $ScriptName
LogBuffer = @()
}
$loggerObject | Add-Member -MemberType ScriptMethod -Name "Debug" -Value $writeDebug
$loggerObject | Add-Member -MemberType ScriptMethod -Name "Error" -Value $writeError
$loggerObject | Add-Member -MemberType ScriptMethod -Name "OutputBuffer" -Value $writeBuffer

if ($CreateSampleConfig) {
New-LoggerSampleConfig
}

New-LogFile -DebugMode $DebugMode -OverWrite $OverWrite -LogFileName $LogFileName -LogFilePath $script:LogFilePath

return $loggerObject
}

#-------------------------------------------------------------------

Export-ModuleMember -Variable 'SectionServerDiscovery', 'SectionComponentsMonitor', 'SectionConnectionMonitor', 'SectionAlertsMonitor', 'SectionHostServer',
'SectionPerformanceMonitor', 'SectionLogParser', 'TagHostsDiscovery', 'TagHostsMonitoring'
Export-ModuleMember -Function 'New-Logger'