Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Next let's take a look at how all these parameters and input fields come together as the environment variables that are used by the script itself. Environment variables aren't normally/displayed or retained by the service monitor when it runs, but they're easy to capture for testing/debugging purposes by adding the appropriate command to your shell/bat script:

  • Linux: printenv > /var/tmp/envvars.txt
  • Windows: set > C:\envvars.

...

  • txt

There's normally plenty of other environment variables that are set, but we've filtered those out to only look at the uptime values that are coming from our service monitor's XML definition.

Code Block
languagesql
UPTIME_ARGS=test-args 
UPTIME_TIMEOUT=60 
UPTIME_SYSTEM_TYPE=NODE 
UPTIME_PORT=9998

...


UPTIME_PROCESS_WINDOWS=plugins/scripts/monitor-custom-remote/monitor-custom-remote.bat 
UPTIME_PROCESS_LINUX=plugins/scripts/monitor-custom-remote/monitor-custom-remote.sh 
UPTIME_REMOTE_SCRIPT=test-script

...


UPTIME_HOSTNAME=cat1.uptimesoftware.com 
UPTIME_PASSWORD=test-pass

Here's a few things you should notice about these: 

  • They all start

...

  • with UPTIME_

...

  •  and the contents of the name attribute from the element tag. 

...

  • The UPTIME_

...

  • HOSTNAME has the hostname of the element the monitor is running against.
  • The rest of our input variables show-up as expected with the same contents as we set in the GUI example above. 

Shell/Bat wrapper scripts

Now let's take a quick look at what the custom_remote.bat & and custom_remote.sh scripts actually do with these environment variables. As mentioned above, most of the Uptime Infrastructure Monitor provided plugins rely on a common PHP script to perform the actual task of monitoring. As such the bat & and sh scripts are just responsible for telling Uptime Infrastructure Monitor's bundled php executable to run the required script, pass along the environment variables in the correct order. The specifics of how this done obviously varies between both Windows and Linux, so let's take a look at both methods.See

For more information about this feature, see monitor-custom-remote.bat on GitHub:.

Code Block
languagesql
@echo off

...


..\..\..\apache\php\php.exe rcs.php -h=%UPTIME_HOSTNAME% -p=%UPTIME_PORT% -s=%UPTIME_PASSWORD% -c=% UPTIME_REMOTE_SCRIPT% -a=%UPTIME_ARGS%

From the example here, you can see that windows allows you access environment variables by wrapping the name in % characters, ie i.e. % UPTIME_HOSTNAME%. So we've used that to arrange the input variables from xml into the order expected by the rcs.php script. You may also notice the .
.\..\..\apache\php\php.exe path used to start php. Whenever a script monitor is triggered the current working directory is set to the location of the
script itself, which is in this case is 'C:\Program Files\uptime software\uptime\plugins\scripts\monitor-custom-remote\monitor-custom-remote.bat'. But the location of the uptime directory can vary depending on where it was initially installed. So we use a relative path from the plugin's location to find <uptime_dir>\apache\php\php.exe. When making your own plugins, you can usually simplify this by using the hardcoded path of your uptime directory.

See For more information about this feature, see monitor-custom-remote.sh on GitHub:
#!.

Code Block
languagesql
#!/bin/

...

sh 

inst=`grep ^inst /etc/init.d/uptime_httpd | cut -d= -f2`
MIBDIRS=$inst/mibs

...


export MIBDIRS

...



/usr/local/uptime/apache/bin/php rcs.php -h=$UPTIME_HOSTNAME -p=$UPTIME_PORT -s=$UPTIME_PASSWORD -c=$UPTIME_REMOTE_SCRIPT -a=$UPTIME_ARGS

In the case of the linux shell script, we can access the contents of our environment variables by prefacing the name with the $ symbol. We then pass the same input variables along to php in the same order as in the bat script above. Just like on Windows the current working directly is set to the location of plugin script itself, but in this case we can use the contents of the /etc/init.d/uptime_httpd script to find the location the Uptime Infrastructure Monitor install directory and use to that to set the location of an extra MIBDIRS environment. This only required for PHP scripts on linux, so that the php_snmp library doesn't complain about being unable to find it's mibdirs. If your not using php in your own script, you can ignore this completely. The other thing worth noticing here is that we've hardcoded the path of Uptime Infrastructure Monitor's bundled php executable, as uptime's Apache folder is always installed into the default location of /usr/local/uptime/apache even if Uptime Infrastructure Monitor was installed elsewhere, this prevents us from using the same relative path method as we did on Windows. 

