BEFORE YOU BEGIN Knowledge of a scripting language like Batch or vb script is required. For scripting languages supported by Datto RMM, refer to Scripting.
SECURITY Permission to manage Components. To create a policy, permission to manage policies at account or site level.
NAVIGATION Components > New Component
NAVIGATION Account > Policies
NAVIGATION Sites > select a site > Policies
About custom component monitors
Component monitors are, in essence, scripts that regularly run on your devices. If a specific condition is met, an alert will be raised. You can find and download a number of pre-configured component monitors from the ComStore that cover antivirus packages, backup systems, CPU temperature, predicted hard drive failures, and so on. Refer to ComStore and components.
EXAMPLE For example, the status of Datto's cloud-based file backup service, Datto File Protection, can also be monitored through a component monitor. For more information, refer to Datto File Protection Integration.
Sometimes, you may want to generate alerts we don't have pre-configured component monitors for. For example, you want to:
- Monitor a backup or antivirus package that we don't currently have a ComStore component for.
- Check whether a specific file or folder exists.
- Run a diagnostic command (such as dcdiag or repadmin) and check the output for issues or failure.
- Interrogate a database using a command line tool (like osql) and check specific values.
For cases like these, and many others, Datto RMM includes a powerful scripting tool that allows you to write your own monitoring scripts called custom component monitors. A knowledge of scripting languages is required.
In this exercise, we will write a batch script that looks for a text file called C:\Test\test.txt. If that file exists, the script will terminate with exit code 0 (which means "success"). If it doesn't, we'll see some output, and it will terminate with exit code 1 (which means "failure").
Our script to do that looks like this:
@echo off echo Checking for file C:\test\test.txt if exist C:\test\test.txt (goto found) ELSE (goto notfound) :foundecho File found - script exiting OK exit /B 0 :notfound echo File not found - script exiting with warning exit /B 1
Save that as a batch file called doesfilexist.bat to your C:\ directory. Ensure your text editor doesn't add a .txt extension.
Before we incorporate the script into a component monitor, we want to make sure the script does what we expect. The easiest way to do this is to write it to run on a single, local device.
- In your C:\ directory, create a folder called "test", but do not create the test.txt file yet.
- Go to the Command prompt and type: doesfileexis.bat > log.txt
- Press Enter. This will execute the batch file and save the results in a log file.
The log file should read:
Checking for file C:\test\test.txt File not found - script exiting with warning
- Add the test.txt file to the directory and run the batch file again.
Now the log file should read:
Checking for file C:\test\test.txt File found - script exiting OK
This confirms we have a working script, checking for the existence of a file called test.txt in C:\test.
NOTE If your script performs differently when it is run locally compared to when it is run within Datto RMM, it may be due to permissions. Scripts run via Quick Jobs run as the most highly-privileged user, which is NT AUTHORITY\SYSTEM for Windows devices and Root for macOS devices.
While the output format above works when the script is run on a local device, we need to make some changes so it works for the output for a custom component monitor.
When the output is formatted the following way, the Real-Time Monitoring Status will show the values of the custom monitor at the device level:
<-Start Result-> OUTPUTVARIABLE=Your Warning/error message <-End Result->
where "OUTPUTVARIABLE" is a variable you configure when you create the monitor. Refer to Turn the script into a component monitor.
To make sure the script generates output that looks like the example above, we modify the output section of the script as follows:
@echo off echo Checking for file C:\test\test.txt if exist C:\test\test.txt (goto found) ELSE (goto notfound) :found echo File found - script exiting OK exit /B 0 :notfound echo ^<-Start Result-^> echo CSMon_Result=File not found - script exiting with warning echo ^<-End Result-^> exit /B 1
IMPORTANT In this case, the < and > characters have to be escaped so they are parsed correctly in a batch file, otherwise they are treated as though they were piping data into or out of a command. Other monitor script types don't need to do this. The vbs equivalent would be:
wscript.echo "<-Start Result->" wscript.echo "CSMon_Result=File not found - script exiting with warning" wscript.echo "<-End Result->" wscript.quit 1
NOTE The same “Start Result/End Result” technique can be used to report that no error has been found and that the script has executed properly. Simply re-write the message to one stating that no error has occurred and exit with code 0 instead of code 1. This output can be observed in the Web Portal. When there is no output to display, the Web Portal will display “No Data”.
If the alert requires more detail than can be captured in a single line summary, it is possible to populate the Diagnostic Summary pane of the alert with a multi-line output using similar output formatting principles. To learn how to check the alert Information, refer to View and act on alert information.
wscript.echo "<-Start Result->" wscript.echo "CSMon_Result=File not found - script exiting with warning" wscript.echo "<-End Result->" wscript.echo "<-Start Diagnostic->" wscript.echo "Diagnostic information line 1" wscript.echo "Diagnostic information line 2" wscript.echo "Diagnostic information line 3" wscript.echo "<-End Diagnostic->" wscript.quit 1
Our script is checking the right things and is outputting in the correct format. Now we are ready to incorporate this script into a component monitor.
NOTE You can use the same type of diagnostic script as in the example above in any custom component. When the component is selected as a response component in a monitor, the diagnostic results will be attached to the alert, which in turn reflects in any PSA ticket associated with the alert.
To create a monitor component using the script you've written, follow these steps:
- Log in to the Datto RMM Web Portal as Administrator (or a user who has the relevant permissions to be able to create components).
- Click the Components tab.
- In the top left corner, click New Component.
- In the window that then appears, select the category Device Monitors.
- Enter a Name and a Description.
- Click Save.
- Scroll down to the Script section of the window that appears.
- In the Script drop-down menu, select Batch (since our example script here is a batch / command shell script). If you're using a different script, choose the appropriate script type.
- Paste your working and tested script into the main window of the script section.
- Next, we need to let Datto RMM know what variable name we're using to capture the output. In this script, we used CSMon_Result, so we need to configure that as Output Data. Click the green "+" alongside the "Output Data" section.
- In the window that appears, specify the output variable name as CSMon_Result, and the type as String, then click Add.
- Click Save. Your monitor component will be saved and added to your component list.
Now that we have a custom component monitor, the final step is to deploy it to one or more devices using a monitoring policy. Refer to Create a Monitoring policy.
- In the Monitor Type drop-down, choose Component Monitor.
- Click Next.
- Your custom component monitor will be listed in the first drop-down menu in the Monitor Details > Trigger Details section. Select your component monitor.
- Configure how often this monitor should be executed.
- For now, we can leave the Alert Details and Auto-Resolution Details at their default values.
- Click Next.
- If you'd like to receive email alerts when this monitor is triggered, check the Email the following recipients box on the Response Details page and enter your email address or just leave the Default recipients check box selected.
- Then click Next until the monitor is created and you are back to the New Policy page.
- Add your targets and push out the policy.
IMPORTANT When you edit a component that is used in a Monitoring policy, make sure to re-push the policy for the component changes to be applied.
If the file you are looking for does not exist on a device the monitor was pushed out to, the designated alert recipients will receive an email like this:
In addition, you can see all raised alerts when you navigate to Account > Monitor > Monitor Alerts. Refer to Manage alerts.
For more details on a raised alert, click the hyperlinked message text: