Files
resatennis_tyroltenn/IDrive/.config/c.Log.php

244 lines
8.9 KiB
PHP
Executable File

<?php
class Log {
public static $log_dir = "LOGS";
public static function ReadLogs($duration = '') {
$jobs = Scheduler::ReadJobs();
$all_jobs = array(Scheduler::$backup_dir.Scheduler::$manual_dir,Scheduler::$localbackup_dir.Scheduler::$manual_dir,Scheduler::$restore_dir.Scheduler::$manual_dir,Scheduler::$localrestore_dir.Scheduler::$manual_dir,Scheduler::$archiveclean_dir.Scheduler::$manual_dir,Scheduler::$restore_dir.Scheduler::$trash_manual_dir, Scheduler::$delete_dir);
foreach($jobs as $k=>$job) {
$jobName = substr($k,(strlen($job["job_type"])+1));
$all_jobs[] = Scheduler::$user_dir.$job["job_type"]."/".$jobName;
$archiveDir = Scheduler::$user_dir.Scheduler::$archive."/".$jobName;
if($job["job_type"] == BACKUPDIR && file_exists($archiveDir)){
$all_jobs[] = $archiveDir;
}
}
$log_files = self::ReadLogFiles(true, $all_jobs, $duration);
return $log_files;
}
public static function ReadAllPid() {
$jobs = Scheduler::ReadJobs();
$all_jobs = array(Scheduler::$backup_dir.Scheduler::$manual_dir,Scheduler::$localbackup_dir.Scheduler::$manual_dir,Scheduler::$restore_dir.Scheduler::$manual_dir,Scheduler::$localrestore_dir.Scheduler::$manual_dir,Scheduler::$archiveclean_dir.Scheduler::$manual_dir);
foreach($jobs as $k=>$job) {
$jobName = substr($k,(strlen($job["job_type"])+1));
$all_jobs[] = Scheduler::$user_dir.$job["job_type"]."/".$jobName;
$archiveDir = Scheduler::$user_dir.Scheduler::$archive."/".$jobName;
if($job["job_type"] == BACKUPDIR && file_exists($archiveDir)){
$all_jobs[] = $archiveDir;
}
}
$watcher = array();
foreach($all_jobs as $job) {
$pidtxt = $job."/".LOG_PID_FILE;
if(file_exists($pidtxt)) {
$log_file_full = file_get_contents($pidtxt);
if($log_file_full != "") {
$log_file_full_arr = explode("/", $log_file_full);
$log_details = explode("_", $log_file_full_arr[count($log_file_full_arr) - 1]);
$full_path = trim($job."/".self::$log_dir."/".$log_details[0]);
$job_details = explode("/", $job);
$log_details_arr = explode(" ", $log_details[0]);
$datetime = $log_details_arr[0]." ".$log_details_arr[1];
$job_name = $job_details[count($job_details) - 1];
$job_type = $job_details[count($job_details) - 2];
$meridian = trim($log_details_arr[2]);
$datetime .= " $meridian";
if(count($log_details) > 1) {
@unlink($pidtxt);
$status = trim($log_details[1]);
}else {
$status = "In Progress";
}
if (file_exists($log_file_full)){
$watcher[$full_path] = array("datetime"=>$datetime, "timestamp"=>strtotime($datetime), "job_name"=>$job_name, "job_type"=>$job_type, "status"=>$status);
}
}
}
}
return $watcher;
}
public static function WatchLogs() {
$watcher = self::ReadAllPid();
return array("watcher"=>$watcher);
}
/*********************************************************************************************************
* Function Name : getDateRange
* Objective : This function will return time stamp for the passed duration
* Added By : Sabin Cheruvattil | 2016-06-07
**********************************************************************************************************/
protected static function getDateRange($duration = '')
{
$date_range = array();
switch($duration) {
case 'all':
$date_range['start'] = '';
$date_range['end'] = '';
break;
case '1month':
$date_range['start'] = mktime(0, 0, 0, date('n') - 1);
$date_range['end'] = mktime(23, 59, 59);
break;
case '2week':
$date_range['start'] = mktime(0, 0, 0, date('n'), date('j') - 14);
$date_range['end'] = mktime(23, 59, 59);
break;
case '1week':
default:
$date_range['start'] = mktime(0, 0, 0, date('n'), date('j') - 7);
$date_range['end'] = mktime(23, 59, 59);
break;
}
return $date_range;
}
/*********************************************************************************************************
* Function Name : ReadLogFiles
* Objective : This function will provide the set of log files
* Modified By : Sabin Cheruvattil | 2016-06-07
**********************************************************************************************************/
public static function ReadLogFiles($array = true, $all_jobs, $duration = '') {
if($array) {
$log_array = array();
$date_range = Log::getDateRange($duration);
foreach($all_jobs as $job) {
$log_dir = $job."/".self::$log_dir;
if(is_dir($log_dir)){
if($dh = opendir($log_dir)) {
while(($file = readdir($dh)) !== false) {
if(($file === ".") || ($file === "..") || (strpos($file, "_ERRORFILE") !== FALSE) || ($file === "PROGRESS_DETAILS") || ($file === "LOG_DETAILS") || (strpos($file, ".pid") !== FALSE)) {
continue;
} else {
$log_details = explode("_", $file);
$key = $log_dir . "/" . $log_details[0];
$log_details_arr = explode(" ", $log_details[0]);
$type = '';
$datetime = '';
if(count($log_details_arr) > 3) {
$datetime = str_replace('-', '/', $log_details_arr[0]) . " " . $log_details_arr[1] . ' ' . $log_details_arr[2];
$type = $log_details_arr[3];
} else {
$datetime = str_replace('-', '/', $log_details_arr[0])." ".$log_details_arr[1];
$type = $log_details_arr[2];
}
$timestamp = strtotime($datetime);
if(($date_range['start'] != '' && $date_range['start'] > $timestamp) || ($date_range['end'] != '' && $date_range['end'] < $timestamp)) {
continue;
}
$log_array[$key] = array();
$job_details = explode("/", $job);
$log_array[$key]["job_name"] = $job_details[count($job_details) - 1];
$log_array[$key]["job_type"] = $job_details[count($job_details) - 2];
if($log_array[$key]["job_type"] == 'Delete')
$log_array[$key]["job_type"] = ucfirst(strtolower($type));
$log_array[$key]["type"] = $type;
$log_array[$key]["datetime"] = $datetime;
$log_array[$key]["timestamp"] = $timestamp;
if(count($log_details) > 1) {
$log_array[$key]["status"] = $log_details[1];
} else {
$log_array[$key]["status"] = "In Progress";
# added by anil for log related issues on 16/11/2018
$log_array[$key]["status"] = self::checkAndRenameFileWithStatus($job, $key);
}
}
}
closedir($dh);
}
}
}
return array("logs" => $log_array, "count" => count($log_array));
}
}
/****************************************************************************************************
* Function Name : checkAndRenameFileWithStatus
* Objective : This is to update status for log which is in progress with out running process.
* Added By : Anil Kumar
******************************************************************************************************/
public static function checkAndRenameFileWithStatus($jobDir, $key)
{
$pidFile = $jobDir."/".PID_FILE;
$logpidFile = $jobDir."/".LOG_PID_FILE;
$startPid = $jobDir."/".START_PID;
$statusToUpdate = "In Progress";
if((!file_exists($pidFile)) && (file_exists($logpidFile)) && (!file_exists($startPid)) ) {
$handle = fopen($logpidFile,'r+');
if(flock($handle, LOCK_EX|LOCK_NB))
{
flock($handle,LOCK_UN);
$buffer = fread($handle, LIMIT);
fclose($handle);
$statusToUpdate = "ABORTED";
# need to check and update status
$considered = $success = $synced = $failed = $status = $logFile = "";
$isSummary = 0;
$cmd = "tail -10 \"$key\"";
$logData = `$cmd`;
$res1 = explode("\n", $logData);
foreach($res1 as $line){
if(trim($line)!=''){
if (!$isSummary && (strpos($line, 'Summary:') !== false)) {
$isSummary = 1;
}
elseif($isSummary && (strpos($line, 'considered') !== false)){
$data = explode(":", $line);
$considered = $data[1];
trim($considered);
} elseif($isSummary && ((strpos($line, 'backed') !== false) || (strpos($line, 'restored'!== false)))){
$data = explode(":", $line);
$success = $data[1];
trim($success);
} elseif($isSummary && (strpos($line, 'already present') !== false)){
$data = explode(":", $line);
$synced = $data[1];
trim($synced);
} elseif($isSummary && (strpos($line, 'failed') !== false)){
$data = explode(":", $line);
$failed = $data[1];
trim($failed);
}
}
}
if($isSummary){
if($failed > 0 || $considered == 0){
$statusToUpdate = "FAILURE";
} elseif($considered == ($success+$synced)){
$statusToUpdate = "SUCCESS";
}
}
# need to change the status.
$newName = "$key"."_".$statusToUpdate;
$resCmd = "mv \"$key\" \"$newName\"";
$res = `$resCmd`;
}
}
return $statusToUpdate;
}
}
?>