Automatically Checking the Limits

As we saw earlier, another method of checking quotas is to use repquota. Using the options below will generate a report for all users that have quotas allocated on all filesystems:

hydrogen# repquota –av
/dev/dsk/c0t0d0s7 (/export/home):
                      Block limits                    File limits
User           used   soft   hard   timeleft   used  soft   hard   timeleft
testuser  +- 358601 300000 500000  1.4 weeks      8    400
hydrogen#

This output can be analyzed on a regular basis and used by the system administrator to inform users if they are approaching their limits. The script below shows an example of how this can be done. It parses the output and sends mail to the administrator and the users concerned when they reach a predetermined limit:

hydrogen# cat checkQuotas
#!/bin/ksh
#
# Script to check the quotas of all "registered" users.
# It will send mail back to the users when they have used
# a predetermined amount of their soft limit.
#
# The output from repquota is formatted as shown below:
#
#/dev/dsk/c0t3d0s0 (/):
#      Block limits                      File limits
#User     used  soft  hard timeleft used soft hard timeleft
#test1 +- 14995 10000 15000 6.9     6    100  200
#test2 -- 0     10000 15000         0    100  200
#
#User settable variables
softLimitHighMark=85               # % value when we alert
adminAccount=root                  # who to send the mail
repquotaOutput=/tmp/rep.out$$
listOfUsers=/tmp/list_of_users$$
tmpOutput=/tmp/quotaMailTest.$$

# Set up the path—
PATH=/usr/ucb:/bin:/etc:/usr/sbin:./bin:/usr/etc:.
# generate the report...
repquota -va > ${repquotaOutput}
if [ $? -ne 0 ]; then
  echo "Error running repquota"
  exit 1
fi
# ...and create a list of users in the report.
awk '{ 
  if ($0 !~ /Block limits/ && 
      $1 != "User" && 
      $1 !~ /^/dev.*/dsk/) { 
    print $1 
  } 
}' ${repquotaOutput} > ${listOfUsers}
# Now start to parse the file.
# Generate a piece of mail for each user.
for person in `cat ${listOfUsers}`; do
  awk '{ 
    # throw away the following lines that are normally
    # produced as part of the report;
    #     Block limits File limits
    #     User used soft hard..
    if ($0 !~ /Block limits/ && $1 != "User") { 
      # at this point we are left with
      #
      # test5 +- 14995 10000 15000 EXPIRED 6 100 200
      # test6 -- 1  10000  15000 120 100 200 EXPIRED
      #
      # Start with the "Block" calculations.
      # First see if we have exceeded the soft limit.
      # We will have a "timeleft" value when
      # this has occurred
      if ($1 == thisUser) { 
        # Split the "+-" quota flag into its two values.
        # From here, ignore if its a + as they will already
        # know they are over-quota.
        if (substr ($2, 1, 1) == "-"){ 
          # below block soft limit
          blockUsed = $3; 
          blockSoft = $4; 
          if (blockUsed > 0 && blockSoft > 0) { 
            SoftLimit = (blockUsed / blockSoft * 100); 
            if (SoftLimit > '${softLimitHighMark}') { 
            printf ("%s is using %d%% of their block soft limit
                      [%d]
", 
                      $1, SoftLimit, blockSoft); 
            } 
          } 
        } 
        if (substr ($2, 2, 1) == "-"){ 
          # below file soft limit
          if (NF == 8){ 
            fileUsed = $6; 
            fileSoft = $7; 
          } 
          if (NF == 9){ 
            fileUsed = $7; 
            fileSoft = $8; 
          } 
          if (fileUsed > 0 && fileSoft > 0) { 
            SoftLimit = (fileUsed / fileSoft * 100); 
            if (SoftLimit > '${softLimitHighMark}') { 
            printf ("%s is using %d%% of their file soft limit
                      [%d]
", 
                      $1, SoftLimit, fileSoft); 
            } 
          } 
        } 
        if (substr ($2, 1, 1) == "+"){ 
          print "Warning: Block soft limit has already expired
                for",$1 
        } 
        if (substr ($2, 2, 1) == "+"){ 
          print "Warning: File soft limit has already expired
                for",$1 
        } 
      } 
    } 
  }' thisUser=${person} ${repquotaOutput} > ${tmpOutput}
  # if we have any output - mail it
  if [ -s ${tmpOutput} ]; then
    cat ${tmpOutput} | mail -s "Quota check - ${person}"
       ${person} ${adminAccount}
  fi
  rm ${tmpOutput}
done
# Clean up by removing the temp files
rm -f ${repquotaOutput} ${listOfUsers}
exit 0
hydrogen#

The Crontab Entry

We'll automate this by running it from cron once a day on every working day. The crontab entry to do this is shown below:

hydrogen# crontab –l
<lines removed for clarity>
#
# check user disk quotas
#
35 01 * * 1-5 /usr/local/utils/bin/checkQuotas
<lines removed for clarity>
hydrogen#

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset