Introduction:

To set up the CPU service monitor on Nagios, you’ll need to follow these general steps. CPU checks CPU usage, load averages, or other performance metrics on the server, and if the process is high, it gives an alert to the Nagois server.

Prerequisite

1. Server root login credentials.

Step 1:

Log in to your Ubuntu or Centos server as a user with sudo privileges. You can use SSH or directly access the server.

$ ssh root@Ip
Step 2:

Edit the /usr/local/nagios/etc/nrpe.cfg configuration file and search for command definition sections. Here you can define or update check commands.
$ vi /usr/local/nagios/etc/nrpe.cfg

Please update the below command for the CPU check.
command[check_cpu]=/usr/local/nagios/libexec/check_cpu
Step 3:

Go to the libexec path and create a new script file in the specified path, such as check_cpu. The script should check if the CPU service checking the server process.
vi /usr/local/nagios/libexec/check_cpu
Please copy the below commands, and paste the check_cpu file, and save the file.
#!/bin/bash
 
# Define your remote server’s SSH details
#SSH_USER=”your_ssh_user”
#SSH_HOST=”remote_server_hostname_or_ip”
 
# SSH command to collect CPU utilization (example command, you can modify it)
#SSH_CMD=”top -bn1 | grep ‘Cpu(s)’ | awk ‘{print \$2 + \$4}'”
# SSH into the remote server and execute the command
#CPU_USAGE=$(ssh -l $SSH_USER $SSH_HOST “$SSH_CMD”)
 
# Check the CPU usage and return an appropriate message
if (( $(echo “$CPU_USAGE >= 90” | bc -l) )); then
    echo “CRITICAL – CPU Usage: $CPU_USAGE%”
    exit 2
elif (( $(echo “$CPU_USAGE >= 80” | bc -l) )); then
    echo “WARNING – CPU Usage: $CPU_USAGE%”
    exit 1
else
    echo “OK – CPU Usage: $CPU_USAGE%”
    exit 0
fi
Step 4:
Give permission to the check_cpu file as below
$ chmod 755 check_cpu
Step 5:
Next, restart the NRPE service. Now it is ready to listen to requests from the Nagios Server.
$ systemctl restart nagios-nrpe-server

Step 6:
Login to nagios server

Step 7:

Open the below for the configuration path.
$ vi /usr/local/nagios/etc/servers/server.cfg
Add the below lines for CPU checking the configuration and save it.
define service {
       use                             generic-service         ; Name of service template to use
       host_name                       testserver2
       service_description           cpu_service
       check_command              check_nrpe!check_cpu
       contact_groups                 admins
       notifications_enabled        1
       }

Step 8:
Save the configuration file.

And finally, restart Nagios to apply the recent configuration changes:
$ systemctl restart nagios

We can verify the cpu_service status in the below snapshot

Conclusion
Following the steps mentioned above, we can set up the CPU service monitor on Nagios.

Leave a Reply