Table of Contents

Website Performance Monitoring with a Custom Nagios XI Plugin

Picture of Ayoub Louragli
Ayoub Louragli
Using nagios for agentless management

Introduction

Monitoring website performance is crucial for ensuring high availability and optimal user experience. Slow response times can lead to lost customers and impact business reputation. In this guide, we introduce a custom Nagios XI plugin that monitors website response times and alerts administrators when predefined thresholds are exceeded.

The Need for Website Response Time Monitoring

Website response time is a key performance metric that reflects how quickly a site responds to user requests. High latency may indicate issues such as server overload, network congestion, or backend inefficiencies. By integrating our custom check_response_time plugin into Nagios XI, administrators can:

  • Monitor website uptime and response times
  • Set warning and critical thresholds for response time alerts
  • Receive real-time notifications when performance degrades

Plugin Overview

Our check_response_time plugin is written in Python and utilizes the requests library to fetch a webpage and measure its response time. It classifies the results into three states:

  • OK: Response time is within acceptable limits
  • WARNING: Response time exceeds the warning threshold
  • CRITICAL: Response time exceeds the critical threshold

Additionally, it handles common network errors, ensuring reliable monitoring.

Plugin Code

Below is the Python code for the Nagios XI plugin:

#!/usr/bin/env python3

import sys
import requests
import argparse

def check_website_health(url, warning_threshold, critical_threshold):
    try:
        response = requests.get(url, timeout=10)
        load_time = response.elapsed.total_seconds() * 1000  # convert to milliseconds

        if response.status_code != 200:
            return (2, f"Website is down. Status Code: {response.status_code}")

        if load_time >= critical_threshold:
            return (2, f"CRITICAL - Response time {load_time:.2f} ms exceeds critical threshold of {critical_threshold} ms")
        elif load_time >= warning_threshold:
            return (1, f"WARNING - Response time {load_time:.2f} ms exceeds warning threshold of {warning_threshold} ms")
        else:
            return (0, f"OK - Response time {load_time:.2f} ms")
    except requests.exceptions.RequestException as e:
        return (3, f"Error checking website: {str(e)}")

def main():
    parser = argparse.ArgumentParser(description="Check website response time")
    parser.add_argument("-H", "--host", required=True, help="Host URL")
    parser.add_argument("-w", "--warning", type=int, required=True, help="Warning threshold in ms")
    parser.add_argument("-c", "--critical", type=int, required=True, help="Critical threshold in ms")
    args = parser.parse_args()

    url = args.host
    if not url.startswith('http://') and not url.startswith('https://'):
        url = 'http://' + url

    status, message = check_website_health(url, args.warning, args.critical)
    print(message)
    sys.exit(status)

if __name__ == '__main__':
    main()

You can access the code on Github: Here

How to Use the Plugin in Nagios XI

1. Upload the Plugin to Nagios XI

  1. Log in to the Nagios XI Web Interface.
  2. Navigate to Admin > System Extensions > Manage Plugins.
  3. Click Browse, select the script file, and click Upload & Install.
image 77
Manage the plugin

2. Define the Plugin as a Nagios XI Command

  1. Go to Configure > Core Config Manager > Commands > Add New.
  2. Enter the following details:
    • Command Name
    • Command Line
    • Command Type: check command
Screenshot 2025 02 07 082459
Adding the command

3. Configure a Nagios XI Service

  1. Navigate to Configure > Core Config Manager > Services > Add New.
  2. Fill in the required fields:
    • Service Name
    • Host: Select the target host
    • Check Command
    • *In this example, a WARNING alert is triggered if the response time exceeds 500ms, and a CRITICAL alert is triggered if it exceeds 1000ms.
  3. Configure the Check Settings and Alert Settings.
  4. Save the service and apply the configuration.
Screenshot 2025 02 07 084046
Adding the service

Testing the Plugin

Screenshot 2025 02 07 083035
Check service status example

Conclusion

By integrating the check_response_time plugin into Nagios XI, administrators gain real-time insights into website performance and uptime. This proactive approach helps mitigate potential downtime and ensures a seamless user experience.

Try deploying this plugin in your Nagios XI setup today and take control of your website performance monitoring!

Share this post