<?php
/*

<dv at josheli.com>

Code for monitoring disk usage.
A house of cards, but it works for me. It scrapes from WHM and cPanel (and since those
can be wrong, gasp!, your milage may vary).
If any domain (yours or a clients) has more than 999mb (i.e. 1 gig of space used)
it won't work. It can be made to, but in it's present form, uh, no.

It can be run from the command line/cron or a browser.
It can email a report or not.

Report looks like this:

Timestamp: September 14, 2003, 6:55 am

Reseller Total: 84.80
Domain: client1.com -- Space used: 19 Meg
Domain: client2.com -- Space used: 11 Meg
Domain: client3.com -- Space used: 104 Meg
Domain: client4.com -- Space used: 0 Meg
Domain: client5.com -- Space used: 14 Meg
Domain: client6.org -- Space used: 13 Meg
Domain: client7.com -- Space used: 8 Meg
Total space used: 253.8
Remaining space: 246.2


*/

/***********************************************************************
Start of configurable variables
************************************************************************/

//run it from command line (eg with cron)?
//the alternative of course is to run from a browser (if so, set this variable to false)
$cmdLine false;

//send an email? or output to screen (tty or browser)
//set to false to output to screen
$sendEmail false;
$emailTo 'dv at josheli.com';//where
$emailFrom 'dv at josheli.com';//from whom (doesn't really matter)
$emailSubject 'Disk Usage Report';

$whmUser 'username';
$whmPass 'password';
$cpUser 'username';
$cpPass 'password';
$url 'www.mydomain.com';//do not include 'https://'
$cpTheme 'vertex';
$resellerQuota 500;//in megabytes (e.g. 1.5 gigs would be '1500')

/***********************************************************************
End of configurable variables
************************************************************************/


$msg 'Timestamp: '.date("F j, Y, g:i a")."\n\n";

//get the main reseller account usage from cpanel
//the other possible way to do this is to do a system call to 'du -ms' 
$page = @file_get_contents("https://$cpUser:$cpPass@$url:2083/frontend/$cpTheme/main.html");

$int preg_match("/>Disk.*?<\/b>/is",$page$piece);
if(
$int) {
    
//$resellerTotal[1] will contain the value
    
preg_match('/<b>(.*)<\/b>/',$piece[0], $resellerTotal);
    
$msg .= 'Reseller Total: '.$resellerTotal[1]."\n";
}

//now get the WHM 'list accounts' page
$page2 = @file_get_contents("https://$whmUser:$whmPass@$url:2087/scripts2/listaccts?viewall=1");

//get each accounts table row
$int2 preg_match_all("/<tr class=(?:tdshade2|tdshade1)>(.*?)<\/tr>/is"$page2$matches);

if(
$int2 && is_array($matches[1])) { 
    foreach(
$matches[1] as $match) {        
        
$account explode('</td><td>',$match);
        
$msg .= 'Domain: '.strip_tags($account[0]).' -- Space used: '.$account[7]."\n";
        
$clientTotal += $account[7];
    }
    
$total ceil($clientTotal $resellerTotal[1]);
    
$msg .= 'Total space used: '.$total." mb \n";

    
$remaining ceil($resellerQuota $total);
    
$msg .= 'Remaining space: '.$remaining." mb \n";
    
    
}
else {
  
$msg .= "No workee.\n";
}

//output it somewhere
if(!$sendEmail) {
    if(!
$cmdLine) {
        echo 
nl2br($msg);//to browser
    
}
    else {
        echo 
$msg;//to tty
    
}
}
else {
//to email

    
$headers "From: <$emailFrom>\r\n";
    
$headers .= "X-Sender: <$emailFrom>\r\n";
    
$headers .= "X-Mailer: PHP\r\n"
    
$headers .= "X-Priority: 3\r\n"//1 UrgentMessage, 3 Normal
    
$headers .= "Return-Path: <$emailFrom>\r\n";
    
$headers .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
            
    
mail($emailTo$emailSubject$msg$headers);
}

?>