Quantcast
Channel: THWACK: All Content - All Communities
Viewing all articles
Browse latest Browse all 13537

Status of multiple services using one monitor

$
0
0

As we all know, ordinarily SAM will create a single monitor for a single service.  If that service is down, the monitor is down.  If it's running, the monitor is up.

 

I have a situation where a customer wants to monitor two services on the same server, but wants the monitor to only show as down if both services are down.  If either one or the other is up, the monitor should show as up.

 

I'm trying to do this via a Powershell monitor which assigns a numerical value to the status of each service.  If the service is Stopped, it assigns a value of 0.  If the service is running, it assigns a value of 1.  It then adds the two results together.  If the result is >0 then the monitor should be seen as up.  If it's 0, it's classed as down, and that triggers an alert.

 

The script I have is this: (apologies if the format jars any veteran PowerShell coders; I'm still a newbie!)

 

$arrService = Get-Service -ComputerName 'Server1' -DisplayName 'Service1'

$arrService1 = Get-Service -ComputerName 'Server1' -DisplayName 'Service2'

$stat1 = $arrService.status

$stat2 = $arrService1.status

If ($stat1 -eq "Stopped") {$ServStat1 = 0} Else {$ServStat1 = 1}

If ($stat2 -eq "Stopped") {$ServStat2 = 0} Else {$ServStat2 = 1}

$ServTotal = $ServStat1 + $ServStat2

write-host "Statistic.ServTotal : $ServTotal"

write-host "Statistic.ServStat1 : $ServStat1"

write-host "Statistic.ServStat2 : $ServStat2"

 

Frustratingly, if I run this script through PowerShell on my desktop, it runs correctly; ServStat1 comes back as 1 (because Service1 is running).  ServStat2 comes back as 0 (because Service2 is not running).  ServTotal comes back as 1, and this would show the monitor to be up.  However, if I copy and paste the same script into the monitor, both variables are assigned 1, and the total comes back as 2.  The services are custom, and so don't exist on any other server.  I've tried setting the machine to run from as both my main Orion monitor server, the node that the services are run on, and also by pointing it at another machine in the same subnet as the server with the services on it and I get the same result every time.  I'm not entirely surprised by this given the -ComputerName switch in the Get-Service statements, but at least it eliminates that as a possibility.

 

I've seen before some .. inconsistencies.. between the flavour of Powershell that runs in Orion and via the desktop, but this one is baffling me.

 

Can anyone shed any light on what's not right, or perhaps even suggest a more efficient way of achieving the same goal?  In some respects I'd prefer an answer to this quandary as an exercise in PowerShell, apart from anything else!


Viewing all articles
Browse latest Browse all 13537

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>