DHCP Server start / stop / restart - Fix dashboard refresh

This commit is contained in:
Akkadius 2017-09-02 08:45:55 -05:00
parent ccf8f9d1ba
commit 8a73d5bb5d
6 changed files with 170 additions and 16 deletions

1
app.js
View File

@ -30,6 +30,7 @@ app.use('/dhcp_config', require('./routes/dhcp_config'));
app.use('/dhcp_config_snapshots', require('./routes/dhcp_config_snapshots'));
app.use('/dhcp_config_snapshot_view', require('./routes/dhcp_config_snapshot_view'));
app.use('/dhcp_config_save', require('./routes/dhcp_config_save'));
app.use('/dhcp_start_stop_restart', require('./routes/dhcp_start_stop_restart'));
app.use('/api_examples', require('./routes/api_examples'));
app.use('/glass_settings', require('./routes/glass_settings'));
app.use('/glass_settings_save', require('./routes/glass_settings_save'));

View File

@ -2,7 +2,21 @@
* Created by cmiles on 8/9/2017.
*/
$( document ).ajaxComplete(function( event, request, settings ) {
check_dashboard_active();
});
function check_dashboard_active() {
if(window.location.pathname == "/" && in_dashboard != 1) {
in_dashboard = 1;
}
else {
in_dashboard = 0;
}
}
function modal (title, content, buttons) {
$('#modal-buttons').html('');
$('#modal-title').html(title);
$('#modal-body').html(content);

View File

@ -0,0 +1,59 @@
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="header">
<h2>
[title]
</h2>
</div>
<div class="body">
[c_content]
<div id="dhcp_start_stop_restart_result"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
function process_action(action) {
switch (action) {
case "stop":
modal(
"Are you sure you want to stop server?",
"Stopping the server can be service affecting! Are you sure?",
'<button type="button" class="btn btn-link waves-effect" onclick="process_action(\'confirm_stop\')">STOP SERVER</button>'
);
break;
case "confirm_stop":
$('#body-content .btn').attr('disabled', true);
$.post( "/dhcp_start_stop_restart", "action=stop", function( data ) {
$( "#dhcp_start_stop_restart_result" ).html( data );
});
break;
case "restart":
modal(
"Are you sure you want to restart the server?",
"Restarting the server can cause very brief interruption. Are you sure?",
'<button type="button" class="btn btn-link waves-effect" onclick="process_action(\'confirm_restart\')">RESTART SERVER</button>'
);
break;
case "confirm_restart":
$('#body-content .btn').attr('disabled', true);
$.post( "/dhcp_start_stop_restart", "action=restart", function( data ) {
$( "#dhcp_start_stop_restart_result" ).html( data );
});
break;
case "start":
$('#body-content .btn').attr('disabled', true);
$.post( "/dhcp_start_stop_restart", "action=start", function( data ) {
$( "#dhcp_start_stop_restart_result" ).html( data );
});
break;
default:
break;
}
}
</script>

View File

@ -105,7 +105,7 @@
</a>
</li>
<li>
<a href="/start_stop_restart" pjax="1">
<a href="/dhcp_start_stop_restart" pjax="1">
<i class="material-icons">refresh</i>
<span>Start / Stop / Restart</span>
</a>
@ -119,6 +119,13 @@
</a>
</li>
<li>
<a href="/glass_alerts" pjax="1">
<i class="material-icons">add_alert</i>
<span>Glass Alerts</span>
</a>
</li>
<li class="header">Glass API</li>
<li>
<a href="/api_examples" pjax="1">
@ -155,7 +162,7 @@
</div>
<div class="modal-footer">
<div id="modal-buttons"></div>
<div id="modal-buttons" style="display:inline-block"></div>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
@ -212,6 +219,8 @@
<script type="text/javascript">
var in_dashboard = 0;
function get_dashboard() {
$.get( "/get_dashboard", function( data ) {
$('#body-content').html(data);
@ -227,20 +236,21 @@
});
}
console.log(window.location.pathname);
dashboard_timer = setInterval(function () {
if (window_focus && $('#shared-networks').length && in_dashboard == 1) {
get_dashboard();
}
}, 5000);
if(window.location.pathname == "/") {
dashboard_timer = setInterval(function () {
if (window_focus && $('#shared-networks').length) {
get_dashboard();
}
}, 5000);
get_stats_timer = setInterval(function () {
if (window_focus && $('#cpu-utilization').length) {
get_stats();
}
}, 5000);
get_dashboard(); get_stats();
get_stats_timer = setInterval(function () {
if (window_focus && $('#cpu-utilization').length && in_dashboard == 1) {
get_stats();
}
}, 5000);
if(window.location.pathname == "/") {
get_stats();
get_dashboard();
}
var window_focus = true;

View File

@ -31,7 +31,7 @@ router.post('/', function(req, res, next) {
output = output.replace("\n", "<br>");
res.send(
'<script type="text/javascript">modal (\'DHCP Config Save\', ' + JSON.stringify('Syntax OK') + ', "");'
'<script type="text/javascript">modal (\'DHCP Config Save\', ' + JSON.stringify('Syntax OK <br><br> Config Snapshot created') + ', "");'
);
/* Read Config */
var json_file = require('jsonfile');

View File

@ -0,0 +1,70 @@
var express = require('express');
var router = express.Router();
var fs = require('fs');
var template_render = require('../lib/render_template.js');
router.get('/', function(req, res, next) {
var content = "";
content = template_render.get_template("dhcp_start_stop_restart");
content = template_render.set_template_variable(content, "title", "Start / Stop / Restart");
var exec = require('child_process').exec;
exec(' ps aux | grep dhcpd | grep -v "grep"', function(err, stdout, stderr) {
is_running = 0;
var result = stdout.split("\n");
for (var i = 0; i < result.length; i++) {
if(/dhcpd/i.test(result[i])){
is_running = 1;
}
// console.log('line ' + result[i]);
}
var return_content = "";
if(is_running){
return_content = return_content + 'DHCP Server is online!<br><br>';
return_content = return_content + '<button type="button" onclick="process_action(\'stop\')" class="btn btn-default waves-effect">Stop Server</button> ';
}
else {
return_content = return_content + 'DHCP Server is offline!<br><br>';
return_content = return_content + '<button type="button" onclick="process_action(\'start\')" class="btn btn-default waves-effect">Start Server</button> ';
}
return_content = return_content + '<button type="button" onclick="process_action(\'restart\')" class="btn btn-default waves-effect">Restart Server</button> ';
content = template_render.set_template_variable(content, "c_content", return_content);
res.send(template_render.get_index_template(content, req.url));
});
});
router.post('/', function(req, res, next) {
var request = req.body;
const execSync = require('child_process').execSync;
switch (request.action) {
case "stop":
dhcp_exec = execSync('service isc-dhcp-server stop && sleep 1');
res.send("<script type='text/javascript'>notification('DHCP Server Stopped');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');$('#mdModal').modal('hide');</script>");
break;
case "start":
dhcp_exec = execSync('service isc-dhcp-server start');
res.send("<script type='text/javascript'>notification('DHCP Server Started');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');</script>");
break;
case "restart":
dhcp_exec = execSync('service isc-dhcp-server restart && sleep 1');
res.send("<script type='text/javascript'>notification('DHCP Server Restarted " + dhcp_exec + "');ignore_cache = 1;do_pjax_request('/dhcp_start_stop_restart');$('#mdModal').modal('hide');</script>");
break;
default:
break;
}
console.log(request);
});
module.exports = router;