Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

solved: timestamp to localtime

by roadtest (Sexton)
on Jun 13, 2011 at 19:08 UTC ( [id://909425]=perlquestion: print w/replies, xml ) Need Help??

roadtest has asked for the wisdom of the Perl Monks concerning the following question:

Here is what i did and your comment is welcome. I have two questions:

1. When I calculate $CurSec, i tried following first and it didn't work. It seems I can't pass list variable to timelocal directly. Is that true?

====

my @CurTime=`date +%S,%M,%H,%d,%m,%Y`; my $CurSec=timelocal(@CurTime);

====

2. how can I aggregate the same client IP together so that report can show how much data were transferred and where data are.

Ideally, I am trying to format the report like:

TIME(second) CLIENT OP OFFSET(KB) BYTES PATHNAME 20,172.22.32.28, W,200,4056099,/export/nfs/file1

TIME - how long we run the trace programe, above line indicates 20 seconds

CLIENT - client server's IP

OP - operation, write or read

OFFSET(KB) - total data transferred during period of TIME

BYTES - size of the file

PATHNAME - location of created file

Thanks in advance,

carl

#!/usr/bin/perl use warnings; use strict; use Time::Local; my $Uptime=`ps -p 1 -o etime | tail -1`; my ($sec,$min,$hour,$mday,$mon,$year)=(localtime)[0,1,2,3,4,5]; my $CurSec=timelocal($sec,$min,$hour,$mday,$mon,$year+1900); sub LastReboot { my ($Days, $Hours, $Minutes, $Seconds, $UpSeconds); if ($Uptime =~ /[0-9]+\-/) { ($Days, $Hours, $Minutes, $Seconds) = $Uptime =~ /([0-9]*)\-?([0-9 +]+):([0-9]+):([0-9]+)/; } elsif ($Uptime =~ /[0-9]+:[0-9]+:[0-9]+/) { ($Hours, $Minutes, $Seconds) = $Uptime =~ /([0-9]+):([0-9]+): +([0-9]+)/; $Days = 0; } else { ($Minutes, $Seconds) = $Uptime =~ /([0-9]+):([0-9]+)/; $Days = 0; $Hours = 0;} $UpSeconds=$Days*24*60*60 + $Hours*60*60 + $Minutes*60 + $Seconds; return $CurSec-$UpSeconds; } my $BaseLine = &LastReboot; print "$BaseLine\n"; print "TIME(seconds),CLIENT,OP,OFFSET(KB),BYTES PATHNAME\n"; while (<DATA>) { next if ($. == 1); chomp; my @output = split; my $MyTime = localtime($BaseLine + $output[0]/1000000000); print "$MyTime,$output[1], $output[2], $output[3],$output[4],$output[5 +]\n"; } __DATA__ TIME(us) CLIENT OP OFFSET(KB) BYTES PATHNAME 13463659875129 172.22.32.28 W 4952 2934 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659884704 172.22.32.28 W 4952 3004 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659927187 172.22.32.28 W 4952 3144 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659899744 172.22.32.28 W 4952 3074 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659937424 172.22.32.28 W 4952 3214 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659999853 172.22.32.28 W 4952 3500 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463660033528 172.22.32.28 W 4952 3570 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463660043638 172.22.32.28 W 4952 3640 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659951798 172.22.32.28 W 4952 3291 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659977355 172.22.32.28 W 4952 3361 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463659990535 172.22.32.28 W 4952 3430 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463660245464 172.22.112.136 W 5104 12471 /export/energ +y/energy_dev/stg/rw/sessions/backup/.nfs7C9F/../ds-259 7025835046513461/10.in 13463660202519 172.22.32.28 W 4952 4126 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463660234779 172.22.32.28 W 4956 100 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463660160523 172.22.32.28 W 4952 3918 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463660249629 172.22.32.28 W 4956 170 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log 13463660267596 172.22.32.28 W 4956 249 /export/energ +y/energy_dev/stg/rw/log/GRIDLOGS/FrontOfficeSessionExt ractCash.log

original posting below

============================================================================

Dtrace is an utility in solaris 10. It can generate report with timestamp like following:

TIME(us) CLIENT OP OFFSET(KB) BYTES PATHNAME 13463659875129 172.22.32.28 W 4952 2934 /export/nfs/f +ile1 tractCash.log 13463659884704 172.22.32.28 W 4952 3004 /export/nfs/f +ile2 13463659927187 172.22.32.28 W 4952 3144 /export/nfs/f +ile1

NOTE: timestamp here is from last system reboot not EPOCH time.

In order to replace the first column with readable localtime, I am planning to do followings:

1. ps -p 1 -o etime | tail -1, it will give me the system uptime time in format "day-hour:minute:second"

2. use timelocal in module Time::Local to convert current time to seconds since EPOCH

3. Value from 2 minus Value from 1 should give me the EPOCH time of last system reboot

4. Value from 3 plus first column in dtrace script output("13463659884704/1000000") should give the EPOCH time of the record

5. localtime(Value 4) should display it in readable local time format.

Do you think this is the right approach? Is there better way to achieve the goal?

Your comment is appreciated!

carl

Replies are listed 'Best First'.
Re: timestamp to localtime
by ikegami (Patriarch) on Jun 13, 2011 at 19:36 UTC

    Corrections:

    1. ps -p 1 -o etime | tail -1 will give the system uptime time in format "day-hour:minute:second"
    2. Use timelocal in module Time::Local to convert the current timesystem uptime to seconds since EPOCH
    3. Value from 2time() minus Value from 12 should give me the EPOCH time of last system reboot
    4. Value from 3 plus first column in dtrace script output divided by 1000000 should give the EPOCH time of the record
    5. localtime(Value 4) should display it in readable local time format.

    If you could get the reboot time directly, you'd get more precise results.

Re: timestamp to localtime
by dwm042 (Priest) on Jun 13, 2011 at 20:23 UTC
    My gut feeling is that an approach using who -b would be more portable across architectures and accurate to within a minute.

    That and a good date manipulation package should be enough, unless you really really do need times down to the second.

    David.
Re: solved: timestamp to localtime
by Anonymous Monk on Jun 15, 2011 at 03:38 UTC
    use Data::Dumper; my @CurTime=`date +%S,%M,%H,%d,%m,%Y`; print Dumper \@CurTime;

    shows that @CurTime is a one element array which is why the timelocal does not seem to work.

    In what way does perl's built in 'time' function not meet your needs?

    perldoc -f time

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://909425]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-16 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found