`
liue
  • 浏览: 19388 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Nodejs monitoring using PM2

 
阅读更多
Come from:https://codeforgeek.com/2016/02/nodejs-monitoring-using-pm2/

Nodejs monitoring in production server is very important and critical part in overall Nodejs application development. Unlike staging or development scenario, if anything goes wrong in production server it directly affects the end-user and most importantly your client. In this tutorial we will look over how to properly monitor our Nodejs server after deploying it to production using PM2 node module and keymetrics as analysis platform.

What is PM2

PM2 is a process manager for Nodejs application. It comes up with its own load balancer. It make sure that your Nodejs application is fully available to end-user by reloading them in case of exception.



Installing PM2

Installing PM2 is easy and you can do this by using following command.

npm install -g pm2@latest
PM2 should be installed as a global node module so that’s what -g will do. You may need to pass sudo access to it in case you are using Mac or Linux-based system.

Integrating PM2 with Nodejs

Before doing the integration let’s create one basic Node.js app using Express.

Use npm init to create a package.json.

package.json
{
  "name": "pm2demo",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
Let’s install Express module. Run following command.

npm install --save express
Let’s create a basic server and integrate pm2 with it.

app.js
var express = require('express');
var app = express();
var router = express.Router();

router.get('/',function(req,res) {
    res.send("Hello World");
});

app.use('/',router);

app.listen(3000);
In order to run this app, we generally using npm or node command. But instead we are going to run this using PM2. Here is a command to do so.

pm2 start app.js
PM2 application server monitoring

You can run following command to check the status of the app every time.

pm2 status
Visit localhost:300 to view the app.

Now if your app crashes due to any issue, PM2 will restart it automatically. In order to stop the app you can run following command.

pm2 stop <app name> OR <app id>
In order to stop all process at once, run following command.

pm2 stop all
To stop the PM2 itself, run following command.

pm2 kill
To monitor the process live, run following command.

pm2 monit
PM2 monitor

This feed will update live as your application keeps running and used.

Integrating PM2 with Keymetrics

Keymetrics is a web platform to monitor your application using PM2. We just had a look above about monitor command in PM2. How about having that live information in your browser ? Pretty amazing to me. Go checkout keymetrics and sign up for new account.

Once you sign up, create a new Bucket.

Node.js monitoring

After this, Keymetrics will provide you the URL by which you can link your app to Keymetrics.

Keymetrics account

Just copy the command and paste it in your terminal.

Linking PM2 app

This is it, within a second you can see the live update of your application in your browser.

Nodejs monitoring using keymetrics

Keymetrics provides various features and admin option which you can use to perform Nodejs monitoring. Let’s have a look over some of them.

CPU and Memory usage

In the main dashboard you can see and visualize the amount of memory and CPU your app is consuming. This data is also live and you can see the comparative graph as well.

Nodejs monitoring using PM2

Exception reporting

This is one of the coolest part which i personally like more. In case your application crashes due to some exception, PM2 will report it to keymetrics and keymetrics will immediately send email to your account and will show the exception detail in dashboard.

In order to replicate it, let’s put some exception in our code. For some reason, throwing an exception is not working so I am going to put one syntax error. In our route code place extra comma like this.

router.get('/',,function(req,res) {
    res.send("Hello World");
});
Now restart the app using following command.

pm2 restart app.js
Hit the route using by visiting localhost:3000 and you should receive an email immediately like this.

Nodejs monitoring using PM2

Also you can see the exception in dashboard.

Nodejs monitoring using PM2

Other than Syntax error, it also catches exception which are dynamic such as call stack size exceeded which we discovered in different application.

Nodejs monitoring using PM2

Monitoring custom events

Other than exception, you can also register for custom events such as “10000 user registered” or anything suitable to your app. You need to install add-on for called pmx in order to emit custom event. Install it using following command.

npm install --save pmx
Then require it in your code and emit the events like this.

var pmx = require('pmx');

pmx.emit("Event name","event detail,supports JSON too");
Wrapping it up

PM2 and Keymetrics together is great tool for Nodejs monitoring in production server. Other than a fact that it keeps your application alive, it also keeps checking for exception and memory, CPU usage. You can also monitor logs in your browser and can restart your application as well.
分享到:
评论

相关推荐

    nodejs高大上的部署方式(PM2)

    目前似乎最常见的线上部署nodejs项目的有forever,pm2这两种。 使用场合: supervisor是开发环境用。 forever管理多个站点,每个站点访问量不大,不需要监控。 nodemon 是开发环境使用,修改自动重启。 pm2 网站...

    nodejs进程通信--pm2 cluster方式

    nodejs进程通信--pm2 cluster方式, pm2版本 2.4.2,node 版本 v7.5.0

    nodejs服务器进程管理工具pm2Manage.zip

    pm2Manage nodejs 服务器进程管理工具 version 0.2.3 pm2 manage 是nodejs的服务器管理工具demo目前实现了期监控功能模块依赖 angularjs nodejs less bootstrap 支持IE8 功能展望 目前pm2已经开放了...

    linux指定用户安装nodejs_pm2,pm2日志定时打包_清理_切换moja用户_moja_shell.zip

    linux指定用户安装nodejs_pm2,pm2日志定时打包_清理_切换moja用户_moja_shell

    ubuntu系统下使用pm2设置nodejs开机自启动的方法

    1.安装pm2 : npm install pm2 -gd 这时在命令行下执行pm2命令可能找不到,需要执行如下命令 1.创建软链接:ln -s /home/XXX/node-node-v6.11.5-linux-x64/bin/pm2 /usr/local/bin/pm2 2.一下命令需要在root用户下...

    nodejs ssh2基本功能的封装

    nodejs ssh2基本功能的封装,连接,下载,上传文件,下载文件,上传文件夹,下载文件夹,创建目录,删除目录,运行shell命令

    Simple-PM-2.5-Monitor:用 NodeJS 编写的 PM2.5 监视器和可视化器

    Simple-PM-2.5-Monitor 基于 NodeJS 和 jqplot PM2.5数据来源: : pgn=32&bt=443314230825975809&st=441909971714727936&id= (这个网站被GFW屏蔽了,所以需要GAE...) 提供上海PM2.5数据实时信息。

    nodejs oauth 2 例子代码

    nodejs 搭建oauth服务器的代码 Install nodejs and npm and then, simply run npm install and npm start. The server should now be running at http://localhost:3000.

    nodejs.pdf nodejs初级教程 nodejs初级教程

    nodejs初级教程

    node-v12.18.0-linux-x64.tar.xz 包含pm2离线包

    包含pm2到node离线包, ### node环境 1. 传输node-v12.18.0-linux-x64.tar.xz到服务器 /home/用户名/node 2. 解压并重命名 1. `tar -xvf node-v12.18.0-linux-x64.tar.xz` 2. `mv node-v12.18.0-linux-x64 nodejs`...

    rh-nodejs14-nodejs-14.17.2-1.el7.x86_64.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    nodejs14.9.0

    nodejs14.9.0

    NodeJS学习笔记和代码

    2. **模块系统**:NodeJS使用CommonJS模块规范,通过`require`引入模块,`exports`或`module.exports`导出模块,便于代码组织和复用。 3. **V8引擎**:NodeJS的高性能得益于Google的V8引擎,它可以将JavaScript代码...

    nodejs:NodeJS示例项目

    节点js NodeJS示例项目

    NodeJS整理手册文档

    2. Express框架:简化了Web应用开发,提供了路由、中间件等功能,是NodeJS最流行的Web框架之一。 3. Socket.IO:实现实时通信,支持WebSocket,广泛应用于实时应用如聊天、游戏等。 4. Promise和async/await:处理...

    nodejs11.zip

    nodejs11安装文件,解决win7操作系统nodeJs环境搭建失败问题:Node.js is only supported on Windows 8.1, Windows Server 2012 R2, or higher

    nodejs2.zip

    nodejs入门到实战(一、二、三)源代码,对应分类:https://blog.csdn.net/zengraoli/category_10327904.html 中的文章 nodejs入门到实战(一)、nodejs入门到实战(二)、nodejs入门到实战(三)

    NodeJS开发指南_nodejs开发指南_

    2. 第一个NodeJS程序:通常,NodeJS程序始于一个`.js`文件,通过`console.log()`输出信息。例如: ```javascript console.log('Hello, World!'); ``` 二、模块系统 NodeJS采用CommonJS规范,支持模块化开发。`...

    NodeJS示例

    2. 安装过程中,你可以选择自定义安装路径,例如 `C:\Program Files\nodejs`。默认情况下,安装程序会自动将 NodeJS 添加到系统环境变量中,使得在任何目录下都可以通过命令行调用 `node` 和 `npm` 命令。 3. 安装...

Global site tag (gtag.js) - Google Analytics