Nodejs|pm2监控进程挂掉或重启邮件通知

之前有写过[进程守护工具pm2基本使用],进程管理起来了,如果意外或者达到了内存使用限制会帮我们自动重启,那么有个问题来了,怎么能知道守护的进程重启了呢?pm2有个插件pm2-health可以实现邮件告警的功能,一起来学习一下.
官方地址:https://github.com/pankleks/pm2-health

准备
先查看一下当前的npm源,因为我在阿里云,所以会先切换到淘宝源,速度快一些

nrm ls

* npm -------- https://registry.npmjs.org/
  yarn ------- https://registry.yarnpkg.com/
  cnpm ------- http://r.cnpmjs.org/
  taobao ----- https://registry.npm.taobao.org/
  nj --------- https://registry.nodejitsu.com/
  npmMirror -- https://skimdb.npmjs.com/registry/
  edunpm ----- http://registry.enpmjs.org/

nrm use taobao

   Registry has been set to: https://registry.npm.taobao.org/

安装

pm2 install pm2-health

[PM2][Module] Installing NPM pm2-health module
[PM2][Module] Calling [NPM] to install pm2-health ...
+ pm2-health@1.11.8
added 322 packages from 261 contributors in 13.601s

2 packages are looking for funding
  run `npm fund` for details

[PM2][Module] Module downloaded
[PM2][WARN] Applications App not running, starting...
[PM2] App [pm2-health] launched (1 instances)
Module: pm2-health
$ pm2 set pm2-health:smtp [object Object]
$ pm2 set pm2-health:mailTo mail1,mail2
$ pm2 set pm2-health:replyTo
$ pm2 set pm2-health:batchPeriodM 0
$ pm2 set pm2-health:batchMaxMessages 0
$ pm2 set pm2-health:events exit
$ pm2 set pm2-health:exceptions true
$ pm2 set pm2-health:messages true
$ pm2 set pm2-health:messageExcludeExps
$ pm2 set pm2-health:metric [object Object]
$ pm2 set pm2-health:metricIntervalS 60
$ pm2 set pm2-health:aliveTimeoutS 300
$ pm2 set pm2-health:addLogs true
$ pm2 set pm2-health:appsExcluded
$ pm2 set pm2-health:snapshot [object Object]
Modules configuration. Copy/Paste line to edit values.
[PM2][Module] Module successfully installed and launched
[PM2][Module] Checkout module options: `$ pm2 conf`

第一次竟然没成功,再执行一次pm2 install pm2-health就好了,估计可能有些包阿里源里也没有,拉取过程中超时了.

配置
默认会将以下配置文件添加到~/.pm2/module_conf.json.我们直接把pm2-helath下的smtp那段填对就好了.

vim ~/.pm2/module_conf.json

{
    "module-db-v2": {
        "pm2-metrics": {},
        "pm2-health": {}
    },
    "pm2-health": {
        "smtp": {
            "host": "your-smtp-host",
            "port": 587,
            "user": "your-smtp-user",
            "password": "your-smtp-password",
            "disabled": false
        },
        "mailTo": "mail1,mail2",
        "replyTo": "",
        "batchPeriodM": 0,
        "batchMaxMessages": 0,
        "events": [
            "exit"
        ],
        "exceptions": true,
        "messages": true,
        "messageExcludeExps": [],
        "metric": {},
        "metricIntervalS": 60,
        "aliveTimeoutS": 300,
        "addLogs": true,
        "appsExcluded": [],
        "snapshot": {
            "url": "",
            "token": "",
            "auth": {
                "user": "",
                "password": ""
            },
            "disabled": false
        }
    }
}

正常应该比较简单吧,但是我用的qq邮箱,现在qq邮箱已经启用授权码了,操作方法

https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256%27)

正确的配置

"smtp": {
    "host": "smtp.qq.com",
    "port": 587,
    "user": "27844xxxx@qq.com",
    "password": "xxxxx",
    "disabled": false
},

要注意的是port竟然必须用587,password为授权码

重启pm2-health

pm2 restart pm2-health

这步是必须的,pm2-health只有在启动时才会读取配置

验证
这步用来验证邮箱配置对不对,正常提示如下,不正常的话,直接看pm2-health的日志吧

pm2 trigger pm2-health mail
1 processes have received command mail
[pm2-health:3]="mail send"

查看日志

pm2 log pm2-health
[TAILING] Tailing last 15 lines for [pm2-health] process (change the value with --lines option)
/root/.pm2/logs/pm2-health-error.log last 15 lines:
3|pm2-heal | 2020-05-01T03:20:45.904Z: can't send mail -> queryA ESERVFAIL your-smtp-host
3|pm2-heal | 2020-05-01T03:21:37.660Z: can't send mail -> queryA ESERVFAIL your-smtp-host
3|pm2-heal | 2020-05-01T03:23:34.722Z: can't send mail -> Greeting never received
3|pm2-heal | 2020-05-01T03:25:17.602Z: can't send mail -> Greeting never received
3|pm2-heal | 2020-05-01T03:27:49.334Z: can't send mail -> Greeting never received
3|pm2-heal | 2020-05-01T03:30:31.930Z: can't send mail -> Greeting never received
3|pm2-heal | 2020-05-01T03:44:53.682Z: can't send mail -> Greeting never received
3|pm2-heal | 2020-05-01T03:49:28.700Z: can't send mail -> queryA ESERVFAIL your-smtp-host

收到测试邮件

126的邮箱配置略有不同,需要增加"secure": true.

"smtp": {
    "host": "smtp.126.com",
    "port": 465,
    "user": "test_ufw@126.com",
    "secure": true,
    "password": "HVVPWKJERCCHVKDX",
    "disabled": false
},

pm2-prometheus


相关博文

About rainbird

IOS攻城狮
This entry was posted in NodeJS and tagged , , , , , , , , , , , . Bookmark the permalink.

发表评论