- 1 year ago
How do I check a website http connectivity by using NSCP in NSClient++?
You can use the check_http
module of NSClient++ to check the connectivity of a website over HTTP. The check_http
module sends an HTTP request to the specified URL and checks the HTTP status code returned by the server.
Here is an example configuration for check_http
module in the nsclient.ini
file:
bash[/settings/external scripts/scripts]
check_http=c:\path\to\check_http.ps1
check_http_timeout=60s
check_http_max_redirects=10
[/settings/external scripts/wrapped scripts]
check_http=cmd /c echo scripts\check_http.ps1; exit($lastexitcode) | powershell.exe -command -
Here, check_http
is the name of the module, check_http.ps1
is the PowerShell script that implements the module, check_http_timeout
sets the timeout for the HTTP request and check_http_max_redirects
sets the maximum number of redirects to follow.
Here is an example implementation of the check_http.ps1
script:
powershellparam(
[Parameter(Mandatory=$true)]
[string]$url
)
try {
$request = Invoke-WebRequest -Uri $url -UseBasicParsing -Method Head -ErrorAction Stop
$status = $request.StatusCode
if ($status -eq 200) {
Write-Host "OK: HTTP status $status"
exit 0
}
else {
Write-Host "WARNING: HTTP status $status"
exit 1
}
}
catch {
Write-Host "CRITICAL: $_"
exit 2
}
This script sends an HTTP HEAD request to the specified URL and checks the HTTP status code returned by the server. If the status code is 200, it returns an OK status. If the status code is not 200, it returns a WARNING status. If there is an error while sending the request, it returns a CRITICAL status.