Service status

Introduction

Hi all ! We all have a number of very 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. It's framed in GB as that is true for the case at hand, but feel free to change to MB if your TM1 server size is smaller.

# Wim Gielis
# Dec. 2020
# https://www.wimgielis.com

#Service to monitor
$Model_Name = "ENTER THIS ONE"
$ServiceToMon = "ENTER THIS ONE"

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

# Report if the services is stopped
$From = "ENTER@THIS.ONE"
$To = "PERSON_1@ENTER-THIS-ONE.COM", "PERSON_2@ENTER-THIS-ONE.COM", "PERSON_3@ENTER-THIS-ONE.COM"
$SMTPServer = "smtp-relay.DOMAIN.NAME"
$Subject = 'The ' + $Model_Name + ' model is: ' + $status

# last admin server log entry for this server
$pattern = '(\d\d\d\d-\d\d-\d\d \d\d:\d\d).*REQUEST TO REGISTER: success - "' + [regex]::escape($Model_Name) + '"'
$AS_date_time = (Select-String -Path "D:\Program Files\ibm\cognos\tm1_64\bin64\tm1admsrv.log" -Pattern $pattern | Select-Object -Last 1).Matches[0].Groups[1].Value

$AS_date = $AS_date_time.SubString(0,10)

if ((Get-Date).Date.tostring("yyyy-MM-dd") -eq $AS_date ) {
    $AS_Registered = 1
    }
else {
    $AS_Registered = 0
}

If ($status -eq "Running") {

    # memory usage
    $service = Get-CimInstance -class win32_service | Where-Object name -eq $ServiceToMon | select name, processid
    $process = Get-Process | Where-Object ID -EQ $service.processid

    $Mem_Usage = [math]::round($process.WorkingSet64 / 1Gb)
    $RAM = Get-WMIObject Win32_PhysicalMemory | Measure -Property capacity -Sum | %{$_.sum/1Gb}
    $Mem_Usage_Perc = [math]::round($process.WorkingSet64 / 1Gb / $RAM * 100)

    $Body = 'The ' + $Model_Name + ' model is RUNNING on ' + $($env:computername) + ', the memory usage is ' + $Mem_Usage + ' Gb (' + $Mem_Usage_Perc + '% of ' + $RAM + ' Gb in total).'
    
    # registration with the Admin server
    If ( $AS_Registered -eq 1) {
    $Body = $Body + 'It    REGISTERED TODAY with the Admin server (last time of registration: {0})' -f $AS_date_time
    }
    Else {
    $Body = $Body + 'CAREFUL !   The ' + $Model_Name + ' model was   NOT   REGISTERED TODAY with the Admin server (last time of registration: {0})' -f $AS_date_time
    }

    Send-MailMessage -from $from -to $to -subject $Subject -smtpserver $SMTPServer -body $Body -BodyAsHTML }

Else {
    $Body = 'Oops, the ' + $Model_Name + ' model is   NOT RUNNING on   ' + $($env:computername) + ' and WILL BE (RE)STARTED   NOW. Make sure to check that the service registers with the Admin server.'
    Send-MailMessage -from $from -to $to -subject $Subject -smtpserver $SMTPServer -body $Body -BodyAsHTML
    # Start service
    Start-Service $ServiceToMon
    timeout /t 10
}

I guess the code is understandable, 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