The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Accessing Diskspace and Bandwidth

Discussion in 'General Discussion' started by andrewhmartin, Apr 25, 2004.

  1. andrewhmartin

    andrewhmartin Registered

    Joined:
    Jan 28, 2004
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Hello all,

    I would like to access diskspace and bandwidth information from CPanel. (or even linux/apache)

    I am already using the PHP acounting API to automatically create acounts, and the $socket = fsockopen($host,2082); method to automate email and ftp account creation. These methods work great for interacting with CPanel, but I have been unable to find any posts showing methods for accessing disk/bandwidth information.

    I do not need to modify any of the quotas, I just want to have a PHP script that reads this inforamtion and displays it for an account. Does anyone have any script samples that can do this, or even tell me the name/location of the files that store this information?

    Thanks in advance for any assistance.

    andrew
     
  2. rickb123

    rickb123 Registered

    Joined:
    Feb 21, 2004
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Any resolution to this? I am wanting to do the same thing... View disk usage/bandwidth with the remote accounting scripts.
     
  3. summy

    summy Active Member

    Joined:
    Jan 14, 2004
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    6
    ditto, i have a customer that wants a php script to tell him how much diskspace he has left.
     
  4. andrewhmartin

    andrewhmartin Registered

    Joined:
    Jan 28, 2004
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    some help

    Here is some PHP code I found to get a percentage of bandwidth used for a domain on a CPanel server. I have not tried playing with/modifying this yet and can not even remember where I found it. If any one can get this working please post any modifications you had to make here.

    <?php
    #thanks to Ciaran for finding and easy way to get the logdir on all servers 8)

    #change cognition-central.com to your domain name
    define("DOMAIN", "cognition-central.com");
    class BandwidthMonitor //0.0.3
    {
    function getUsage()
    {
    $bw_tot = 0;
    if(!$log_dir = @dir("/usr/local/apache/domlogs/")) return false;
    while(false !== ($filename = @$log_dir->read()))
    {
    if(eregi(DOMAIN . "-bytes_log$", $filename))
    {
    if(!$bwfile = @file($log_dir->path . $filename)) continue;
    for($i = 0; $i < count($bwfile); $i++)
    {
    if(!ereg("^[0-9]{1,}$", $n))
    {
    $bwfile[$i] = explode(" ", $bwfile[$i]);
    $bw_tot += $bwfile[$i][1];
    }
    else
    {
    $bw_tot += $bwfile[$i];
    }
    }
    }
    }
    return $bw_tot;
    }

    function getPercentage($max_bw = 5)
    {
    //accurate to 14 (!) decimal places ????
    return ((BandwidthMonitor::getUsage() / ($max_bw * 1024 * 1024 * 1024)) * 100); //([CURRENT USAGE (bytes)] / ([MAX BANDWIDTH (gives as Gb so multiplied up to bytes)])) * 100
    }
    }
    ?>

    I found another script as well at one point and am still trying to find that one to post also.

    -ah martin
     
  5. andrewhmartin

    andrewhmartin Registered

    Joined:
    Jan 28, 2004
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    more code to play with

    Here is code from <a href="http://http://josheli.com/vox/">josheli.com</a>. Check out the site there is some other good code sample there as well..

    <?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 'http://'
    $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("http://$cpUser:$cpPass@$url:2082/frontend/vertex/main.html",'r');

    $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("http://$whmUser:$whmPass@$url:2086/scripts2/listaccts?viewall=1");

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

    if($int2 > 0 && 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 = $clientTotal + $resellerTotal[1];
    $msg .= 'Total space used: '.$total."\n";

    $remaining = $resellerQuota - $total;
    $msg .= 'Remaining space: '.$remaining."\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);
    }

    ?>

    It looks like this script just does a string match on the WHM list account page. Should be able to mod this for reading other info from WHM..

    Hope this helps everyone.. Please let me know if you modify any of these to use succefully..
     
  6. hellfish

    hellfish Member

    Joined:
    Jun 25, 2004
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Re: some help

    Please forgive my newb ignorance but how do you run this script?
     
Loading...

Share This Page