HTTP/1.1 // 38.107.191.89 // // 41283 // // CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

Clicktale

cPanel Bandwidth Checking Script

Oct
27
2009

I’ve been pretty frustrated with the way that cPanel notifies (or doesn’t notify) me of bandwidth overages. Seems sometimes I get them and sometimes not. So when I found out that you can do this with the WHM XML api I wanted to get it set up ASAP. This script can be used along with cron and PHPMailer to notify you of the bandwidth usage for all your domains in WHM. I have mine set up to notify me daily so I can keep track of my bandwidth usage.

key-setup
To get your Access key for WHM, login and select Setup Remote Access Key.

If you need more help with the WHM XML API, you can find it here. You can download PHPMailer here.

<?php
// cPanel Bandwith Checking Script
// Author: James Ehly
// Website: devtrench.com

$email      = "youremail@example.com"; // your email address, used for FROM and TO address
$email_name = "John Doe";              // your name, used for FROM and TO address

$user = array();

$whmusername = "whm_user_name";  // your WHM username
$whmhash     = "whm_access_key"; // your WHM access key
$whmserver   = "example.com";    // domain you access WHM at

$user[] = array($whmusername, $whmhash, $whmserver);  // you can make as many of these array entries as needed if you have more than one whm server

$output = "<h1>Bandwidth</h1>";

foreach ($user as $v)
{

  $query = "https://".$v[2].":2087/xml-api/showbw";

  $curl = curl_init();																												# Create Curl Object
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);																# Allow certs that do not match the domain
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);																# Allow self-signed certs
  curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);																# Return contents of transfer on curl_exec
  $header[0] = "Authorization: WHM $v[0]:" . preg_replace("'(\r|\n)'","",$v[1]);	# Remove newlines from the hash
  curl_setopt($curl,CURLOPT_HTTPHEADER,$header);															# Set curl header
  curl_setopt($curl, CURLOPT_URL, $query);																		# Set your URL
  $result = curl_exec($curl);																									# Execute Query, assign to $result
  if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
  }
  curl_close($curl);

  $xml = new SimpleXMLElement($result);

$output .= "<h3>".$v[2]."</h3>";
  $output .= "<table border='1' style='border-collapse:collapse;font-family:monospace' cellpadding='5'>";
      $output .= '<tr>';
      $output .= '<th>Domain</th>';
      $output .= '<th>Limit</th>';
      $output .= '<th colspan="2">Percent</th>';
      $output .= '</tr>';

  foreach($xml->bandwidth[0] as $acct)
  {
    if (isset($acct->limit))
    {
      $limit = $acct->limit;
      $mbs   = floatval($limit)/(1024*1024);
      $total = $acct->totalbytes;
      $percent = round((floatval($total)/floatval($limit))*100);
      $warn = ($percent >= 90) ? 'style="background:#F00;color:#FFF;font-weight:bold"' : '';
      if($percent>=90)
      {
        $color = '#f00';
      }
      elseif($percent>=75)
      {
        $color = '#FFE900';
      }
      else
      {
        $color = '#00E70A';
      }
      $output .= "<tr $warn>";
      $output .= '<td>' . $acct->maindomain . '</td>';
      $output .= '<td>' . $mbs . 'MB</td>';
      $output .= '<td>' . $percent .  '%</td>';
      $output .= '<td><div style="width:100px;height:10px;border:1px solid #000"><div style="height:10px;width:'.$percent.'px;background:'.$color.'"></div></div></td>';
      $output .= '</tr>';
    }
  }
  $output .= "</table>";
}

include_once('class.phpmailer.php');

$mail=new PHPMailer();

$mail->From     = $email;
$mail->FromName = $email_name";

$mail->Subject = 'Bandwidth Usage for ' . date('D M d, Y');

$mail->MsgHTML($output);

$mail->AddAddress($email, $email_name);

if(!$mail->Send()) {
  echo 'Failed to send mail';
}
?>

Comments »

Allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

No comments yet.