New sleep.awk

I’ve made a change to the output. I added percent sleep and wake.

sleep.awk
awk ’
NR==1 { a=substr($1,6,2); b=substr($1,9,2); c=substr($1,12,8); y=substr($1,1,4)}
{if ($7 ~ /after/){
hh1=int($8/3600)
mm1=int(($8%3600)/60)
ss1=($8%3600)%60
TotalSleep = TotalSleep + $8
printf “%2s %2s %8s %8s %5d %2d:%02d:%02d\n”,substr($1,6,2),substr($1,9,2) ,substr($11,0,9),substr($1,12,8),$8 ,hh1,mm1,ss1}}
END {
hh1=int(TotalSleep/3600)
mm1=mm1=int(($8%3600)/60)
ss1=($8%3600)%60
printf “Total Sleep Time: %2d:%02d:%02d\n” ,hh1,mm1,ss1
start= a “/” b “/” y " " c
“date +%Y”|getline year
end= substr($1,6,2) “/” substr($1,9,2) “/” year " " substr($1,12,8)
$1=start
cmd=“date --date=”"$1"" +%s"
cmd|getline st
printf “Start %s\n” ,st
$1=end
cmd=“date --date=”"$1"" +%s"
cmd|getline end
printf “End %s\n” ,end
Total=end-st
hh1=int(Total/3600)
mm1=mm1=int((Total%3600)/60)
ss1=(Total%3600)%60
Percent=int((TotalSleep/Total)*100)
WakeTime= int(100 - Percent)
printf “Total Up Time: %2d:%02d:%02d %2d%% Sleep %2d%% Wake\n” ,hh1,mm1,ss1,Percent,WakeTime
}’

RAC

2 Likes

How does the script affect the unit’s sleep behavior? What are the advantages?

The script doesn’t affect the units sleep behavior, what it does do is list/display when and for how long the unit has gone to sleep.

The reason for the script was explained in the following thread.

https://community.wd.com/t/sleep-time/95608

I thought I had this working before. Now I get error messages. (I know I can fix them).

When I look at my /var/log/user.log* s, The first line is

May 23 20:52:03 WDMyCloud logger: exit standby after 8 (since 2017-05-23 20:51:55.607313002 -0500)

Can you add as a code comment a line that shows your expected input please?

(Note you can use the </> from the tool bar at the top left of the edit window on highlighted code/data/err msgs to keep the proper formatting)

Thanks for sharing!

I have two files. one called sleeptime.sh and the other called sleep.awk. All you need to do is sh sleeptime.sh

sleeptime.sh
gunzip -c /var/log/user.log.2.gz > tmpuserlog
cat tmpuserlog /var/log/user.log.1 /var/log/user.log|/shares/Public/sleep.awk
rm tmpuserlog
sleep.awk
awk ’
NR==1 { a=substr($1,6,2); b=substr($1,9,2); c=substr($1,12,8); y=substr($1,1,4)}
{if ($7 ~ /after/){
hh1=int($8/3600)
mm1=int(($8%3600)/60)
ss1=($8%3600)%60
TotalSleep = TotalSleep + $8
printf “%2s %2s %8s %8s %5d %2d:%02d:%02d\n”,substr($1,6,2),substr($1,9,2) ,substr($11,0,9),substr($
1,12,8),$8 ,hh1,mm1,ss1}}
END {
hh1=int(TotalSleep/3600)
mm1=mm1=int(($8%3600)/60)
ss1=($8%3600)%60
printf “Total Sleep Time: %2d:%02d:%02d\n” ,hh1,mm1,ss1
start= a “/” b “/” y " " c
“date +%Y”|getline year
end= substr($1,6,2) “/” substr($1,9,2) “/” year " " substr($1,12,8)
$1=start
cmd=“date --date=”"$1"" +%s"
cmd|getline st
printf “Start %s\n” ,st
$1=end
cmd=“date --date=”"$1"" +%s"
cmd|getline end
printf “End %s\n” ,end
Total=end-st
hh1=int(Total/3600)
mm1=mm1=int((Total%3600)/60)
ss1=(Total%3600)%60
Percent=int((TotalSleep/Total)*100)
WakeTime= int(100 - Percent)
printf “Total Up Time: %2d:%02d:%02d %2d%% Sleep %2d%% Wake\n” ,hh1,mm1,ss1,Percent,WakeTime
}’

