#!/usr/bin/perl # This program writes a line containing the current time in seconds to a data # file for suspend and resume events so that the time asleep and a history of # time asleep can be parsed use strict; # If you like, change this.. However it makes sense to keep it in /root because # most likely have to be root to suspend anyway. my $file = "/root/.sleeptime"; # No need to edit below here.. my $now = time(); # epoch seconds my $linefailed = 0; my $lastlinefailed; my $arg=@ARGV[0]; if ($arg eq "-h" || $arg eq "--help") { printhelp(); exit; } # Record a "sleep" record if ($arg eq "sleep") { open(OUT,">>".$file) || die "Could not open $file for writing\n"; print OUT "S:" . $now . "\n"; } # Record a "resume" record if ($arg eq "resume") { open(OUT,">>".$file) || die "Could not open $file for writing\n"; print OUT "R:" . $now . "\n"; } # Print the latest sleep time result if ($arg eq "sleeptime" || $arg eq "") { open(IN, "$file") || die "Could not open $file for reading\n"; my ($line,$prevline,$oldline,$lastline); foreach $line () { chomp($line); $oldline = $lastline; $lastline = $line; } sleeptime($oldline,$lastline); } # Print the entire sleep history. In this routine, we do some additional work to # check to see if there was some bad data, ie: Multiple suspend or resume lines # in a row. As could happen if the laptop doesn't resume properly, etc. # We kludge the test a little to make sure we don't hit sleeptime() twice. if ($arg eq "sleephistory") { open(IN, "$file") || die "Could not open $file for reading\n"; my ($line,$prevline,$oldline,$lastline); my $test = 1; foreach $line () { chomp($line); $oldline = $lastline; $lastline = $line; # We go here if the last data pair failed if ($linefailed) { $test = 1; $linefailed = 0; sleeptime($oldline,$lastline); } # Only hit sleeptime every other $line we read unless ($test % 2) { sleeptime($oldline,$lastline); } $test++; } } # This function validates the two lines of data and sends the time difference # to the printtime() function if there is no bad data. sub sleeptime() { my ($oldline,$lastline) = (@_); my ($var,$oldlinetime) = split(':',$oldline); unless ($var eq "S") { # print "Previous line not sleep! Corrupted data?\n"; $linefailed = 1; $lastlinefailed = $lastline; } my ($var,$lastlinetime) = split(':',$lastline); unless ($var eq "R") { # print "last line not resume! Corrupted data?\n"; $linefailed = 1; $lastlinefailed = $lastline; } my $sleeptime = $lastlinetime - $oldlinetime; unless ($linefailed) { printtime($sleeptime); } } # Print the amount of time that we were asleep in D:H:M:S and a string.. sub printtime() { my $sleeptime = shift; my @parts = gmtime($sleeptime); my ($days,$hours,$minutes,$seconds) = @parts[7,2,1,0]; if ($days > 0) { printf ("\(%02d:%02d:%02d:%02d\) ",$days,$hours,$minutes,$seconds); print ("Asleep $days days $hours hours $minutes minutes $seconds seconds\n"); $hours = $minutes = $seconds = 0; } if ($hours > 0) { printf ("\(%02d:%02d:%02d:%02d\) ",$days,$hours,$minutes,$seconds); printf ("Asleep $hours hours $minutes minutes $seconds seconds\n"); $minutes = $seconds = 0; } if ($minutes > 0) { printf ("\(%02d:%02d:%02d:%02d\) ",$days,$hours,$minutes,$seconds); printf ("Asleep $minutes minutes $seconds seconds\n"); $seconds = 0; } if ($seconds > 0) { printf ("\(%02d:%02d:%02d:%02d\) ",$days,$hours,$minutes,$seconds); printf ("Asleep $seconds seconds\n"); } } sub printhelp() { print "Usage: [sleep] [resume] [sleeptime] [sleephistory]\n\n"; print " sleep - Records a Sleep record\n"; print " resume - Records a Resume record\n"; print " sleeptime - Prints the last time asleep (default)\n"; print "sleephistory - Prints the entire history\n\n"; print "If multiple Sleep or Resume entries are found in a row when"; print "parsing the data file the program attempts to print the data"; print "which makes sense. It skips the duplicate data until it finds"; print "a valid S/R pair.\n\n"; print "If no arguments are given, sleeptime is assumed.\n"; }