HOW TO SET UP THE CPU SERVICE MONITOR ON NAGIOS WITH PERCENTAGES
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 | 
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 | 
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 | 
| #!/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 | 
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 | 
Login to Nagios server
Step 7:
Open the below for the configuration path.
| $ vi /usr/local/nagios/etc/servers/server.cfg | 
| 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 } | 
Save the configuration file.
And finally, restart Nagios to apply the recent configuration changes:

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