Solved

How do I get the year to display using the lat command?

Posted on 2006-06-18
9
550 Views
Last Modified: 2013-12-27
Using the followng command I can see when all the system reboots occurred but the date field does not include the year.

last | grep reboot

reboot    system boot                   Sun Jun 18 08:23
reboot    system boot                   Tue May 23 19:55
reboot    system boot                   Tue Apr 18 11:15
reboot    system boot                   Wed Jul 13 09:29
reboot    system boot                   Tue Jul 12 10:07
reboot    system boot                   Tue Jun 28 09:46
reboot    system boot                   Tue Jun 14 09:08

Does anyone know how I can get the year to display. If not the date and times show the most recent at the top. Does anyone have a script that will look at the month and time and put in the year based on seeing something like Jun occurring again after Jul when viewing the output  or is there a file or a command that will provide me with information on reboots that include year.

Thanks for your help
0
Comment
Question by:bradd138
  • 4
  • 3
9 Comments
 

Author Comment

by:bradd138
Comment Utility
Sorry for the typo in the title.  Using the last command not lat.
0
 
LVL 38

Expert Comment

by:yuzh
Comment Utility
last command on Solaris systems doesn't report the year included in its records,

please have a look at the following doc for workarounds:
http://www.itworld.com/nl/unix_insider/03312005/pf_index.html

other tools:
http://www.boran.com/security/sp/solaris/
0
 

Author Comment

by:bradd138
Comment Utility
Thanks for the help yuzh but I couldn't seen to get the unpack commands to work correctly. Had had trouble with the stucture part recommend in the
http://www.oreilly.com/catalog/perlsysadm/chapter/ch09.html . I brought back information the field size weren't correct so it was hard to work with.

I put together the below pearl script that although it is not very elegant since I rarely code in pearl it appears to work.

#!/usr/bin/perl
# Bradd Baldwin
# Program to add year to last reboot

system("last | grep reboot | awk '{print \$1,\$2,\$3,\$4,\$5,\$6,\$7}' > last.txt");
open(MYFILE, "last.txt") || die "opening last.txt: $!";
@LAST_REBOOT=<MYFILE>;
close(MYFILE);
$month=0;
$year="2006";

$current_month=`date +"%m"`;#+%m-%d-%y`;
chomp($current_month);
print "$current_month test\n\n";

foreach $LAST_REBOOT (@LAST_REBOOT) {
        @fields=split(/ /, $LAST_REBOOT);
                $check_month=join(' ',@fields[4]);

        if ($check_month eq "Jan") {
                $month=1;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        $bradd=test;
        if ($check_month eq "Feb") {
                $month=2;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Mar") {
                $month=3;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Apr") {
                $month=4;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "May") {
                $month=5;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Jun") {
                $month=6;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Jul") {
                $month=7;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Aug") {
                $month=8;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Sep") {
                $month=9;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Oct") {
                $month=10;
                if ($current_month < $month) {
                        $year=$year-1 ;
                        }
        }
        if ($check_month eq "Nov") {
                $month=11;
                if ($current_month < $month) {
                        $year=$year-1 ;
                                }
        }
        if ($check_month eq "Dec") {
                $month=12;
                if ($current_month < $month) {
                        $year=$year-1 ;
                                }
        }
        if ($month eq $month) {
                $DISPLAY_INFO=join(' ',@fields[0,1,2,3,4,5,6]);
        chomp($DISPLAY_INFO);
        print "$DISPLAY_INFO $year\n";
        }
        $current_month=$month;
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 38

Expert Comment

by:yuzh
Comment Utility
>but I couldn't seen to get the unpack commands to work correctly.
I'm not a perl guru, but I think the script in:
http://www.oreilly.com/catalog/perlsysadm/chapter/ch09.html

and you can modify it as in:
http://www.itworld.com/nl/unix_insider/03312005/pf_index.html

Anyway, you can use your own version of the script if you are happy with it.
you can modify:
   grep reboot
to:
   grep $1
   so that your can get the infor for a login name or reboot as well.


0
 

Author Comment

by:bradd138
Comment Utility
The perl script in Oreilly bring back the info directly form last which does not incule the year. My perl script has a problem if the first reboot comes in a year prior to the one we are currently in. This is also a problem for finding out years for when a person last logged in.  I would prefer to use the unpack script but it didn't work because the format of my wtmpx file does not match the struct utmp they provide. The code works it just brings back parts of the fields and not the ones I am looking for.  

From Oreilly page
"As we've shown, the format of the wtmp file differs from Unix variant to Unix variant. On top of this, a vendor can change the format of wtmp between OS releases, rendering your perfectly good unpack( ) template invalid. "

does anyone know the format of the wtmpx file on Solaris starting with version 5.6 and going to 5.9
 
0
 
LVL 38

Accepted Solution

by:
yuzh earned 350 total points
Comment Utility
>>does anyone know the format of the wtmpx file on Solaris starting with version 5.6 and going to 5.9

Please have a look at   /usr/include/utmpx.h for wtmp format , also have
a look at this doc:
http://www.sdsc.edu/~victor/acct.html
0
 

Author Comment

by:bradd138
Comment Utility
Thanks for all your help yuzh I finally found the right unpack format and this works.

#!/usr/bin/perl
$template = "A32 A4 A32 l s s2 x2 l2 l x20 s A257 x";
# determine the size of a record
$recordsize = length(pack($template,(  )));
$recordsize = length(pack($template,(  )));
# open the file
open(WTMP,"/var/adm/wtmpx") or die "Unable to open wtmpx:$!\n";

# read through it one record at a time
while (read(WTMP,$record,$recordsize)) {
    ($ut_user,$ut_id,$ut_line,$ut_pid,$ut_type,$ut_e_termination,
     $ut_e_exit,$tv_sec,$tv_usec,$ut_session,$ut_syslen,$ut_host)=
       unpack($template,$record);

    if ($ut_line eq "system boot"){
        print "rebooted ".scalar localtime($tv_sec)."\n";
        $reboots++;
    }
}

close(WTMP);
print "Total reboots: $reboots\n";

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now