2014-02-03 03:03:05 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# UTILITY MODULE
|
|
|
|
####################################################################################################################################
|
|
|
|
package pg_backrest_utility;
|
|
|
|
|
2014-02-21 15:34:17 +03:00
|
|
|
use threads;
|
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Carp;
|
|
|
|
use IPC::System::Simple qw(capture);
|
2014-02-15 03:56:28 +03:00
|
|
|
use Fcntl qw(:DEFAULT :flock);
|
2014-04-28 16:13:25 +03:00
|
|
|
use File::Path qw(remove_tree);
|
2014-06-07 18:51:27 +03:00
|
|
|
use File::Basename;
|
|
|
|
|
|
|
|
use lib dirname($0) . "/../lib";
|
|
|
|
use BackRest::Exception;
|
2014-02-03 03:03:05 +03:00
|
|
|
|
|
|
|
use Exporter qw(import);
|
|
|
|
|
2014-02-14 04:29:42 +03:00
|
|
|
our @EXPORT = qw(data_hash_build trim common_prefix wait_for_file date_string_get file_size_format execute
|
|
|
|
log log_file_set log_level_set
|
2014-02-15 03:56:28 +03:00
|
|
|
lock_file_create lock_file_remove
|
2014-05-14 22:07:37 +03:00
|
|
|
TRACE DEBUG ERROR ASSERT WARN INFO OFF true false);
|
2014-02-03 03:03:05 +03:00
|
|
|
|
|
|
|
# Global constants
|
|
|
|
use constant
|
|
|
|
{
|
|
|
|
true => 1,
|
|
|
|
false => 0
|
|
|
|
};
|
|
|
|
|
2014-02-14 01:11:53 +03:00
|
|
|
use constant
|
|
|
|
{
|
|
|
|
TRACE => 'TRACE',
|
|
|
|
DEBUG => 'DEBUG',
|
|
|
|
INFO => 'INFO',
|
|
|
|
WARN => 'WARN',
|
|
|
|
ERROR => 'ERROR',
|
|
|
|
ASSERT => 'ASSERT',
|
|
|
|
OFF => 'OFF'
|
|
|
|
};
|
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
my $hLogFile;
|
2014-02-14 01:11:53 +03:00
|
|
|
my $strLogLevelFile = ERROR;
|
|
|
|
my $strLogLevelConsole = ERROR;
|
|
|
|
my %oLogLevelRank;
|
|
|
|
|
2014-04-28 16:13:25 +03:00
|
|
|
my $strLockPath;
|
2014-02-15 03:56:28 +03:00
|
|
|
my $hLockFile;
|
|
|
|
|
2014-02-14 01:11:53 +03:00
|
|
|
$oLogLevelRank{TRACE}{rank} = 6;
|
|
|
|
$oLogLevelRank{DEBUG}{rank} = 5;
|
|
|
|
$oLogLevelRank{INFO}{rank} = 4;
|
|
|
|
$oLogLevelRank{WARN}{rank} = 3;
|
|
|
|
$oLogLevelRank{ERROR}{rank} = 2;
|
|
|
|
$oLogLevelRank{ASSERT}{rank} = 1;
|
|
|
|
$oLogLevelRank{OFF}{rank} = 0;
|
2014-02-13 21:54:43 +03:00
|
|
|
|
2014-02-15 03:56:28 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# LOCK_FILE_CREATE
|
|
|
|
####################################################################################################################################
|
|
|
|
sub lock_file_create
|
|
|
|
{
|
2014-04-28 16:13:25 +03:00
|
|
|
my $strLockPathParam = shift;
|
|
|
|
|
|
|
|
my $strLockFile = $strLockPathParam . "/process.lock";
|
2014-02-15 03:56:28 +03:00
|
|
|
|
|
|
|
if (defined($hLockFile))
|
|
|
|
{
|
2014-04-28 16:13:25 +03:00
|
|
|
confess &lock(ASSERT, "${strLockFile} lock is already held");
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-04-28 16:13:25 +03:00
|
|
|
$strLockPath = $strLockPathParam;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-04-28 16:13:25 +03:00
|
|
|
unless (-e $strLockPath)
|
|
|
|
{
|
|
|
|
if (system("mkdir -p ${strLockPath}") != 0)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "Unable to create lock path ${strLockPath}");
|
|
|
|
}
|
2014-02-15 03:56:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sysopen($hLockFile, $strLockFile, O_WRONLY | O_CREAT)
|
|
|
|
or confess &log(ERROR, "unable to open lock file ${strLockFile}");
|
|
|
|
|
|
|
|
if (!flock($hLockFile, LOCK_EX | LOCK_NB))
|
|
|
|
{
|
|
|
|
close($hLockFile);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-15 03:56:28 +03:00
|
|
|
return $hLockFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# LOCK_FILE_REMOVE
|
|
|
|
####################################################################################################################################
|
|
|
|
sub lock_file_remove
|
|
|
|
{
|
|
|
|
if (defined($hLockFile))
|
|
|
|
{
|
|
|
|
close($hLockFile);
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-04-28 16:13:25 +03:00
|
|
|
remove_tree($strLockPath) or confess &log(ERROR, "unable to delete lock path ${strLockPath}");
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-15 03:56:28 +03:00
|
|
|
$hLockFile = undef;
|
2014-04-28 16:13:25 +03:00
|
|
|
$strLockPath = undef;
|
2014-02-15 03:56:28 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "there is no lock to free");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# DATA_HASH_BUILD - Hash a delimited file with header
|
|
|
|
####################################################################################################################################
|
|
|
|
sub data_hash_build
|
|
|
|
{
|
2014-06-02 23:26:37 +03:00
|
|
|
my $oHashRef = shift;
|
2014-02-03 03:03:05 +03:00
|
|
|
my $strData = shift;
|
|
|
|
my $strDelimiter = shift;
|
|
|
|
my $strUndefinedKey = shift;
|
|
|
|
|
|
|
|
my @stryFile = split("\n", $strData);
|
|
|
|
my @stryHeader = split($strDelimiter, $stryFile[0]);
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
for (my $iLineIdx = 1; $iLineIdx < scalar @stryFile; $iLineIdx++)
|
|
|
|
{
|
|
|
|
my @stryLine = split($strDelimiter, $stryFile[$iLineIdx]);
|
|
|
|
|
|
|
|
if (!defined($stryLine[0]) || $stryLine[0] eq "")
|
|
|
|
{
|
|
|
|
$stryLine[0] = $strUndefinedKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (my $iColumnIdx = 1; $iColumnIdx < scalar @stryHeader; $iColumnIdx++)
|
|
|
|
{
|
2014-06-02 23:26:37 +03:00
|
|
|
if (defined(${$oHashRef}{"$stryHeader[0]"}{"$stryLine[0]"}{"$stryHeader[$iColumnIdx]"}))
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
|
|
|
confess "the first column must be unique to build the hash";
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-06-02 23:26:37 +03:00
|
|
|
if (defined($stryLine[$iColumnIdx]) && $stryLine[$iColumnIdx] ne "")
|
|
|
|
{
|
|
|
|
${$oHashRef}{"$stryHeader[0]"}{"$stryLine[0]"}{"$stryHeader[$iColumnIdx]"} = $stryLine[$iColumnIdx];
|
|
|
|
}
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# TRIM - trim whitespace off strings
|
|
|
|
####################################################################################################################################
|
|
|
|
sub trim
|
|
|
|
{
|
|
|
|
my $strBuffer = shift;
|
|
|
|
|
2014-05-27 16:00:24 +03:00
|
|
|
if (!defined($strBuffer))
|
|
|
|
{
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
$strBuffer =~ s/^\s+|\s+$//g;
|
|
|
|
|
|
|
|
return $strBuffer;
|
|
|
|
}
|
|
|
|
|
2014-02-14 04:29:42 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# WAIT_FOR_FILE
|
|
|
|
####################################################################################################################################
|
|
|
|
sub wait_for_file
|
|
|
|
{
|
|
|
|
my $strDir = shift;
|
|
|
|
my $strRegEx = shift;
|
|
|
|
my $iSeconds = shift;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-14 04:29:42 +03:00
|
|
|
my $lTime = time();
|
|
|
|
my $hDir;
|
|
|
|
|
|
|
|
while ($lTime > time() - $iSeconds)
|
|
|
|
{
|
|
|
|
opendir $hDir, $strDir or die "Could not open dir: $!\n";
|
|
|
|
my @stryFile = grep(/$strRegEx/i, readdir $hDir);
|
|
|
|
close $hDir;
|
|
|
|
|
|
|
|
if (scalar @stryFile == 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
confess &log(ERROR, "could not find $strDir/$strRegEx after $iSeconds second(s)");
|
|
|
|
}
|
|
|
|
|
2014-02-05 18:40:49 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# COMMON_PREFIX
|
|
|
|
####################################################################################################################################
|
|
|
|
sub common_prefix
|
|
|
|
{
|
|
|
|
my $strString1 = shift;
|
|
|
|
my $strString2 = shift;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-05 18:40:49 +03:00
|
|
|
my $iCommonLen = 0;
|
|
|
|
my $iCompareLen = length($strString1) < length($strString2) ? length($strString1) : length($strString2);
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-05 18:40:49 +03:00
|
|
|
for (my $iIndex = 0; $iIndex < $iCompareLen; $iIndex++)
|
|
|
|
{
|
|
|
|
if (substr($strString1, $iIndex, 1) ne substr($strString2, $iIndex, 1))
|
|
|
|
{
|
|
|
|
last;
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-05 18:40:49 +03:00
|
|
|
$iCommonLen ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $iCommonLen;
|
|
|
|
}
|
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# FILE_SIZE_FORMAT - Format file sizes in human-readable form
|
|
|
|
####################################################################################################################################
|
|
|
|
sub file_size_format
|
|
|
|
{
|
|
|
|
my $lFileSize = shift;
|
|
|
|
|
|
|
|
if ($lFileSize < 1024)
|
|
|
|
{
|
|
|
|
return $lFileSize . "B";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lFileSize < (1024 * 1024))
|
|
|
|
{
|
|
|
|
return int($lFileSize / 1024) . "KB";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lFileSize < (1024 * 1024 * 1024))
|
|
|
|
{
|
|
|
|
return int($lFileSize / 1024 / 1024) . "MB";
|
|
|
|
}
|
|
|
|
|
|
|
|
return int($lFileSize / 1024 / 1024 / 1024) . "GB";
|
|
|
|
}
|
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
####################################################################################################################################
|
|
|
|
# DATE_STRING_GET - Get the date and time string
|
|
|
|
####################################################################################################################################
|
|
|
|
sub date_string_get
|
|
|
|
{
|
2014-02-13 21:54:43 +03:00
|
|
|
my $strFormat = shift;
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
if (!defined($strFormat))
|
|
|
|
{
|
|
|
|
$strFormat = "%4d%02d%02d-%02d%02d%02d";
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
|
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
return(sprintf($strFormat, $year+1900, $mon+1, $mday, $hour, $min, $sec));
|
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# LOG_FILE_SET - set the file messages will be logged to
|
|
|
|
####################################################################################################################################
|
|
|
|
sub log_file_set
|
|
|
|
{
|
|
|
|
my $strFile = shift;
|
2014-02-14 01:11:53 +03:00
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
$strFile .= "-" . date_string_get("%4d%02d%02d") . ".log";
|
|
|
|
my $bExists = false;
|
2014-02-14 01:11:53 +03:00
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
if (-e $strFile)
|
|
|
|
{
|
|
|
|
$bExists = true;
|
|
|
|
}
|
2014-02-14 01:11:53 +03:00
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
open($hLogFile, '>>', $strFile) or confess "unable to open log file ${strFile}";
|
2014-02-14 01:11:53 +03:00
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
if ($bExists)
|
|
|
|
{
|
|
|
|
print $hLogFile "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
print $hLogFile "-------------------PROCESS START-------------------\n";
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
2014-02-14 01:11:53 +03:00
|
|
|
# LOG_LEVEL_SET - set the log level for file and console
|
2014-02-03 03:03:05 +03:00
|
|
|
####################################################################################################################################
|
2014-02-14 01:11:53 +03:00
|
|
|
sub log_level_set
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2014-02-14 01:11:53 +03:00
|
|
|
my $strLevelFileParam = shift;
|
|
|
|
my $strLevelConsoleParam = shift;
|
|
|
|
|
2014-06-04 04:22:07 +03:00
|
|
|
if (defined($strLevelFileParam))
|
2014-02-14 01:11:53 +03:00
|
|
|
{
|
2014-06-04 04:22:07 +03:00
|
|
|
if (!defined($oLogLevelRank{"${strLevelFileParam}"}{rank}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "file log level ${strLevelFileParam} does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
$strLogLevelFile = $strLevelFileParam;
|
2014-02-14 01:11:53 +03:00
|
|
|
}
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2014-06-04 04:22:07 +03:00
|
|
|
if (defined($strLevelConsoleParam))
|
2014-02-14 01:11:53 +03:00
|
|
|
{
|
2014-06-04 04:22:07 +03:00
|
|
|
if (!defined($oLogLevelRank{"${strLevelConsoleParam}"}{rank}))
|
|
|
|
{
|
|
|
|
confess &log(ERROR, "console log level ${strLevelConsoleParam} does not exist");
|
|
|
|
}
|
2014-02-14 01:11:53 +03:00
|
|
|
|
2014-06-04 04:22:07 +03:00
|
|
|
$strLogLevelConsole = $strLevelConsoleParam;
|
|
|
|
}
|
2014-02-14 01:11:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# LOG - log messages
|
|
|
|
####################################################################################################################################
|
2014-02-03 03:03:05 +03:00
|
|
|
sub log
|
|
|
|
{
|
|
|
|
my $strLevel = shift;
|
|
|
|
my $strMessage = shift;
|
2014-06-07 18:51:27 +03:00
|
|
|
my $iCode = shift;
|
|
|
|
|
|
|
|
my $strMessageFormat = $strMessage;
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2014-02-14 01:11:53 +03:00
|
|
|
if (!defined($oLogLevelRank{"${strLevel}"}{rank}))
|
|
|
|
{
|
|
|
|
confess &log(ASSERT, "log level ${strLevel} does not exist");
|
|
|
|
}
|
2014-06-04 05:02:56 +03:00
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
|
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
if (!defined($strMessageFormat))
|
2014-02-03 03:03:05 +03:00
|
|
|
{
|
2014-06-07 18:51:27 +03:00
|
|
|
$strMessageFormat = "(undefined)";
|
2014-02-03 03:03:05 +03:00
|
|
|
}
|
|
|
|
|
2014-02-13 21:54:43 +03:00
|
|
|
if ($strLevel eq "TRACE")
|
|
|
|
{
|
2014-06-07 22:30:13 +03:00
|
|
|
$strMessageFormat =~ s/\n/\n /g;
|
2014-06-07 18:51:27 +03:00
|
|
|
$strMessageFormat = " " . $strMessageFormat;
|
2014-02-13 21:54:43 +03:00
|
|
|
}
|
|
|
|
elsif ($strLevel eq "DEBUG")
|
|
|
|
{
|
2014-06-07 22:30:13 +03:00
|
|
|
$strMessageFormat =~ s/\n/\n /g;
|
2014-06-07 18:51:27 +03:00
|
|
|
$strMessageFormat = " " . $strMessageFormat;
|
2014-02-13 21:54:43 +03:00
|
|
|
}
|
2014-06-07 22:30:13 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$strMessageFormat =~ s/\n/\n /g;
|
|
|
|
}
|
2014-02-13 21:54:43 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
$strMessageFormat = sprintf("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec) .
|
|
|
|
(" " x (7 - length($strLevel))) . "${strLevel} " . (" " x (2 - length(threads->tid()))) .
|
2014-06-07 22:30:13 +03:00
|
|
|
threads->tid() . ": ${strMessageFormat}" .
|
|
|
|
(defined($iCode) ? " (code ${iCode})" : "") . "\n";
|
2014-02-13 21:54:43 +03:00
|
|
|
|
2014-02-14 01:11:53 +03:00
|
|
|
if ($oLogLevelRank{"${strLevel}"}{rank} <= $oLogLevelRank{"${strLogLevelConsole}"}{rank})
|
2014-02-13 21:54:43 +03:00
|
|
|
{
|
2014-06-07 18:51:27 +03:00
|
|
|
print $strMessageFormat;
|
2014-02-14 01:11:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($oLogLevelRank{"${strLevel}"}{rank} <= $oLogLevelRank{"${strLogLevelFile}"}{rank})
|
|
|
|
{
|
|
|
|
if (defined($hLogFile))
|
|
|
|
{
|
2014-06-07 18:51:27 +03:00
|
|
|
print $hLogFile $strMessageFormat;
|
2014-02-14 01:11:53 +03:00
|
|
|
}
|
2014-02-13 21:54:43 +03:00
|
|
|
}
|
2014-02-03 03:03:05 +03:00
|
|
|
|
2014-06-07 18:51:27 +03:00
|
|
|
if (defined($iCode))
|
|
|
|
{
|
|
|
|
return BackRest::Exception->new(iCode => $iCode, strMessage => $strMessage);
|
|
|
|
}
|
|
|
|
|
2014-02-03 03:03:05 +03:00
|
|
|
return $strMessage;
|
|
|
|
}
|
|
|
|
|
2014-06-04 18:58:30 +03:00
|
|
|
1;
|