Introduction:
To set up the CPU service monitor on Nagios with percentages, follow these general instructions. CPU monitors CPU utilization, load averages, and other server performance measures, and if a process exceeds a certain threshold, it alerts 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.

$ 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

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
Copy the below commands, and paste the check_cpu file, and save the file.


#!/bin/bash

#Get CPU usage percentage
CPU_USAGE=$(top -bn1 | grep “Cpu(s)” | awk ‘{print $2 + $4}’ | cut -d. -f1)

#Define thresholds for CPU usage
WARNING_THRESHOLD=70
CRITICAL_THRESHOLD=80

#Compare CPU usage with the thresholds
if [ “$CPU_USAGE” -ge “$CRITICAL_THRESHOLD” ]; then
echo “CPU usage is ${CPU_USAGE}%. Critical threshold exceeded!”
exit 2 # Nagios return code for critical status
elif [ “$CPU_USAGE” -ge “$WARNING_THRESHOLD” ]; then
echo “CPU usage is ${CPU_USAGE}%. Warning threshold exceeded!”
exit 1 # Nagios return code for warning status
else
echo “CPU usage is ${CPU_USAGE}%.”
exit 0 # Nagios return code for OK status
fi
Step 4:
Give permission to the check_cpu file as below
$ chmod 755 check_cpu

Restart the NRPE service.

$ systemctl restart nagios-nrpe-server
or
$ $ systemctl restart nrpe
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 snaps

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

Leave a Reply