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.


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.
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.
# Define your remote server’s SSH details
# 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
# 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
    echo “OK – CPU Usage: $CPU_USAGE%”
    exit 0
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 7:
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

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

Leave a Reply