Skip to content

Commit

Permalink
Log file adjustments (#2)
Browse files Browse the repository at this point in the history
Add memcache support (#2)
  • Loading branch information
austinwbest committed Nov 19, 2023
1 parent e95d21d commit a59fdbf
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 49 deletions.
12 changes: 6 additions & 6 deletions root/app/www/public/ajax/containers.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@
case '4': //-- Pull
$image = $container['inspect'][0]['Config']['Image'];
$pull = dockerPullContainer($image);
$inspectContainer = dockerInspect($container['Names']);
$inspectContainer = dockerInspect($container['Names'], false);
$inspectContainer = json_decode($inspectContainer, true);
$inspectImage = dockerInspect($image);
$inspectImage = dockerInspect($image, false);
$inspectImage = json_decode($inspectImage, true);

$pulls[md5($container['Names'])] = [
Expand All @@ -210,7 +210,7 @@
break;
}

$processList = dockerProcessList();
$processList = dockerProcessList(false);
$processList = json_decode($processList, true);
$containerProcess = [];
foreach ($processList as $process) {
Expand All @@ -220,7 +220,7 @@
}
}

$dockerStats = dockerStats();
$dockerStats = dockerStats(false);
$dockerStats = json_decode($dockerStats, true);
$containerStats = [];
foreach ($dockerStats as $dockerStat) {
Expand Down Expand Up @@ -262,7 +262,7 @@
dockerStartContainer($container['Names']);
}

$processList = dockerProcessList();
$processList = dockerProcessList(false);
$processList = json_decode($processList, true);
$containerProcess = [];
foreach ($processList as $process) {
Expand All @@ -272,7 +272,7 @@
}
}

