Operating System - HP-UX
1829928 Members
2770 Online
109998 Solutions
New Discussion

Getting and graphing user login totals

 
SOLVED
Go to solution
John Love_3
Regular Advisor

Getting and graphing user login totals

I want to graph the user login load of my server(s). I can use who -q and then grep for "users=" and it gives me the whole line.

What I really need is just the number value that comes after # users=

Anybody know how I can accomplish this and then set cron to get the value every 5 minutes?

I'll worry about how to graph it on a web page later, unless you all have some suggestions about this as well.

Thanks for your help.
8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: Getting and graphing user login totals

John,

How about this:

Create a script called get_num_users

#!/sbin/sh
/usr/bin/who -q | grep users | awk -F= '{ print $2 }' >> /dir/num_users

Then put the following entry in cron for root ('crontab -e'):

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /dirname/get_num_users

This should run the get_num_users script every 5 minutes, 24 hours a day, 7 days a week, and append the output to a file called get_num_users.
James R. Ferguson
Acclaimed Contributor

Re: Getting and graphing user login totals

Hi John:

# 0,5,10,15,20,25,30,35,40,45,50,55 * * * * who|wc -l >> /tmp/usercount

A crontab entry like this will write the usercount into the file /tmp/usercount every 5-minutes.

See man 'crontab' for more information.

...JRF...
John Love_3
Regular Advisor

Re: Getting and graphing user login totals

Patrick,
Excellent! I knew it had to be AWK or SED, but I'm a complete moron with scripting stuff.

Another wrinkle I forgot. Do you think it is possible to add the date and time in the file as well?

I'd like to have it read like this:
25,date (with time)

rather than have it append the date after (below) the user value using >>

That way I could import it into Excel or something and graph it that way.

Thanks again!
James R. Ferguson
Acclaimed Contributor
Solution

Re: Getting and graphing user login totals

Hi John:

# 0,5,10,15,20,25,30,35,40,45,50,55 * * * *
(date;echo "users =";who|wc -l)|xargs >> /tmp/usercount

will put a one-line value into the file every 5-minutes without the need for 'awk'.

...JRF...
Patrick Wallek
Honored Contributor

Re: Getting and graphing user login totals

John,

Here's a new script for you:

#!/sbin/sh
num_user=`/usr/bin/who -q | grep users | awk -F= '{ print $2 }'`
date1=`date '+%x %X'`
echo "$num_user,$date1" >> /tmp/pww/numuser

Eduardo Uribe_1
Occasional Advisor

Re: Getting and graphing user login totals

Just add some lines to Patrick's script...
and make an HTML table...
Graphing it would be a little bit more complicated...

#!/sbin/sh
num_user=`/usr/bin/who -q | grep users | awk -F= '{ print $2 }'`
date1=`date '+%x %X'`
echo "$num_user,$date1" >> /tmp/pww/numuser.html

## HTML Table builder
echo " User logging totals " >> /tmp/pww/numuser.html
echo "" >> /tmp/pww/numuser.html
echo "" >> /tmp/pww/numuser.html
echo "
" >> /tmp/pww/numuser.html
echo "$num_user" >> /tmp/pww/numuser.html
echo "
" >> /tmp/pww/numuser.html
echo "$date1" >> /tmp/pww/numuser.html
echo "
" >>$WEB_SERVER/html/numuser.htm

cp /tmp/pww/numuser.html
$WEB_SERVER/html

This way you would always have a "real time" table to Examine in numuser.html

Of course you can add fancy stuff to it...

Ed
Eduardo Uribe_1
Occasional Advisor

Re: Getting and graphing user login totals

Just add some lines to Patrick's script...
and make an HTML table...
Graphing it would be a little bit more complicated...

#!/sbin/sh
num_user=`/usr/bin/who -q | grep users | awk -F= '{ print $2 }'`
date1=`date '+%x %X'`
echo "$num_user,$date1" >> /tmp/pww/numuser.html

## HTML Table builder
echo " User logging totals " >> /tmp/pww/numuser.html
echo "" >> /tmp/pww/numuser.html
echo "" >> /tmp/pww/numuser.html
echo "
" >> /tmp/pww/numuser.html
echo "$num_user" >> /tmp/pww/numuser.html
echo "
" >> /tmp/pww/numuser.html
echo "$date1" >> /tmp/pww/numuser.html
echo "
" >>/tmp/pww/numuser.html

cp /tmp/pww/numuser.html
$WEB_SERVER/html

This way you would always have a "real time" table to Examine in numuser.html

Of course you can add fancy stuff to it...

Ed
David Child_1
Honored Contributor

Re: Getting and graphing user login totals

I just wanted to say that I agree with James. Why use the long, less efficient "awk" version of the script when "who|wc -l)|xargs >> /tmp/usercount" would get the same information.

You could still use the other script tips that were offered and you would have a more efficient script.

Hey, it's Friday... don't work so hard ;)

Cheers,
David