Files
cloudron-box/src/views/system.js

288 lines
12 KiB
JavaScript
Raw Normal View History

2018-11-15 19:59:24 +01:00
'use strict';
2020-06-03 23:17:06 +02:00
/* global angular */
/* global $ */
/* global async */
/* global Chart */
2020-02-25 14:54:29 -08:00
angular.module('Application').controller('SystemController', ['$scope', '$location', '$timeout', 'Client', function ($scope, $location, $timeout, Client) {
2020-02-24 12:56:13 +01:00
Client.onReady(function () { if (!Client.getUserInfo().isAtLeastAdmin) $location.path('/'); });
2018-11-15 19:59:24 +01:00
2018-11-20 16:53:42 +01:00
$scope.config = Client.getConfig();
2020-01-28 09:37:25 -08:00
$scope.memory = null;
$scope.activeTab = 0;
2018-11-15 19:59:24 +01:00
2020-03-05 18:26:58 -08:00
// http://stackoverflow.com/questions/1484506/random-color-generator-in-javascript
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
var colorIndex = 0;
var colors = [ '#2196F3', '#3995b1', '#f0ad4e', '#ff4c4c' ];
function getNextColor() {
if (colors[colorIndex+1]) return colors[colorIndex++];
return getRandomColor();
}
2020-03-17 22:09:34 -07:00
$scope.reboot = {
busy: false,
show: function () {
$scope.reboot.busy = false;
$('#rebootModal').modal('show');
},
submit: function () {
$scope.reboot.busy = true;
Client.reboot(function (error) {
if (error) return Client.error(error);
$('#rebootModal').modal('hide');
// trigger refetch to show offline banner
$timeout(function () { Client.getStatus(function () {}); }, 8000);
});
}
};
2020-01-28 09:37:25 -08:00
2020-04-18 22:48:09 -07:00
$scope.disks = {
busy: true,
errorMessage: '',
disks: [],
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
setError: function (error) {
$scope.disks.errorMessage = 'Error loading disk : ' + error.message;
},
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
update: function () {
$scope.disks.busy = true;
// https://graphite.readthedocs.io/en/latest/render_api.html#paths-and-wildcards
// on scaleway, for some reason docker devices are collected as part of collectd
// until we figure why just hardcode popular disk devices - https://www.mjmwired.net/kernel/Documentation/devices.txt
Client.disks(function (error, result) {
if (error) return $scope.disks.setError(error);
// segregate locations into the correct disks based on 'filesystem'
result.disks.forEach(function (disk, index) {
disk.id = index;
disk.contains = [];
if (disk.filesystem === result.platformDataDisk) disk.contains.push({ label: 'Platform data', id: 'platformdata', usage: 0 });
if (disk.filesystem === result.boxDataDisk) disk.contains.push({ label: 'Box data', id: 'boxdata', usage: 0 });
if (disk.filesystem === result.dockerDataDisk) disk.contains.push({ label: 'Docker images', id: 'docker', usage: 0 });
if (disk.filesystem === result.mailDataDisk) disk.contains.push({ label: 'Email data', id: 'maildata', usage: 0 });
if (disk.filesystem === result.backupsDisk) disk.contains.push({ label: 'Backup data', id: 'cloudron-backup', usage: 0 });
const apps = Object.keys(result.apps).filter(function (appId) { return result.apps[appId] === disk.filesystem; });
apps.forEach(function (appId) {
var app = Client.getCachedAppSync(appId);
disk.contains.push({ app: app, label: app.label || app.fqdn, id: appId, usage: 0 });
2020-03-05 18:26:58 -08:00
});
2020-04-18 22:48:09 -07:00
});
$scope.disks.disks = result.disks; // [ { filesystem, type, size, used, available, capacity, mountpoint }]
// render data of each disk
2020-06-03 23:17:06 +02:00
async.eachSeries(result.disks, function (disk, iteratorCallback) {
2020-04-18 22:48:09 -07:00
// /dev/sda1 -> sda1
// /dev/mapper/foo -> mapper_foo (see #348)
var diskName = disk.filesystem.slice(disk.filesystem.indexOf('/', 1) + 1);
diskName = diskName.replace(/\//g, '_');
// use collectd instead of df data so the timeframe matches with the du data
Client.graphs([
'absolute(collectd.localhost.df-' + diskName + '.df_complex-free)',
'absolute(collectd.localhost.df-' + diskName + '.df_complex-reserved)', // reserved for root (default: 5%) tune2fs -l/m
'absolute(collectd.localhost.df-' + diskName + '.df_complex-used)'
], '-1min', {}, function (error, data) {
if (error) return iteratorCallback(error);
disk.size = data[2].datapoints[0][0] + data[1].datapoints[0][0] + data[0].datapoints[0][0];
disk.free = data[0].datapoints[0][0];
disk.occupied = data[2].datapoints[0][0];
colorIndex = 0;
disk.contains.forEach(function (content) {
content.color = getNextColor();
});
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
// get disk usage data
var graphiteQueries = disk.contains.map(function (content) {
return 'absolute(collectd.localhost.du-' + content.id + '.capacity-usage)';
});
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
Client.graphs(graphiteQueries, '-1day', { noNullPoints: true }, function (error, data) {
if (error) return iteratorCallback(error);
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
var usageOther = disk.occupied;
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
data.forEach(function (d) {
var content = disk.contains.find(function (content) { return d.target.indexOf(content.id) !== -1; });
if (!content) return; // didn't match any content
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
var tmp = d.datapoints[d.datapoints.length-1][0];
content.usage = tmp;
2020-03-05 18:26:58 -08:00
2020-04-18 22:48:09 -07:00
// deduct from overal disk usage to track other
usageOther -= tmp;
});
2020-04-03 10:41:04 -07:00
disk.contains.sort(function (x, y) { return y.usage - x.usage; }); // sort by usage
if ($scope.disks.disks[0] === disk) { // the root mount point is the first disk. keep this 'contains' in the end
2020-04-18 22:48:09 -07:00
disk.contains.push({
label: 'Everything else (Ubuntu, Swap, etc)',
id: 'other',
color: '#27CE65',
usage: usageOther
});
} else {
disk.contains.push({
label: 'Used',
id: 'other',
color: '#27CE65',
usage: usageOther
});
2020-04-18 22:48:09 -07:00
}
iteratorCallback();
});
2020-03-05 18:26:58 -08:00
});
2020-04-18 22:48:09 -07:00
}, function iteratorDone(error) {
if (error) $scope.disks.setError(error);
$scope.disks.busy = false;
2020-03-05 18:26:58 -08:00
});
});
2020-04-18 22:48:09 -07:00
}
};
2020-01-28 09:37:25 -08:00
$scope.graphs = {
error: {},
period: 6,
periodLabel: '6 hours',
memoryChart: null,
diskChart: null,
setPeriod: function (hours, label) {
$scope.graphs.period = hours;
$scope.graphs.periodLabel = label;
$scope.graphs.show();
},
show: function () {
2020-05-13 23:34:14 +02:00
var apps = Client.getInstalledApps();
// both in minutes
var timePeriod = $scope.graphs.period * 60;
var timeBucketSize = $scope.graphs.period > 24 ? (6*60) : 5;
2020-05-13 23:34:14 +02:00
function fillGraph(canvasId, data, additionalData, label, chartPropertyName, max, valueDivider) {
// translate the data from bytes to MB
var datapoints = data.datapoints.map(function (d) { return parseInt((d[0] / valueDivider).toFixed(2)); });
var labels = datapoints.map(function (d, index) {
var dateTime = new Date(Date.now() - ((timePeriod - (index * timeBucketSize)) * 60 *1000));
2020-09-02 18:53:46 +02:00
if ($scope.graphs.period > 24) {
return dateTime.toLocaleDateString();
} else {
return dateTime.toLocaleTimeString();
}
});
2020-05-13 23:34:14 +02:00
var datasets = [{
label: label,
backgroundColor: '#82C4F844',
borderColor: '#2196F3',
borderWidth: 1,
radius: 0,
data: datapoints
}];
if (Array.isArray(additionalData)) {
additionalData.forEach(function (data, index) {
datasets.push({
label: apps[index].fqdn,
fill: false,
borderColor: getRandomColor(),
borderWidth: 1,
radius: 0,
data: data.datapoints.map(function (d) { return parseInt((d[0] / valueDivider).toFixed(2)); })
});
});
}
var data = {
labels: labels,
2020-05-13 23:34:14 +02:00
datasets: datasets
};
var options = {
maintainAspectRatio: true,
aspectRatio: 2.5,
legend: {
display: false
},
tooltips: {
intersect: false
},
scales: {
2020-09-02 18:53:46 +02:00
xAxes: [{
ticks: {
autoSkipPadding: 20,
}
}],
yAxes: [{
ticks: {
min: 0,
max: max,
beginAtZero: true
}
}]
}
};
var ctx = $(canvasId).get(0).getContext('2d');
if ($scope.graphs[chartPropertyName]) $scope.graphs[chartPropertyName].destroy();
$scope.graphs[chartPropertyName] = new Chart(ctx, { type: 'line', data: data, options: options });
}
var cpuQuery = 'summarize(sum(collectd.localhost.aggregation-cpu-average.cpu-system, collectd.localhost.aggregation-cpu-average.cpu-user), "' + timeBucketSize + 'min", "avg")';
2020-05-13 23:34:14 +02:00
var systemMemoryQuery = 'summarize(sum(collectd.localhost.memory.memory-used, collectd.localhost.swap.swap-used), "' + timeBucketSize + 'min", "avg")';
var appQueries = [];
apps.forEach(function (app) {
appQueries.push('summarize(collectd.localhost.table-' + app.id + '-memory.gauge-rss, "' + timeBucketSize + 'min", "avg")')
});
2020-05-13 23:34:14 +02:00
Client.graphs([ cpuQuery, systemMemoryQuery ].concat(appQueries), '-' + timePeriod + 'min', {}, function (error, result) {
if (error) return console.error(error);
2020-05-13 23:34:14 +02:00
fillGraph('#graphsCPUChart', result[0], null, 'CPU Usage', 'cpuChart', 100, 1);
fillGraph('#graphsSystemMemoryChart', result[1], result.slice(2), 'Memory', 'memoryChart', Number.parseInt(($scope.memory.memory + $scope.memory.swap) / 1024 / 1024), 1024 * 1024);
});
}
};
2018-11-18 20:01:53 +01:00
Client.onReady(function () {
2020-03-05 18:26:58 -08:00
Client.memory(function (error, memory) {
if (error) console.error(error);
2018-11-18 20:01:53 +01:00
2020-03-05 18:26:58 -08:00
$scope.memory = memory;
2020-04-18 22:48:09 -07:00
$scope.disks.update();
2018-11-18 20:01:53 +01:00
});
});
2020-03-17 22:09:34 -07:00
Client.onReconnect(function () {
$scope.reboot.busy = false;
});
2018-11-15 19:59:24 +01:00
}]);