87b6f07e by Rafal Wilinski

:frog: Initial Commit

0 parents
/node_modules/
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js"></script>
</head>
<body>
<div style="width: 400px; margin: auto">
<canvas id="myChart" width="400" height="200"></canvas>
</div>
<script>
var socket = io('http://localhost:41338');
var labels = [0];
var dataset = [{
label: 'CPU Usage in %',
data: [0],
lineTension: 0,
pointRadius: 0
}];
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: dataset,
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
},
tooltips: {
enabled: false
}
}
});
socket.on('stats', function (data) {
console.log(data);
labels = data.osStats.map((point) => point.timestamp);
dataset[0].data = data.osStats.map((point) => point.cpu);
myChart.data.datasets[0].data.push(data.osStats[data.osStats.length - 1].cpu);
myChart.data.labels.push(data.osStats[data.osStats.length - 1].timestamp);
myChart.update();
});
</script>
</body>
</html>
\ No newline at end of file
(function () {
'use strict';
const path = require('path');
const onHeaders = require('on-headers');
const pidusage = require('pidusage');
const responseTimes = [];
const osStats = [];
const defaultConfig = {
socketPort: 41338,
path: '/status',
interval: 1,
retention: 30,
};
const gatherOsMetrics = (io) => {
pidusage.stat(process.pid, (err, stat) => {
stat.timestamp = Date.now();
osStats.push(stat);
sendMetrics(io);
});
};
const sendMetrics = (io) => {
osStats.slice(Math.max(osStats.length - defaultConfig.retention, 1));
io.emit('stats', {
osStats: osStats.slice(Math.max(osStats.length - defaultConfig.retention, 1)),
responseTimes
});
};
const middlewareWrapper = (config) => {
if (config === null || config === {} || config === undefined) {
config = defaultConfig;
}
if (config.path === undefined || !config instanceof String) {
config.path = defaultConfig.path;
}
if (config.socketPort === undefined || !config instanceof Number) {
config.socketPort = defaultConfig.socketPort;
}
if (config.interval === undefined || !config instanceof Number) {
config.interval = defaultConfig.interval;
}
const io = require('socket.io')(config.socketPort);
setInterval(() => gatherOsMetrics(io), config.interval * 1000);
io.on('connection', (socket) => {
});
return (req, res, next) => {
const startTime = process.hrtime();
if (req.path === config.path) {
res.sendFile(path.join(__dirname + '/index.html'));
} else {
onHeaders(res, () => {
var diff = process.hrtime(startTime);
var responseTime = diff[0] * 1e3 + diff[1] * 1e-6;
responseTimes.push({
endpoint: req.path,
responseTime,
timestamp: Date.now()
});
});
next();
}
};
};
module.exports = middlewareWrapper;
}());
\ No newline at end of file
{
"name": "node-monitor-status",
"version": "1.0.0",
"description": "Node Monitoring module.",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"node",
"status",
"monitoring"
],
"author": "Rafal Wilinski",
"license": "ISC",
"dependencies": {
"on-headers": "^1.0.1",
"pidusage": "^1.0.4",
"socket.io": "^1.4.8"
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!