Monitoring Response Time in 4 Steps: Nagios XI Plugin Creation Guide

Picture of Nyi Set
Nyi Set
A terminal window displaying a Bash script that uses curl to measure response time and check connectivity to a specified host. The script captures start and end times, reports failures, and exits with a critical status if the connection fails.

This guide explains how to create a Nagios XI plugin in Bash to monitor server and website response times, using either HTTP requests (for websites) or ping (for network devices). The plugin checks if the response time is within the specified thresholds, providing alerts for critical or warning conditions.

Prerequisites

  • Nagios XI installed
  • A text editor (e.g., VS Code)
  • A target server/website to monitor

Step 1: Writing the Nagios Plugin

Create the plugin script to check server response times.

Open your text editor (e.g., VS Code) and create a new file named check_server_response_time.sh. Then, begin writing the code. In the script, define the following exit codes:

#Nagios exit codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3

These are the exit codes used by Nagios to indicate the state of the check. Nagios relies on exit codes to determine whether a service is OK, in a Warning state, or in Critical condition.

Initialize the script variables.

#initial values
HOST=""
WAR_THRESHOLD=""
CRT_THRESHOLD=""

We initialize three variables: HOST (target server/website), WAR_THRESHOLD (warning threshold in ms), and CRT_THRESHOLD (critical threshold in ms). These will be populated with user-provided values when the script runs.

Parse Command-Line Arguments

#parse arguments
while getopts ":H:w:c:" arg; do
	case $arg in
		H)HOST=${OPTARG} ;;
		w)WAR_THRESHOLD=${OPTARG} ;;
		c)CRT_THRESHOLD=${OPTARG} ;;
	esac
done

ECHO=$(which echo)

This section parses command-line arguments: -H <host> (server/website), -w <warning_threshold> (warning time in ms), and -c <critical_threshold> (critical time in ms), using getopts to assign values to the variables. All options are required when running the script.

Check for Missing Arguments

<span role="button" tabindex="0" data-code="#validate arguments if [ -z "$HOST" ] || [ -z "$WAR_THRESHOLD" ] || [ -z "$CRT_THRESHOLD" ]; then echo "UNKNOWN – Invalid Arguments. Usage : $0 -H <host> -w <warning_threshold_in_ms> -c
#validate arguments
if [ -z "$HOST" ] || [ -z "$WAR_THRESHOLD" ] || [ -z "$CRT_THRESHOLD" ]; then
	echo "UNKNOWN - Invalid Arguments. Usage : $0 -H <host> -w <warning_threshold_in_ms> -c <critical_threshold_in_ms>"
	echo "Host: $HOST, W: $WAR_THRESHOLD, C: $CRT_THRESHOLD "
	exit $UNKNOWN
fi

This section checks for missing arguments (HOST, WAR_THRESHOLD, CRT_THRESHOLD) using the -z condition. If any are missing, it displays an error and exits with the UNKNOWN code.

Strip Protocol from Host URL

#stripping protocol (http or https)
HOST=$(echo "$HOST" | sed 's#^https\?://##')

