How to set up the PM2 service monitor on Nagios
Introduction:
To set up the PM2 service monitor on Nagios, you’ll need to follow these general steps. PM2 is a process manager for Node.js applications, so we’ll assume you’re monitoring Node.js processes managed by PM2.
Prerequisite:
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 /etc/nagios/nrpe.cfg configuration file and search for command definition 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 in the specified path, such as check_pm2_port.sh. The script should check if the PM2 service port is open or not.
vi /usr/lib/nagios/plugins/check_pm2_port.sh |
Please copy the below commands, and 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 |
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:
vi /usr/local/nagios/etc/objects/commands.cfg |
Here, we want to add the below command to a file and save it.
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.
vi /usr/local/nagios/etc/servers/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 the recent configuration changes:
systemctl restart nagios |
We can verify the PM2 status in the below snapshot.
Conclusion
Following the steps mentioned above, we can set up the PM2 service monitor on Nagios.