2017-01-24 22:57:08 +00:00
|
|
|
// Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>
|
2017-01-15 23:52:22 +00:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
var Console = (function () {
|
|
|
|
var CONSOLE_PUSH_COUNT = 50;
|
|
|
|
var CONSOLE_PUSH_FREQ = 200;
|
2017-01-15 23:52:22 +00:00
|
|
|
|
2017-01-26 21:12:33 +00:00
|
|
|
var terminalQueue;
|
|
|
|
var terminal;
|
2017-03-17 23:05:54 +00:00
|
|
|
var recievedInitialLog = false;
|
2017-01-15 23:52:22 +00:00
|
|
|
|
2017-01-26 21:20:04 +00:00
|
|
|
var cpuChart;
|
|
|
|
var cpuData;
|
|
|
|
var memoryChart;
|
|
|
|
var memoryData;
|
|
|
|
var timeLabels;
|
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
var $terminalNotify;
|
|
|
|
|
2017-01-26 21:12:33 +00:00
|
|
|
function initConsole() {
|
2017-01-26 21:57:33 +00:00
|
|
|
terminalQueue = [];
|
2017-01-26 21:12:33 +00:00
|
|
|
terminal = $('#terminal').terminal(function (command, term) {
|
|
|
|
Socket.emit('send command', command);
|
|
|
|
}, {
|
|
|
|
greetings: '',
|
|
|
|
name: Pterodactyl.server.uuid,
|
|
|
|
height: 450,
|
|
|
|
exit: false,
|
|
|
|
prompt: Pterodactyl.server.username + ':~$ ',
|
|
|
|
scrollOnEcho: false,
|
|
|
|
scrollBottomOffset: 5,
|
|
|
|
onBlur: function (terminal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2017-01-26 21:57:33 +00:00
|
|
|
|
|
|
|
$terminalNotify = $('#terminalNotify');
|
|
|
|
$terminalNotify.on('click', function () {
|
|
|
|
terminal.scroll_to_bottom();
|
|
|
|
$terminalNotify.addClass('hidden');
|
|
|
|
})
|
|
|
|
|
|
|
|
terminal.on('scroll', function () {
|
|
|
|
if (terminal.is_bottom()) {
|
|
|
|
$terminalNotify.addClass('hidden');
|
|
|
|
}
|
|
|
|
})
|
2017-01-26 21:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initGraphs() {
|
|
|
|
var ctc = $('#chart_cpu');
|
2017-01-26 21:57:33 +00:00
|
|
|
timeLabels = [];
|
|
|
|
cpuData = [];
|
|
|
|
cpuChart = new Chart(ctc, {
|
2017-01-26 21:20:04 +00:00
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: timeLabels,
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label: "Percent Use",
|
|
|
|
fill: false,
|
|
|
|
lineTension: 0.03,
|
2017-01-26 21:57:33 +00:00
|
|
|
backgroundColor: "#3c8dbc",
|
|
|
|
borderColor: "#3c8dbc",
|
2017-01-26 21:20:04 +00:00
|
|
|
borderCapStyle: 'butt',
|
|
|
|
borderDash: [],
|
|
|
|
borderDashOffset: 0.0,
|
|
|
|
borderJoinStyle: 'miter',
|
2017-01-26 21:57:33 +00:00
|
|
|
pointBorderColor: "#3c8dbc",
|
2017-01-26 21:20:04 +00:00
|
|
|
pointBackgroundColor: "#fff",
|
|
|
|
pointBorderWidth: 1,
|
|
|
|
pointHoverRadius: 5,
|
2017-01-26 21:57:33 +00:00
|
|
|
pointHoverBackgroundColor: "#3c8dbc",
|
2017-01-26 21:20:04 +00:00
|
|
|
pointHoverBorderColor: "rgba(220,220,220,1)",
|
|
|
|
pointHoverBorderWidth: 2,
|
|
|
|
pointRadius: 1,
|
|
|
|
pointHitRadius: 10,
|
|
|
|
data: cpuData,
|
|
|
|
spanGaps: false,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'CPU Usage (as Percent Total)'
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
animation: {
|
|
|
|
duration: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var ctm = $('#chart_memory');
|
2017-01-26 21:57:33 +00:00
|
|
|
memoryData = [];
|
|
|
|
memoryChart = new Chart(ctm, {
|
2017-01-26 21:20:04 +00:00
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: timeLabels,
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
label: "Memory Use",
|
|
|
|
fill: false,
|
|
|
|
lineTension: 0.03,
|
2017-01-26 21:57:33 +00:00
|
|
|
backgroundColor: "#3c8dbc",
|
|
|
|
borderColor: "#3c8dbc",
|
2017-01-26 21:20:04 +00:00
|
|
|
borderCapStyle: 'butt',
|
|
|
|
borderDash: [],
|
|
|
|
borderDashOffset: 0.0,
|
|
|
|
borderJoinStyle: 'miter',
|
2017-01-26 21:57:33 +00:00
|
|
|
pointBorderColor: "#3c8dbc",
|
2017-01-26 21:20:04 +00:00
|
|
|
pointBackgroundColor: "#fff",
|
|
|
|
pointBorderWidth: 1,
|
|
|
|
pointHoverRadius: 5,
|
2017-01-26 21:57:33 +00:00
|
|
|
pointHoverBackgroundColor: "#3c8dbc",
|
2017-01-26 21:20:04 +00:00
|
|
|
pointHoverBorderColor: "rgba(220,220,220,1)",
|
|
|
|
pointHoverBorderWidth: 2,
|
|
|
|
pointRadius: 1,
|
|
|
|
pointHitRadius: 10,
|
|
|
|
data: memoryData,
|
|
|
|
spanGaps: false,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'Memory Usage (in Megabytes)'
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
display: false,
|
|
|
|
},
|
|
|
|
animation: {
|
|
|
|
duration: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-26 21:12:33 +00:00
|
|
|
|
2017-01-26 21:20:04 +00:00
|
|
|
function addSocketListeners() {
|
|
|
|
// Update Listings on Initial Status
|
2017-01-26 21:12:33 +00:00
|
|
|
Socket.on('initial status', function (data) {
|
2017-03-17 23:05:54 +00:00
|
|
|
if (! recievedInitialLog) {
|
|
|
|
updateServerPowerControls(data.status);
|
2017-01-26 21:20:04 +00:00
|
|
|
|
2017-03-17 23:05:54 +00:00
|
|
|
if (data.status === 1 || data.status === 2) {
|
|
|
|
Socket.emit('send server log');
|
|
|
|
}
|
2017-01-26 21:12:33 +00:00
|
|
|
}
|
|
|
|
});
|
2017-01-26 21:20:04 +00:00
|
|
|
|
|
|
|
// Update Listings on Status
|
|
|
|
Socket.on('status', function (data) {
|
|
|
|
updateServerPowerControls(data.status);
|
|
|
|
});
|
|
|
|
|
2017-03-17 23:05:54 +00:00
|
|
|
Socket.on('server log', function (data) {
|
|
|
|
if (! recievedInitialLog) {
|
|
|
|
terminal.clear();
|
|
|
|
terminalQueue.push(data);
|
|
|
|
recievedInitialLog = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
Socket.on('console', function (data) {
|
|
|
|
terminalQueue.push(data.line);
|
|
|
|
});
|
|
|
|
|
2017-01-26 21:20:04 +00:00
|
|
|
Socket.on('proc', function (proc) {
|
|
|
|
if (cpuData.length > 10) {
|
|
|
|
cpuData.shift();
|
|
|
|
memoryData.shift();
|
|
|
|
timeLabels.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
var cpuUse = (Pterodactyl.server.cpu > 0) ? parseFloat(((proc.data.cpu.total / Pterodactyl.server.cpu) * 100).toFixed(3).toString()) : proc.data.cpu.total;
|
|
|
|
cpuData.push(cpuUse);
|
|
|
|
memoryData.push(parseInt(proc.data.memory.total / (1024 * 1024)));
|
|
|
|
|
|
|
|
var m = new Date();
|
|
|
|
timeLabels.push($.format.date(new Date(), 'HH:mm:ss'));
|
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
cpuChart.update();
|
|
|
|
memoryChart.update();
|
2017-01-26 21:20:04 +00:00
|
|
|
});
|
2017-01-15 23:52:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 21:12:33 +00:00
|
|
|
function pushOutputQueue() {
|
2017-01-26 21:57:33 +00:00
|
|
|
if (terminalQueue.length > CONSOLE_PUSH_COUNT) {
|
2017-01-26 21:12:33 +00:00
|
|
|
// console throttled warning show
|
2017-01-15 23:52:22 +00:00
|
|
|
}
|
2017-01-26 21:12:33 +00:00
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
if (terminalQueue.length > 0) {
|
|
|
|
for (var i = 0; i < CONSOLE_PUSH_COUNT && terminalQueue.length > 0; i++) {
|
|
|
|
terminal.echo(terminalQueue[0]);
|
|
|
|
terminalQueue.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show
|
|
|
|
if (!terminal.is_bottom()) {
|
|
|
|
$terminalNotify.removeClass('hidden');
|
2017-01-26 21:12:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.setTimeout(pushOutputQueue, CONSOLE_PUSH_FREQ);
|
2017-01-15 23:52:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 21:20:04 +00:00
|
|
|
function updateServerPowerControls (data) {
|
|
|
|
// Server is On or Starting
|
|
|
|
if(data == 1 || data == 2) {
|
|
|
|
$('[data-attr="power"][data-action="start"]').addClass('disabled');
|
|
|
|
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').removeClass('disabled');
|
|
|
|
} else {
|
|
|
|
if (data == 0) {
|
|
|
|
$('[data-attr="power"][data-action="start"]').removeClass('disabled');
|
|
|
|
}
|
|
|
|
$('[data-attr="power"][data-action="stop"], [data-attr="power"][data-action="restart"]').addClass('disabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(data !== 0) {
|
|
|
|
$('[data-attr="power"][data-action="kill"]').removeClass('disabled');
|
|
|
|
} else {
|
|
|
|
$('[data-attr="power"][data-action="kill"]').addClass('disabled');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:12:33 +00:00
|
|
|
return {
|
|
|
|
init: function () {
|
2017-01-26 21:20:04 +00:00
|
|
|
|
|
|
|
initConsole();
|
|
|
|
pushOutputQueue();
|
|
|
|
initGraphs();
|
|
|
|
addSocketListeners();
|
|
|
|
|
2017-01-26 21:12:33 +00:00
|
|
|
$('[data-attr="power"]').click(function (event) {
|
|
|
|
if (! $(this).hasClass('disabled')) {
|
|
|
|
Socket.emit('set status', $(this).data('action'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2017-01-15 23:52:22 +00:00
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
getTerminal: function () {
|
2017-01-26 21:12:33 +00:00
|
|
|
return terminal
|
|
|
|
},
|
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
getTerminalQueue: function () {
|
2017-01-26 21:12:33 +00:00
|
|
|
return terminalQueue
|
|
|
|
},
|
2017-01-15 23:52:22 +00:00
|
|
|
}
|
2017-01-26 21:12:33 +00:00
|
|
|
|
2017-01-26 21:57:33 +00:00
|
|
|
})();
|
2017-01-26 21:12:33 +00:00
|
|
|
|
|
|
|
$(document).ready(function () {
|
|
|
|
Console.init();
|
2017-01-15 23:52:22 +00:00
|
|
|
});
|