How to setup check CPU in Nagios alert?
How to setup check_CPU in Nagios alert
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:
Please go to the libexec path and Create a new script file, such as check_cpu, in the specified path. The script should check if the CPU in the server high and low.
cd /usr/local/nagios/libexec
Or
cd /usr/lib/nagios/plugins/
Step 3:
create a new script file
vi 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 and follow the below command:
chmod 755 check_cpu
Step 5:
Edit the /usr/local/nagios/etc/nrpe.cfg configuration file and search for COMMAND DEFINITIONS sections. Here you can define or update check commands.
vi /usr/local/nagios/etc/nrpe.cfg
or
vi /etc/nagios/nrpe.cfg
Please update the below command for the CPU check.
command[check_cpu]=/usr/local/nagios/libexec/check_cpu
or
command[check_cpu]=/usr/lib/nagios/plugins/check_cpu
Step 6:
Next, restart the NRPE service. Now it is ready to listen to requests from the Nagios server
systemctl restart nrpe.service
Or
systemctl restart nagios-nrpe-server.service
check the nrpe status
systemctl status nrpe.service
or
systemctl status nagios-nrpe-server.service
open the Nagios server
Step 7:
Please open the below for the configuration path
cd /usr/local/nagios/etc/servers/
open the configuration file using the below commands
vi testserver2.cfg
Add the below lines for pm2 checking the configuration and save it
Step 8:
Save the configuration file
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
}
And finally, restart Nagios to apply recent configuration changes:
systemctl restart nagios
It is working cpu_service status the below snapshot