Service status

Introduction

Hi all ! We all have a number of really important TM1 models out there. The models that really need to run, and come up after a service restart. We have periodic model reboots, for example every Saturday evening, but we absolutely need to be certain that the model runs again. Normally it should but for some reason reality could be different.

That's why I wrote a small PowerShell script. It runs periodically, some time after the server restart. It sends an email about the status of the service: running, stopped, stopping, … In case the service would not be running it will be started automatically. Still the email is sent such that you or the other administrators/key persons are aware and can take action.

Obviously, I am aware that automated systems can do this for us. IT likely has such tools too. Or IBM Planning Analytics Workspace could be configured to do so, with thresholds and alerts. Still I believe this code has a value. Not everyone has IT involvement. Not everyone is using PAW. The script can be used in a broader range of situations. Yes, also on your own laptop where some important TM1 models could be operational.

Below you can find my PowerShell code.

#Service to monitor
$ServiceToMonitor = "SERVICE_NAME"

$status = (Get-Service $ServiceToMonitor).Status

# Report if the service is stopped
$From = "[email protected]"
$To = "[email protected]", "[email protected]", "[email protected]"
$SMTPServer = "SMTP-RELAY.XXXXX.XXX"
$Subject = "The $ServiceToMonitor model is: $status"

If ($status -eq "Running") {
    $service = Get-CimInstance -class win32_service | Where-Object name -eq $ServiceToMonitor | select name, processid
    $process = Get-Process | Where-Object ID -EQ $service.processid
    $Mem_Usage = [math]::floor($process.WorkingSet64/1GB)

    $Body = "The $ServiceToMonitor model is running on $($env:computername), the memory usage is $Mem_Usage GB."
    Send-MailMessage -from $from -to $to -subject $Subject -smtpserver $SMTPServer -body $Body }
Else {
    $Body = "Oops, The $ServiceToMonitor model is NOT running on $($env:computername) and will be (re)started now."
    Send-MailMessage -from $from -to $to -subject $Subject -smtpserver $SMTPServer -body $Body
    # Start service
    Start-Service $ServiceToMonitor
    timeout /t 10
}

I guess the code is pretty obvious, at least for those who will need this kind of solution. Paste the code in a new text file and give it the extension .ps1
Then use the Windows Task Scheduler to execute it whenever needed. For the Windows Task Scheduler, with Action:

  1. Choose: Start a program
  2. Program/script: powershell
  3. Add arguments (optional): -file "full path and filename to the ps1 file"

Added bonus, the memory usage in GB is shown as well. If your TM1 model is smaller, then change it in the code to MB (twice). Have a nice weekend !




Homepage

Section contents

About Wim

Wim Gielis is a Business Intelligence consultant and Excel expert

Other links