Using Time::Piece with Apache::Log::Parser - apache

I am using Apache::Log::Parser to parse Apache log files.
I extracted the date from log file using the following code.
my $parser = Apache::Log::Parser->new(fast=>1);
my $log = $parser->parse($data);
$t = $log->{date};
Now,I tried to use Time::Piece to parse dates, but I'm unable to do it.
print "$t->day_of_month";
But, it's not working. How to use Time::Piece to parse date?

You cannot call methods on objects inside of string interpolation. It will probably output something like this:
Sat Feb 18 12:44:47 2017->day_of_month
Remove the double quotes "" to call the method.
print $t->day_of_month;
Now the output is:
18
Note that you need to create a Time::Piece object with localtime or gmtime if you have an epoch value in your log, or using strptime if the date is some kind of timestamp.

Related

Carbon Date M-y convert to Y-m?

I have a string "May-20". Is it possible to use Carbon Library to convert the date as "2020-05"?
Any function can covert it?
$date = new DateTime('Aug-20');
echo $date->format('Y-m');
Use the native method of PHP createFromFormat():
Carbon::createFromFormat('!M-y', 'Aug-20')
See the list of available format here:
https://www.php.net/manual/fr/datetime.format.php
If your string can be in non-English languages, see the Carbon method translateTimeString
Try this:
$carbon_date = new Carbon('Aug-20');
echo $carbon_date->format("Y-m");
Same in one line
echo (new Carbon('Aug-20'))->format("Y-m");

Python - Use %s in value of config file

I use a config file (type .ini) to save my SQL queries, then i get a query by its key. All work fine, until creating a query with parameters, example :
;the ini file
product_by_cat = select * from products where cat =%s
I use :
config = configparser.ConfigParser()
args= ('cat1')
config.read(path_to_ini_file)
query= config.get(section_where_are_stored_thequeries,key_of_the_query)
complete_query= query%args
I get the error :
TypeError: not all arguments converted during string formatting
So it try to format the string at retrieving the value from the ini file.
Any proposition of my problem.
You can use format function like this
ini file
product_by_cat = select * from products where cat ={}
python:
complete_query= query.format(args)
depending on the versions of ConfigParser (Python 2 or Python 3) you may need to double the % like this or it throws an error:
product_by_cat = select * from products where cat =%%s
Although a better way would be to use the raw version of the config parser, so the % char isn't interpreted
config = configparser.RawConfigParser()

monit alert based of previous log line in check file

In the following auth.log
Mon DD HH:MM:SS SFTPHOST internal-sftp[21583]: realpath "/path/to/*.txt"
Mon DD HH:MM:SS SFTPHOST internal-sftp[21583]: sent status No such file
I only want an alert on "sent status No such file" IFF the previous line does NOT contain *. As a stretch goal it would be nice to check that that line has the same PID (number in the square brackets).
Any way to do that? Or am I using the wrong tool?
You can do that with a CHECK PROGRAM combined with a custom script that will do all the hard work (something similar to https://stackoverflow.com/a/17228241/374236 if I understand you correctly).

dse pig datetime functions

Can someone give a full example of date time functions including the 'register' jar ? I have been trying to get CurrentTime() and ToDate() running without much success. I have the piggybank jar in classpath and registered the same. But it always says the function has to be defined before usage.
I read this question comparing datetime in pig before this.
Datetime functions can be easily implemented using native pig, you no need to go for piggybank jar.
Example:
In this example i will read set of dates from the input file, get the current datetime and calculate the total no of days between previous and current date
input.txt
2014-10-12T10:20:47
2014-08-12T10:20:47
2014-07-12T10:20:47
PigScript:
A = LOAD 'input.txt' AS (mydate:chararray);
B = FOREACH A GENERATE ToDate(mydate) AS prevDate,CurrentTime() AS currentDate,DaysBetween(CurrentTime(),ToDate(mydate)) AS diffDays;
DUMP B;
Output:
(2014-10-12T10:20:47.000+05:30, 2014-12-12T10:39:15.455+05:30, 61)
(2014-08-12T10:20:47.000+05:30, 2014-12-12T10:39:15.455+05:30, 122)
(2014-07-12T10:20:47.000+05:30, 2014-12-12T10:39:15.455+05:30, 153)
You can refer few examples from my old post
Human readable String date converted to date using Pig?
Storing Date and Time In PIG
how to convert UTC time to IST using pig

Write a script to extract a date string from a text file and replace it with a date for 7 days after in Windows

We have a text file which is telling WinRAR to zip all of the files after the mentioned date:
"C:\Program Files\winrar\Rar.exe" a -r -m0 -ta20140501 zip.rar e:\test\*
(-ta<date>: Process files modified after the specified date)
I want a script to extract the date information from -ta20140501 and replace it with a date 7 days later. That is, it will be 20140508.
Is there a way to do that?
Here is a script which can be executed with text editor UltraEdit to modify the date string in the text file.
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Define the parameters for a case-sensitive (faster) Perl
// regular expression find in current file from top to bottom.
UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.mode=0;
UltraEdit.activeDocument.findReplace.matchCase=true;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchDown=true;
if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
{
UltraEdit.activeDocument.findReplace.searchInColumn=false;
}
// Move caret to top of the active file.
UltraEdit.activeDocument.top();
// Is a date string in format YYYYMMDD after switch -ta found?
if(UltraEdit.activeDocument.findReplace.find("(?<=-ta)\\d{8}"))
{
// Get this date string from the current text file.
var sDate = UltraEdit.activeDocument.selection;
// Get year, month and day of month as integer numbers.
var nYear = parseInt(sDate.substr(0,4),10);
var nMonth = parseInt(sDate.substr(4,2),10);
var nDay = parseInt(sDate.substr(6,2),10);
// Get number of milliseconds since epoch (1970-01-01 00:00:00 GMT).
var nMsecEpoch = Date.UTC(nYear,nMonth-1,nDay,0,0,0);
// Create a new date object with a time 7 days after found date.
var oNewDate = new Date();
oNewDate.setTime(7*86400*1000+nMsecEpoch);
// Get year, month and day of month for this new date
// and convert it to a date string in format YYYYMMDD.
var sNewDate = oNewDate.getUTCFullYear().toString(10);
nMonth = oNewDate.getUTCMonth()+1;
if (nMonth < 10) sNewDate += "0";
sNewDate += nMonth.toString(10);
nDay = oNewDate.getUTCDate();
if (nDay < 10) sNewDate += "0";
sNewDate += nDay.toString(10);
// Write new string to file replacing still selected date string.
UltraEdit.activeDocument.write(sNewDate);
// Close the file with saving it.
UltraEdit.closeFile(UltraEdit.activeDocument.path,1);
}
}
UltraEdit uses the JavaScript core engine as the scripting engine. The JavaScript Date object, with its methods, is used in this UltraEdit script to add 7 days to date string found in the text file.
This scripting code needs to be copied into a new ASCII file saved, for example, to folder %APPDATA%\IDMComp\UltraEdit\Scripts with name UpdateDateString.js.
The following command can be used in a batch file to run UltraEdit with the file to modify and this script to modify the date string in the text file.
#start "Update Date" /wait /min "%ProgramFiles(x86)%\IDMComp\UltraEdit\uedit32.exe" /fni "Path to\Command Text File\WinRAR_Command_File.txt" /s,e="%APPDATA%\IDMComp\UltraEdit\Scripts\UpdateDateString.js"
"Path to\Command Text File\WinRAR_Command_File.txt" is the name of the text file to modify with complete path.
/fni must be the first parameter for uedit32.exe and specifies to force a new instance of UltraEdit for loading the text file and modifying it with the script. This is an important parameter supported since version 13.10 of UltraEdit for Windows in case of an UltraEdit instance is already running and the configuration setting Allow multiple instances is not enabled in configuration of UltraEdit.
/s,e="..." is the parameter to instruct UltraEdit to load the script file specified within the double quotes, run it on the file loaded, and exit after script finished.
As UltraEdit is a Windows GUI application, it is necessary to use command start to run UltraEdit with a minimized window and halt execution of the batch file until UltraEdit terminates itself.
But honestly, I think, it would make more sense and is definitely much easier, to use the WinRAR command line switch -tn7d instead of -taYYYYMMDD to archive all files modified in the last 7 days measured from current time.