1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var http = require('http');
|
7 | var os = require('os');
|
8 | var pm2 = require('../index.js');
|
9 | var urlT = require('url');
|
10 | var cst = require('../constants.js');
|
11 |
|
12 |
|
13 |
|
14 | pm2.connect(function() {
|
15 | startWebServer(pm2);
|
16 | });
|
17 |
|
18 | function startWebServer(pm2) {
|
19 | http.createServer(function (req, res) {
|
20 |
|
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 |
|
26 | res.setHeader('Content-Type','application/json');
|
27 |
|
28 | var path = urlT.parse(req.url).pathname;
|
29 |
|
30 | if (path == '/') {
|
31 |
|
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 |
|
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 |
|
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 | }
|