2019-07-18 11:54:39 -04:00
function New-IcingaCheckPackage ( )
{
2021-05-07 08:38:10 -04:00
param (
[ string ] $Name = '' ,
[ switch ] $OperatorAnd = $FALSE ,
[ switch ] $OperatorOr = $FALSE ,
[ switch ] $OperatorNone = $FALSE ,
2020-10-15 09:17:13 -04:00
[ int ] $OperatorMin = -1 ,
[ int ] $OperatorMax = -1 ,
[ array ] $Checks = @ ( ) ,
[ int ] $Verbose = 0 ,
[ switch ] $IgnoreEmptyPackage = $FALSE ,
2021-05-07 08:38:10 -04:00
[ switch ] $Hidden = $FALSE ,
[ switch ] $AddSummaryHeader = $FALSE
2019-07-18 11:54:39 -04:00
) ;
2021-05-07 08:38:10 -04:00
$IcingaCheckPackage = New-IcingaCheckBaseObject ;
$IcingaCheckPackage . Name = $Name ;
$IcingaCheckPackage . __ObjectType = 'IcingaCheckPackage' ;
$IcingaCheckPackage . __SetHidden ( $Hidden ) ;
$IcingaCheckPackage . __SetVerbosity ( $Verbose ) ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorAnd' -Value $OperatorAnd ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorOr' -Value $OperatorOr ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorNone' -Value $OperatorNone ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorMin' -Value $OperatorMin ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'OperatorMax' -Value $OperatorMax ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'IgnoreEmptyPackage' -Value $IgnoreEmptyPackage ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name 'AddSummaryHeader' -Value $AddSummaryHeader ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__Checks' -Value @ ( ) ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__OkChecks' -Value @ ( ) ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__WarningChecks' -Value @ ( ) ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__CriticalChecks' -Value @ ( ) ;
$IcingaCheckPackage | Add-Member -MemberType NoteProperty -Name '__UnknownChecks' -Value @ ( ) ;
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Name 'ValidateOperators' -Value {
if ( $this . OperatorMin -ne -1 ) {
return ;
2019-07-19 13:38:09 -04:00
}
2019-08-19 12:07:51 -04:00
2021-05-07 08:38:10 -04:00
if ( $this . OperatorMax -ne -1 ) {
2019-07-23 05:42:16 -04:00
return ;
}
2021-05-07 08:38:10 -04:00
if ( $this . OperatorNone -ne $FALSE ) {
return ;
2019-07-22 02:37:54 -04:00
}
2019-07-18 11:54:39 -04:00
2021-05-07 08:38:10 -04:00
if ( $this . OperatorOr -ne $FALSE ) {
2019-07-23 05:42:16 -04:00
return ;
}
2021-05-07 08:38:10 -04:00
# If no operator is set, use And as default
$this . OperatorAnd = $TRUE ;
2019-07-22 07:15:44 -04:00
}
2021-05-07 08:38:10 -04:00
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Name 'AddCheck' -Value {
param ( [ array ] $Checks ) ;
2019-10-30 13:17:39 -04:00
2021-05-07 08:38:10 -04:00
if ( $null -eq $Checks -Or $Checks . Count -eq 0 ) {
return ;
2019-10-30 13:17:39 -04:00
}
2021-05-07 08:38:10 -04:00
foreach ( $check in $Checks ) {
$check . __SetIndention ( $this . __NewIndention ( ) ) ;
$check . __SetCheckOutput ( ) ;
$check . __SetVerbosity ( $this . __GetVerbosity ( ) ) ;
$check . __SetParent ( $this ) ;
$this . __Checks + = $check ;
2019-10-30 13:17:39 -04:00
}
}
2021-05-07 08:38:10 -04:00
# Override shared function
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__SetIndention' -Value {
param ( $Indention ) ;
2019-09-28 15:45:59 -04:00
2021-05-07 08:38:10 -04:00
$this . __Indention = $Indention ;
2019-09-28 15:45:59 -04:00
2021-05-07 08:38:10 -04:00
foreach ( $check in $this . __Checks ) {
$check . __SetIndention ( $this . __NewIndention ( ) ) ;
2019-09-28 15:45:59 -04:00
}
}
2021-05-07 08:38:10 -04:00
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Name '__SetCheckState' -Value {
param ( $State ) ;
2019-07-18 11:54:39 -04:00
2021-05-07 08:38:10 -04:00
if ( $this . __GetCheckState ( ) -lt $State ) {
$this . __CheckState = $State ;
$this . __SetCheckOutput ( ) ;
2019-07-19 13:38:09 -04:00
}
2021-05-07 08:38:10 -04:00
}
2019-07-18 11:54:39 -04:00
2021-05-07 08:38:10 -04:00
# Override shared function
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__SetCheckOutput' -Value {
param ( $PluginOutput ) ;
$UnknownChecks = '' ;
$CriticalChecks = '' ;
$WarningChecks = '' ;
$CheckSummary = [ System.Text.StringBuilder ] :: New ( ) ;
[ bool ] $HasContent = $FALSE ;
# Only apply this to the top parent package
if ( $this . __GetIndention ( ) -eq 0 ) {
if ( $this . __UnknownChecks . Count -ne 0 ) {
$UnknownChecks = [ string ] :: Format ( ' [UNKNOWN] {0}' , ( [ string ] :: Join ( ', ' , $this . __UnknownChecks ) ) ) ;
$HasContent = $TRUE ;
$CheckSummary . Append (
[ string ] :: Format ( ' {0} Unknown' , $this . __UnknownChecks . Count )
) | Out-Null ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
if ( $this . __CriticalChecks . Count -ne 0 ) {
$CriticalChecks = [ string ] :: Format ( ' [CRITICAL] {0}' , ( [ string ] :: Join ( ', ' , $this . __CriticalChecks ) ) ) ;
$HasContent = $TRUE ;
$CheckSummary . Append (
[ string ] :: Format ( ' {0} Critical' , $this . __CriticalChecks . Count )
) | Out-Null ;
2020-10-15 09:17:13 -04:00
}
2021-05-07 08:38:10 -04:00
if ( $this . __WarningChecks . Count -ne 0 ) {
$WarningChecks = [ string ] :: Format ( ' [WARNING] {0}' , ( [ string ] :: Join ( ', ' , $this . __WarningChecks ) ) ) ;
$HasContent = $TRUE ;
$CheckSummary . Append (
[ string ] :: Format ( ' {0} Warning' , $this . __WarningChecks . Count )
) | Out-Null ;
2019-07-18 11:54:39 -04:00
}
}
2021-05-07 08:38:10 -04:00
if ( [ string ] :: IsNullOrEmpty ( $this . __ErrorMessage ) -eq $FALSE ) {
$HasContent = $TRUE ;
}
if ( $this . __OkChecks . Count -ne 0 ) {
$CheckSummary . Append (
[ string ] :: Format ( ' {0} Ok' , $this . __OkChecks . Count )
) | Out-Null ;
$HasContent = $TRUE ;
2019-07-19 14:02:09 -04:00
}
2021-05-07 08:38:10 -04:00
if ( $this . AddSummaryHeader -eq $FALSE ) {
$CheckSummary . Clear ( ) | Out-Null ;
$CheckSummary . Append ( '' ) | Out-Null ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
$this . __CheckOutput = [ string ] :: Format (
'{0} {1}{2}{3}{4}{5}{6}{7}{8}' ,
$IcingaEnums . IcingaExitCodeText [ $this . __GetCheckState ( ) ] ,
$this . Name ,
( & { if ( $HasContent ) { return ':' ; } else { return '' ; } } ) ,
$CheckSummary . ToString ( ) ,
( [ string ] :: Format ( '{0}{1}' , ( & { if ( $this . __ErrorMessage . Length -gt 1 ) { return ' ' ; } else { return '' ; } } ) , $this . __ErrorMessage ) ) ,
$UnknownChecks ,
$CriticalChecks ,
$WarningChecks ,
$this . __ShowPackageConfig ( )
) ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
$IcingaCheckPackage | Add-Member -Force -MemberType ScriptMethod -Name 'Compile' -Value {
$this . __OkChecks . Clear ( ) ;
$this . __WarningChecks . Clear ( ) ;
$this . __CriticalChecks . Clear ( ) ;
$this . __UnknownChecks . Clear ( ) ;
$WorstState = $IcingaEnums . IcingaExitCode . Ok ;
$BestState = $IcingaEnums . IcingaExitCode . Ok ;
$NotOkChecks = 0 ;
$OkChecks = 0 ;
if ( $this . __Checks . Count -eq 0 -And $this . IgnoreEmptyPackage -eq $FALSE ) {
$this . __ErrorMessage = 'No checks added to this package' ;
$this . __SetCheckState ( $IcingaEnums . IcingaExitCode . Unknown ) ;
$this . __SetCheckOutput ( ) ;
return ;
2019-07-19 14:02:09 -04:00
}
2021-05-07 08:38:10 -04:00
$this . __Checks = $this . __Checks | Sort-Object -Property Name ;
2019-07-18 11:54:39 -04:00
2021-05-07 08:38:10 -04:00
# Loop all checks to understand the content of result
foreach ( $check in $this . __Checks ) {
2019-07-18 11:54:39 -04:00
2021-05-07 08:38:10 -04:00
$check . Compile ( ) ;
2019-07-18 11:54:39 -04:00
2021-05-07 08:38:10 -04:00
if ( $WorstState -lt $check . __GetCheckState ( ) ) {
$WorstState = $check . __GetCheckState ( ) ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
if ( $BestState -gt $check . __GetCheckState ( ) ) {
$BestState = $check . __GetCheckState ( ) ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
[ string ] $CheckStateOutput = [ string ] :: Format (
'{0}{1}' ,
$check . __GetName ( ) ,
$check . __GetHeaderOutputValue ( )
) ;
2019-10-30 13:17:39 -04:00
2021-05-07 08:38:10 -04:00
switch ( $check . __GetCheckState ( ) ) {
$IcingaEnums . IcingaExitCode . Ok {
$this . __OkChecks + = $CheckStateOutput ;
$OkChecks + = 1 ;
break ;
} ;
$IcingaEnums . IcingaExitCode . Warning {
$this . __WarningChecks + = $CheckStateOutput ;
$NotOkChecks + = 1 ;
break ;
} ;
$IcingaEnums . IcingaExitCode . Critical {
$this . __CriticalChecks + = $CheckStateOutput ;
$NotOkChecks + = 1 ;
break ;
} ;
$IcingaEnums . IcingaExitCode . Unknown {
$this . __UnknownChecks + = $CheckStateOutput ;
$NotOkChecks + = 1 ;
break ;
} ;
}
2019-09-19 05:55:39 -04:00
}
2021-05-07 08:38:10 -04:00
if ( $this . OperatorAnd -And $NotOkChecks -ne 0 ) {
$this . __SetCheckState ( $WorstState ) ;
} elseif ( $this . OperatorOr -And $OkChecks -eq 0 ) {
$this . __SetCheckState ( $WorstState ) ;
} elseif ( $this . OperatorNone -And $OkChecks -ne 0 ) {
$this . __SetCheckState ( $WorstState ) ;
} elseif ( $this . OperatorMin -ne -1 ) {
if ( -Not ( $this . __Checks . Count -eq 0 -And $this . IgnoreEmptyPackage -eq $TRUE ) ) {
if ( $this . OperatorMin -gt $this . __Checks . Count ) {
$this . __SetCheckState ( $IcingaEnums . IcingaExitCode . Unknown ) ;
$this . __ErrorMessage = [ string ] :: Format ( 'Minium check count ({0}) is larger than number of assigned checks ({1})' , $this . OperatorMin , $this . __Checks . Count ) ;
} elseif ( $OkChecks -lt $this . OperatorMin ) {
$this . __SetCheckState ( $WorstState ) ;
$this . __ErrorMessage = '' ;
2020-08-13 11:54:00 -04:00
}
2019-10-30 13:17:39 -04:00
}
2021-05-07 08:38:10 -04:00
} elseif ( $this . OperatorMax -ne -1 ) {
if ( -Not ( $this . __Checks . Count -eq 0 -And $this . IgnoreEmptyPackage -eq $TRUE ) ) {
if ( $this . OperatorMax -gt $this . __Checks . Count ) {
$this . __SetCheckState ( $IcingaEnums . IcingaExitCode . Unknown ) ;
$this . __ErrorMessage = [ string ] :: Format ( 'Maximum check count ({0}) is larger than number of assigned checks ({1})' , $this . OperatorMax , $this . __Checks . Count ) ;
} elseif ( $OkChecks -gt $this . OperatorMax ) {
$this . __SetCheckState ( $WorstState ) ;
$this . __ErrorMessage = '' ;
2019-09-19 05:56:06 -04:00
}
}
2019-09-13 14:44:15 -04:00
}
2021-05-07 08:38:10 -04:00
$this . __SetCheckOutput ( ) ;
2019-07-19 13:38:09 -04:00
}
2021-05-07 08:38:10 -04:00
# Override default behaviour from shared function
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__SetVerbosity' -Value {
param ( $Verbosity ) ;
# Do nothing for check packages
2019-10-30 13:17:39 -04:00
}
2021-05-07 08:38:10 -04:00
# Override default behaviour from shared function
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__GetCheckOutput' -Value {
2019-07-19 13:38:09 -04:00
2021-05-07 08:38:10 -04:00
if ( $this . __IsHidden ( ) ) {
return ''
} ;
2019-07-24 07:01:50 -04:00
2021-05-07 08:38:10 -04:00
if ( $this . _CanOutput ( ) -eq $FALSE ) {
return '' ;
2019-09-19 05:55:39 -04:00
}
2021-05-07 08:38:10 -04:00
$CheckOutput = [ string ] :: Format (
'{0}{1}' ,
( New-StringTree -Spacing $this . __GetIndention ( ) ) ,
$this . __CheckOutput
2019-07-19 13:38:09 -04:00
) ;
2021-05-07 08:38:10 -04:00
foreach ( $check in $this . __Checks ) {
if ( $check . __IsHidden ( ) ) {
continue ;
2019-07-19 13:38:09 -04:00
} ;
2021-05-07 08:38:10 -04:00
if ( $check . _CanOutput ( ) -eq $FALSE ) {
continue ;
2019-07-19 13:38:09 -04:00
}
2021-05-07 08:38:10 -04:00
$CheckOutput = [ string ] :: Format (
'{0}{1}{2}' ,
$CheckOutput ,
" `n " ,
$check . __GetCheckOutput ( )
) ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
return $CheckOutput ;
}
2019-10-30 13:17:39 -04:00
2021-05-07 08:38:10 -04:00
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__HasNotOkChecks' -Value {
2019-10-30 13:17:39 -04:00
2021-05-07 08:38:10 -04:00
if ( $this . __WarningChecks . Count -ne 0 ) {
return $TRUE ;
2019-10-30 13:17:39 -04:00
}
2021-05-07 08:38:10 -04:00
if ( $this . __CriticalChecks . Count -ne 0 ) {
return $TRUE ;
2019-10-30 13:17:39 -04:00
}
2021-05-07 08:38:10 -04:00
if ( $this . __UnknownChecks . Count -ne 0 ) {
return $TRUE ;
2019-07-19 14:02:09 -04:00
}
2019-10-30 13:17:39 -04:00
2021-05-07 08:38:10 -04:00
return $FALSE ;
}
2019-10-30 13:17:39 -04:00
2021-05-07 08:38:10 -04:00
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__ShowPackageConfig' -Value {
if ( $this . __GetVerbosity ( ) -lt 3 ) {
return '' ;
2019-07-18 11:54:39 -04:00
}
2019-10-30 13:17:39 -04:00
2021-05-07 08:38:10 -04:00
if ( $this . OperatorAnd ) {
return ' (All must be [OK])' ;
}
if ( $this . OperatorOr ) {
return ' (Atleast one must be [OK])' ;
}
if ( $this . OperatorMin -ne -1 ) {
return ( [ string ] :: Format ( ' (Atleast {0} must be [OK])' , $this . OperatorMin ) ) ;
}
if ( $this . OperatorMax -ne -1 ) {
return ( [ string ] :: Format ( ' (Not more than {0} must be [OK])' , $this . OperatorMax ) ) ;
2019-10-30 13:17:39 -04:00
}
2021-05-07 08:38:10 -04:00
return '' ;
2019-07-19 13:38:09 -04:00
}
2019-07-18 11:54:39 -04:00
2021-05-07 08:38:10 -04:00
# Override default behaviour from shared function
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__GetPerformanceData' -Value {
2019-09-13 14:44:15 -04:00
[ string ] $perfData = '' ;
2021-05-07 08:38:10 -04:00
[ hashtable ] $CollectedPerfData = @ { } ;
2019-09-13 14:44:15 -04:00
# At first lets collect all perf data, but ensure we only add possible label duplication only once
2021-05-07 08:38:10 -04:00
foreach ( $check in $this . __Checks ) {
$data = $check . __GetPerformanceData ( ) ;
2019-09-13 14:44:15 -04:00
if ( $null -eq $data -Or $null -eq $data . label ) {
continue ;
}
if ( $CollectedPerfData . ContainsKey ( $data . label ) ) {
continue ;
}
2019-09-28 15:45:59 -04:00
$CollectedPerfData . Add ( $data . label , $data ) ;
2019-09-13 14:44:15 -04:00
}
2021-05-07 08:38:10 -04:00
return @ {
'label' = $this . Name ;
'perfdata' = $CollectedPerfData ;
'package' = $TRUE ;
}
}
# __GetTimeSpanThreshold(0, 'Core_30_20', 'Core_30')
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name '__GetTimeSpanThreshold' -Value {
param ( $TimeSpanLabel , $Label ) ;
foreach ( $check in $this . __Checks ) {
$Result = $check . __GetTimeSpanThreshold ( $TimeSpanLabel , $Label ) ;
2019-09-13 14:44:15 -04:00
2021-05-07 08:38:10 -04:00
if ( [ string ] :: IsNullOrEmpty ( $Result ) -eq $FALSE ) {
return $Result ;
}
2019-09-13 14:44:15 -04:00
}
return @ {
2021-05-07 08:38:10 -04:00
'Warning' = '' ;
'Critical' = '' ;
} ;
}
# Override shared function
$IcingaCheckPackage | Add-Member -MemberType ScriptMethod -Force -Name 'HasChecks' -Value {
if ( $this . __Checks . Count -eq 0 ) {
return $FALSE ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
return $TRUE ;
2019-07-18 11:54:39 -04:00
}
2021-05-07 08:38:10 -04:00
$IcingaCheckPackage . ValidateOperators ( ) ;
$IcingaCheckPackage . AddCheck ( $Checks ) ;
2019-07-19 13:38:09 -04:00
2021-05-07 08:38:10 -04:00
return $IcingaCheckPackage ;
2019-07-18 11:54:39 -04:00
}