Operating System - HP-UX
1834216 Members
2272 Online
110066 Solutions
New Discussion

Re: Disk Usage/Auditing and e-mail

 
randy lee
Advisor

Disk Usage/Auditing and e-mail

I am attempting to write a little script to get the disk usage from every user's home directory and send them a report. Currently the script is sound as far as getting the proper data. I haven't been able to figure out how to manipulate any files/commands to extract the username according to the specific users directory. Any suggestions would be great. Here is what I have for a script, where "users.dir" is a file of user directories in /home.

for i in `cat users.dir`
do
cd /home
du -k $i | mailx -s "Weekly Disk Usage for servername" [e-mail address]
done
8 REPLIES 8
Patrick Wallek
Honored Contributor

Re: Disk Usage/Auditing and e-mail

You could try something similar to this to get the owner of the home directory:

owner=`ll -d /home/$i | awk ' {print $3} '`

If you need the real name of the person, if it is in your passwd file you could do this then:

owner2=`grep $owner /etc/passwd | awk -F: ' {print $5}'`

You could probably combine the two statements into one if you like.

Hope this helps.
randy lee
Advisor

Re: Disk Usage/Auditing and e-mail

Yes, I want to extract the real user's name, so that I can format it to our e-mail format first_last@ham.honda.com.
Patrick Wallek
Honored Contributor

Re: Disk Usage/Auditing and e-mail

OK, Try this statement.

owner_email=`grep $owner /etc/passwd | awk -F: '{print $5}' | awk '{print $1"_"$2"@ham.honda.com"}'`

This statement is taking the value of the $owner variable that was set in the script in my last reply and this takes the place of owner2. This is also assuming that the users real name in the /etc/passwd file (5th field) is in the form of 'first_name last_name' with no punctuation in there. If the first name and last name are reversed just do a $2"_"$1 in the above statement.

I believe that this will give you what you want. It may not be as elegant as some of the other scripting gurus out there can do, but it should work for you. It may be possible to combine the two statements, I haven't played with that. I may do that while I'm at the office tomorrow. I'll post results here if I succeed.
Ralph Grothe
Honored Contributor

Re: Disk Usage/Auditing and e-mail

just a slight "improvement" to Patrick's suggestion.
Why bothering grep while awk can do the job alone?

awk -v pwnam=$owner -F: '$1==pwnam {print $5}' /etc/passwd

If you want to be kind of portable don't rely on awk's -v flag though.
Not all awk versions support this flag.
Instead you would rather pass it the shell's variable owner as extra argument.
But with HP's awk it should work.

If you're keen on using Perl you could do something like this:

perl -e 'while (($user,$gcos)=(getpwent())[0,5]) {print "$gcosn" if ($user eq $ARGV[0])}' $owner

For details see POD
perldoc -f getpwent

Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Disk Usage/Auditing and e-mail

Aargh...
I always forget to extra quote backslashes in this forum!

Of course, in the Perl line it has to read

... print "$gcos\n" ...
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Disk Usage/Auditing and e-mail

Sorry, looping through each passwd entry isn't really perlish.
This seems more appropriate:

perl -e 'print "@{[(getpwnam($ARGV[0]))[5]]}\n"' $owner

Madness, thy name is system administration
Rainer_1
Honored Contributor

Re: Disk Usage/Auditing and e-mail

within your script $i already points to the user name, so you can do
mail_user=$(pwget -n $i |awk -F: '{print $5}')@ham.honda.com
Patrick Wallek
Honored Contributor

Re: Disk Usage/Auditing and e-mail

Randy,

Here's a complete script that should work for you. This includes some of Ralph's suggestions, so be sure to assign points to him too.

Rainer's suggestion will only work if the home directory name is the same as the user name, which is not always the case.

Here is the complete script:

for i in `cat users.dir`

do

cd /home

owner=`ll -d /home/$i | awk '{print $3}'`

email=`awk -v pwnam=$owner -F: '$1==pwnam {print $5}' /etc/passwd | awk '{print $1"_"$2"@ham.honda.com"}'`

du -k $i | mailx -s "Weekly Disk Usage for `uname -n`" $email

done




Hope this is what you need.