Execute powershell script for checking the URL's

Hello Team,
I want to check some url’s in my windows through icinga

I am using “powershell base” command and executing the powershell script which is inside the remote machine

Powershell script is like this

[string] $url = ‘https://myurl.com
function Get-WebStatus($url) {
try {
[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.Method = “HEAD”
[net.httpWebResponse] $res = $req.getResponse()
if ($res.StatusCode -eq “200”) {
echo “OK - Site is UP”
$returnCode=0
}
else
{
write-host “nSite $url is downn” -ForegroundColor red echo "Critical" $returnCode=1 } } catch { write-host "nSite $url (dns issue?). Try again.n" -ForegroundColor red
}
}
Get-WebStatus $url
exit ($returnCode)

  1. How to use the exit codes (checked ) to get the warning and critical
  2. Can somebody please share alternative powershell script for meet the requirement. Iam not powershell expert
  3. Is it possible to check with icinga if this url’s are logged in based url’s and calculate the logged in time
  4. How do i get the graphite graphs for these powershell based checks

Thanks in Advance

1 Like

Hi @sreekanth

the variable $returnCode only “lives” in the function “Get-WebStatus”. You could make the function return the code with return $returnCode and exit the script with the result of the function:

  return $returnCode
}
exit (Get-WebStatus $url)

Also the catch branch does not change the variable $returnCode.

Is there a reason why you use a powershell script? You could also use check_http to check URLs from a linux machine.

What do you mean by ‘logged in based urls’?

You will have to add performance data to the script. Link

Kind regards

1 Like

@ritzgu Thanks for your reply

Also the catch branch does not change the variable $returnCode.
can you please exaplain in brief?

Is there a reason why you use a powershell script? You could also use [check_http](https://www.monitoring-plugins.org/doc/man/check_http.html) to check URLs from a linux machine.
Actually the clients requirement is to monitor developer laptops which they have to logged in to some url’s and monitor their productivity

So what exactly i want is whether they are logged in to these website and track the number of hours their developers worked ? Can we achieve with icinga?

Thanks and regards,
Sreekanth

1 Like

@ritzgu Thanks for your reply

Also the catch branch does not change the variable $returnCode.
can you please exaplain in brief?

catch {
write-host “nSite $url (dns issue?). Try again.n” `
-ForegroundColor red }

contains no reference to set $returnCode - what value do you expect it to have
if this section of the code is executed?

Actually the clients requirement is to monitor developer laptops which they
have to logged in to some url’s and monitor their productivity

Firstly, this strikes me a something better measured from the server end - the
URLs that are being logged into, rather than the machines which are being used
to log in from.

Secondly, though, what is the definition of “productivity”?

Are you simply looking for a measurement of how many hours there are between
someone logging and logging out of a website?

Does it matter what they do in the intervening time?

And, are you certain that they need to log out at all - if not, how do you
know when they stop working?

Antony.

1 Like

Hi @Pooh

1.The reason for using powershell script is because all the hosts and clients(laptops) are of Windows
2.The user basically would like to know if the URL is accessible from the laptop

It is difficult to measure from server end

I know it is difficult to get the output from icinga .Its Ok for the time being I just want to check whether some url’s are working or not

changed my powershell script like below still I am not getting the correct exit codes
also where exactly need to add the performance metrics (graphs are coming not sure what values should give but plugin output showing some errors)

[string] $url = ‘https://myurl.com
function Get-WebStatus($url) {
try {
[net.httpWebRequest] $req = [net.webRequest]::create($url)
$req.Method = “HEAD”
[net.httpWebResponse] $res = $req.getResponse()
if ($res.StatusCode -eq “200”) {
echo “OK - Site is UP”
$returnCode=0
} else {
echo “Critical - Site is DOWN”
$returnCode=1
}
} catch {
echo “DNS issue try again”
$returnCode=2
}
}
return $returnCode | ‘time’=0.1 ‘myperfdatavalue’=1.0
exit (Get-WebStatus $url)

Plugin Output: Site “OK - Site is UP”

When we try to do the same for other URLs
Plugin Output: DNS issue try again. even though the url is opening in the laptop

It will be great if you could share some example powershell script or help me to rewrite the above script

Thanks in advance
Sreekanth

1 Like

Hi @sreekanth

It would really help if you format your script. It is really hard to read.
There are a few things:

  1. It looks like the return statement is outside of the function.
  2. The exit statement requires an Integer (see this Link).
    So return should only return the exit code in your case. You could add the plugin output with Write-Host before returning.
  3. The catch statement will catch any exception. Maybe you could add the exception message to the plugin output.

There are lots of powershell plugins on the icinga exchange which may give you a good idea on developing your own plugins.
Also: If you plan on using the icinga powershell framework, there’s a full step-by-step tutorial on how to write custom plugins for it.

Kind regards

1 Like

@ritzgu

Thanks for sharing the step-by-step-tutorial
Let me try this

sorry I am just started trying power shell scripts

I’ve been playing around with the code and somehow its working (not the graphs) Find my updated one.

url.txt (462 Bytes)
Thanks in advance
Sreekanth