#!/usr/bin/perl ############################################################################### # quotareport.pl - Daily quota report script for cPanel boxes # 15/DEC/2007 - Version 0.1.4 beta # (c) 2003 - 2007 Juan R. Pozo # http://html.conclase.net/cp/scripts/ # mailto:jrpozo@conclase.net ############################################################################### # This program is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 2 of the License, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ############################################################################### # INSTALLATION # 1) Copy the script somewhere in your server, this way: # cd /root (or cd /home/username) # wget http://html.conclase.net/cp/scripts/quotareport.perl.txt # 2) chown youruser.youruser quotareport.perl.txt # 3) chmod 0700 quotareport.perl.txt # 4) mv quotareport.perl.txt quotareport.pl # 5) Optionally, customize your settings, see below # 6) Add the script to your crontab ############################################################################### # CUSTOMIZING YOUR SETTINGS # To customize your settings, create a file named quotareport.conf in the same # directory as the script, or in your home directory, or inside /etc/: # 1) touch quotareport.conf # 2) chown youruser.youruser quotareport.conf # 3) chmod 0600 quotareport.conf # 4) Now edit quotareport.conf and add the following four lines: # RECIPIENT=your email address # TOTALMB=total allocated hard drive space, in MB # SENDTEXT=0 # SENDHTML=0 # Set SENDTEXT=0 for not to receive the report in plain text # Set SENDHTML=0 for not to receive the report in HTML # SENDTEXT=0 is ignored if SENDHTML=0 is set ############################################################################### # The script requires the MIME::Lite module. Remember that it can be easilly # installed from WHM. ############################################################################### # Note: If you get an error about your access hash not being found or specified # login in your WHM and click on Server Setup / Configure Remote Access Key # That will create your access hash. ############################################################################### # Changes: # 0.1.4. - 15/DEC/2007 # - Remove tainting for the script to work with cPanel 11 # Thanks to Chris Paige for letting me know about this # 0.1.3. - 01/MAR/2004 # - Allows for unlimited quotas # - Fixed headers (it said bandwidth instead of quota) # Thanks to Fokke Slottje for the bug fixes # 0.1.2. - 20/SEP/2003 # - Doesn't require user to include username or recipient info in config file # Config file is now completely optional # 0.1.1. - 15/APR/2003 # - Doesn't require user to include access hash information in the config file # 0.1. - 17/MAR/2003 # - First public release ############################################################################### # Please consider making a donation today. Visit my amazon.com wishlist at: # http://html.conclase.net/link/wishlist # Thank you :) ############################################################################### use strict; use MIME::Lite; BEGIN { push (@INC,"/usr/local/cpanel"); }; use Cpanel::Accounting; ############################################################################## my ($address, $totalmb, $sendhtml, $sendtext) = (undef, undef, 1, 1); my $username = (getpwuid($<))[0]; my $homedir = (getpwuid($<))[7]; my $conffile = undef; foreach ("./quotareport.conf", "$homedir/quotareport.conf", "/etc/quotareport.conf") { if (-r $_) { $conffile = $_; last; } } if (defined $conffile) { open (CONF, "<$conffile") or die "Cannot open configuration file $conffile"; while () { if (/^\s*RECIPIENT\s*=\s*(\S+)/i) { $address = $1; } elsif (/^\s*TOTALMB\s*=\s*(\S+)/i) { $totalmb = $1; } elsif (/^\s*SENDTEXT\s*=\s*0\s*$/i) { $sendtext = 0; } elsif (/^\s*SENDHTML\s*=\s*0\s*$/i) { $sendhtml = 0; } } } if (!defined($address) || $address eq "") { if (-r "$homedir/.contactemail") { open(EMAIL, "<$homedir/.contactemail") or die("Cannot open .contactemail file"); $address = ; close(EMAIL); $address =~ s/\s//g; if ($address eq "") { die("Cannot find contact address (check your .contactemail file"); } } else { die "Recipient address not specified"; } } my $accesshash = undef; if (-r "$homedir/.accesshash") { open (HASH, "$homedir/.accesshash") or die "Cannot open access hash file"; read (HASH, $accesshash, -s HASH); close (HASH); $accesshash =~ s/\s//g; } else { die "Cannot find access hash file\n"; } if (!$sendhtml) { $sendtext = 1; } ############################################################################## my ($whm) = Cpanel::Accounting->new; $whm->{host} = "localhost"; $whm->{user} = $username; $whm->{usessl} = 1; $whm->{accesshash} = $accesshash; my ($user, $domain, $inform, @page, $host, $subject, $slen, $msg); my ($body1, $output1); my ($body2, $output2); my ($current, $limit, $sumcurrent, $sumlimit) = (0, 0, 0, 0); if ($whm->{host} eq "localhost") { $host = `hostname`; } else { $host = $whm->{host}; } $host =~ s/\s*$//g; $subject = "Daily quota report from $host\n"; $slen = length($subject)-1; $body1 = $subject; $body1 .= "-" x $slen . "\n\n"; $output1 = ""; $body2 = "$subject\n"; $body2 .= "\n"; $body2 .= "

$subject

\n"; $output2 = ""; (@page) = $whm->whmreq("/scripts/quotalist"); $inform = 0; $domain = ""; $user = ""; foreach (@page) { s/\ //g; if ($inform) { if ($user eq "") { if (/(\S+) \[([^\]]+)\]/) { $domain = $1; $user = $2; } } else { if (/(\d+)M<\/td>/) { $current = $1; $sumcurrent += $current; } elsif (//) { $limit = $1; if ($limit =~ /^\d+$/) { $sumlimit += $limit; } } elsif (/<\/form>/) { $output1 .= userline_text($user, $domain, $current, $limit); $output2 .= userline_html($user, $domain, $current, $limit); $inform = 0; $user = ""; $domain = ""; } } } elsif (/
\n"; $body2 .= "UserDomainQuota usedQuota limitPercentage used\n"; $body2 .= $output2; $body2 .= "\n"; $body2 .= "

Space used: $sumcurrent MB

\n"; $body2 .= "

Global limit: $sumlimit MB

\n"; if (defined $totalmb) { $body2 .= "

Space allocated: $totalmb MB

\n"; } $body2 .= ""; $msg = MIME::Lite->new( "From" => $address, "To" => $address, "Subject" => $subject, "Type" => "multipart/mixed" ); if ($sendtext) { $msg->attach( "Type" => 'TEXT', "Data" => $body1 ); } if ($sendhtml) { $msg->attach( "Type" => 'text/html', "Data" => $body2 ); } $msg->send; exit; sub userline_text { my ($user, $domain, $current, $limit) = @_; if (($limit == 0) || ($limit eq "unlimited")) { return "$user - $domain - $current out of unlimited MB\n"; } else { my ($percent, $note); $percent = sprintf("%.2f", $current/$limit*100); if ($percent >= 100) { $note = " - ".$percent."\% WARNING, ACCOUNT MAXED OUT!!"; } elsif ($percent >= 80) { $note = " - ".$percent."\% WARNING, 80% EXCEEDED!"; } else { $note = " - ".$percent."\%"; } return "$user - $domain - $current out of $limit MB$note\n"; } } sub userline_html { my ($user, $domain, $current, $limit) = @_; if (($limit == 0) || ($limit eq "unlimited")) { return "$user$domain$current MBunlimitedN/A\n"; } else { my ($line, $percent, $note); $percent = sprintf("%.2f", $current/$limit*100); if ($percent >= 100) { $line = ""; } elsif ($percent > 80) { $line = ""; } else { $line = ""; } $line .= "$user$domain$current MB$limit MB${percent}\%\n"; return $line; } }