How to set up the PM2 service monitor on Nagios

From PheonixSolutions
Jump to navigation Jump to search

How to set up the PM2 service monitor on Nagios

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 /etc/nagios/nrpe.cfg configuration file and search for COMMAND DEFINITIONS sections. Here you can define or update check commands.

    $vi /etc/nagios/nrpe.cfg

Please update the below command for the PM2 check.

   $command[check_pm2_port]= /usr/lib/nagios/plugins/check_pm2_port.sh

Step 3:

Please go to the plugin's path and Create a new script file, such as check_pm2_port.sh, in the specified path. The script should check if the PM2 service port is open or not.

   $cd /usr/lib/nagios/plugins/

create a new script file

   $Vi check_pm2_port.sh

Please copy the below commands, paste the check_pm2_port.sh file, and save the file.

   $#!/bin/bash
   # Modify the following variables according to your setup
   PM2_HOST="localhost"
   PM2_PORT=3000
   nc -z -w 2 $PM2_HOST $PM2_PORT > /dev/null 2>&1
   result=$?
   if [ $result -eq 0 ]; then
   echo "OK - PM2 service is listening on port $PM2_PORT"
   exit 0
   else
   echo "CRITICAL - PM2 service is not listening on port $PM2_PORT"
   exit 2
   fi

sample output

File:Media/image1.png

Step 4:

Give permission to the check_pm2_port.sh file and follow the below command:

   $chmod 755 check_pm2_port.sh

Step 5:

Next, restart the NRPE service. Now it is ready to listen to requests from the Nagios server

   $systemctl restart nagios-nrpe-server

open the Nagios server

Step 6:

Go to the path of the object using the below commands:

   $cd /usr/local/nagios/etc/objects/

open the commands.cfg files

   $vi commands.cfg

The below line is already in the file, so skip that line.

define command{

   $command_name check_nrpe
    command_line /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -t 60 -c $ARG1$
    }

Here, the above command wants to add the below command to in file and save it to files.

    $define command {
     command_name check_pm2_port
     command_line /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c check_pm2_port
      }

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

     $define service {
      use generic-service ; Name of service template to use
      host_name testserver2
      service_description pm2_service
      check_command check_nrpe!check_pm2_port
      contact_groups admins
      notifications_enabled 1
      }

Step 8:

Save the configuration file

And finally, restart Nagios to apply recent configuration changes:

   $systemctl restart nagios

It is working pm2 status the below snapshot

File:Media/image2.png