Created
November 2, 2017 22:40
-
-
Save absolutejam/48cc1776fc29342aa3e9dedb02897d56 to your computer and use it in GitHub Desktop.
Sensu check to ensure iSCSI connections are present & connected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# check-iscsi.ps1 | |
# | |
# DESCRIPTION: | |
# This plugin checks that each ISCSI IQN passed to it is currently connected. | |
# | |
# OUTPUT: | |
# plain text | |
# | |
# PLATFORMS: | |
# Windows | |
# | |
# DEPENDENCIES: | |
# | |
# USAGE: | |
# Please run this check with the '-help' parameter for more info. | |
# | |
# NOTES: | |
# | |
# LICENSE: | |
# Copyright 2016 James Booth <james@absolutejam.co.uk> | |
# Released under the same terms as Sensu (the MIT license); see LICENSE for details. | |
# | |
[CmdletBinding()] | |
Param( | |
[Parameter( | |
Mandatory = $False, | |
Position = 1 | |
)] | |
[string]$IQN, | |
[switch]$Help | |
) | |
if ($Help) { | |
Write-Output @" | |
Checks whether an ISCSI connection is connected. | |
Arguments: | |
-iqn A string with comma separated ISCS iqn values | |
-Help Show help | |
Note: | |
Because this check is designed to make full use of Sensu's check token | |
substitution (https://sensuapp.org/docs/latest/reference/checks.html#check-token-substitution) | |
feature some special considerations have been taken for how the arguments are parsed. | |
Where noted above, these parameters support a list of of comma-separated strings. | |
eg. -param1 'value1','value2' | |
They also accepts accept a single string with comma-separated values. | |
eg. -param1 'value1,value2' | |
...or you can mix & match the two and all of the values will be merged. | |
eg. -param1 'value1','value2,value3' | |
Additionally, any use of the word 'None' will be ignored out. | |
eg. -param1 'value1','None' | |
This is because Sensu's client attribute substitution tokens only work with | |
flat strings, but by supporting both of the above methods, it allows the use | |
of multiple values within a single check/client attribute AND multiple | |
different client/check attributes to be used. Additionally, because 'None' | |
is ignored, it allows the user to provide a default value in the check | |
token substitution, as not to throw an error if the value is missing. | |
Token substitution example: | |
Check attribute 'check_attr' = "alpha,bravo" | |
Client attribute 'client_attr' = "charlie,delta" | |
powershell.exe -File check.ps1 -param1 ":::check_attr|None:::,:::missing_attr|None:::,:::client_attr|None:::" | |
Which expands to: | |
powershell.exe -File check.ps1 -param "alpha,bravo,None,charlie,delta" | |
The script then strips any instances of 'None' and processes the | |
comma-separated string as an array. | |
Example usage: | |
./check-iscsi.ps1 -iqn iqn1,iqn2 | |
./check-services.ps1 -criticalservices :::vars.iscsi.iqn1:::,:::vars.iscsi.iqn2|None::: | |
"@ | |
Exit 0 | |
} | |
Function Format-ParamArray ($Param) { | |
# Flatten the (potential) array into a string | |
$Param = $Param -Join ',' | |
# Strip any instances of 'None' | |
$Param = $Param -Replace 'None[,]?','' | |
# Replace multiple consecutive commas with a single comma | |
$Param = $Param -Replace '[,]{2,}',',' | |
# If the last character is a comma, remove it | |
if ($Param[-1] -eq ',') { | |
$Param = $Param.substring(0, $Param.Length-1) | |
} | |
# Split into an array | |
$Param = $Param -split ',' | |
return $Param | |
} | |
# Setup | |
$CheckName = 'CheckIscsi' | |
$Status = 0 | |
$Output = New-Object -TypeName System.Collections.ArrayList | |
# Merge the cli parameters into a single string | |
$IQNList = Format-ParamArray -Param $IQN | |
if (!($IQN)) { | |
Write-Output "$CheckName OK: No iSCSI connections specified." | |
Exit 0 | |
} | |
$AllSessions = Get-IscsiSession | Select-Object -ExpandProperty TargetNodeAddress | |
$DisconnectedSessions = Get-IscsiSession | | |
Where-Object { $_.IsConnected -ne $True } | | |
Select-Object -ExpandProperty TargetNodeAddress | |
if ($DisconnectedSessions.Length -eq 0) { $DisconnectedSessions = @() } | |
# Check that all of the sessions specified are present | |
$MissingSessions = Compare-Object -ReferenceObject $IQNList -DifferenceObject $AllSessions | | |
Where-Object { $_.SideIndicator -eq '<=' } | | |
Select-Object -ExpandProperty InputObject | |
if ($MissingSessions.Length -eq 0) { $MissingSessions = @() } | |
if ($MissingSessions.Length -gt 0) { | |
$Status = 1 | |
$Message = "$CheckName CRITICAL: The following sessions are missing:`r`n" + ($MissingSessions -join "`r`n") | |
$Output.Add($Message) | Out-Null | |
} | |
# Now, check that all of the sessions that are present are connected | |
$CriticalSessions = Compare-Object -ReferenceObject $IQNList -DifferenceObject $DisconnectedSessions -IncludeEqual | | |
Where-Object { $_.SideIndicator -eq '==' } | | |
Select-Object -ExpandProperty InputObject | |
if ($CriticalSessions.Length -gt 0) { | |
$Status = 2 | |
$Message = "$CheckName CRITICAL: The following iSCSI sessions are disconnected:`r`n" + ($CriticalSessions -join "`r`n") | |
$Output.Insert(0, $Message) | Out-Null | |
} | |
# No issues so far! | |
if ($Output.Length -eq 0) { | |
$Output.Add("$CheckName OK: All iSCSI sessions are connected!") | |
} | |
# Return! | |
Write-Output ($Output -join "`r`n") | |
Exit $Status |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment