UNPKG

2.3 kBJavaScriptView Raw
1/**
2 * Copyright 2013-2021 the PM2 project authors. All rights reserved.
3 * Use of this source code is governed by a license that
4 * can be found in the LICENSE file.
5 */
6var http = require('http');
7var os = require('os');
8var pm2 = require('../index.js');
9var urlT = require('url');
10var cst = require('../constants.js');
11
12// Default, attach to default local PM2
13
14pm2.connect(function() {
15 startWebServer(pm2);
16});
17
18function startWebServer(pm2) {
19 http.createServer(function (req, res) {
20 // Add CORS headers to allow browsers to fetch data directly
21 res.setHeader('Access-Control-Allow-Origin', '*');
22 res.setHeader('Access-Control-Allow-Headers', 'Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With');
23 res.setHeader('Access-Control-Allow-Methods', 'GET');
24
25 // We always send json
26 res.setHeader('Content-Type','application/json');
27
28 var path = urlT.parse(req.url).pathname;
29
30 if (path == '/') {
31 // Main monit route
32 pm2.list(function(err, list) {
33 if (err) {
34 return res.send(err);
35 }
36 var data = {
37 system_info: { hostname: os.hostname(),
38 uptime: os.uptime()
39 },
40 monit: { loadavg: os.loadavg(),
41 total_mem: os.totalmem(),
42 free_mem: os.freemem(),
43 cpu: os.cpus(),
44 interfaces: os.networkInterfaces()
45 },
46 processes: list
47 };
48
49 if (cst.WEB_STRIP_ENV_VARS === true) {
50 for (var i = data.processes.length - 1; i >= 0; i--) {
51 var proc = data.processes[i];
52
53 // Strip important environment variables
54 if (typeof proc.pm2_env === 'undefined' && typeof proc.pm2_env.env === 'undefined') return;
55
56 delete proc.pm2_env.env;
57 }
58 }
59
60 res.statusCode = 200;
61 res.write(JSON.stringify(data));
62 return res.end();
63
64 })
65 }
66 else {
67 // 404
68 res.statusCode = 404;
69 res.write(JSON.stringify({err : '404'}));
70 return res.end();
71 }
72 }).listen(process.env.PM2_WEB_PORT || cst.WEB_PORT, cst.WEB_IPADDR, function() {
73 console.log('Web interface listening on %s:%s', cst.WEB_IPADDR, cst.WEB_PORT);
74 });
75
76}