#!/usr/bin/perl # You may need to change this path to /usr/local/bin/perl $mailprog = "/usr/sbin/sendmail"; $date_command = "/bin/date"; #************************************************************** # # Script to rollover bandwidth usage: PWSrollbandwidth V2.01 # # Written by: # Premier Website Solutions - http://www.premierwebsitesolutions.com # Created - February 21, 2004 (V1.0) # Modified - February 27, 2004 (V2.0) # - added limiting option # Last Modified - February 28, 2004 (V2.01) # - changed $email to single quotes to not require escaping the @ symbol # # Files being accessed or modified for this script: # /var/cpanel/users/[username] - read and altered # /var/cpanel/bandwidth/[username] - read only # # # All you need to do is set a few variables below, upload this script to your # server, and set a cronjob to run it AFTER the monthly bandwidth total has # been read. It is recommended to set the cron job to run in the early morning # hours of day 2 of each month, like this: 40 3 2 * * /path/to/script # This would run the script at 3:40am on the 2nd of each month. # The script can also be run manually from shell and will not process accounts # that have already been processed for the current month. # # # Registered users of this script will be notified of any future updates. # If you registered this copy with me, put your email here for future reference. # This copy is registered to: # #************************************************************** # For safety, it is highly recommended that an absolute maximum is set # for the monthly bandwidth. There are 3 options to set a limit. Choose the one # you prefer, set the variable, and uncomment that variable by removing the # at # the start of the line. Make sure only 1 of the variables is uncommented. # Option 1 - multiplier # Set the $absmax1 variable to a multiple of the regular bandwidth allowance. # For example, 5 would mean the accumulated maximum can not go over 5 times the # normal monthly limit. # Option 2 - fixed maximum # Set the $absmax2 variable to a fixed maximum in GB. Make sure it's higher than # the default maximum. # If you do not want any absolute maximum, just have both variables commented out. # (very highly not recommended though) #$absmax1 = 5; # multiple of normal monthly limit $absmax2 = 10; # fixed maximum in GB # If you want to be emailed when this script runs, put your email address here. # If you use spamassassin, you should include a name, like this: # $email = 'me '; $email = ''; # Change this subject line if you want. $email_subject = "Bandwidth rollover has been processed"; # You can manually alter the message directly in the subroutine at the end of # this script if you feel safe doing so. # If you prefer to receive email in html format, change the next line to Y. $usehtml = "N"; # Here is where you specify the location of the file with the list of accounts, # by username, that you want the rollover bandwidth applied to. It's a simple # .txt file with a list of usernames, 1 per line, that you want handled by # this script. # A future version of this script will include the ability to apply the script # to all accounts by having the .txt file be blank. $accountsfile = "/scripts/mine/PWSrollbandwidthusers.txt"; #------------------------------------------------------------------- #------------------------------------------------------------------- # UNLESS YOU KNOW CGI, DO NOT EDIT ANYTHING BELOW HERE # Feel free to study the code, but alterations are at your own risk #------------------------------------------------------------------- #------------------------------------------------------------------- open(datafile,"$accountsfile"); @accounts = ; close(datafile); foreach $acc (@accounts) { chomp($acc); } local($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime(time); $year+=1900; $mon+=1; if ($mon < 10) {$mon = "0" . $mon} $prev_mon = $mon - "1"; $prev_year = $year; if ($prev_mon eq "0") {$prev_mon = "12";$prev_year-=1} if ($prev_mon < 10) {$prev_mon = "0" . $prev_mon} $previous_record = $prev_mon . "." . $prev_year; foreach $a (@accounts) { $bwlimit=""; $newbwlimit=""; $bwlimitreal=""; $lastproc=""; $lastprocmonth=""; $lastprocyear=""; $previousbandwidth=""; $absmaxbw=""; open(datafile,"/var/cpanel/users/$a"); @temp1 = ; close(datafile); foreach $t1 (@temp1) { chomp($t1); ($var,$value) = split(/=/, $t1); if ($var eq "BWLIMIT") {$bwlimit = $value} if ($var eq "BWLIMITREAL") {$bwlimitreal = $value} if ($var eq "LASTPROC") {$lastproc = $value} } open(datafile,"/var/cpanel/bandwidth/$a"); @temp2 = ; close(datafile); foreach $t2 (@temp2) { chomp($t2); ($var,$value) = split(/=/, $t2); if ($var eq $previous_record) {$previousbandwidth = $value} } if ($lastproc) { ($lastprocmonth, $lastprocyear) = split(/\./, $lastproc); } if ($lastprocmonth eq $mon && $lastprocyear eq $year) { $action = "already processed this month"; } elsif ($lastprocmonth eq "" && $lastprocyear eq "") { $action = "never processed yet"; push(@altered,$a); &make_adjustment1; } else { $action = "was processed last month"; push(@altered,$a); &make_adjustment2; } print "account = $a --> $action\n"; print "day - $mday\nmonth - $mon\nyear - $year\n"; print "last processed - $lastproc\n"; print "real bandwidth limit - $bwlimitreal\nbandwidth limit (pre-script) - $bwlimit\n"; print "bandwidth used last month - $previousbandwidth\nabsolute maximum bandwidth - $absmaxbw\nbandwidth limit (post-script) - $newbwlimit\n"; print "\n\n"; } # next a if ($email ne "") { if ($usehtml eq "Y") { &send_mail_html; } else { &send_mail; } } exit; sub make_adjustment1 { $bwlimitreal = $bwlimit; $newlastproc = $mon . "." . $year; $newbwlimit = $bwlimit - $previousbandwidth + $bwlimitreal; if ($absmax1) { $absmaxbw = $bwlimitreal * $absmax1; if ($newbwlimit > $absmaxbw) {$newbwlimit = $absmaxbw} } elsif ($absmax2) { $absmaxbw = $absmax2 * 1073741824; if ($newbwlimit > $absmaxbw) {$newbwlimit = $absmaxbw} } else { } $search = "BWLIMIT=" . $bwlimit; $replace = "BWLIMIT=" . $newbwlimit; open(datafile,"/var/cpanel/users/$a"); @lines = ; close(datafile); foreach $l (@lines) { chomp($l); } open(datafile,">/var/cpanel/users/$a"); flock (datafile,2); seek (datafile,0,0); foreach $l (@lines) { if ($l =~ $search) { $l =~ s/$search/$replace/; } print datafile "$l\n"; } print datafile "BWLIMITREAL=$bwlimitreal\n"; print datafile "LASTPROC=$newlastproc\n"; close(datafile); chmod(0644,"/var/cpanel/users/$a"); } sub make_adjustment2 { $newlastproc = $mon . "." . $year; $newbwlimit = $bwlimit - $previousbandwidth + $bwlimitreal; if ($absmax1) { $absmaxbw = $bwlimitreal * $absmax1; if ($newbwlimit > $absmaxbw) {$newbwlimit = $absmaxbw} } elsif ($absmax2) { $absmaxbw = $absmax2 * 1073741824; if ($newbwlimit > $absmaxbw) {$newbwlimit = $absmaxbw} } else { } $search1 = "BWLIMIT=" . $bwlimit; $replace1 = "BWLIMIT=" . $newbwlimit; $search2 = $lastproc; $replace2 = $newlastproc; open(datafile,"/var/cpanel/users/$a"); @lines = ; close(datafile); foreach $l (@lines) { chomp($l); } open(datafile,">/var/cpanel/users/$a"); flock (datafile,2); seek (datafile,0,0); foreach $l (@lines) { if ($l =~ $search1) { $l =~ s/$search1/$replace1/; } elsif ($l =~ $search2) { $l =~ s/$search2/$replace2/; } else { } print datafile "$l\n"; } close(datafile); chmod(0644,"/var/cpanel/users/$a"); } sub send_mail { $mailprog = "/usr/sbin/sendmail"; # Open The Mail Program open(MAIL,"|$mailprog -t"); print MAIL "To: $email\n"; print MAIL "From: webmaster\@premierwebsitesolutions.com\n"; print MAIL "Subject: $email_subject\n"; print MAIL "Bandwidth rollover was processed for the month of $mon/$year.\n\n"; print MAIL "The following accounts were altered:\n"; foreach $account (@altered) { print MAIL "- $account\n"; } print MAIL "\n\n"; close (MAIL); } sub send_mail_html { $mailprog = "/usr/sbin/sendmail"; # Open The Mail Program open(MAIL,"|$mailprog -t"); print MAIL "Content-Type: text/html; charset=iso-8859-1\n"; print MAIL "To: $email\n"; print MAIL "From: webmaster\@premierwebsitesolutions.com\n"; print MAIL "Subject: $email_subject\n"; print MAIL "Bandwidth rollover was processed for the month of $mon/$year.

\n\n"; print MAIL "The following accounts were altered:
\n"; print MAIL "
    "; foreach $account (@altered) { print MAIL "
  • $account
  • \n"; } print MAIL "
"; print MAIL "

\n\n"; close (MAIL); }