Custom monitor examples in Java

Simple drive space usage monitor

General steps for creating a custom monitor are:

  1. Write a script for calculating metrics to monitor
  2. Create a monitor by making addMonitor API call. You can use one of the Monitis API SDKs or implement the HTTP call yourself. In the examples below we will use the Monitis API Java SDK
  3. Parse the response and store id of the monitor locally
  4. Use the monitor id to make an API call to add monitoring results
  5. Schedule your script to run periodically

Let’s create a very simple drive monitor, which checks the free space on drive. (Look through the list of parameters required for creating a monitor.) Let’s define one monitor parameter – drive, which is going to identify the drive to be monitored, and one result parameter – freeSpace, which will show the free space on the drive.

Insert your apikey and secretkey in the code above and run it. Go to Monitis dashboard to see the created monitor. From the top menu choose Monitors → Custom → List.

Now we need to add monitoring results. We read the monitorId from a local file, then make a monitorInfo api call to get the name of the drive to be monitored. After then we calculate the free space of the drive and make an api call to add results. It is important to get the drive name before calculating results as monitor info can be edited both from the dashboard and by making correspondent api call.

Run the code above and go to Monitis dashboard to see the created monitor. From the top menu choose Monitors → Custom → List, select the monitor and click the Add To Window button.

Extended drive monitor - io and space usage for several drives

Suppose you want to monitor several drives on your machine. Using the example above you will have to create a separate monitor for each drive. Instead you can add a result parameter named drive and specify the drive name for each result you send. To distinguish drive result parameter from other numeric columns you should set parameter hasFixedVaues=true for it. Instead of sending one result for one check you will send as much results as much drives you are monitoring. On Monitis dashboard you will see one row of data for each drive. Let’s assume you have written also a drive IO monitor with columns reads and writes. Now let’s join it with drive space usage monitor. So you will have monitor with 5 columns: drive, usedSpace, freeSpace, reads and writes. Here are 2 logical parameter groups: space and io. So it will be meaningful to define parameter groups for each parameter. usedSpace and freeSpace will be in space group, reads and writes will be in io group, drive will be in both groups. On Monitis dashboard you will be given option to choose the parameter group to show in the module. Here is the sample code demonstrating creating of a monitor described above.

Now that we’ve created the monitor, we need to add results. In this case we upload data for not one drive but several, so it is very important not to mix values for different drives. Below is the code demonstrating uploading multiple results for one checktime. Please note that MonResult class should receive a JsonArray as the second argument and the order of parameters values for different drives in the array must be the same for all parameters’ JSON arrays. E.g. if drive array is [‘C:’, ‘D:’] and freeSpace array is [10,20] then 10 is the value for C: drive and 20 is for D:. Let’s look at the code.

Here is how monitor module will look on Monitis dashboard after you run the code above.