<#
Ester Niclos Ferreras

Returns nomber of web site connections


UNKNOWN - not found
OK - connections
warning - current connections greater than warning value
critical - current connection greater than critical value

#>


#
# Shell arguments
#
[CmdletBinding()]
Param(
   [Parameter(Mandatory=$True,Position=1)]
   [string]$website,
   [Parameter(Mandatory=$True,Position=2)]
   [int]$warning_value,
   [Parameter(Mandatory=$True,Position=3)]
   [int]$critical_value
   )

$bReturnOK       = $TRUE
$bReturnCritical = $FALSE
$bReturnWarning  = $FALSE

$returnStateOK       = 0
$returnStateWarning  = 1
$returnStateCritical = 2
$returnStateUnknown  = 3

$DataTexte   = ""
$strCritical = ""
$strWarning  = ""

$counter = Get-Counter "\Service Web($website)\connexions actives"

# Nagios output

$resultstring='CONNECTIONS UNKNOWN ' + $website + ' not found' 
$exit_code = $UNKNOWN

If ($counter -ne $null) {
    $connections=$counter.CounterSamples.CookedValue
	
	If ($connections -gt $critical_value) {
		$strCritica = 'CONNECTIONS CRITICAL '+ $website +' connections '+ $connections
		$bReturnCritical = $TRUE
	}
	Elseif ($connections -gt $warning_value) {
		$strWarning = 'CONNECTIONS WARNING '+ $website +' connections '+ $connections
		$bReturnWarning = $TRUE
	}
	Else {
		$str= 'CONNECTIONS OK '+ $website +' connections '+ $connections
	}

	$DataTexte = "connections=" + $connections + ';' + $warning_value + ';' + $critical_value + "; "
}

If ($bReturnCritical) {
    write-output $strCritica "|" $DataTexte
    exit $returnStateCritical
}
Elseif ($bReturnWarning) {
    write-output $strWarning "|" $DataTexte
    exit $returnStateWarning
}
Else {
    write-output $str "|" $DataTexte
    exit $returnStateOK
}
