Ok Guys,
I had already started this when Urivan posted his script. That wasn't the
route that I wanted to go, so I wrote this shell script. The script should
be installed into each Virtual Server. It should also be included in the
skel, so that will be available on new Virtual Servers. The script can
either be run manually or run from a cron job daily or a few times a day.
When the script runs, it checks the quotas for the Virtual Server and each
user.
There are three warning levels, EMERGENCY, CRITICAL, and WARNING. The
WARNING and CRITICAL levels are soft limits that default to 80% and 90%
respectively. The EMERGENCY level is set when a server or user reach their
limit.
When checking the Virtual Server quotas, the Virtual Server and Host
administrators will receive messages. If a server reaches the EMERGENCY
level, the Virtual Server Administrator may not be able to check mail, so
the Host Administrator is notified.
When checking the User quotas, the Virtual Server Administrator, and the
User will receive the messages.
This is pretty bleeding edge. I have not even set up my cron job yet.
Manually, I have tested this for the WARNING and CRITICAL levels for user,
but I have not been able to test it for checking the Virtual Server quotas
because I couldn't figure out how to change the quota on a Virtual Server.
I changed the vsd.conf file, but the change was not reflected. However, I
have run the script using the -x flag and it seems to be working fine.
Let me know if you have problems.
Take care,
Paul
#!/bin/bash
####################################################################
# FreeVSD Quota Notification Script #
# #
# Written by Paul Marshall <[EMAIL PROTECTED]> #
# From Protelligence http://www.protelligence.com #
# #
# This file is subject to the terms and conditions of the GNU #
# General Public License. #
# #
# Copyright (C) 2001 Protelligence. All rights reserved. #
####################################################################
# This script is designed to work with the FreeVSD quota system.
# It should be run from cron on a daily or hourly basis.
# The script checks the quota for both the Virtual Server and
# individual users.
# The VS Check will check the quota for the Virtual Server and email the
# administrator when the quota reaches two different notification levels.
# If a Virtual server exceeds the quota, the script then mails the
# host administrator.
# The User Check will check the quota for each user that has a quota.
# When the user's quota reaches or exceeds one of two notification levels,
# both the user and the Virtual Server administrator will receive an
# email. If the user exceeds their quota, the Virtual Server
# administrator receives an email.
# Set some variables
PATH=/bin:/sbin:/usr/bin:/usr/sbin
# Path to quotastats
QUOTASTATS="/usr/sbin/quotastats"
# Path to the mail program
MAIL_PROG="/bin/mail"
# The Warning Level
# This is the warning level represented by a percentage of the total
# quota. This is when the first notice is sent. (default is 80%)
warn_level=0.80 # This a decimal percentage value
# The Critical Level
# This is the critical level represented by a percentage of the total
# quota. This is when the second and last warning is sent. (default is
# 90%)
crit_level=0.90 # This is a decimal percentage value
# The host administrator email address
host_admin="[EMAIL PROTECTED]"
#The Virtual Server
vs=`hostname` # Automatically get hostname
# The Virtual Server admin user
vs_admin="admin" # Use the username or email address
# OK, now we can start
# Setup the mailing functions
# Sends an email to the Virtual Server Administrator and the Host
# Administrator when a Virtual Server reaches the quota limit.
function mail_vs_over
{
sbj="$level: ${vs} has reached the disk quota limit"
msg="\\nThe Virtual Server ${vs} has reached the disk quota limit. \
The max quota for this server is ${vs_max}K and the server is \
currently using ${vs_used}K.\\n\\n \
DATA IS CURRENTLY BEING LOST!\\n\\n \
Either clear some space on this server or increase the quota.\\n"
echo -e ${msg} | ${MAIL_PROG} ${host_admin} -s "${sbj}" -c
${vs_admin};
}
# Sends an email to the Virtual Server Adminitrator and the Host Administrator
# when the server reaches one of the warning levels.
function mail_vs_warn
{
sbj="${level}: ${vs} is over the quota limit"
msg="\\nThe Virtual Server ${vs} is approaching the disk quota \
limit. The server has reached the ${level} level. The server is \
currently using ${vs_used}K of the ${vs_max}K quota. \\n\\n \
You can try the following solutions to correct the situation:\\n\\n \
1. Consider deleting some old files or purging some log files.\\n\\n \
2. See if you have users who are leaving excessive amounts of \
mail on the server. If so, set a quota limit for that user or \
ask them to purge the mail by removing the Leave Mail on Server \
option in their mail program.\\n\\n \
3. Contact your host administrator to have the quota on your \
server increased. (There may be additional costs.)"
echo -e ${msg} | ${MAIL_PROG} ${vs_admin} -s "${sbj}" -c
${host_admin};
}
# Sends an email to the User and the Virtual Server Administrator when a
# user reaches their quota limit.
function mail_user_over
{
sbj="$level: ${user} has reached the disk quota limit"
msg="\\n${user} has reached their disk quota limit. \
The max quota for this user is ${user_quota}K and the server is \
currently using ${user_used}K.\\n\\n \
DATA IS CURRENTLY BEING LOST!\\n\\n \
Either clear some space on this server or increase the quota.\\n"
echo -e ${msg} | ${MAIL_PROG} ${vs_admin} -s "${sbj}" -c ${user};
}
# Sends and email to the User and the Virtual Server Administrator when
# the user has reached on of the warning levels.
function mail_user_warn
{
sbj="${level}: ${user} is approaching the quota limit"
msg="\\nYour user, ${user}, is approaching the disk quota \
limit. The user has reached the ${level} level. The user is \
currently using ${user_used}K of the ${user_quota}K quota. \\n\\n \
As a user, you may not have much control over this, however, there \
are a few things you can try. You can try the following solutions to \
correct the situation:\\n\\n \
1. If you have access to your files on the server, consider deleting \
some old files or purging some log files.\\n\\n \
2. Check your mail program settings. If you have the Leave Mail on
Server \
option checked, uncheck it and try checking your mail. This should
download \
the mail to your computer and delete it from the server. \\n\\n \
3. Contact your host administrator to have the quota for your user \
}
vs_max=`${QUOTASTATS} | grep "Max\/available" | awk -F\| '{print ($3+0)}'`
vs_crit=`echo $vs_alloc*$crit_level | bc -l | awk -F. '{print ($1)}'`
vs_warn=`echo $vs_alloc*$warn_level | bc -l | awk -F. '{print ($1)}'`
if [ $vs_used -ge $vs_max ]; then
level="EMERGENCY"
mail_vs_over $vs $vs_max $vs_used $level;
break;
fi
if [ $vs_used -ge $vs_crit -a $vs_used -lt $vs_max ]; then
level="CRITICAL"
mail_vs_warn $vs $vs_max $vs_used $vs_crit $level;
break
fi
if [ $vs_used -ge $vs_warn -a $vs_used -lt $vs_crit ]; then
level="WARNING"
mail_vs_warn $vs $vs_max $vs_used $vs_warn $level;
break
fi
# Get an array of usernames who have quotas
for user in $quota_users; do
user_quota=`${QUOTASTATS} ${user} | grep "Limit" | awk '{print
$3}' | awk '{ print ($1+0)}'
`
user_used=`${QUOTASTATS} ${user} | grep "Current use" | awk
'{print $4}' | awk '{ print ($1
+0)}'`
user_crit=`echo $user_quota*$crit_level | bc -l | awk -F. '{print
($1)}'`
user_warn=`echo $user_quota*$warn_level | bc -l | awk -F. '{print
($1)}'`
if [ $user_used -ge $user_quota ]; then
level="EMERGENCY"
mail_user_over $user $user_quota $user_used $level;
continue
fi
if [ $user_used -ge $user_crit -a $user_used -lt $user_quota ]; then
level="CRITICAL"
mail_user_warn $user $user_quota $user_used $user_crit $level;
continue
fi
if [ $user_used -ge $user_warn ]; then
level="WARNING"
mail_user_warn $user $user_quota $user_used $user_warn $level;
continue
fi
done
exit 0
End of script
Enjoy