Re: Script to calculate the total idle minutes (532 Views)
Reply
Frequent Advisor
Rita Li
Posts: 59
Registered: ‎09-17-2000
Message 1 of 16 (532 Views)
Accepted Solution

Script to calculate the total idle minutes

I have a small script that reads in an output with format as follows:

ivcheung pts/th .
ivcheung pts/tp .
ktam pts/tx 0:02
kwchow pts/tr 0:54
mmak pts/tk 1:16
ritali pts/tc 0:01
rwong pts/tt 0:08
tchan pts/tw 0:15

Then find out those telnet sessions that have been idled for more than 30 minutes

My script is like :

num=`grep pts $tempdir/whodo.out | wc | cut -f1 -d' '`
lines=1
while (( num > 0 ))
do
info=`head -n $lines $tempdir/whodo.out | tail -1`
userid=`echo $info | cut -f1 -d' '`
tty=`echo $info | cut -f2 -d' '`
idle=`echo $info | cut -f3 -d' '`
if [ $idle = "old" ]
then
idle_min=999999
else
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))
fi
if [ $idle_min -gt 30 ]
then
tty="/dev/"$tty
( invoke another script to kill the idled session )
fi
(( lines = lines + 1 ))
(( num = num - 1 ))
done

This script used to work fine until recently I moved from a 11.0 server to another 11.i server, if the idled time = "0:08" or "0:09", error message would occur

08 : The specified number is not valid for this command.

I know I can use

echo $(( 10#08 ))

to force a base 10 echo but don't know how to embed this into the old lines

Rita
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 2 of 16 (532 Views)

Re: Script to calculate the total idle minutes

There have been other posts on this issue of leading zeros. You may want to use:
typeset -LZ idle=$(echo $info | cut -f3 -d' ')

>num=`grep pts $tempdir/whodo.out | wc | cut -f1 -d' '`

Why do you need the cut? wc -l will give lines directly. Also, you can use grep -c, so you don't need wc.

Any reason you use num and a while loop and getting the lines one at a time with: info=`head -n $lines $tempdir/whodo.out | tail -1`
Why not use awk to process the whole file until EOF?

By using awk, you can make it lots simpler to read. And no stinkin' problems with garbage "octal" leading zeroes. And you don't need to use cut. You would have to use substr for the ":" extraction of the MM:SS time.

If you aren't familiar with awk, you could at least use:
grep pts $tempdir/whodo.out |
while read userid tty idle; do
...
done
Frequent Advisor
Rita Li
Posts: 59
Registered: ‎09-17-2000
Message 3 of 16 (532 Views)

Re: Script to calculate the total idle minutes

My problem is: I am not familar with awk
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 4 of 16 (532 Views)

Re: Script to calculate the total idle minutes

Rita,
you could try:
idle_hr=`echo ${idle} |cut -f1 -d':'`
idle_min=`echo ${idle} |cut -f2 -d':'`
idle_total=`echo "$idle_hr * 60 + $idle_min"|bc`

instead of your:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 5 of 16 (532 Views)

Re: Script to calculate the total idle minutes

You need to use typeset of the one with leading zeroes:
typeset -LZ idle_min=$(echo $info | cut -f3 -d' ')

>My problem is: I am not familar with awk

Then use my "grep | while read userid tty idle; do" suggestion.
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 6 of 16 (532 Views)

Re: Script to calculate the total idle minutes

>typeset -LZ idle_min=$(echo $info | cut -f3 -d' ')

Oops that's not your initial value. Just add this before your code:
typeset -LZ idle_min
Honored Contributor
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 7 of 16 (532 Views)

Re: Script to calculate the total idle minutes

Hi Rita,


instead of this line:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':') ))

try this:
(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo ${idle#[0-9]*:0}|cut -f2 -d':'

regards,
John K.
it would be nice if you always got a second chance
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 8 of 16 (532 Views)

Re: Script to calculate the total idle minutes

According to sh-posix man pages, forcing the base to be 10 is not necessary.

In fact, I tested your script in my 11.11 box and it runs fine.

Which shell is using in 11.11 ?
Have you tried several shells (ksh, sh-posix) ?
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 9 of 16 (532 Views)

Re: Script to calculate the total idle minutes

>Oscar: According to sh-posix man pages, forcing the base to be 10 is not necessary.

Perhaps UNIX95 was set?

It is hard to argue when Rita had the error message:
08 : The specified number is not valid for this command.
Of course it would be nice to know which command. I have no problems duplicating it on 11.23:
./itrc_leadzero.sh[26]: 08 : The specified number is not valid for this command.
26 (( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':') ))

Which means you need my: typeset -LZ idle_min
With Peter's extra temps:
idle_hr=`echo ${idle} |cut -f1 -d':'`
idle_min=`echo ${idle} |cut -f2 -d':'`
idle_total=(( idle_hr * 60 + idle_min ))

Of course bc(1) probably doesn't have this scummy C octal rule.

Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 10 of 16 (532 Views)

Re: Script to calculate the total idle minutes

Hi Rita
Hi Dennis

I agree Denis when he wrote that the problem might be UNIX95.

According to sh-posix man pages:

If UNIX95 is defined, shell arithmetic evaluators let command,
$((...)) and ((...)) recognizes interger constants beginning with 0
and 0x (or 0X) as octal and hexadecimal numbers.
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 11 of 16 (532 Views)

Re: Script to calculate the total idle minutes

Try

(( idle_min = 10#$(echo $idle|cut -f1 -d':') * 60 + 10#$(echo $idle|cut -f2 -d':') ))
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 12 of 16 (532 Views)

Re: Script to calculate the total idle minutes

Dear Rita,

Please consider the following free advice, worth every penny.

1) This particular wheel has been invented many times over. Please look around for existing commercial or freeware solutions.
It looks simple enough now, but it is probably too simple. You'll soon need exclusion lists and time ranges and stuff like that.

2) Every Unix sysadm MUST try to aquire some awk and perl skills. Not saying you should start doing everything with those (allthough you could :-), but for your own career you MUST get going on those powerful tools.

3) Idle killers suck... IMHO. They suck computer resources, they suck positive energy out of users, they suck at providing security, they suck admin time. They do exactly waht their name implies, but not what you want: They kill idleness and replace it with busyness. Sounds like a bad virus to me!

4) Consider replacing the "ge" in you script with a simple ">". The leading zero minute field compares nicely as a string instead of numbers.

5) Just to trigger your cusiosity consider this perl script reading the smaple input your gave:

#perl -lne 'if (/(\d+):(\d\d)$/) { ($u,$t,$i)=split; print qq($u on $t idle for $i) if ($1>0 or $2>30)}' tmp.txt
kwchow on pts/tr idle for 0:54
mmak on pts/tk idle for 1:16


#perl -lne -e = cmmand follows, -l=print newlines, -n=loop throug input Not printing.

if (/(\d+):(\d\d)$/){ = Look for decimals, a colong and two decimals at the end of the line.
If found remember in $1 and $2 and execute block.

($u,$t,$i)=split; # AAyup that's what it does.
print qq($u on $t idle for $i) if # conditional statement
($1>0 or $2>30)} # The hour not 0, or minutes greate than 30


Cheers,
Hein.


Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 13 of 16 (532 Views)

Re: Script to calculate the total idle minutes

As bc(1) doesn't have this scummy octal rule; replace...

(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':'
) ))

with...

(( idle_min = $(echo $idle|cut -f1 -d':') * 60 + $(echo $idle|cut -f2 -d':' | bc
) ))
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 14 of 16 (532 Views)

Re: Script to calculate the total idle minutes

Sandman,
please see my bc solution posted at 08:58:06
;-)
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 15 of 16 (532 Views)

Re: Script to calculate the total idle minutes

>Oscar: According to sh-posix man pages: If UNIX95 is defined,

On 11.23, this UNIX95 has been removed. In all cases it:
... will be processed according to ISOC standard
Frequent Advisor
Rita Li
Posts: 59
Registered: ‎09-17-2000
Message 16 of 16 (532 Views)

Re: Script to calculate the total idle minutes

Thank you all for the great support

I found more than 1 solution, but I choose the most understanable one, the "|bc", also rewrote this script which is written few years ago in a more structured format

Correct, I need to acquire to learn some awk knowledge & someone else in my team is learning Perl, so may rewrite this script in Perl later

Thanks,
Rita
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.