About Listing 2
In this script, we first define all the required variables on
lines 15 to 74, which includes strings, integers, floating point
numbers, and file/directory handles. Implement a while()
loop on lines 94 through 295 to calculate the current size of each
database directory. In this loop, use the readdir() function
for scanning through the directories, and then stat() for
getting the size of each file. The file sizes are added to size
on line 159 so that when the nested while() loop ends on
line 161, size holds the complete size for the currently scanned
directory (in bytes). However, it's not very nice to present
the results of the quota checking in bytes, so size is converted
to megabytes at line 173.
We must retrieve the maximum allowed database size from some source
in order to make use of this information. Fortunately, when we set
up the database, we also create a file in which we specify the maximum
size for the specific database in megabytes. The name of this file
is always the same as the database name or username. Lines 179 through
194 open the quota file for the currently scanned database and save
the information stored in the quota file in the variable quota.
Line 216 checks whether the quota was exceeded. If so, the code
inside the if() statements on line 216 through 264 are executed.
We also check whether there's a file called $DB_NAME_reported
(known as the reported file) in the $MYSQL_BASE_DIR/quota
directory. If not, the $DB_NAME_reported file will be created,
and an email will be sent to the admin (daniel@solin.org, in this
case) reporting that this particular database exceeded its quota.
On the other hand, if the file already exists, it means that the
problem has already been reported and should not be reported again.
If we skipped this feature, the result would be that an email would
be sent every time the quota was checked (in this case, every two
seconds).
Lines 273 through 294 provide another nice feature that makes
sure that when a reported file exists for a database but the quota
for it is no longer exceeded, the program assumes that the quota
problem has been resolved and subsequently removes the quota file.
It then sends an email to the administrator and reports that the
problem has been fixed. In reality, "fixing the quota problem"
simply means that database size has decreased, or the maximum size
has been increased in the database's corresponding quota file.
|