This line removes the protocol (http:// or https://) from the HOST, leaving only the domain or IP address for the request.

Check if the Host is a Website or Device

if [[ "$HOST" == *".com"* || "$HOST" == *".org"* || "$HOST" == *".net"* || "$HOST" == *".edu"* ]]; then

This line checks if the HOST has common website extensions (.com, .org, .net, .edu). If so, it uses curl to check response time; otherwise, it uses ping for devices like servers or routers.

Measure Website Response Time with curl

START_TIME=$(date +%s%3N)
	curl -o /dev/null -s -w "%{http_code}" "$HOST" > /dev/null
	if [[ $? -ne 0 ]]; then 
		echo "CRITICAL - Unable to connect to $HOST"
		exit CRITICAL
	fi 
	END_TIME=$(date +%s%3N)

If the HOST is a website, the script uses curl to send an HTTP request and measure response time. It captures the start and end times in milliseconds. If curl fails, it exits with a CRITICAL status.

Measure Device Response Time with ping

else
	START_TIME=$(date +%s%3N)
	ping -c 1 -W 1 "$HOST" > /dev/null
	if [[ $? -ne 0 ]]; then
		echo "CRITICAL - Unable to ping to $HOST"
		exit CRITICAL
	fi
	END_TIME=$(date +%s%3N)
fi

For a device, the script uses ping to measure round-trip time. If ping fails, it exits with a CRITICAL status. Start and end times are captured similarly to the website check.

Calculate Response Time and Comparing Thresholds

RESPONSE_TIME=$((END_TIME - START_TIME))

#compare response time  to threshold
if [ "$RESPONSE_TIME" -ge "$CRT_THRESHOLD" ]; then 
	echo "CRITICAL - Response time is ${RESPONSE_TIME} ms,exceeding the critical threshold of ${CRT_THRESHOLD} ms for $HOST"
	exit $CRITICAL
elif [ "$RESPONSE_TIME" -ge "$WAR_THRESHOLD" ]; then
	echo "WARNING - Response time is ${RESPONSE_TIME} ms,exceeding the warning threshold of ${WAR_THRESHOLD} ms for $HOST"
	exit $WARNING
else
	echo "OK - Response time is ${RESPONSE_TIME} ms for $HOST"
	exit $OK
fi

This section compares the response time to the thresholds: if it exceeds the critical threshold, it returns CRITICAL; if above the warning but below critical, it returns WARNING; otherwise, it returns OK.

Step 2: Making the Nagios Plugin Executable

After creating and saving the plugin script, you need to make it executable. You can either use VS Code’s integrated terminal or your regular terminal.

  • Open your terminal and navigate to the directory where you saved the script.
  • Run the following command to make the script executable:
sudo chmod +x /path/to/check_server_response_time.sh

Step 3: Integrate the Plugin with Nagios XI

To integrate the plugin with Nagios XI, first go to Nagios XI and navigate to ‘Admin’ icon.

art6
Navigating to Admin icon in Nagios XI

Then navigate to ‘System Extensions‘.

art2
Navigating to ‘System Extensions’ in Nagios XI

Once the options under ‘System Extensions’ are displayed, select ‘Manage Plugins’.

art4
Navigating to ‘Mange Plugins‘ in Nagios XI

After clicking the ‘Manage Plugins’ button, an upload input bar will appear, allowing you to select and upload your file.

art5
Upload file page in Nagios XI

Once you have uploaded your file, you should see a confirmation message stating, ”New plugin was installed successfully‘.

art7
New Plugin successfully installed.

For more information on integrating and managing your plugins with Nagios XI, visit Managing Plugins.

Step 4: Verify the Plugin is Setup Correctly

The final step is verifying the plugin you just created is working properly. To do this first you need to add a new service that utilize your plugin Nagios XI.

You can accomplish this by navigating to ‘Core Config Manager’>’Commands’.

art20
Navigating to ‘Core Config Manager’ then ‘Commands

After selecting ‘Commands’, you can add a new command by clicking the ‘Add New’ button.

art10 1
Navigating to ‘Add New‘ Button

Then you can add the Command Name and the Command Line.

art11
Command Management page to add a new command.

After entering the necessary information, click the ‘Save’ button. Two confirmation messages will appear.

art12
Notification to Apply Configuration to Nagios XI

Next, click ‘Apply Configuration’ to incorporate the new command. Upon completion, a message should appear stating, ‘Nagios Core was restarted with an updated configuration‘.

at13
Updated configuration notification for Nagios XI

Finally add your new service to an exiting host.

art55
Host Management page to add new service to a Host.

How it looks in Nagios

You should see the newly added service displaying the status based on the response time and the thresholds you’ve set. The service status will indicate whether the response time exceeds the critical or warning threshold and will also display a performance graph.

art14 3
Service status of a host in Nagios XI.
art15
Performance graph of a service in Nagios XI.

After breaking down the Nagios XI plugin code and explaining each section’s purpose, you can see how it’s a big help with monitoring server and website response times. Whether you’re checking websites or network devices, the plugin is highly customizable and can be adapted for various monitoring use cases.

Install the Plugin

To get started with the plugin, simply visit Nagios Enterprises GitHub repository and download the ZIP file. Once downloaded, open Nagios in your browser, navigate to Admin > System Extensions > Manage Config Wizards, then click Browse and upload the file. This will add the wizard and plugin to your Nagios instance and get you set up to monitor space weather conditions. To learn more about the wizard before using it, press here.

If you’d like to create your own plugin click the link here for a tutorial using bash, or if you’d prefer a video watch the video below to get a better understanding of how creating and configuring plugins work.

Still need help?

Schedule a free help session with one of our support techs or contact sales by emailing us at sales@nagios.com.

Visit Nagios Support Forum           Visit Nagios Knowledge Base        Schedule a Quickstart

Share

Table of Contents