Novice question 
Author Message
 Novice question

Hi. I'm new to awk and have a simple question. I want to write a simple
script that does the following:

- It lists the contents of the /var/mail folder containing all users'
mailboxes.

- Then it sends a warning email to those users whose mailbox has exceeded a
certain size.

I suppose this is a common situation so someone must have developed such a
script. So any help would be appreciated.

Thanks



Mon, 06 Dec 2004 05:47:24 GMT  
 Novice question

% Hi. I'm new to awk and have a simple question. I want to write a simple
% script that does the following:
%
% - It lists the contents of the /var/mail folder containing all users'
% mailboxes.

You need an external tool to find this out (for instance, du).

% - Then it sends a warning email to those users whose mailbox has exceeded a
% certain size.

It's probably possible to connect to an SMTP server with some flavour of
gawk, but you might as well use the mail program to do this.

  du -ks /var/mail/*

gives a list of user directories with kb used in the first column and
user name at the end. On my system at this moment:

  0       /var/mail/patrick
  0       /var/mail/ptjm
  20      /var/mail/root

(this is indented by 2 -- there is no white space to the left of the output
of du on my system).

mail (on non-BSD systems the command that works this way is often called
mailx) takes a list of names to send the mail to as an argument, and
reads the mail text from the command-line. It has a -s option to set
the subject.

So, we want to loop over the output of du, parse out the amount of
disk space used and the user name, and bing off a {*filter*} note if the
disk space is too large.

We use the awk pipe operator (|) to send data two and from these
commands. We use the awk getline command or function or whatever
it is to read from a pipe, and print to write through a pipe.

I've suggested using fmt to wrap lines of text, rather than doing it
by hand:

  BEGIN {
     # for convenence parsing out the user name
     # -- it's important that the numbers output from du are flush
     #    left, since otherwise the numbers would be $2 rather than $1
     FS = "[ /]+"

     # allow 5000 kb of mail
     toomuch = 5000

     # users who can have as much mail as they want
     privusers["root"]; privusers["ptjm"]

     ducmd = "du -ks /var/mail/*"
     mailbase = "fmt | mail -s 'You had too much mail, even before this one' "

     while ((ducmd | getline) > 0) {
        if ($NF in privusers) continue

        if ($1 > toomuch) {
            mailcmd = mailbase $NF
            print "Dear", $NF "\n\nYou had", $1 "kb of mail" | mailcmd
            print "in your inbox, even before you received" | mailcmd
            print "this one. Read it and delete it or move it" | mailcmd
            print "because it's getting on my nerves.\n" | mailcmd
            print "Yours Sincerely,\n" | mailcmd
            print "sysadmin" | mailcmd

            # this is important if you wnat the mail to be sent
            close(mailcmd)
        }

     }

     # this would be important if you wanted to do the whole thing again
     close(ducmd)
  }
--

Patrick TJ McPhee
East York  Canada



Mon, 06 Dec 2004 10:35:12 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Super novice question..

2. Novice Question - Array Elements

3. Novice question - child window in VW5i.2

4. Yet another novice question

5. novice question: shallow and deep copies

6. Novice questions: Smalltalk 3.0 for DOS

7. novice question: subtyping String class?

8. Novices Questions

9. Loop for Items in .S (Novice question)

10. Novice Questions

11. Haskell novice question - deriving from Show

12. Novice question

 

 
Powered by phpBB® Forum Software