Examples of JobMonitor Monitoring

Last updated on 08 February, 2023

Use the following JobMonitor examples, Monitoring a Linux Cron Job and Monitoring a Windows Scheduler Task, to gain a better understanding of the job monitoring process. This process usually includes four steps:

  1. Creating a job monitor definition
  2. Wrapping up the job or task
  3. Setting the environment variables
  4. Checking the execution

Warning: Do not provide sensitive information, such as usernames or passwords, in the command line. This information should be handled inside the script, using any secrets management tool or environment variables. (Environment variables are preferred over plain text credentials in code.) LogicMonitor recommends leveraging a dedicated secrets management tool over using environmental variables.

Example 1: Monitor Linux Cron Job

Assume the monitored device in LogicMonitor is app1, and the collector monitoring app1 runs on the device agenthost. There is a cron job cleanlogs running at 8:30, 12:30, and 16:30 everyday on app1. LogicMonitor should monitor if:

  • The task cleanlogs starts on time at 8:30, 12:30, and 16:30 everyday.
  • The task finished successfully.

Step 1: Create the JobMonitor Definition

The first step of monitoring a Linux cron task is to create a batch job definition.

  1. Log on to your LogicMonitor account.
  2. Go to Settings > LogicModules > JobMonitors.
  3. Click Add and then click JobMonitor.
  4. Edit the JobMonitor definition.
  5. Click Submit to save the JobMonitor definition.
Step 1: Create the Batchjob definition

The JobMonitor definition is associated with device app1. It triggers an alert if the JobMonitor isn’t finished in ten minutes and triggers a warning alert if the job starts more than 15 minutes after its scheduled run times of 8:30, 12:30, and 16:30. The JobMonitor associated with the device app1 is displayed in the Devices page of your account.

Step 2: Wrap Up the Cron Job

The cron job cleanlogs currently executes a script /usr/local/cleanlog.pl at 8:30, 12:30, and 16:30 everyday:

Step 2: Wrap up the cron job
  1. Copy lmbatchjobwrapper.sh (this file is stored in /usr/local/logicmonitor/agent/lib of collectorhost) to app1.
  2. Run crontab -e (or equivalent) and update the command line to:
30 8,12,16 * * * /usr/local/lmcollector lmbatchjobwrapper.sh agenthost:7214 app1 "Clean log monitoring" /usr/local/cleanlog.pl
  1. Review the elements in the code sample:
    • agenthost:7214 is the name of the device on which the collector is running and the port on which the collector listen to accept batch job execution notification sent from lmJobMonitorwrapper.js.
    • app1 is the name of the monitored device on which the batch job is running.
    • Clean log monitoring is the name given to this scheduled task in LogicMonitor.
    • /usr/local/cleanlog.pl tells the wrapper how to start the program to clean the logs.

Step 3: Set the Linux Environment Variables

Set the variables in /etc/environment. These variables can be accessed by any user and persist through power-offs.

Note: Variable expansion is not supported in the /etc/environment. 

  1. Open the terminal and enter the following command: $ sudo -H vi /etc/environment
  2. Enter the password. 
  3. Add the variables in the file that’s opened. (PASSWORD=pass1234)
  4. Save and close the file.
  5. Log out, and then log back in.

Note: Password can be used in the script with $PASSWORD. For more information, see Environment Variables with Linux/Unix.

Step 4: Check the Job Executions

You can check every execution of the scheduled task from the Devices page. Executions that didn’t start on time, or that finished abnormally, are shown with an error.

Example 2: Monitor Windows Scheduler Task

Assume the monitored device is app1, and the collector monitoring app1 runs on the device agenthost. There is a Windows scheduler task cleanlogs running at 8:30, 12:30, and 16:30 everyday on app1. LogicMonitor should monitor if:

  • The task cleanlogs is started on time at 8:30, 12:30, and 16:30 everyday.
  • The task finished successfully.

Step 1: Create the JobMonitor Definition

The first step of monitoring a Windows scheduler task is to create a batch job definition.

  1. Log on to your LogicMonitor account.
  2. Go to Settings > LogicModules > JobMonitors.
  3. Click Add and then click JobMonitor.
  4. Edit the JobMonitor definition.
  5. Click Submit to save the JobMonitor definition

The JobMonitor definition is associated with device app1. It triggers an alert if the JobMonitor isn’t finished in 10 minutes and triggers a warning alert if the job starts more than 15 minutes after its scheduled run times of 8:30, 12:30 and 16:30. The JobMonitor associated with the device app1 is displayed in the Devices page of your account.

Step 2: Wrap Up the Windows Scheduled Task

The scheduled task cleanlogs executes a script C:\scripts\cleanlogs.bat at 8:30, 12:30, and 16:30 everyday:

Step 2: Wrap up the Windows scheduled task
  1. Copy lmbatchjobwrapper.js (stored in <collectorInstallationDir>\Agent\lib of collectorhost) to app1.
  2. Open the Properties dialog of the scheduled task Cleanlogs, and update the command line to:

    cscript.exe C:\lmcollector\lmbatchjobwrapper.js agenthost:7214 app1 "Clean log monitoring" "cmd.exe /C C:\script\cleanlogs.bat"

    Where:
    • agenthost:7214 is the name of the device on which the collector is running and the port on which the collector listen to accept batch job execution notification sent from lmbatchjobwrapper.js.
    • app1 is the name of the monitored device on which the batch job is running.
    • Clean log monitoring is the name given to this scheduled task in LogicMonitor.
    • cmd.exe /C C:\script\cleanlogs.bat tells the wrapper how to start the program to clean the logs.

Step 3: Set the Windows Environment Variables

  1. Open the Control Panel and select System.
  2. Select Advanced system settings.
  3. In the System Properties dialog box, under Advanced, select Environment Variables.
  4. Under user variables, select New and then enter the key and value. (PASS pass1234)
  5. Click Save.

Note: The variables can be provided in a PowerShell script as $PASSWORD=$env:PASS.

Step 4: Check the Job Executions

You can check every execution of the scheduled task from the Devices page. Executions that didn’t start on time, or that finished abnormally, are shown with an error.

In This Article