Enhancing Power Monitoring With Nagios XI

Picture of Jack Brisben
Jack Brisben
Three robots in a server room working on fixing equipment and analyzing monitoring data.

Introduction: The Need for Power Monitoring in Data Centers

In the digital age, power consumption is a critical aspect of IT infrastructure, especially in data centers. Monitoring power consumption helps ensure operational efficiency, reduces energy costs, and prevents unexpected power-related outages. The Check_Power plugin for Nagios XI is designed to track power consumption in real time, assess its impact, and report any critical deviations. This plugin adds an additional layer of monitoring for organizations looking to keep track of energy use and optimize their IT infrastructure.

In this article, we will walk you through integrating the Check_Power Java plugin with Nagios XI. This plugin fetches data from a power API, calculates the Power Usage Effectiveness (PUE), and sends alerts based on the power consumption and PUE values.

Overview of the Nagios Check Power Plugin

The Check_Power plugin is a Java-based solution that connects to a given power API endpoint, retrieves the current power consumption value, and calculates the PUE for your data center’s efficiency. The plugin categorizes the power consumption status as:

  • OK: Power consumption is within acceptable limits.
  • WARNING: Power consumption is above average but not critical.
  • CRITICAL: Power consumption exceeds safe thresholds, triggering an alert.

The PUE value is also calculated using a fixed formula for energy efficiency in data centers:

PUE = Total Facility Energy / IT Energy

This plugin serves as an essential tool for power monitoring in both smaller and larger IT environments.

How It Works

  1. Fetch Power Data: The plugin makes a GET request to the specified power API endpoint and retrieves the power consumption data.
  2. Calculate PUE: The plugin calculates the Power Usage Effectiveness (PUE) value based on the total facility energy and IT energy.
  3. Evaluate Power Status: The plugin checks the retrieved power consumption and returns an alert status based on defined thresholds (e.g., critical for >1000W, warning for >500W).
  4. Alert Reporting: The plugin sends the status message and power consumption details back to Nagios XI for monitoring.

Preview of Nagios XI Power Consumption Plugin Code

import java.io.*;
import java.net.*;

public class Check_Power {

    public static final int OK = 0;
    public static final int WARNING = 1;
    public static final int CRITICAL = 2;
    public static final int UNKNOWN = 3;

    private static final double efficiencyThreshold = 1.5;

    public static void main(String[] args) {
        
        if (args.length != 1) {
            System.err.println("UNKNOWN - Missing power API endpoint URL argument.");
            System.exit(UNKNOWN);
        }

        String powerAPIEndpoint = args[0]; // Get the URL from the command-line argument

        try {
            double powerConsumption = getPowerConsumption(powerAPIEndpoint);
            double pue = calculatePUE();

            String status = checkPowerStatus(powerConsumption);
            String message = String.format("Power consumption: %.2f W | PUE: %.2f", powerConsumption, pue);

            System.out.println(status + " - " + message);
            System.exit(getExitCode(status));

        } catch (Exception e) {
            System.err.println("UNKNOWN - Error: " + e.getMessage());
            System.exit(UNKNOWN);
        }
    }

    private static double getPowerConsumption(String powerAPIEndpoint) throws IOException {
        URL url = new URL(powerAPIEndpoint);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

       
        String jsonResponse = response.toString();
        System.out.println("Raw JSON Response: " + jsonResponse);

        return parsePowerConsumption(jsonResponse);
    }

    private static double parsePowerConsumption(String jsonResponse) {
    
        try {
            
            int startIdx = jsonResponse.indexOf(":") + 1;
            int endIdx = jsonResponse.indexOf("}");
            String powerValueString = jsonResponse.substring(startIdx, endIdx).trim();
            return Double.parseDouble(powerValueString);
        } catch (Exception e) {
            System.err.println("Error parsing power consumption: " + e.getMessage());
            return 0.0; 
        }
    }

    private static double calculatePUE() {
        double totalFacilityEnergy = 5000;
        double itEnergy = 3000;
        return totalFacilityEnergy / itEnergy;
    }

    private static String checkPowerStatus(double powerConsumption) {
        if (powerConsumption > 1000) {
            return "CRITICAL";
        } else if (powerConsumption > 500) {
            return "WARNING";
        } else {
            return "OK";
        }
    }

    private static int getExitCode(String status) {
        switch (status) {    
            case "OK":
                return OK;
            case "WARNING":
                return WARNING;
            case "CRITICAL":
                return CRITICAL;
            default:
                return UNKNOWN;
        }
    }
}

You can access the code on Github: https://github.com/NagiosEnterprises/plugins-extra/tree/check_power

Output on Nagios-XI

Screenshot 2025 01 17 152902
Enhancing Power Monitoring With Nagios XI 2

Integrating the Check Power Plugin into Nagios XI

To integrate the Check_Power plugin with Nagios XI, follow these steps:

  1. Upload the Plugin to Nagios XI:
    • Log into the Nagios XI Web Interface.
    • Navigate to Admin > System Extensions > Manage Plugins.
    • Click Browse, select the compiled Check_Power.jar file, and click Upload Plugin.
  2. Set Executable Permissions:
    • After uploading, ensure the plugin is executable:
chmod +x /usr/local/nagios/libexec/check_power.jar

3. Define the Plugin Command:

Command Line:

Go to Configure > Core Config Manager > Commands > Add New.

Add the following details:

Command Name: check_power

java -jar $USER1$/check_power.jar $HOSTADDRESS$ $ARG1$
    • Command Type: Check command

Configure Power Monitoring Service:

  • Navigate to Configure > Core Config Manager > Services > Add New.
  • Fill in the necessary fields:
    • Service Name: Power Monitoring
    • Host: Select the desired host.
    • Check Command: check_power!http://your-power-api-endpoint.com
  • Save the service and apply the configuration.

AI Assistance Acknowledgment

This plugin and article development were assisted by AI, demonstrating how artificial intelligence can streamline IT solutions and inspire innovation. We encourage others to explore AI-driven development to enhance their IT operations.

Why This Plugin is Valuable for IT Security and Monitoring

  1. Real-Time Power Consumption Monitoring: Track energy consumption and detect inefficiencies in real-time.
  2. Energy Efficiency Reporting: The calculated PUE value provides insight into the efficiency of the entire data center’s energy use.
  3. Proactive Alerting: Alerts on power consumption thresholds help prevent potential damage to IT infrastructure.
  4. Seamless Integration: Easily integrates with Nagios XI, leveraging its centralized monitoring and alerting system.

Best Practices for Using the Check Power Plugin

  1. Set Up Regular Monitoring: Schedule periodic checks to consistently track power usage and avoid unexpected surges.
  2. Optimize Energy Consumption: Leverage the PUE value to identify opportunities to improve the energy efficiency of your data center.
  3. Integrate with Other IT Management Tools: Combine with other Nagios XI plugins for a comprehensive IT management strategy.

Conclusion: Strengthening Energy Management with the Check_Power Plugin

The Check_Power plugin for Nagios XI is a powerful tool for monitoring power consumption and improving the overall energy efficiency of data centers. By proactively tracking power usage and calculating the PUE, IT teams can enhance their infrastructure’s performance, reduce energy costs, and prevent potential issues before they arise. With easy integration into Nagios XI, this plugin provides an accessible, efficient solution for IT professionals striving for better energy management and sustainability.

Share:

Table of Contents