Coffee Break: Proactive Monitoring with Task Scheduler

Here in Part V of  the “Monitoring and Admin” coffee breaks, we will schedule a Windows PowerShell script with these two purposes:

  • Check for heartbeat (check if Dynamics NAV is still running), and
  • Check for a threshold value, for example MEM or CPU exceeding a max value

Customer Story:

We’re a system administrator who is monitoring a large number of Dynamics NAV services. Rather than spending every morning checking that everything is OK, we can schedule checks. Then we only need to take action if something is not working OK. This can also enable a more proactive support, where the system administrator is notified of problems before users notice anything.

Heartbeat

As a simple form for heartbeat: Just invoke Codeunit 1, Function Run, as this function will exist in every Dynamics NAV installation. In good times it will just run. If it doesn’t then we know that something is wrong, and that there is a need to check. Run it like this:

Import-Module 'C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll'
Invoke-NAVCodeunit dynamicsnav90 -CodeunitId 1

Also see further details here (whether you are using SCOM or not):

http://blogs.msdn.com/b/nav/archive/2013/11/14/microsoft-dynamics-nav-2013-r2-management-pack-for-system-center-released.aspx

Now this script will just error on the screen if it fails. So here is a version that will send a notification by email instead:

Import-Module 'C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll'
$EmailList = 'Admin1@CRONUS.com','Admin2@CRONUS.com'
$SMTPServer = 'smtphost.HQ.corp.CRONUS.com'
 
try{
Invoke-NAVCodeunit dynamicsnav90 -CodeunitId 1 -ErrorAction Stop
}
catch{
Send-MailMessage -To $EmailList -Body "NAV Server is not running." -Subject "NST WARNING" -from 'MyAccount@CRONUS.com' -SmtpServer $SMTPServer
}

Schedule it

An easy way to schedule a PowerShell script is, to use Windows Task Scheduler. Open Task Scheduler, then on the right hand side click Create Task. Give it any name, then on the Triggers tab, click New, and set a trigger to run daily and repeat for every 5 minutes. Under Actions, leave it to Run a program, type in PowerShell, then add an argument that is a PowerShell script like the one above, that you saved (.ps1).

Check for Threshold

Let’s say a Dynamics NAV Service frequently increases CPU or MEM usage and at this time the System Administrator needs to take some action. If CPU and MEM is OK then just log it to create a baseline. Here is an example of a script to do this:

$EmailList = 'Admin1@CRONUS.com','Admin2@CRONUS.com'
$SMTPServer = 'smtphost.HQ.corp.CRONUS.com'
$LogFile = 'C:\Temp\NAVLog.txt'

$Result = Get-Process | Where-Object {$_.Name -like "*NAV*" -and $_.Path -like "*90*"}
if ($LogFile) {Get-Date | out-file -FilePath $LogFile -Append ; $Result | out-file -FilePath $LogFile -Append }
 
if ($Result.WorkingSet -gt 700000000)
{
Send-MailMessage -To $EmailList -Body "Memory is too high." -Subject "Memory high" -from 'MyAccount@CRONUS.com' -SmtpServer $SMTPServer
}
 
 
if ($Result.CPU -gt 5)
{
Send-MailMessage -To $EmailList -Body "CPU is too high" -Subject "CPU high" -from 'MyAccount@CRONUS.com' -SmtpServer $SMTPServer
}

As shown above, you can set this to run every 5 minutes via Windows Task Scheduler, then let that do some of the routine work for you.

 

Best regards

Jasminka Thunes, Escalation Engineer Dynamics NAV EMEA

Lars Lohndorf-Larsen, Escalation Engineer Dynamics NAV EMEA

Bas Graaf, Senior Software Engineer Dynamics NAV

 

PS: Going forward, we’ll post these coffee breaks as:

navblog_coffeebreakteam