Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/xcjs/blur-monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
xcjs committed Aug 27, 2016
2 parents 39540ed + 77f1d2d commit 639564a
Show file tree
Hide file tree
Showing 21 changed files with 413 additions and 143 deletions.
16 changes: 9 additions & 7 deletions blurmonitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

var Hapi = require('hapi');
var Inert = require('inert');
var argv = require('yargs').argv;
var path = require('path');

var env = require('./util/environment');
var startup = require('./util/startup');

var server = new Hapi.Server({
connections: {
Expand All @@ -24,7 +24,7 @@ server.connection({
port: env.port
});

server.register(Inert, () => {});
server.register(Inert);

var routers = [
require('./routes/static'),
Expand All @@ -43,10 +43,12 @@ routers.forEach(function (router) {
});
});

server.start((err) => {
if (err) {
throw err;
}
startup.run(function() {
server.start(function(err) {
if (err) {
throw err;
}

console.log('Server running at:', server.info.uri);
console.log('Server running at:', server.info.uri);
});
});
13 changes: 9 additions & 4 deletions blurmonitor/routes/disks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

var diskInfo = require('nodejs-disks');
var _ = require('lodash/array');

module.exports = getRoutes();

Expand Down Expand Up @@ -36,12 +37,14 @@ function getDisks() {
function (err, data) {
var i = 0;

data.forEach(function(item) {
var uniqueDrives = _.uniq(data, 'mountpoint');

uniqueDrives.forEach(function(item) {
item.id = i;
i++;
});

resolve(data);
resolve(uniqueDrives);
}
)
}
Expand All @@ -57,8 +60,10 @@ function getDisk(id) {
function (err, drives) {
diskInfo.drivesDetail(drives,
function (err, data) {
data[id].id = id;
resolve(data[id]);
var uniqueDrives = _.uniq(data, 'mountpoint');

uniqueDrives[id].id = id;
resolve(uniqueDrives[id]);
}
)
}
Expand Down
116 changes: 86 additions & 30 deletions blurmonitor/routes/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,96 @@ function getRoutes() {
}

function getMemoryStats() {
var free = spawn('free', []);
var free = spawn('free', ['-m']);

var promise = new Promise(function (resolve) {
var promise;
promise = new Promise(function (resolve) {
free.stdout.setEncoding("utf8");
free.stdout.on('data', function (data) {
var lines = data.toString().split(/\n/g),
line1 = lines[1].split(/\s+/),
line3 = lines[3].split(/\s+/),
total = parseInt(line1[1], 10),
free = parseInt(line1[3], 10),
buffers = parseInt(line1[5], 10),
cached = parseInt(line1[6], 10),
actualFree = free + buffers + cached,
actualUsed = total - actualFree,
swapTotal = parseInt(line3[1], 10),
swapUsed = parseInt(line3[2], 10),
swapFree = parseInt(line3[3], 10),
memory = {
total: total,
used: parseInt(line1[2], 10),
free: free,
shared: parseInt(line1[4], 10),
buffers: buffers,
cached: cached,
actualUsed: actualUsed,
actualFree: actualFree,
swapTotal: swapTotal,
swapUsed: swapUsed,
swapFree: swapFree
};

resolve(memory);
free.stdout.on('data', function (stdout) {
resolve(parseFree(stdout));
});
});

return promise;
}

function parseFree(stdout) {
if(stdout.indexOf('available') > -1) {
// >= 3.3.10
return parseNewFree(stdout);
} else {
// < 3.3.10
return parseOldFree(stdout);
}
}

function parseOldFree(stdout) {
var lines = stdout.toString().split(/\n/g);

var line1 = lines[1].split(/\s+/);
var line3 = lines[3].split(/\s+/);

var total = parseInt(line1[1], 10);
var free = parseInt(line1[3], 10);

// buffers + cache
var cache = parseInt(line1[5], 10) + parseInt(line1[6], 10);

var available = free + cache;
var used = total - available;

var shared = parseInt(line1[4], 10);

var swapTotal = parseInt(line3[1], 10);
var swapUsed = parseInt(line3[2], 10);
var swapFree = parseInt(line3[3], 10);

return {
total: total,
used: used,
free: free,
cache: cache,
available: available,
shared: shared,
swap: {
total: swapTotal,
used: swapUsed,
free: swapFree
}
};
}

function parseNewFree(stdout) {
var lines = stdout.toString().split(/\n/g);

var line1 = lines[1].split(/\s+/);
var line2 = lines[2].split(/\s+/);

var total = parseInt(line1[1], 10);
var used = parseInt(line1[2], 10);

var free = parseInt(line1[3], 10);
var cache = parseInt(line1[5], 10);

var available = parseInt(line1[6], 10);

var shared = parseInt(line1[4], 10);

var swapTotal = parseInt(line2[1], 10);
var swapUsed = parseInt(line2[2], 10);
var swapFree = parseInt(line2[3], 10);

return {
total: total,
used: used,
free: free,
cache: cache,
available: available,
shared: shared,
swap: {
total: swapTotal,
used: swapUsed,
free: swapFree
}
};
}
7 changes: 5 additions & 2 deletions blurmonitor/util/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
'use strict';

var argv = require('yargs').argv;
var conf = require('../../gulp/conf');
var path = require('path');

var environments = {
dev: 'dev',
Expand All @@ -15,6 +17,7 @@ module.exports = getEnvironment();

function getEnvironment() {
var env = {
conf: conf,
environments: environments,
current: getCurrentEnvironment(),
port: getPort(),
Expand All @@ -40,9 +43,9 @@ function getStaticRoot() {
var staticRoot = null;

if(currentEnvironment === environments.prod) {
staticRoot = './release/';
staticRoot = conf.paths.dist;
} else {
staticRoot = ['./.tmp/serve/', './src/'];
staticRoot = [path.join(conf.paths.tmp, 'serve'), conf.paths.src];
}

return staticRoot;
Expand Down
32 changes: 32 additions & 0 deletions blurmonitor/util/startup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*jslint node: true */
/*jslint esversion: 6 */

'use strict';

var env = require('./environment');
var fs = require('fs');
var fsExtra = require('fs.extra');
var path = require('path');

module.exports = {
run: run
};

function run(cb) {
loadDistributorLogo(cb);
}

function loadDistributorLogo(cb) {
var logoName = 'distributor-logo.png';
var systemDistributorLogo = path.join('/usr/share/icons/hicolor/48x48/apps', logoName);

var debugPath = path.join(env.conf.paths.tmp, 'serve', 'assets/img', logoName);
var releasePath = path.join(env.conf.paths.dist, 'assets/img', logoName);

try {
fsExtra.copy(systemDistributorLogo, debugPath, { replace: true }, cb);
fsExtra.copy(systemDistributorLogo, releasePath, { replace: true }, cb);
} catch(e) {
console.error(e);
}
}
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"angular-xeditable": "~0.1.9",
"ng-js-tree": "~0.0.7",
"angular-resource": "1.4.8",
"angular-moment": "^0.10.3"
"angular-moment": "^0.10.3",
"lodash": "^4.15.0"
},
"overrides": {
"amcharts": {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@
},
"dependencies": {
"array-includes": "^3.0.2",
"fs.extra": "^1.3.2",
"hapi": "^13.4.1",
"inert": "^4.0.0",
"lodash": "^3.10.1",
"node-json-transform": "^1.0.6",
"nodejs-disks": "^0.2.1",
"ps-node": "^0.1.1",
Expand Down
5 changes: 4 additions & 1 deletion src/app/config/blur-monitor.config.constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

angular.module('BlurMonitor.config')
.constant('moment', window.moment)
.constant('_', window._)
// The time interval to call the various services in milliseconds.
.constant('refreshInterval', 1000)
.constant('maxSnapshots', 20);
.constant('maxSnapshots', 20)
.constant('processorPercentageThreshold', 90)
.constant('memoryPercentageThreshold', 90);
})();
Loading

0 comments on commit 639564a

Please sign in to comment.