{"id":7316,"date":"2023-08-19T12:41:05","date_gmt":"2023-08-19T07:11:05","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7316"},"modified":"2023-08-19T12:41:09","modified_gmt":"2023-08-19T07:11:09","slug":"how-to-set-up-the-pm2-service-monitor-on-nagios","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-set-up-the-pm2-service-monitor-on-nagios\/","title":{"rendered":"How to set up the PM2 service monitor on Nagios"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To set up the PM2 service monitor on Nagios, you&#8217;ll need to follow these general steps. PM2 is a process manager for Node.js applications, so we&#8217;ll assume you&#8217;re monitoring Node.js processes managed by PM2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Prerequisite<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Server root login credentials.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Log in to your Ubuntu or Centos server as a user with sudo privileges. You can use SSH or directly access the server.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ ssh root@Ip<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Edit the \/etc\/nagios\/nrpe.cfg configuration file and search for <strong><em>command<\/em><\/strong> definition sections. Here you can define or update check commands.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>$ vi \/etc\/nagios\/nrpe.cfg<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Please update the below command for the PM2 check.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>command[check_pm2_port]= \/usr\/lib\/nagios\/plugins\/check_pm2_port.sh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please go to the plugin&#8217;s path and create a new script file&nbsp;in the specified path, such as check_pm2_port.sh. The script should check if the PM2 service port is open or not.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>vi \/usr\/lib\/nagios\/plugins\/check_pm2_port.sh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Please copy the below commands, and paste the check_pm2_port.sh file, and save the file.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>#!\/bin\/bash<br>&nbsp;<br># Modify the following variables according to your setup<br>PM2_HOST=&#8221;localhost&#8221;<br>PM2_PORT=3000<br>&nbsp;<br>nc -z -w 2 $PM2_HOST $PM2_PORT &gt; \/dev\/null 2&gt;&amp;1<br>result=$?<br>&nbsp;<br>if [ $result -eq 0 ]; then<br>&nbsp;&nbsp;&nbsp; echo &#8220;OK &#8211; PM2 service is listening on port $PM2_PORT&#8221;<br>&nbsp;&nbsp;&nbsp; exit 0<br>else<br>&nbsp;&nbsp;&nbsp; echo &#8220;CRITICAL &#8211; PM2 service is not listening on port $PM2_PORT&#8221;<br>&nbsp;&nbsp;&nbsp; exit 2<br>fi<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Give permission to the check_pm2_port.sh file and follow the below command:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>chmod 755 check_pm2_port.sh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, restart the NRPE service. Now it is ready to listen to requests from the Nagios Server.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>systemctl restart nagios-nrpe-server<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Open the Nagios server.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 6:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go to the path of the object using the below commands:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>vi \/usr\/local\/nagios\/etc\/objects\/commands.cfg<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here, we want to add the below command to a file and save it.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>define command {<br>&nbsp;&nbsp;&nbsp; command_name&nbsp;&nbsp;&nbsp; check_pm2_port<br>&nbsp;&nbsp;&nbsp; command_line&nbsp;&nbsp;&nbsp; \/usr\/lib\/nagios\/plugins\/check_nrpe -H $HOSTADDRESS$ -c check_pm2_port<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 7:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please open the below for the configuration path.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>vi \/usr\/local\/nagios\/etc\/servers\/testserver2.cfg<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Add the below lines for PM2 checking the configuration and save it.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>define service {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; use&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       generic-service&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Name of service template to use<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; host_name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testserver2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; service_description&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pm2_service<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; check_command&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; check_nrpe!check_pm2_port<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; contact_groups&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;admins<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; notifications_enabled&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 8:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Save the configuration file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And finally, restart Nagios to apply the recent configuration changes:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>systemctl restart nagios<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We can verify the PM2 status in the below snapshot.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-14-071154.png\"><img decoding=\"async\" width=\"1024\" height=\"23\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-14-071154-1024x23.png\" alt=\"\" class=\"wp-image-7318\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-14-071154-1024x23.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-14-071154-300x7.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-14-071154-768x17.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-14-071154-850x19.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2023\/08\/Screenshot-2023-08-14-071154.png 1438w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Following the steps mentioned above, we can set up the PM2 service monitor on Nagios.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: To set up the PM2 service monitor on Nagios, you&#8217;ll need to follow these general steps. PM2 is a [&hellip;]<\/p>\n","protected":false},"author":499,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1019],"tags":[],"class_list":["post-7316","post","type-post","status-publish","format-standard","hentry","category-cloud-aws","psol-cat-cloud-aws"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1U0","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/499"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7316"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7316\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}