Last year we kicked off some PHP and iControl with a “Getting Started” article that’s been enhanced and added to by a couple of users (thanks rpaan and orangepeelbeef).

I recently decided to integrate my BIG-IP with Twitter so that it could accept commands via tweets as well as send updates. In the course of doing that, it was necessary to write a couple of PHP functions to return statistics and information from BIG-IP.

One of the most frustrating parts of the solution was simply dealing with 64-bit integers. The System.Statistics objects in iControl used to return statistics information from BIG-IP return a 64-bit integer split into its high and low values. That means you need to recreate the 64-bit integer. PHP isn't necessarily very friendly to 64-bit integers natively, but after a bit of searching, the answer was found on the Internet and is included in the code along with kudos to the developer who offered the solution (which works great, by the way).

If you haven’t previously set up your environment to handle iControl via PHP, you’ll want to check out the Getting Started article. Please be sure to read through rpaan’s recreated solution, as he’s offered up a solution that doesn’t require PEAR.

This solution still assumes the use of PEAR, but should be easily ported to rpaan’s solution.

This solution implements two functions. The first can be used to retrieve a specific HTTP statistic from BIG-IP via the get_http_statistics call. The complete list of enumerated statistics types can be found here (#170-#214, statistics beginning with STATISTIC_HTTP) as this solution uses only a subset of the statistics available via the get_http_statistics operation.  

The second function returns version information as returned by the get_version operation.

The code is included below.

<?

require_once 'SOAP/Client.php';

 

// Thanks to "thegeek" for a solution to dealing with 64-bit integers in PHP

// http://www.gerd-riesselmann.net/archives/2006/01/a-64-bit-integer-for-php

 

define("BIGINT_DIVIDER", 0x7fffffff + 1);

 

// This function takes one parameter: $reqstat that indicates which statistic is being

// requested. Possible values are:

//

// ALL       Return number of total requests handled

// 200       Return number of 2xx responses returned

// 400       Return number of 4xx responses returned

// 500       Return number of 5xx responses returned

// GET       Return number of GET requests handled

// POST      Return number of POST requests handled

//

// This function returns a string containing the label and the value of the statistic requested

// in the following format:   label:value

 

function getstats($reqstat) {

  $soapoptions = array('namespace' => 'urn:iControl');

  $wsdl_url = 'http://<BIG-IP hostname>/bigip/wsdl/System.Statistics.wsdl';

 

  $proxy_parms = array( 'user' => '<username here>', 'pass' => '<password here>');

 

  $params = array();

  $client = new SOAP_Client($wsdl_url, true, '', $proxy_parms ); 

  $client->setOpt('curl', CURLOPT_SSL_VERIFYPEER, 0);

  $client->setOpt('curl', CURLOPT_SSL_VERIFYHOST, 0);

 

  // call to get_http_statistics takes no parms, returns a System.Statistics structure

  $response    = $client->call('get_http_statistics', $params, $soapoptions);

 

  if (PEAR::isError($response)) {       

          print "an error occurred in the call " . $response;

  }

  else {

         $finalresponse = "";

          foreach ($response as $stat) {  

              foreach ($stat as $stats) {  

                 $label = "";

                 if ($stats->type == 'STATISTIC_HTTP_TOTAL_REQUESTS' && $reqstat == "ALL") {

                      $label = "TOTAL REQUESTS";

                  }   

                  else

                  if ($stats->type == 'STATISTIC_HTTP_2XX_RESPONSES' && $reqstat == "200") {

                     $label = "HTTP 2xx RESPONSES";

                  }

                  else

                  if ($stats->type == 'STATISTIC_HTTP_4XX_RESPONSES' && $reqstat == "400") {

                     $label = "HTTP 4xx RESPONSES";

                  }

                  else

                  if ($stats->type == 'STATISTIC_HTTP_5XX_RESPONSES' && $reqstat == "500") {

                     $label = "HTTP 5xx RESPONSES";

                  }

                  else

                  if ($stats->type == 'STATISTIC_HTTP_GET_REQUESTS' && $reqstat == "GET") {

                     $label = "GET REQUESTS";

                  }

                  else

                  if ($stats->type == 'STATISTIC_HTTP_POST_REQUESTS' && $reqstat == "POST") {

                     $label = "POST REQUESTS";

                  }

                

                  // if the label has been set recreate the value by recombining the high and low

                  // bit values into a 64-bit integer value

                  if ($label != "" ) {

                    $realvalue1 = $stats->value->high * BIGINT_DIVIDER + $stats->value->low;

                    $finalresponse .= "$label:$realvalue1   ";

                  }

              }

          }

       return $finalresponse;

  }

 

}

 

?>

 

<? 
require_once 'SOAP/Client.php';
 
// This function takes no parameters. It makes a call to get_version and returns the information
// in a string formatted as follows: Version:value 
 
function getversion() {
 
  $soapoptions = array('namespace' => 'urn:iControl'); 
  $wsdl_url = 'http://<BIG-IP host name>/bigip/wsdl/System.SystemInfo.wsdl'; 
 
  $proxy_parms = array( 'user' => '<username here>', 'pass' => '<password here>');
 
  $params = array(); 
  $client = new SOAP_Client($wsdl_url, true, '', $proxy_parms );  
  $client->setOpt('curl', CURLOPT_SSL_VERIFYPEER, 0); 
  $client->setOpt('curl', CURLOPT_SSL_VERIFYHOST, 0); 
 
  $response    = $client->call('get_version', $params, $soapoptions);
  
  if (PEAR::isError($response)) {        
          print "an error occurred in the call " . $response;
  }
  else {
         $finalresponse = "Version: $response";
          return $finalresponse;
  }
 
}
 
?>