Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created November 6, 2010 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softlayer/665595 to your computer and use it in GitHub Desktop.
Save softlayer/665595 to your computer and use it in GitHub Desktop.
Retrieve a bandwidth graph for a single server.
<?php
/**
* Retrieve a bandwidth graph for a single server.
*
* Retrieve a bandwidth graph for a single server for an arbitrary start and
* end date, specifying graph size and other graphing options. We can do this
* with two calls to the SoftLayer API.
*
* Counter data such as bandwidth counters and CCI resource use are stored in
* a server's metric tracking object. Our first call retrieves that server's
* tracking object record. The second call is to the tracking object service
* which will generate a PNG image of our bandwidth graph.
*
* See below for more details.
*
* This assumes the SoftLayer API PHP client
* <http://github.com/softlayer/softlayer-api-php-client> is installed in the
* directory '/SoftLayer' in this script's path and that you wish to use the
* SOAP client instead of our XML-RPC client.
*
* @see http://sldn.softlayer.com/reference/services/SoftLayer_Hardware_Server/getObject
* @see http://sldn.softlayer.com/reference/services/SoftLayer_Metric_Tracking_Object/getBandwidthGraph
* @see http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Bandwidth_GraphOutputs
* @license <http://sldn.softlayer.com/article/License>
* @author SoftLayer Technologies, Inc. <sldn@softlayer.com>
*/
// Include XmlrpcClient.class.php if you'd like to use our XML-RPC client
// instead.
require_once(dirname(__FILE__) . '/SoftLayer/SoapClient.class.php');
/**
* Your SoftLayer API username.
*
* @var string
*/
$apiUsername = 'set me!';
/**
* Your SoftLayer API key. Generate and API key at the SoftLayer customer
* portal:
* https://manage.softlayer.com/Administrative/apiKeychain
*
* @var string
*/
$apiKey = 'set me too!';
/**
* The id number of the server whose graph you wish to retrieve. Call the
* getHardware() method in the SoftLayer_Account API service to retrieve a list
* of the servers on your account.
*
* @var int
*/
$serverId = 1234;
/**
* The date at which you wish to start graphing bandwidth. Pass any string that
* is compatible with the PHP strtotime() function.
*
* @var string
*/
$startDate = '2010-10-1';
/**
* The date at which you wish to end graphing bandwidth.
*
* @var string
*/
$endDate = '2010-11-1';
/**
* Whether to get a graph for 'public' or 'private' bandwidth usage.
*
* @var string
*/
$graphType = 'public';
/**
* The height of the text in the bandwidth graph in pixels.
*
* @var int
*/
$fontSize = 8;
/**
* The width of the graph to retrieve in pixels.
*
* @var int
*/
$graphWidth = 827;
/**
* The height of the graph to retrieve in pixels.
*
* @var int
*/
$graphHeight = 273;
/**
* Whether or not to hide time zone display on the graph.
*
* @var bool
*/
$hideTimeZone = true;
/**
* Create a connection to the SoftLayer_Hardware_Server API service and call the
* getMetricTrackingObject() method to get the server's associated tracking
* object record.
*
* @var SoftLayer_SoapClient
*/
$client = Softlayer_SoapClient::getClient('SoftLayer_Hardware_Server', $serverId, $apiUsername, $apiKey);
try {
// Retrieve our tracking object.
$trackingObject = $client->getMetricTrackingObject();
} catch (Exception $e) {
die('Unable to retrieve server record: ' . $e->getMessage());
}
/**
* Re-declare our client variable to talk to the
* SoftLayer_Metric_Tracking_Object API service.
*/
$client = SoftLayer_SoapClient::getClient('SoftLayer_Metric_Tracking_Object', $trackingObject->id, $apiUsername, $apiKey);
try {
// getBandwidthGraph() returns a SoftLayer_Container_Bandwidth_GraphOutputs
// object. The contents of the bandwidth image is in $image->graphImage.
// From here you can write it to the filesystem, display it to a web
// browser, or run other functions on it.
$image = $client->getBandwidthGraph($startDate, $endDate, $graphType, $fontSize, $graphWidth, $graphHeight, $hideTimeZone);
echo 'Image retrieved!';
} catch (Exception $e) {
die('Unable to retrieve bandwidth image: ' . $e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment