<# Version: 0.3 Date: Aug 7, 2020 Author: ivan.stetka@live.com Summary: This is a Nagios NCPA plugin for check windows file/folder size in units KiB, MiB and GiB -N -fileFolderName # full path to file/folder # example folder: 'C:\Program Files (x86)\CA\DSM\logs\' # example file: 'C:\Program Files (x86)\CA\DSM\logs\TRC_AMAGENT_1.log' -w -warn # warning threshold of file/folder size in the corresponding unit, example: 2.3 -c -crit # critical threshold of file/folder size in the corresponding unit, example: 3.4 -u -unit # unit of file/folder size in binary prefix: # B - byte # KiB - kibibyte (1 KiB = 1024 bytes) # MiB - mebibyte (1 MiB = 1024 KiB) # GiB - gibibyte (1 GiB = 1024 MiB) On host side copy plugin to folder 'C:\Program Files (x86)\Nagios\NCPA\plugins\' Nagios example: ./check_ncpa.py -H '' -t '' -M 'plugins/check_filefolder_size.ps1' -a "'C:\Program Files (x86)\CA\DSM\logs\' 20 50 MiB" #> param ( [parameter(Mandatory = $true)] [Alias("N")] [string] $fileFolderName, [parameter(Mandatory = $true)] [Alias("w")] [float] $warn, [parameter(Mandatory = $true)] [Alias("c")] [float] $crit, [parameter(Mandatory = $true)] [Alias("u")] [string] $unit ) $fileFolderName = invoke-expression $fileFolderName $STATES = @{ OK = 0; WARNING = 1; CRITICAL= 2; UNKNOWN = 3 } $UNITS = @{ B = '1'; KiB = '1KB'; MiB = '1MB'; GiB = '1GB' } $sizeInUnit = 0 $fileFolderCount = 0 $countText = '.' function perf { Write-Output ("| $fileFolderType size=$sizeInUnit$unit;$warn;$crit;") } if ($UNITS.$unit) { $fileUnit = ($UNITS.$unit) } else { Write-Output "Incorrect format of unit '$unit'. Accepted units are only B, KiB, MiB or GiB." Exit($STATES.UNKNOWN) } if ($warn -gt $crit) { Write-Output "The warning threshold '$warn' cannot be greater than critical threshold '$crit'." Exit($STATES.UNKNOWN) } $fileFolderCount = (Get-ChildItem $fileFolderName -EA SilentlyContinue).Count if (Test-Path -Path $fileFolderName -PathType Container) { $fileFolderType = "Folder" $countText = "contains $fileFolderCount items." $sizeInUnit = (Get-ChildItem $fileFolderName -R | Measure-Object Length -s).Sum /$fileUnit } elseif (Test-Path -Path $fileFolderName -PathType Leaf){ $fileFolderType = "File" $sizeInUnit = (Get-ChildItem $fileFolderName | Measure-Object Length -s).Sum /$fileUnit } else { Write-Output "CRITICAL: File or folder does not exist. `nItem: $fileFolderName" Exit($STATES.CRITICAL) } if ($fileFolderCount -eq 0 ){ Write-Output "CRITICAL: Folder is empty. `nFolder: '$fileFolderName'" $sizeInUnit = 0 perf Exit($STATES.CRITICAL) } $sizeInUnit = [math]::Round($sizeInUnit,5) $additText = "$fileFolderType size: $sizeInUnit $unit. `n$fileFolderType`: $fileFolderName $countText" if ($sizeInUnit -ge $crit) { Write-Output "CRITICAL: $fileFolderType is bigger than $crit $unit." $additText perf Exit($STATES.CRITICAL) } elseif ($sizeInUnit -ge $warn) { Write-Output "WARNING: $fileFolderType is bigger than $warn $unit." $additText perf Exit($STATES.WARNING) } else { Write-Output "OK: $additText" perf Exit($STATES.OK) } Write-Output "CRITICAL: unexpected plugin error." Exit($STATES.CRITICAL)