We won't get into the details of the rcs.php script itself as this will typically change depending on the details of the plugin itself. But the full contents of this file on GitHub here GitHub here if you are interested.

Output variables

...

Just like input variables, output variables are also defined in the xml file via the same element tags. They're covered by the same basic rules outlined above, and full details for their XML definitions are available here: 'Integration Guide -> Plugin Guide'.

Below is the two output variables that are defined in the Custom Remote Monitor's xml file. For reference the full version of this file is available on GitHub  he re GitHub here.
<element

Code Block
languagesql
            <element name='custom1' parameter_type='output' data_type='string' gui_basic='1' range_type='0' units=''>

...


                 <control_options>size:8</control_options>

...


                 <default_value></default_value>

...


                  <short_description>Output</short_description>

...


                  <long_description>Output from the script</long_description>

...


                  <validation_rule/>

...


                  <error_message/>
                  <gui_type>
                        <string/>
                  </gui_type>
            </element>
            <element name="timer" parameter_type="output" data_type="integer" gui_basic="0" range_type="0" units="ms">

...


                  <control_options>size:8</control_options>

...


                  <default_value/>

...


                  <short_description>Response time</short_description>

...


                  <long_description>Response time in ms</long_description>

...


                  <validation_rule/>

...


                  <error_message/>

...


                  <gui_type>
                        <integer/>
                  </gui_type>
            </element>
      </elements>
      <help>Execute remote scripts via the up.time agent. Output from the remote string is returned as String Data.</help>
</uptime>

Here's some things you notice about these examples:

  • Both cases, the parameter_type is set as output . This tells uptime that this is output we expect to see back form the script.
  • There's an optional unit attribute in the element tag, which is used to show that the response time is being stored in 'milliseconds' . This unit will also be displayed when graphing these metrics within Uptime Infrastructure Monitor.
  • We have an example of both the integer & string data_types, which in the case of output variables controls what type of threshold comparisons can be used to set the monitors WARN/CRIT status. Integer type values allow for Greater Than/Less Than numerical comparisons. Where-as strings have contains/does not contains and regex style comparisons.

...

Now that we've see what the output variables look like in the xml itself, lets take a look at what the script needs to return so that Uptime Infrastructure Monitor knows which output variable is which. This based on the element tag's name attribute. Which is used to look for a line of output from the script that starts with that name, which in the case above is 'custom1'. The rest of that line will then be pulled into Uptime Infrastructure Monitor as the variable and can used for threshold comparisons. In our example here, this custom1 variable will be treated as string. In monitors with multiple output variables, the output from the script itself can come any order, but any lines that don't start with the name of a variable will be ignored. 

The 'timer' output variable is a special case that will always contain the time it took the service monitor to run in milliseconds. This doesn't need to be provided as part of the output from your custom script.

The best way to understand how this works it to look at the output on the 'Test Service Monitor' screen. Which will show you the raw output from the service monitor, and then a list of any output variables that Uptime Infrastructure Monitor was able to parse out based on their names. For an example here, we'll use the 'custom remote monitor' with an agent side script called 'check-uptime' which returns output similar to:

Code Block
languagesql
uptime_core is stopped 
 MON Role

When this is triggered via the 'Test Service Monitor' button, you would see something like this:

The first thing you'll likely notice here is that the Monitor has set it's Status to OK , which is the default state for a service monitor that's run successfully, as long as none of it's threshold comparisons are triggered. The 'message' line shows use that the process returned a valid status , which is then followed by the output from the plugin monitor. ie. ' custom1 uptime_core is stopped MON Role'

You may wonder where the 'custom1' string is coming from as it's not in the example output of the agent side script above. This is inserted by the rcs.php and used to mark that the output from the agent script should link up with the 'custom1' variable from the XML. Which you can see on the next line 'Output' where 'custom1' has been replaced with that variable's Short-Description also form the xml. The next line is an example of Uptime Infrastructure Monitor attempting parse the second line of output from our agent script into a variable called 'MON' but because this isn't defined in our xml file, it won't be available for comparisons within the GUI. The final line here is the Response time value, which is special timer mentioned above.

The general take-away from this test service monitor example is that when Uptime Infrastructure Monitor is able to parse/understand the output from a script, the matched up variables will be listed line by line after the message. With the 'name' of the variable replaced by it's short description. If you don't see your output variable listed like this, then double check the names of your output variables and how the script itself is returning its output.

...