$dockerStats = dockerStats();
$dockerStats = dockerStats(false);
$dockerStats = json_decode($dockerStats, true);
$containerStats = [];
foreach ($dockerStats as $dockerStat) {
Expand Down
43 changes: 30 additions & 13 deletions root/app/www/public/ajax/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@
require 'shared.php';

if ($_POST['m'] == 'init') {
$logFiles = [];
$logDir = ABSOLUTE_PATH . LOGS_PATH;
$dir = opendir($logDir);
while ($log = readdir($dir)) {
if ($log[0] != '.' && !is_dir($log)) {
$logFiles[] = ['name' => $log, 'size' => filesize($logDir . $log)];
$logFiles = $cronLogs = [];

$logDir = LOGS_PATH . 'crons';
if (is_dir($logDir)) {
$dir = opendir($logDir);
while ($log = readdir($dir)) {
if ($log[0] != '.' && !is_dir($log)) {
$cronLogs[] = ['name' => $log, 'size' => filesize($logDir . $log)];
}
}
closedir($dir);

if ($cronLogs) {
$logFiles['crons'] = $cronLogs;
}
}
closedir($dir);

if (!$logFiles) {
echo 'No log files have been generated yet.';
Expand All @@ -30,14 +37,24 @@
<div class="col-sm-3">
<ul>
<?php
foreach ($logFiles as $log) {
$logHash = md5($log['name']);
foreach ($logFiles as $group => $groupLogs) {
echo '<h4>' . $group . '</h4>';

if (!$groupLogs) {
continue;
}

foreach ($groupLogs as $log) {
$logHash = md5($log['name']);
?>
<li id="logList-<?= $logHash ?>" onclick="viewLog('<?= $group .'/'. $log['name'] ?>', '<?= $logHash ?>')" style="cursor: pointer;" class="text-info"><?= $log['name'] ?> (<?= byteConversion($log['size']) ?>)</li>
<?php
}
}
?>
<li id="logList-<?= $logHash ?>" onclick="viewLog('<?= $log['name'] ?>', '<?= $logHash ?>')" style="cursor: pointer;" class="text-info"><?= $log['name'] ?> (<?= byteConversion($log['size']) ?>)</li>
<?php } ?>
</ul>
</div>
<div class="col-sm-9"><span id="logHeader"></span><pre id="logViewer" style="max-height: 500px; overflow: auto;"></pre></div>
<div class="col-sm-9"><span id="logHeader"></span><pre id="logViewer" style="max-height: 500px; overflow: auto;">Select a log from the left to view</pre></div>
</div>
</div>
</div>
Expand All @@ -46,7 +63,7 @@
}

if ($_POST['m'] == 'viewLog') {
$log = file(ABSOLUTE_PATH . LOGS_PATH . $_POST['name']);
$log = file(LOGS_PATH . $_POST['name']);
$header = 'Lines: ' . count($log);

foreach ($log as $index => $line) {
Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<tfoot>
<tr>
<td colspan="9" align="center">
<button type="button" class="btn btn-info" onclick="saveNotificationSettings()">Save</button>
<button type="button" class="btn btn-info" onclick="saveNotificationSettings()">Save Changes</button>
</td>
</tr>
</tfoot>
Expand Down
41 changes: 40 additions & 1 deletion root/app/www/public/ajax/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,46 @@
</tbody>
</table>
</div>
<div align="center"><button type="button" class="btn btn-info m-2" onclick="saveGlobalSettings()">Save</button></div>
<h4 class="mt-3">Memcached</h4>
Optionally memcached can be used to speed things up in the UI while navigating
<div class="table-responsive mt-2">
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Setting</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Enabled</th>
<td><?= ($memcache ? 'Yes' : 'No') ?></td>
<td>Is memcached connected and being used</td>
</tr>
<tr>
<th scope="row">Prefix</th>
<td><?= MEMCACHE_PREFIX ?></td>
<td>Everything in memcached will be prefixed with this key to ensure no collisions</td>
</tr>
<tr>
<th scope="row">Server</th>
<td>
<input class="form-control" type="text" id="globalSetting-memcachedServer" value="<?= $globalSettings['memcachedServer'] ?>">
</td>
<td>The container, ip, hostname of where memcached is installed</td>
</tr>
<tr>
<th scope="row">Port</th>
<td>
<input class="form-control" type="number" id="globalSetting-memcachedPort" value="<?= $globalSettings['memcachedPort'] ?>">
</td>
<td>The memcached port (default is 11211)</td>
</tr>
</tbody>
</table>
</div>
<div align="center"><button type="button" class="btn btn-info m-2" onclick="saveGlobalSettings()">Save Changes</button></div>
<sup>1</sup> Checked every 5 minutes
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/crons/housekeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
define('ABSOLUTE_PATH', str_replace('crons', '', __DIR__));
require ABSOLUTE_PATH . 'loader.php';

$logfile = ABSOLUTE_PATH . LOGS_PATH . 'cron-housekeeper-' . date('Ymd') . '.log';
$logfile = LOGS_PATH . 'crons/cron-housekeeper-' . date('Ymd') . '.log';
logger($logfile, 'Cron run started');
echo 'Cron run started: housekeeper' . "\n";

Expand Down
6 changes: 3 additions & 3 deletions root/app/www/public/crons/pulls.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require ABSOLUTE_PATH . 'loader.php';
set_time_limit(0);

$logfile = ABSOLUTE_PATH . LOGS_PATH . 'cron-pulls-' . date('Ymd') . '.log';
$logfile = LOGS_PATH . 'crons/cron-pulls-' . date('Ymd') . '.log';
logger($logfile, 'Cron run started');
echo 'Cron run started: pulls' . "\n";

Expand Down Expand Up @@ -40,13 +40,13 @@
$msg = 'Inspecting container: ' . $containerState['Names'];
logger($logfile, $msg);
echo $msg. "\n";
$inspectContainer = dockerInspect($containerState['Names']);
$inspectContainer = dockerInspect($containerState['Names'], false);
$inspectContainer = json_decode($inspectContainer, true);

$msg = 'Inspecting image: ' . $image;
logger($logfile, $msg);
echo $msg. "\n";
$inspectImage = dockerInspect($image);
$inspectImage = dockerInspect($image, false);
$inspectImage = json_decode($inspectImage, true);

$msg = 'Updating pull data: ' . $containerState['Names'];
Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/crons/state.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
define('ABSOLUTE_PATH', str_replace('crons', '', __DIR__));
require ABSOLUTE_PATH . 'loader.php';

$logfile = ABSOLUTE_PATH . LOGS_PATH . 'cron-state-' . date('Ymd') . '.log';
$logfile = LOGS_PATH . 'crons/cron-state-' . date('Ymd') . '.log';
logger($logfile, 'Cron run started');
echo 'Cron run started: state' . "\n";
setFile(STATE_FILE, dockerState());
Expand Down
6 changes: 6 additions & 0 deletions root/app/www/public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,10 @@
.testimonial-carousel .owl-dot.active {
background: var(--dark);
border-color: var(--primary);
}

pre {
background-color: #000;
color: #FFF;
padding: 1em;
}
55 changes: 37 additions & 18 deletions root/app/www/public/functions/docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,46 @@ function dockerPermissionCheck()
return empty(json_decode($response, true)) ? false : true;
}

function dockerStats()
function dockerProcessList($useCache = true)
{
$cmd = '/usr/bin/docker stats --all --no-trunc --no-stream --format="{{json . }}" | jq -s --tab .';
return shell_exec($cmd . ' 2>&1');
$cacheKey = 'dockerProcessList';
$cache = memcacheGet($cacheKey);
if ($cache && $useCache) {
return $cache;
} else {
$cmd = '/usr/bin/docker ps --all --no-trunc --format="{{json . }}" | jq -s --tab .';
$shell = shell_exec($cmd . ' 2>&1');
memcacheSet($cacheKey, $shell, MEMCACHE_DOCKER_PROCESS);
return $shell;
}
}

function dockerInspect($containerName)
function dockerStats($useCache = true)
{
$cmd = '/usr/bin/docker inspect ' . $containerName . ' --format="{{json . }}" | jq -s --tab .';
return shell_exec($cmd . ' 2>&1');
$cacheKey = 'dockerStats';
$cache = memcacheGet($cacheKey);
if ($cache && $useCache) {
return $cache;
} else {
$cmd = '/usr/bin/docker stats --all --no-trunc --no-stream --format="{{json . }}" | jq -s --tab .';
$shell = shell_exec($cmd . ' 2>&1');
memcacheSet($cacheKey, $shell, MEMCACHE_DOCKER_STATS);
return $shell;
}
}

function dockerInspect($containerName, $useCache = true)
{
$cacheKey = 'dockerInspect.' . md5($containerName);
$cache = memcacheGet($cacheKey);
if ($cache && $useCache) {
return $cache;
} else {
$cmd = '/usr/bin/docker inspect ' . $containerName . ' --format="{{json . }}" | jq -s --tab .';
$shell = shell_exec($cmd . ' 2>&1');
memcacheSet($cacheKey, $shell, MEMCACHE_DOCKER_INSPECT);
return $shell;
}
}

function dockerContainerLogs($containerName, $log)
Expand All @@ -61,24 +91,13 @@ function dockerContainerLogs($containerName, $log)
}
return $return;
}

if ($log == 'docker') {
$cmd = '/usr/bin/docker logs ' . $containerName;
return shell_exec($cmd . ' 2>&1');
}
}

function dockerProcessList()
{
$cmd = '/usr/bin/docker ps --all --no-trunc --format="{{json . }}" | jq -s --tab .';
return shell_exec($cmd . ' 2>&1');
}

function dockerCopyFile($from, $to)
{
$cmd = '/usr/bin/docker cp ' . $from . ' ' . $to;
return shell_exec($cmd . ' 2>&1');
}

function dockerStartContainer($containerName)
{
$cmd = '/usr/bin/docker start ' . $containerName;
Expand Down
52 changes: 52 additions & 0 deletions root/app/www/public/functions/memcache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
----------------------------------
------ Created: 111923 ------
------ Austin Best ------
----------------------------------
*/

function memcacheBust($key)
{
global $memcache;

if (!$memcache) {
return;
}

$memcache->set(MEMCACHE_PREFIX . $key, null, 0);
}

function memcacheGet($key)
{
global $memcache;

if (!$memcache) {
return;
}

return $memcache->get(MEMCACHE_PREFIX . $key);
}

function memcacheSet($key, $data, $seconds)
{
global $memcache;

if (!$memcache) {
return;
}

$memcache->set(MEMCACHE_PREFIX . $key, $data, $seconds);
}

function memcacheStats()
{
global $memcache;

if (!$memcache) {
return;
}

return $memcache->getStats();
}
9 changes: 8 additions & 1 deletion root/app/www/public/includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
----------------------------------
*/

//-- PATHS
define('SETTINGS_FILE', '/config/settings.json');
define('STATE_FILE', '/config/state.json');
define('PULL_FILE', '/config/pull.json');
define('LOGS_PATH', 'logs/');
define('LOGS_PATH', '/config/logs/');

//-- MEMCACHE
define('MEMCACHE_PREFIX', 'dockwatch-');
define('MEMCACHE_DOCKER_STATS', 10);
define('MEMCACHE_DOCKER_PROCESS', 10);
define('MEMCACHE_DOCKER_INSPECT', 10);

//-- WHAT DATA TO GET WHEN VIEWING A PAGE
$getStats = ['overview', 'containers'];
Expand Down
8 changes: 7 additions & 1 deletion root/app/www/public/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}

//-- CREATE DIRECTORIES
createDirectoryTree(ABSOLUTE_PATH . LOGS_PATH);
createDirectoryTree(LOGS_PATH . 'crons');

//-- INITIALIZE THE NOTIFY CLASS
$notifications = new Notifications();
Expand All @@ -44,3 +44,9 @@

//-- PULLS
$pulls = getFile(PULL_FILE);

//-- INITIALIZE MEMCACHE
if ($settings['global']['memcachedServer'] && $settings['global']['memcachedPort']) {
$memcache = new Memcached();
$memcache->addServer($settings['global']['memcachedServer'], $settings['global']['memcachedPort']);
}
Loading

0 comments on commit a59fdbf

Please sign in to comment.