Make small modification on sleeptime.sh by rac8006
I modified how the footer printed

awk ’
NR==1 { a=substr($1,6,2); b=substr($1,9,2); c=substr($1,12,8); y=substr($1,1,4)}
{if ($7 ~ /after/){
hh1=int($8/3600)
mm1=int(($8%3600)/60)
ss1=($8%3600)%60
TotalSleep = TotalSleep + $8
printf “%2s %2s %8s %8s %5d %2d:%02d:%02d\n”,substr($1,6,2),substr($1,9,2) ,substr($11,0,9),substr($1,12,8),$8 ,hh1,mm1,ss1}}
END {
hh1=int(TotalSleep/3600)
mm1=mm1=int(($8%3600)/60)
ss1=($8%3600)%60
start= a “/” b “/” y " " c
“date +%Y”|getline year
end= substr($1,6,2) “/” substr($1,9,2) “/” year " " substr($1,12,8)
print “Start : " start
$1=start
cmd=“date --date=\x27”$1”\x27 +%s"
#print cmd
cmd|getline st
#printf “Start %s\n” ,st
print “End : " end
$1=end
cmd=“date --date=\x27”$1”\x27 +%s"
#print cmd
cmd|getline end
#printf “End %s\n” ,end

Total=end-st
Percent=int((TotalSleep/Total)*100)
printf “Total Sleep Time: %2d:%02d:%02d %2d%%\n” ,hh1,mm1,ss1,Percent

hh1=int(Total/3600)
mm1=mm1=int((Total%3600)/60)
ss1=(Total%3600)%60
WakeTime= int(100 - Percent)
printf “Total WakeUp Time: %2d:%02d:%02d %2d%%\n” ,hh1,mm1,ss1,WakeTime
#printf “%2d%% Sleep %2d%% Wake\n” ,Percent,WakeTime
}’

This is the new footer

Start: 05/15/2018 05:34:39
End : 05/17/2018 10:33:49
Total Sleep Time: 14:05:56 27%
Total WkeUp Time: 52:59:10 73%

FYI, you may want to edit your post to put your new script into code blocks. Otherwise certain elements may be stripped out of your current post rendering problems for others if they copy and paste your new script into their sleep.awk file.

Code block example:
Example code goes here

Only recently have I found this script to examine WDMC sleep behaviour.
Thank you very much for sharing it, @rac8006

I don’t know much about awk-script, but I wonder:
Would not the lines (11-12) just after “END” all have to have the “TotalSleep” instead of “$8”?
Otherwise the minutes and the seconds would be taken from the last entry instead of the Totalsleep duration, no?

Also I wonder why it is “mm1=mm1=int(…” in line 11, and not “mm1=int(…”
I am a awk-nook, so please forgive my ignorance if this is a dumb question.

I see, @robbycandra has already taken care of these things, and also implemented a workaround for the double "-signs in the lines with the date-commands. Thanks for that too!

I ran the code to get the output. Then I made the changes and reran the code. The total output was identical. Don’t understand what is wrong with the - signs?

Please post rrobbycandra’s version.

Sorry, I meant to say the double " signs

And with your code, the TotalSleep output has the minutes and the seconds not from the actual sum, but is equal to the minutes and seconds from the last sleep period.

The version with modifications by robbycandra is just three posts above:

PS: This is not intended to be offensive, but only helpful

No offense taken. I wrote the code 3 years ago while I was debugging the sleep problem. It worked for me. I posted it so other people could use it. If they want to make changes so be it.

Strangely when I run that code (on a first gen v4.x single bay My Cloud), while it shows a percentage for the total sleep time, it doesn’t show the actual time value.

sleeptimeerror