#!/usr/bin/perl -T ############################################################################### # bwreport.pl - Daily bandwidth report script for CPanel boxes # Version 0.3 beta - 20/SEP/2003 # (c) 2002, 2003 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 (for example /root) this way: # cd /root (or cd /home/username) # wget http://html.conclase.net/cp/scripts/bwreport.perl.txt # 2) chown youruser.youruser bwreport.perl.txt # 3) chmod 0700 bwreport.perl.txt # 4) mv bwreport.perl.txt bwreport.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 bwreport.conf in the same # directory as the script, or inside /etc/: # 1) touch bwreport.conf # 2) chown youruser.youruser bwreport.conf # 3) chmod 0600 bwreport.conf # 4) Now edit bwreport.conf and add the following lines: # RECIPIENT=your email address # TOTALBW=your total available bandwidth, in GB/month # 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 ############################################################################### # 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. ############################################################################### # The script requires the MIME::Lite module. Remember that it can be easilly # installed from WHM. ############################################################################### # Please consider making a donation today. Visit my amazon.com wishlist at: # http://html.conclase.net/link/wishlist # Thank you :) ############################################################################### use strict; $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; use MIME::Lite; BEGIN { push (@INC,"/usr/local/cpanel"); }; use Cpanel::Accounting; ############################################################################## my ($address, $totalbw, $sendhtml, $sendtext) = (undef, undef, 1, 1); my $username = (getpwuid($<))[0]; my $homedir = (getpwuid($<))[7]; my $conffile = undef; foreach ("./bwreport.conf", "$homedir/bwreport.conf", "/etc/bwreport.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*TOTALBW\s*=\s*(\S+)/i) { $totalbw = $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"); $address = ; close(EMAIL); $address =~ s/\s//g; if ($address eq "") { die "Recipient address not found. Check you .contactemail file."; } } else { die "Recipient address not specified"; } } if (!$sendhtml) { $sendtext = 1; } my $accesshash = ""; open(HASH, "<$homedir/.accesshash") or die("Cannot open access hash"); while () { $accesshash .= $_; } close(HASH); $accesshash =~ s/\s//g; ############################################################################## my ($whm) = Cpanel::Accounting->new; $whm->{host} = "localhost"; $whm->{user} = $username; $whm->{usessl} = 1; $whm->{accesshash} = $accesshash; my ($user, %accts, @acctinfo, @page, $host, $subject, $slen, $msg); my ($body1, $output1); my ($body2, $output2); my ($current, $limit, $percent, @overbw, $note); my ($current_total, $limit_total, $percent_total, $current_real) = (0, 0, "N/A", 0); %accts = $whm->listaccts(); if ($whm->{error} ne "") { print "There was an error while processing your request: Cpanel::Accounting returned [".$whm->{error}."]\n"; exit; } if ($whm->{host} eq "localhost") { $host = `hostname`; } else { $host = $whm->{host}; } $host =~ s/\s*$//g; $subject = "Daily bandwidth report from $host\n"; $slen = length($subject)-1; $body1 = $subject; $body1 .= "-" x $slen . "\n\n"; $body1 .= "Number of accounts: " . scalar(keys %accts) . "\n\n"; $output1 = ""; $body2 = "$subject\n"; $body2 .= "\n"; $body2 .= "

$subject

\n"; $body2 .= "

Number of accounts: ".scalar(keys %accts)."

\n"; $output2 = ""; foreach $user (sort keys %accts) { @acctinfo = @{$accts{$user}}; (@page) = $whm->whmreq("/scripts2/limitbw?user=".$user); if ($#page == -1) { (@page) = $whm->whmreq("/scripts/limitbw?user=".$user); } $current = 0; $limit = 0; foreach my $line (reverse @page) { if ($line =~ /^\s*]*>(\d+(?:\.\d+)?)[^<]*<\/td>\s*$/) { $current = $1; $current_real += $current; } elsif ($line =~ /^\s*]*>= 100) { $note = " - ".$percent."\% BANDWIDTH EXCEEDED!!"; push @overbw, $user; } elsif ($percent >= 80) { $note = " - ".$percent."\% WARNING, 80% EXCEEDED!"; } else { $note = " - ".$percent."\%"; } } $output1 .= "$user - $acctinfo[0] - $current out of $limit MB$note\n"; if ($percent >= 100) { $output2 .= ""; } elsif ($percent>80) { $output2 .= ""; } else { $output2 .= ""; } $output2 .= "$user$acctinfo[0]$current MB$limit MB${percent}\%\n"; sleep(1); } if ($limit_total == 0) { if (defined($totalbw) && $totalbw > 0) { my $percent = sprintf("%.2f", $current_real/($totalbw*1024)*100); $body1 .= "Data transferred (total): $current_real out of ".($totalbw*1024)." MB ($percent\%)\n\n"; $body2 .= "

Data transferred (total): $current_real out of ".($totalbw*1024)." MB ($percent\%)

\n"; } else { $body1 .= "Data transferred (total): $current_real\n\n"; $body2 .= "

Data transferred (total): $current_real

\n"; } } else { $percent_total = sprintf("%.2f", $current_total/$limit_total*100); if ($current_real != $current_total) { if (defined($totalbw) && $totalbw > 0) { my $percent = sprintf("%.2f", $current_real/($totalbw*1024)*100); $body1 .= "Data transferred (total): $current_real out of ".($totalbw*1024)." MB ($percent\%)\n\n"; $body2 .= "

Data transferred (total): $current_real out of ".($totalbw*1024)." MB ($percent\%)

\n"; } else { $body1 .= "Data transferred (total): $current_real\n\n"; $body2 .= "

Data transferred (total): $current_real

\n"; } } $body1 .= "Data transferred (limited accounts): $current_total out of $limit_total MB ($percent_total\%)\n\n"; $body1 .= "-" x $slen. "\n\n"; $body2 .= "

Data transferred (limited accounts): $current_total out of $limit_total MB ($percent_total\%)

\n"; } if (@overbw > 0) { $body1 .= "Attention, ".@overbw." user".((@overbw==1)?"":"s")." over bandwidth!\n\n"; $body2 .= "
\n

Attention, ".@overbw." user".((@overbw==1)?"":"s")." over bandwidth!

\n"; $body2 .= "\n"; $body2 .= "\n"; foreach my $user (@overbw) { my $user_owner = get_owner($user); my $user_email = get_email($user); my $owner_email = get_email($user_owner); $body1 .= "$user, owned by $user_owner (owner email: $owner_email - user email: $user_email)\n"; $body2 .= "\n"; sleep(1); } $body1 .= "\n" . "-" x $slen. "\n\n"; $body2 .= "
UsernameOwnerOwner's emailUser's email
$user$user_owner$owner_email$user_email
\n
\n"; } $body1 .= $output1; $body2 .= "\n"; $body2 .= "\n"; $body2 .= $output2; $body2 .= ""; $msg = MIME::Lite->new( "From" => $address, "To" => $address, "Subject" => $subject, "Type" => "multipart/mixed" ); if ($sendtext == 1) { $msg->attach( "Type" => 'TEXT', "Data" => $body1 ); } if ($sendhtml == 1) { $msg->attach( "Type" => 'text/html', "Data" => $body2 ); } $msg->send; exit; sub get_owner { my $user = shift; my $owner = "N/A"; my (@page) = $whm->whmreq("/scripts/changeowner?user=${user}"); sleep(1); foreach (reverse @page) { if (/
UserDomainBandwith usedBandwidth limitPercentage used