PM2-health : can i use pm2-health module for sending email alerts/notifications?

Viewed 2069

I have a nodejs application which runs on pm2 and I need to be able to send email notifications whenever a crash/ restart occurs. My idea is to monitor the application for crashes and trigger a mail action from pm2-health. The documentation of pm2-health module is here but I'm unable to use it for sending email alerts. Can anyone explain how to use it for this purpose?

P.S: Also, it would be great if you could explain about SMTP configuration for gmail.(I have configured postfix to use gmail smtp according to this and it works fine for test gmail but doesn't work with pm2-health)
2 Answers

This is how I could get pm2-health working with my Gmail account:

  1. Install pm2-health module:

pm2 install pm2-health

  1. Open PM2 module config file:

vim ~/.pm2/module_conf.json

  1. Update it with the Gmail account’s SMTP parameters:
{
    "pm2-health": {
        "smtp": {
            "host": "smtp.gmail.com",
            "port": 465,
            "user": "EXAMPLE_sender@gmail.com",
            "password": "PASSWORD",
            "secure": true,
            "disabled": false
        },
        "mailTo": "NOTIFICATION_RECIPIENT_EMAIL_ADDRESS",
        "replyTo": "EXAMPLE_SENDER@gmail.com",
        "events": [
            "exit"
        ],
        "exceptions": true,
        "messages": true,
        "messageExcludeExps": [],
        "metric": {},
        "metricIntervalS": 60,
        "aliveTimeoutS": 300,
        "addLogs": false,
        "appsExcluded": [],
        "snapshot": {
            "url": "",
            "token": "",
            "auth": {
                "user": "",
                "password": ""
            },
            "disabled": false
        }
    },
    "module-db-v2": {
        "pm2-health": {}
    }
}
  1. Save and close it
  2. Restart pm2-health:

pm2 restart pm2-health

Test it by restarting one of your PM2-managed Node processes. You should receive an email about that event.

Related