Skip to content

Instantly share code, notes, and snippets.

@adamzero1
Created May 12, 2022 11:00
Show Gist options
  • Save adamzero1/f652f5b7bacfa5b66594c11a057f36e3 to your computer and use it in GitHub Desktop.
Save adamzero1/f652f5b7bacfa5b66594c11a057f36e3 to your computer and use it in GitHub Desktop.
Analyse Magento var directory
<?php
use Magento\Setup\Module\I18n\Parser\Parser;
if(!is_file('bin/magento')){
echo 'Unable to find "bin/magento", this needs to be ran from the root of the project.'.PHP_EOL;
return 1;
}
define('REPORT_WIDTH', 200);
define('LOG_FILE_SIZE', 104857600); // 100mb
define('LOG_FILE_SIZE_HUMAN', '100mb');
define('SYSTEM_LOG_LAST_WRITTEN_TO', 2592000);
define('SYSTEM_LOG_LAST_WRITTEN_TO_HUMAN', '30 days');
function scanDirRescursive($dir, &$listing){
$results = scandir($dir);
foreach($results as $fileOrdirectory){
if(in_array($fileOrdirectory, ['.', '..'])){ continue; }
$fullPath = $dir.'/'.$fileOrdirectory;
if(is_file($fullPath)){
$listing[$fullPath] = $fullPath;
}elseif(is_dir($fullPath)){
scanDirRescursive($fullPath, $listing);
}
}
}
function checkVarLogFileSizes(&$reportData){
$reportData['var_file_sizes'] = [
'errors' => [],
'large_files' => [],
];
if(!is_dir('var/log')){
$reportData['var_file_sizes']['errors'][] = 'var/log is not a directory unable to report on file sizes.';
return;
}
$files = [];
scanDirRescursive('var/log', $files);
foreach($files as $file){
$fileSize = filesize($file);
if($fileSize > LOG_FILE_SIZE){
$reportData['var_file_sizes']['large_files'][] = $file.' exceeds '.LOG_FILE_SIZE_HUMAN.' ('.floor(($fileSize / 1024 / 1024)).'mb)';
}
}
$reportData['var_file_sizes']['title'] = 'Large Files';
$result = 'PASS';
if(count($reportData['var_file_sizes']['errors']) || count($reportData['var_file_sizes']['large_files'])){
$result = 'FAIL';
}
$reportData['var_file_sizes']['result'] = $result;
$report = 'Large files ('.$reportData['var_file_sizes']['result'].')'.PHP_EOL.PHP_EOL;
foreach($reportData['var_file_sizes']['errors'] as $error){
$report .= 'ERROR: '.$error.PHP_EOL;
}
if(count($reportData['var_file_sizes']['errors'])){
$report .= PHP_EOL;
}
if(count($reportData['var_file_sizes']['large_files'])){
foreach($reportData['var_file_sizes']['large_files'] as $file){
$report .= $file.PHP_EOL;
}
}
$reportData['var_file_sizes']['report'] = $report;
}
function checkSystemLogLastWrittenTo(&$reportData){
// system_log_last_written_to
$reportData['system_log_last_written_to'] = [
'title' => 'Check last written to date of system.log',
'errors' => [],
'last_written_to' => '',
'result' => 'PASS',
'report' => '',
];
if(!is_file('var/log/system.log')){
$reportData['system_log_last_written_to']['result'] = 'FAIL';
$reportData['system_log_last_written_to']['errors'][] = 'Unable to locate var/log/system.log';
}else{
$currentTime = time();
$reportData['system_log_last_written_to']['last_written_to'] = $lastModified = filemtime('var/log/system.log');
}
if(!$lastModified || $lastModified > ($currentTime - SYSTEM_LOG_LAST_WRITTEN_TO)){
$reportData['system_log_last_written_to']['result'] = 'FAIL';
}
$report = $reportData['system_log_last_written_to']['title'].' '.$reportData['system_log_last_written_to']['result'].PHP_EOL;
$report .= 'var/log/system.log last modified: '.date('Y-m-d H:i:s', $lastModified).PHP_EOL;
$reportData['system_log_last_written_to']['report'] = $report;
}
function checkSystemLogErrorCount(&$reportData){
// system_log_last_written_to
$reportData['system_log_error_count'] = [
'title' => 'Check error count system.log',
'errors' => [],
'log_errors' => [],
'log_unknowns' => [],
'result' => 'PASS',
'report' => '',
];
if(!is_file('var/log/system.log')){
$reportData['system_log_error_count']['result'] = 'FAIL';
$reportData['system_log_error_count']['errors'][] = 'Unable to locate var/log/system.log';
}else{
$data = shell_exec('tail -n1000 var/log/system.log');
$data = explode(PHP_EOL, $data);
$lineCounter = 0;
foreach($data as $logLine){
$lineCounter++;
if(strpos($logLine, '[') !== 0){
if(!isset($reportData['system_log_error_count']['log_unknowns'][$logLine])){
$reportData['system_log_error_count']['log_unknowns'][$logLine] = 1;
}else{
$reportData['system_log_error_count']['log_unknowns'][$logLine]++;
}
}
$levelsWeDontCareAbout = [
'.NOTICE:',
'.INFO:',
'.DEBUG:'
];
$next = false;
foreach($levelsWeDontCareAbout as $levelWeDontCareAbout){
if(strpos($logLine, $levelWeDontCareAbout) !== false){
$next = true;
break;
}
}
if($next){
continue;
}
$levelsWeDoCareAbout = [
'.ERROR:',
'.WARNING:',
'.CRITICAL:'
];
$next = false;
foreach($levelsWeDoCareAbout as $levelWeDoCareAbout){
if(strpos($logLine, $levelWeDoCareAbout) !== false){
// remove the date
$parsedLogLine = substr($logLine, strpos($logLine, $levelWeDoCareAbout)+1);
if(!isset($reportData['system_log_error_count']['log_errors'][$parsedLogLine])){
$reportData['system_log_error_count']['log_errors'][$parsedLogLine] = 1;
}else{
$reportData['system_log_error_count']['log_errors'][$parsedLogLine]++;
}
$next = true;
break;
}
}
if($next){
continue;
}
if(!isset($reportData['system_log_error_count']['log_unknowns'][$logLine])){
$reportData['system_log_error_count']['log_unknowns'][$logLine] = 1;
}else{
$reportData['system_log_error_count']['log_unknowns'][$logLine]++;
}
}
if(count($reportData['system_log_error_count']['log_errors'])){
$reportData['system_log_error_count']['result'] = 'FAIL';
}
}
$report = $reportData['system_log_error_count']['title'].' ('.$reportData['system_log_error_count']['result'].')'.PHP_EOL;
if(count($reportData['system_log_error_count']['log_errors'])){
$report .= 'Errors found'.PHP_EOL;
arsort($reportData['system_log_error_count']['log_errors']);
}
foreach($reportData['system_log_error_count']['log_errors'] as $error => $count){
$report .= $count.' '.trim($error).PHP_EOL;
}
if(count($reportData['system_log_error_count']['log_unknowns'])){
$report .= PHP_EOL.'Unknown Lines (top 10)'.PHP_EOL;
arsort($reportData['system_log_error_count']['log_unknowns']);
}
$counter = 0;
foreach($reportData['system_log_error_count']['log_unknowns'] as $error => $count){
$counter++;
if($counter == 10){
break;
}
$report .= $count.' '.preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $error).PHP_EOL;
}
$reportData['system_log_error_count']['report'] = $report;
}
function checkExceptionLogLastWrittenTo(&$reportData){
$reportData['exception_log_last_written_to'] = [
'title' => 'Check last written to date of exception.log',
'errors' => [],
'last_written_to' => '',
'result' => 'PASS',
'report' => '',
];
if(!is_file('var/log/exception.log')){
}else{
$currentTime = time();
$reportData['exception_log_last_written_to']['last_written_to'] = $lastModified = filemtime('var/log/exception.log');
}
if(!$lastModified || $lastModified > ($currentTime - SYSTEM_LOG_LAST_WRITTEN_TO)){
$reportData['exception_log_last_written_to']['result'] = 'FAIL';
}
$report = $reportData['exception_log_last_written_to']['title'].' '.$reportData['exception_log_last_written_to']['result'].PHP_EOL;
if($reportData['exception_log_last_written_to']['last_written_to']){
$report .= 'var/log/exception.log last modified: '.date('Y-m-d H:i:s', $lastModified).PHP_EOL;
}else{
$report .= 'var/log/exception.log doesn\'t exist'.PHP_EOL;
}
$reportData['exception_log_last_written_to']['report'] = $report;
}
function checkExceptionLogErrorCount(&$reportData){
$reportData['exception_log_error_count'] = [
'title' => 'Check error count exception.log',
'errors' => [],
'log_errors' => [],
'log_unknowns' => [],
'result' => 'PASS',
'report' => '',
];
if(!is_file('var/log/exception.log')){
}else{
$data = shell_exec('tail -n1000 var/log/exception.log');
$data = explode(PHP_EOL, $data);
$lineCounter = 0;
foreach($data as $logLine){
$lineCounter++;
if(strpos($logLine, '[') !== 0){
if(!isset($reportData['exception_log_error_count']['log_unknowns'][$logLine])){
$reportData['exception_log_error_count']['log_unknowns'][$logLine] = 1;
}else{
$reportData['exception_log_error_count']['log_unknowns'][$logLine]++;
}
}
$levelsWeDontCareAbout = [
'.NOTICE:',
'.INFO:',
'.DEBUG:'
];
$next = false;
foreach($levelsWeDontCareAbout as $levelWeDontCareAbout){
if(strpos($logLine, $levelWeDontCareAbout) !== false){
$next = true;
break;
}
}
if($next){
continue;
}
$levelsWeDoCareAbout = [
'.ERROR:',
'.WARNING:',
'.CRITICAL:'
];
$next = false;
foreach($levelsWeDoCareAbout as $levelWeDoCareAbout){
if(strpos($logLine, $levelWeDoCareAbout) !== false){
// remove the date
$parsedLogLine = substr($logLine, strpos($logLine, $levelWeDoCareAbout)+1);
if(!isset($reportData['exception_log_error_count']['log_errors'][$parsedLogLine])){
$reportData['exception_log_error_count']['log_errors'][$parsedLogLine] = 1;
}else{
$reportData['exception_log_error_count']['log_errors'][$parsedLogLine]++;
}
$next = true;
break;
}
}
if($next){
continue;
}
if(!isset($reportData['exception_log_error_count']['log_unknowns'][$logLine])){
$reportData['exception_log_error_count']['log_unknowns'][$logLine] = 1;
}else{
$reportData['exception_log_error_count']['log_unknowns'][$logLine]++;
}
}
if(count($reportData['exception_log_error_count']['log_errors'])){
$reportData['exception_log_error_count']['result'] = 'FAIL';
}
}
$report = $reportData['exception_log_error_count']['title'].' ('.$reportData['exception_log_error_count']['result'].')'.PHP_EOL;
if(count($reportData['exception_log_error_count']['log_errors'])){
$report .= 'Errors found'.PHP_EOL;
arsort($reportData['exception_log_error_count']['log_errors']);
}
foreach($reportData['exception_log_error_count']['log_errors'] as $error => $count){
$report .= $count.' '.trim($error).PHP_EOL;
}
if(count($reportData['exception_log_error_count']['log_unknowns'])){
$report .= PHP_EOL.'Unknown Lines (top 10)'.PHP_EOL;
arsort($reportData['exception_log_error_count']['log_unknowns']);
}
$counter = 0;
foreach($reportData['exception_log_error_count']['log_unknowns'] as $error => $count){
$counter++;
if($counter == 10){
break;
}
$report .= $count.' '.preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $error).PHP_EOL;
}
$reportData['exception_log_error_count']['report'] = $report;
}
function checkForRecentReports(&$reportData){
$reportData['recent_reports'] = [
'title' => 'Check for recent reports',
'errors' => [],
'recent_reports' => [],
'old_reports' => 0,
'result' => 'PASS',
'report' => '',
];
if(!is_dir('var/report')){
// all good?
}else{
$files = [];
scanDirRescursive('var/report', $files);
$currentTime = time();
foreach($files as $file){
$lastModified = filemtime($file);
if($lastModified > ($currentTime - SYSTEM_LOG_LAST_WRITTEN_TO)){
$reportData['recent_reports']['recent_reports'][$file] = $lastModified;
}else{
$reportData['recent_reports']['old_reports']++;
}
}
}
if(count($reportData['recent_reports']['recent_reports'])){
$reportData['recent_reports']['result'] = 'FAIL';
arsort($reportData['recent_reports']['recent_reports']);
}
$report = $reportData['recent_reports']['title'].' '.$reportData['recent_reports']['result'].PHP_EOL;
if(count($reportData['recent_reports']['recent_reports'])){
$report .= '10 Most Recent Reports'.PHP_EOL;
$counter = 0;
foreach($reportData['recent_reports']['recent_reports'] as $file => $lastModified){
$counter++;
if($counter == 10){
break;
}
$report .= date('Y-m-d H:i:s', $lastModified).' '.$file.PHP_EOL;
}
}
if($reportData['recent_reports']['old_reports'] > 0){
$report .= PHP_EOL.$reportData['recent_reports']['old_reports'].' old reports found recommend running: "find ./report -type f -mtime +30 -exec rm {} \;" on production'.PHP_EOL;
}
$reportData['recent_reports']['report'] = $report;
}
function printReport($reportData){
echo str_repeat('=', REPORT_WIDTH).PHP_EOL;
$title = 'Log and report files analysis';
echo str_repeat(' ', floor((REPORT_WIDTH / 2) - (strlen($title) / 2))).$title.PHP_EOL;
echo str_repeat('=', REPORT_WIDTH).PHP_EOL;
$longestReportTitleLength = 0;
foreach($reportData as $reportId => $reportdata){
if(strlen($reportdata['title']) > $longestReportTitleLength){
$longestReportTitleLength = strlen($reportdata['title']);
}
}
$longestReportTitleLength += 10;
echo 'Summary'.PHP_EOL;
foreach($reportData as $reportId => $reportdata){
echo $reportdata['title'].str_repeat(' ', ($longestReportTitleLength - strlen($reportdata['title']))).$reportdata['result'].PHP_EOL;
}
echo PHP_EOL;
echo PHP_EOL.str_repeat('-', REPORT_WIDTH).PHP_EOL;
echo 'Detail'.PHP_EOL;
foreach($reportData as $reportId => $reportdata){
echo $reportdata['report'];
echo PHP_EOL.str_repeat('-', REPORT_WIDTH).PHP_EOL;
}
}
$reportData = [];
checkVarLogFileSizes($reportData);
// checkSystemLogLastWrittenTo($reportData);
checkSystemLogErrorCount($reportData);
checkExceptionLogLastWrittenTo($reportData);
checkExceptionLogErrorCount($reportData);
checkForRecentReports($reportData);
printReport($reportData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment