Initial implementation of log rotation.

This commit is contained in:
Richard Underwood 2016-08-24 11:32:43 +01:00
parent a8abca1121
commit d1b817443c
4 changed files with 77 additions and 1 deletions

View file

@ -7,7 +7,12 @@ $apiproto = 'http'; # http | https
$apisslverify = FALSE; # Verify SSL Certificate if using https for apiproto $apisslverify = FALSE; # Verify SSL Certificate if using https for apiproto
$allowzoneadd = FALSE; # Allow normal users to add zones $allowzoneadd = FALSE; # Allow normal users to add zones
$logging = TRUE; $logging = TRUE;
$allowclearlogs = TRUE; # Allow clearing of log entries
$allowrotatelogs = FALSE;# Allow rotation to text file on server
# Log directory - if allowrotatelogs is set, this is where the logs will
# be written. It must be writeable by the web server user.
$logsdirectory = "../etc";
# If you configure this, nsedit will try to authenticate via WeFact too. # If you configure this, nsedit will try to authenticate via WeFact too.
# Debtors will be added to the sqlitedatabase with their crypted password. # Debtors will be added to the sqlitedatabase with their crypted password.

View file

@ -277,6 +277,37 @@ function clearlogs() {
writelog("Logtable truncated."); writelog("Logtable truncated.");
} }
function rotatelogs() {
global $logging, $logsdirectory;
if ($logging !== TRUE)
return FALSE;
if(!is_dir($logsdirectory) || !is_writable($logsdirectory)) {
writelog("Logs directory cannot be written to.");
return FALSE;
}
date_default_timezone_set('UTC');
$filename = date("Y-m-d-His") . ".json";
$file = fopen($logsdirectory . "/" . $filename, "x");
if($file === FALSE) {
writelog("Can't create file for log rotation.");
return FALSE;
}
if(fwrite($file,json_encode(getlogs())) === FALSE) {
writelog("Can't write to file for log rotation.");
fclose($file);
return FALSE;
} else {
fclose($file);
clearlogs();
return $filename;
}
}
function writelog($line) { function writelog($line) {
global $logging; global $logging;
if ($logging !== TRUE) if ($logging !== TRUE)

View file

@ -116,6 +116,9 @@ if ($blocklogin === TRUE) {
<div id="clearlogs" style="display: none;"> <div id="clearlogs" style="display: none;">
Are you sure you want to clear all the logs? Maybe save them first? Are you sure you want to clear all the logs? Maybe save them first?
</div> </div>
<div id="rotatelogs" style="display: none;">
Are you sure you want to rotate the logs?
</div>
<div id="searchlogs" style="display: none; text-align: right;"> <div id="searchlogs" style="display: none; text-align: right;">
<table border="0"> <table border="0">
<tr><td>User:</td><td><input type="text" id ="searchlogs-user"><br></td></tr> <tr><td>User:</td><td><input type="text" id ="searchlogs-user"><br></td></tr>
@ -1043,6 +1046,31 @@ $(document).ready(function () {
}); });
} }
}, },
<?php if($allowrotatelogs === TRUE) { ?>
{
icon: 'img/export.png',
text: 'Rotate logs',
click: function() {
$("#rotatelogs").dialog({
modal: true,
title: "Rotate logs",
width: 'auto',
buttons: {
Ok: function() {
$.get("logs.php?action=rotate");
$( this ).dialog( "close" );
$('#Logs').jtable('load');
},
Cancel: function() {
$( this ).dialog( "close" );
return false;
}
}
});
}
},
<?php } ?>
<?php if($allowclearlogs === TRUE) { ?>
{ {
icon: 'img/delete_inverted.png', icon: 'img/delete_inverted.png',
text: 'Clear logs', text: 'Clear logs',
@ -1065,6 +1093,7 @@ $(document).ready(function () {
}); });
} }
}, },
<?php } ?>
{ {
icon: 'img/export.png', icon: 'img/export.png',
text: 'Save logs', text: 'Save logs',

View file

@ -61,7 +61,18 @@ case "export":
break; break;
case "clear": case "clear":
clearlogs(); if($allowclearlogs === TRUE) {
clearlogs();
} else {
jtable_respond(null, 'error', 'Invalid action');
}
break;
case "rotate":
if($allowrotatelogs === TRUE) {
rotatelogs();
} else {
jtable_respond(null, 'error', 'Invalid action');
}
break; break;
default: default:
jtable_respond(null, 'error', 'Invalid action'); jtable_respond(null, 'error', 'Invalid action');