Skip to content

Instantly share code, notes, and snippets.

@FlorianHeigl
Last active May 4, 2021 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianHeigl/7eb4c655af1b0f6f3583dd0d2e0f6cc2 to your computer and use it in GitHub Desktop.
Save FlorianHeigl/7eb4c655af1b0f6f3583dd0d2e0f6cc2 to your computer and use it in GitHub Desktop.
check_mk local quota check - extended version
#Requires -Modules @{ ModuleName='FileServerResourceManager'; ModuleVersion='2.0' }
<#
.SYNOPSIS
Check_MK Plugin to monitor Quotas
.DESCRIPTION
Check_MK Plugin to monitor Quotas with FSRM Module.
.EXAMPLE
PS C:\> ./local_check_quota.ps1
0 FSRM-Abteilung-C - Usage:0 MB/200 MB Share:\\\\FSSRV01.test.local\\Abteilung-C Path:C:\\Share\\Abt_C
0 FSRM-Abteilung-B - Usage:0 MB/100 MB Share:\\\\FSSRV01.test.local\\Abteilung-B Path:C:\\Share\\Abt_B
0 FSRM-Abteilung-A - Usage:0 MB/100 MB Share:\\\\FSSRV01.test.local\\Abteilung-A Path:C:\\Share\\Abt_A
normaly this Script is located at the local plugin folder from check_mk
.NOTES
original check by Christopher Pope (Mokkujin), at https://github.com/Mokkujin/Checks-for-Check_MK
(see blogpost at www.hope-this-helps.de)
This is not the original. Extended version by Florian Heigl
* Powershell 3/4 compatible
* only monitors Quotas on active shares
* adds processing performance data
* default data unit changed to GB
* service names modified (ever so slightly)
* graph autoscaling
.LINK
https://gist.github.com/FlorianHeigl/7eb4c655af1b0f6f3583dd0d2e0f6cc2
#>
# Change your Levels here - default is $WarningAt = 0.85 and $CriticalAt = 0.90
[float]$script:WarningAt = 0.90
[float]$script:CriticalAt = 0.99
function script:Get-Rounded
{
<#
.SYNOPSIS
Get-Rounded round a value
.DESCRIPTION
Get-Rounded round a value to a defind unit, the Input must be in Byte
.Parameter RoundTo
The Target Unit to Round .. possible Values are TB/GB/MB/KB default is MB
.Parameter Value
The Value to be calculate
.EXAMPLE
PS C:\> Get-Rounded -Value 123123123
Round the value to Megabyte
.EXAMPLE
PS C:\> Get-Rounded -Value 123123123 -RoundTo GB
Round the value to Gigabyte
#>
Param (
[Parameter(HelpMessage = 'Round to TB / GB / MB / KB ?')]
[string]$RoundTo,
[Parameter(Mandatory, HelpMessage = 'Value to round - Input is in Byte')]
[int64]$Value
)
switch ($RoundTo)
{
KB
{
$Value = ([math]::Round($Value / 1024))
}
MB
{
$Value = ([math]::Round(($Value / 1024) / 1024))
}
GB
{
$Value = ([math]::Round((($Value / 1024) / 1024 ) / 1024))
}
TB
{
$Value = ([math]::Round(((($Value / 1024) / 1024 ) / 1024 ) / 1024))
}
default
{
$RoundTo = 'GB'
$Value = ([math]::Round((($Value / 1024) / 1024 ) / 1024))
}
}
# Unit nicht mehr angeben, sonst kann Perfdata nicht verarbeitet werden.
#[string]$Value = ("$Value {0}" -f $RoundTo)
# Output
$Value
}
function script:Test-Quota
{
<#
.SYNOPSIS
Test-Quota test with FSRM and SMB
.DESCRIPTION
Test-Quota test a quota set on the localserver and try to get the sbmshare name for the checkmk message
.PARAMETER Path
The Path to the configured quota
.PARAMETER Size
The maximum size of the quota
.PARAMETER Usage
actual usage of the quota
.EXAMPLE
PS C:\> Test-Quota -Path $Path -Size $Size -Usage $Usage
Round the value to Megabyte
#>
Param (
[Parameter(Mandatory, HelpMessage = 'Path to Share')]
[string]$Path,
[Parameter(Mandatory, HelpMessage = 'Size of Share')]
[int64]$Size,
[Parameter(Mandatory, HelpMessage = 'Usage of Share')]
[int64]$Usage
)
[int64]$Warning = ([math]::ceiling($script:WarningAt * $Size))
[int64]$Critical = ([math]::ceiling($script:CriticalAt * $Size))
[string]$ShareName = (Get-SmbShare | Where-Object { $_.Path -eq $Path } | Select-Object -ExpandProperty Name)
$PathMessage = ($Path.Replace("\","/"))
$PathMessage = ($PathMessage.Replace(" ","_"))
$PathMessage = ($PathMessage.Replace("$","@"))
If (-not $ShareName)
{
#[string]$ShareName = $script:LocalQuotas
# option 1: das hier ist fuer uniqueness, aber ein name muss auch sein, damit der servicename benutzbar ist
#$script:LocalQuotas++
# option 2: wir koennen den namen ein wenig anpassen
#[string]$p =
#[string]$ShareName = Split-Path -Path $Path -Leaf
#[string]$ShareNameFull = ('Path:{0}' -f $PathMessage)
# option 3: oder wir ueberspringend fuer den moment erstmal alle quotas ohne share drauf
Continue
}
else
{
[string]$ShareNameFull = ('Share: \\{0}\{1} Path: {2}' -f $ServerName, $Sharename, $PathMessage)
}
# Unit: GB
# crit == voll
[int64]$perf_value = (script:Get-Rounded -Value $Usage)
[int64]$perf_warn = (script:Get-Rounded -Value ($Size * $script:WarningAt))
[int64]$perf_crit = (script:Get-Rounded -Value ($Size * $script:CriticalAt))
[int64]$perf_min = 0
[int64]$m = (( $perf_value, $perf_crit, $Size | Measure -Max).Maximum * 1.33 )
$perf_max = (script:Get-Rounded -Value $m)
#TODO: build string here, use later.
# ( ";".join([ perf_alle ] )
switch ($Usage)
{
( { ($_ -lt $Warning) } )
{
Write-Output ('0 "Quota_{0}" usage_gb={1};{2};{3};0;{4} OK - quota sufficient. {5}' -f $PathMessage, $perf_value, $perf_warn, $perf_crit, $perf_max, $ShareNameFull)
}
( { ($_ -ge $Warning) -and ($_ -lt $Critical) } )
{
Write-Output ('1 "Quota_{0}" usage_gb={1};{2};{3};0;{4} WARN - quota almost reached. {5}' -f $PathMessage, $perf_value, $perf_warn, $perf_crit, $perf_max, $ShareNameFull)
}
( { ($_ -ge $Critical) } )
{
Write-Output ('2 "Quota_{0}" usage_gb={1};{2};{3};0;{4} CRIT - quota exceeded. {5}' -f $PathMessage, $perf_value, $perf_warn, $perf_crit, $perf_max, $ShareNameFull)
}
default
{
Write-Output ('3 "Quota_{0}" - Unknown Error')
}
}
}
[string]$ServerName = ([System.Net.Dns]::GetHostByName(($env:computerName)) | Select-Object -ExpandProperty HostName)
$AllSharesWithQuotas = (Get-FSRMQuota)
$CheckMKOutput = New-Object System.Collections.Generic.List[System.Object]
[int64]$script:LocalQuotas = 0
foreach ($Share in $AllSharesWithQuotas)
{
[int64]$Max = $Share.Size
[int64]$Usage = $Share.Usage
$Path = $Share.Path
$result = (script:Test-Quota -Path $Path -Size $Max -Usage $Usage)
$CheckMKOutput.Add($result)
}
$CheckMKOutput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment