Blog

cPanel Bandwidth Checking Script

Oct
27

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:
  1. <?php
  2. // cPanel Bandwith Checking Script
  3. // Author: James Ehly
  4. // Website: devtrench.com
  5.  
  6. $email      = "youremail@example.com"; // your email address, used for FROM and TO address
  7. $email_name = "John Doe";              // your name, used for FROM and TO address
  8.  
  9. $user = array();
  10.  
  11. $whmusername = "whm_user_name"// your WHM username
  12. $whmhash     = "whm_access_key"; // your WHM access key   
  13. $whmserver   = "example.com";    // domain you access WHM at
  14.  
  15. $user[] = array($whmusername, $whmhash, $whmserver)// you can make as many of these array entries as needed if you have more than one whm server
  16.  
  17. $output = "<h1>Bandwidth</h1>";
  18.  
  19. foreach ($user as $v)
  20. {
  21.  
  22.   $query = "https://".$v[2].":2087/xml-api/showbw";
  23.  
  24.   $curl = curl_init();                                                        # Create Curl Object
  25.   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);                # Allow certs that do not match the domain
  26.   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);                # Allow self-signed certs
  27.   curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);                # Return contents of transfer on curl_exec
  28.   $header[0] = "Authorization: WHM $v[0]:" . preg_replace("'(\r|\n)'","",$v[1]);    # Remove newlines from the hash
  29.   curl_setopt($curl,CURLOPT_HTTPHEADER,$header);                                                            # Set curl header
  30.   curl_setopt($curl, CURLOPT_URL, $query);                                    # Set your URL
  31.   $result = curl_exec($curl);                                                                           # Execute Query, assign to $result
  32.   if ($result == false) {
  33.     error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
  34.   }
  35.   curl_close($curl);
  36.  
  37.   $xml = new SimpleXMLElement($result);
  38.  
  39. $output .= "<h3>".$v[2]."</h3>";
  40.   $output .= "<table border='1' style='border-collapse:collapse;font-family:monospace' cellpadding='5'>";
  41.       $output .= '<tr>';
  42.       $output .= '<th>Domain</th>';
  43.       $output .= '<th>Limit</th>';
  44.       $output .= '<th colspan="2">Percent</th>';
  45.       $output .= '</tr>';
  46.  
  47.   foreach($xml->bandwidth[0] as $acct)
  48.   {
  49.     if (isset($acct->limit))
  50.     {
  51.       $limit = $acct->limit;
  52.       $mbs   = floatval($limit)/(1024*1024);
  53.       $total = $acct->totalbytes;
  54.       $percent = round((floatval($total)/floatval($limit))*100);
  55.       $warn = ($percent>= 90) ? 'style="background:#F00;color:#FFF;font-weight:bold"' : '';
  56.       if($percent>=90)
  57.       {
  58.         $color = '#f00';
  59.       }
  60.       elseif($percent>=75)
  61.       {
  62.         $color = '#FFE900';
  63.       }
  64.       else
  65.       {
  66.         $color = '#00E70A';
  67.       }
  68.       $output .= "<tr $warn>";
  69.       $output .= '<td>' . $acct->maindomain . '</td>';
  70.       $output .= '<td>' . $mbs . 'MB</td>';
  71.       $output .= '<td>' . $percent'%</td>';
  72.       $output .= '<td><div style="width:100px;height:10px;border:1px solid #000"><div style="height:10px;width:'.$percent.'px;background:'.$color.'"></div></div></td>';
  73.       $output .= '</tr>';
  74.     }
  75.   }
  76.   $output .= "</table>";
  77. }
  78.  
  79. include_once('class.phpmailer.php');
  80.  
  81. $mail=new PHPMailer();
  82.  
  83. $mail->From     = $email;
  84. $mail->FromName = $email_name";
  85. $mail->Subject = 'Bandwidth Usage for ' . date('D M d, Y');
  86. $mail->MsgHTML($output);
  87. $mail->AddAddress($email, $email_name);
  88. if(!$mail->Send()) {
  89.   echo 'Failed to send mail';
  90. }
  91. ?>

Share With Friends
  • Print
  • Facebook
  • FriendFeed
  • Google Bookmarks
  • Sphinn
  • SphereIt
  • Digg
  • del.icio.us
  • Mixx
  • Fark
  • HackerNews
  • Reddit
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter
  • Yahoo! Buzz

Related Posts


Comment Here » 205 views
Posted in: Programming, Sys Admin, cPanel

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.

« MODx Chunk Cache Plugin | Where to Find Cheap SSL Certificates »