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

Comments (9)

  1. Hi,
    will that also work with Dynamics NAV 2013 ?

    1. navteam says:

      Hello,
      No, this applies to Dynamics NAV 2016 only.

      1. Cláudio Freitas says:

        Hi guys,

        Can i use the Dynamics NAV 2016 management powershell module to invoke a NavCodeUnit on a Dynamics Nav 2013 or 2009 server?

        Thanks in advance,
        Best regrads.

    2. guido robben says:

      This works in NAV 2015

  2. Hi.
    Very nice post indeed!
    One question: can this monitoring be done using SCOM (System Center Operations Manager)?

    Thanks.

  3. Isak Sigurdsson says:

    Hi,
    Is there any way to do this without setting the service tier user as a NAV user ?

    Thanks.
    Isak

  4. Daniel Berg says:

    Hi,

    I want to share my modified script.
    At first I added a check, if it’s running with administrator- priviliges, Secon I made an array-list, where you can enter your names of NAV-instances you want to check.

    To get the Administrator Mode working you need two files with the same name. One of them is the powershell Script and the Second is a batch file, which opens the powershell with administration privileges.

    Check Instances.cmd
    @echo off
    echo This programm needs Administration rights!
    PowerShell.exe -NoProfile -Command “”

    Check Instances.ps1
    Import-Module ‘C:\Program Files\Microsoft Dynamics NAV\90\Service\Microsoft.Dynamics.Nav.Management.dll’
    if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”))
    {
    Write-Output ‘The check from the NAV instances has started’
    pause

    $Instance_List= @(“instance1″,”instance2”)
    for($i=0;$i -lt @($Instance_List).length;$i++)
    {
    invoke-NAVCodeunit $Instance_List[$i] -CodeunitId 1
    }
    pause
    }
    else
    {
    Write-Output ‘The programm needs administration rights!’
    Pause
    }

    regards,
    Daniel Berg

    the source of the .cmd file: http://www.howtogeek.com/204088/how-to-use-a-batch-file-to-make-powershell-scripts-easier-to-run/

  5. Jens Saustrup says:

    Thanks for the good script.
    I had it running in our setup for two weeks monitoring our NAV 2016 queue instances.
    I ran into an issue where the call to Codeunit 1 still succeeds but all queues were stopped for some reason hence the services were not restarted.
    I have decided to go for monitoring heartbeat instead.

Skip to main content