Initial Commit
0 parents
Showing
4 changed files
with
158 additions
and
0 deletions
.gitignore
0 → 100644
1 | /node_modules/ | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
index.html
0 → 100644
1 | <!DOCTYPE html> | ||
2 | <html> | ||
3 | <head> | ||
4 | <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.min.js"></script> | ||
5 | <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js"></script> | ||
6 | </head> | ||
7 | <body> | ||
8 | <div style="width: 400px; margin: auto"> | ||
9 | <canvas id="myChart" width="400" height="200"></canvas> | ||
10 | </div> | ||
11 | <script> | ||
12 | var socket = io('http://localhost:41338'); | ||
13 | var labels = [0]; | ||
14 | var dataset = [{ | ||
15 | label: 'CPU Usage in %', | ||
16 | data: [0], | ||
17 | lineTension: 0, | ||
18 | pointRadius: 0 | ||
19 | }]; | ||
20 | |||
21 | var ctx = document.getElementById("myChart"); | ||
22 | var myChart = new Chart(ctx, { | ||
23 | type: 'line', | ||
24 | data: { | ||
25 | labels: labels, | ||
26 | datasets: dataset, | ||
27 | }, | ||
28 | options: { | ||
29 | scales: { | ||
30 | yAxes: [{ | ||
31 | ticks: { | ||
32 | beginAtZero: true | ||
33 | } | ||
34 | }], | ||
35 | }, | ||
36 | tooltips: { | ||
37 | enabled: false | ||
38 | } | ||
39 | } | ||
40 | }); | ||
41 | |||
42 | socket.on('stats', function (data) { | ||
43 | console.log(data); | ||
44 | labels = data.osStats.map((point) => point.timestamp); | ||
45 | dataset[0].data = data.osStats.map((point) => point.cpu); | ||
46 | |||
47 | myChart.data.datasets[0].data.push(data.osStats[data.osStats.length - 1].cpu); | ||
48 | myChart.data.labels.push(data.osStats[data.osStats.length - 1].timestamp); | ||
49 | myChart.update(); | ||
50 | }); | ||
51 | </script> | ||
52 | </body> | ||
53 | </html> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
index.js
0 → 100644
1 | (function () { | ||
2 | 'use strict'; | ||
3 | |||
4 | const path = require('path'); | ||
5 | const onHeaders = require('on-headers'); | ||
6 | const pidusage = require('pidusage'); | ||
7 | const responseTimes = []; | ||
8 | const osStats = []; | ||
9 | |||
10 | const defaultConfig = { | ||
11 | socketPort: 41338, | ||
12 | path: '/status', | ||
13 | interval: 1, | ||
14 | retention: 30, | ||
15 | }; | ||
16 | |||
17 | const gatherOsMetrics = (io) => { | ||
18 | pidusage.stat(process.pid, (err, stat) => { | ||
19 | stat.timestamp = Date.now(); | ||
20 | osStats.push(stat); | ||
21 | sendMetrics(io); | ||
22 | }); | ||
23 | }; | ||
24 | |||
25 | const sendMetrics = (io) => { | ||
26 | osStats.slice(Math.max(osStats.length - defaultConfig.retention, 1)); | ||
27 | |||
28 | io.emit('stats', { | ||
29 | osStats: osStats.slice(Math.max(osStats.length - defaultConfig.retention, 1)), | ||
30 | responseTimes | ||
31 | }); | ||
32 | }; | ||
33 | |||
34 | const middlewareWrapper = (config) => { | ||
35 | if (config === null || config === {} || config === undefined) { | ||
36 | config = defaultConfig; | ||
37 | } | ||
38 | |||
39 | if (config.path === undefined || !config instanceof String) { | ||
40 | config.path = defaultConfig.path; | ||
41 | } | ||
42 | |||
43 | if (config.socketPort === undefined || !config instanceof Number) { | ||
44 | config.socketPort = defaultConfig.socketPort; | ||
45 | } | ||
46 | |||
47 | if (config.interval === undefined || !config instanceof Number) { | ||
48 | config.interval = defaultConfig.interval; | ||
49 | } | ||
50 | |||
51 | const io = require('socket.io')(config.socketPort); | ||
52 | |||
53 | setInterval(() => gatherOsMetrics(io), config.interval * 1000); | ||
54 | |||
55 | io.on('connection', (socket) => { | ||
56 | |||
57 | }); | ||
58 | |||
59 | return (req, res, next) => { | ||
60 | const startTime = process.hrtime(); | ||
61 | if (req.path === config.path) { | ||
62 | res.sendFile(path.join(__dirname + '/index.html')); | ||
63 | } else { | ||
64 | onHeaders(res, () => { | ||
65 | var diff = process.hrtime(startTime); | ||
66 | var responseTime = diff[0] * 1e3 + diff[1] * 1e-6; | ||
67 | |||
68 | responseTimes.push({ | ||
69 | endpoint: req.path, | ||
70 | responseTime, | ||
71 | timestamp: Date.now() | ||
72 | }); | ||
73 | |||
74 | }); | ||
75 | |||
76 | next(); | ||
77 | } | ||
78 | }; | ||
79 | }; | ||
80 | |||
81 | module.exports = middlewareWrapper; | ||
82 | |||
83 | }()); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
package.json
0 → 100644
1 | { | ||
2 | "name": "node-monitor-status", | ||
3 | "version": "1.0.0", | ||
4 | "description": "Node Monitoring module.", | ||
5 | "main": "app.js", | ||
6 | "scripts": { | ||
7 | "test": "echo \"Error: no test specified\" && exit 1" | ||
8 | }, | ||
9 | "keywords": [ | ||
10 | "node", | ||
11 | "status", | ||
12 | "monitoring" | ||
13 | ], | ||
14 | "author": "Rafal Wilinski", | ||
15 | "license": "ISC", | ||
16 | "dependencies": { | ||
17 | "on-headers": "^1.0.1", | ||
18 | "pidusage": "^1.0.4", | ||
19 | "socket.io": "^1.4.8" | ||
20 | } | ||
21 | } |
-
Please register or sign in to post a comment