Plugin to Check CPU Usage

Picture of Johnny Mengistu
Johnny Mengistu
Nag Dash Example

Benefits of using a plugin

1. Resource Management – Since you only install the plugins you need, you can avoid unnecessary resource usage that would come with more comprehensive software packages.

2. Enhanced Functionality – Plugins add new features and capabilities to existing software without needing to modify the core program. This allows you to expand what your software can do.

3. Customization – You can pick and choose exactly which additional features you want, creating a personalized setup that matches your specific needs rather than dealing with bloated software.

4. Cost-Effective – Instead of buying entirely new software, you can often just add the specific functionality you need through plugins, which are typically less expensive or even free.

5. Easy Updates – Plugins can be updated independently from the main software, making it easier to maintain and improve specific features without affecting the whole system.

6. Development Efficiency – For developers, plugins provide a way to extend applications without having to rebuild them from scratch. The modular nature makes maintenance and debugging easier.

7. Community Innovation – Popular software often has a vibrant plugin ecosystem where developers create innovative solutions for specific needs, benefiting the entire user community.

The Purpose of a CPU Usage Plugin

The purpose of a “Check CPU Usage” plugin is to monitor the percentage of CPU resources being used by the system. It checks whether the CPU usage is within an acceptable range (e.g., below 80%), and alerts if it crosses a threshold, indicating that the system is under heavy load.

Typically, the plugin will return one of the following statuses:

  • OK (Exit Code 0): CPU usage is within acceptable limits.
  • WARNING (Exit Code 1): CPU usage is above normal but below the critical threshold.
  • CRITICAL (Exit Code 2): CPU usage is too high and action should be taken.
  • UNKNOWN (Exit Code 3): The plugin encountered an error, and it could not determine the CPU usage.
Picture of plugin within computer screen
Plugin to Check CPU Usage 4

How it works

This script checks how much disk space is being used on your system’s root drive (“/”) and compares it to two important thresholds: warning and critical.

  • Warning Threshold (80%): This is the point at which the disk usage is high enough to warn you, but it’s not critical yet.
  • Critical Threshold (90%): This is the point at which the disk usage is so high that it’s considered an urgent issue.

Based on the disk usage, the script will return one of three possible results:

OK (if disk usage is below 80%)

CRITICAL (if disk usage is 90% or higher)

WARNING (if disk usage is between 80% and 90%)

Breaking it down

Line 1: #!/bin/bash

  • This tells the system to run this script using bash, a common scripting language.

Line 3-4: Set Thresholds

  • WARNING_THRESHOLD=80 and CRITICAL_THRESHOLD=90: The script sets two thresholds — 80% for warning and 90% for critical. These numbers are used to compare against the current disk usage.

Line 6: Get Disk Usage

  • USAGE=$(df / | tail -n 1 | awk '{print $5}' | sed 's/%//')
  • This command checks the disk usage of the root directory (/).
    • df / gets the disk space details.
    • tail -n 1 picks the last line of output (which contains the disk usage).
    • awk '{print $5}' extracts the disk usage percentage.
    • sed 's/%//' removes the “%” symbol from the usage number.

Line 9-16: Compare Disk Usage to Thresholds

  • The script then checks if the disk usage ($USAGE) is above the thresholds:
    • If disk usage is 90% or more: it prints a CRITICAL message, along with performance data, and exits with code 2 (to indicate a critical issue).
    • If disk usage is between 80% and 90%: it prints a WARNING message, along with performance data, and exits with code 1.
    • If disk usage is less than 80%: it prints an OK message, along with performance data, and exits with code 0 (which means everything is fine).

CPU Usage Code

  • The script checks the disk usage percentage on the root filesystem (/).
  • It compares this percentage with warning and critical levels.
  • Based on the result, it gives a message like “OK”, “WARNING”, or “CRITICAL”.
  • It also outputs performance data to Nagios in a format it can understand.

Example Output in Nagios XI

Summary

A “Check CPU Usage” Nagios plugin works by gathering the system’s CPU usage data, comparing it against defined thresholds, and returning an appropriate status and message. By creating this plugin, you can effectively monitor CPU performance on your systems and ensure that administrators are alerted if CPU usage exceeds acceptable limits.

This plugin can be customized with different thresholds, enhanced with more sophisticated checks, or extended to monitor specific processes or resource consumption on the server.

You can access the full code on GitHub: CPU plugin

Share: