Skip to content

Instantly share code, notes, and snippets.

@underscorephil
Created May 31, 2013 14:06
Show Gist options
  • Save underscorephil/5685206 to your computer and use it in GitHub Desktop.
Save underscorephil/5685206 to your computer and use it in GitHub Desktop.
<?php
/**
* Add new configuration for a monitoring agent.
*
* This example shows how to use the SoftLayer API to add new configuration for a monitoring agent for the hardware or virtual guest
* server instance.
*
* @license <http://sldn.softlayer.com/wiki/index.php/License>
* @author SoftLayer Technologies, Inc. <sldn@softlayer.com>
*/
require_once(dirname(__FILE__) . '/SoftLayer/SoapClient.class.php');
/**
* Your SoftLayer API username.
*
* @var string
*/
$apiUsername = 'user name';
/**
* Your SoftLayer API key. Generate and API key at the SoftLayer customer
* portal:
* https://manage.softlayer.com/Administrative/apiKeychain
*
* @var string
*/
$apiKey = 'api key';
/**
* The id number of the server
*
* @var int
*/
$serverId = 809518;
/**
* The value for disk path
*
* @var int
*/
$diskPathValue = "/dev";
/**
* The value for Graph Disk Usage
*
* @var boolean
*/
$graphDiskUsageValue = "TRUE";
/**
* The value for Disk Usage Error Alarm
*
* @var boolean
*/
$diskUsageErrorAlarmValue = "TRUE";
/**
* Create a connection to the SoftLayer_Hardware_Server API service and call the
* getMonitoringAgents() method to get the server's associated tracking
* object record.
*
* @var SoftLayer_SoapClient
*/
$client = Softlayer_SoapClient::getClient('SoftLayer_Virtual_Guest', $serverId, $apiUsername, $apiKey);
try {
// Retrieve server's monitoring agents. as SoftLayer_Monitoring_Agent objects.
// getMonitoringAgents() returns an array of SoftLayer_Monitoring_Agent objects.
// From here you can loop through these agents to perform defferent functions
$monitoringAgents = $client->getMonitoringAgents();
} catch (Exception $e) {
die('Unable to retrieve server monitoring agents : ' . $e->getMessage());
}
/**
*
* Find the id of the specific agent we are wanting, in this example the "Cpu, Disk, and Memory" monitoring agent.
*
*/
$cdmAgent = null;
foreach ($monitoringAgents as $agent) {
if ($agent->name == 'Cpu, Disk, and Memory Monitoring Agent') {
$cdmAgent = $agent;
break;
}
}
if($cdmAgent == null) {
die('Unable to find Cpu, Disk, and Memory agent for the server');
}
/**
* Next we can create an array of all configuration variables that we are able to track.
* Re-declare our client variable to talk to the
* SoftLayer_Monitoring_Agent API service.
*/
$client = SoftLayer_SoapClient::getClient('SoftLayer_Monitoring_Agent', $cdmAgent->id, $apiUsername, $apiKey);
$objectMask = new SoftLayer_ObjectMask();
$objectMask->configurationTemplate->configurationSections->subSections->definitions;
$client->setObjectMask($objectMask);
try {
$cdmConfigurationTemplate = $client->getConfigurationTemplate();
} catch (Exception $e) {
die('Unable to retrieve CDM monitoring agent configuration template : ' . $e->getMessage());
}
$configurationValues = array();
// Loop through each section
foreach ($cdmConfigurationTemplate->configurationSections as $configurationSection) {
// Find Disk Profile section
if ($configurationSection->name == 'Disk Profile') {
// Loop through the sections's sub-sections
foreach ($configurationSection->subSections as $configSubSection) {
// Find General sub-section
if ($configSubSection->name == 'General') {
// Loop through sub-section's definitions
foreach ($configSubSection->definitions as $configDefinition) {
if ($configDefinition->name == 'Disk Path') {
try {
// Get resource specific values
$resourceValues = $client->getAvailableConfigurationValues($configDefinition->id);
} catch (Exception $e) {
die('Unable to retrieve resource specific values : ' . $e->getMessage());
}
$validFlag = false;
foreach($resourceValues as $resourceValue) {
if($diskPathValue == $resourceValue->value) {
$validFlag = true;
break;
}
}
if(!$validFlag) {
die("Invalid disk path is given");
} else {
$param = new stdClass();
$param->configurationDefinitionId = $configDefinition->id;
$param->agentId = $cdmAgent->id;
$param->value = $diskPathValue;
$configurationValues[] = $param;
}
}
}
} else if ($configSubSection->name == 'Graphing') {
foreach ($configSubSection->definitions as $configDefinition) {
if ($configDefinition->name == 'Graph Disk Usage') {
$param = new stdClass();
$param->configurationDefinitionId = $configDefinition->id;
$param->agentId = $cdmAgent->id;
$param->value = $graphDiskUsageValue;
$configurationValues[] = $param;
}
}
} else if ($configSubSection->name == 'Alarms') {
foreach ($configSubSection->definitions as $configDefinition) {
if ($configDefinition->name == 'Disk Usage Error Alarm') {
$param = new stdClass();
$param->configurationDefinitionId = $configDefinition->id;
$param->agentId = $cdmAgent->id;
$param->value = $diskUsageErrorAlarmValue;
$configurationValues[] = $param;
}
}
}
}
}
}
try {
$transaction = $client->addConfigurationProfile($configurationValues);
print "Successfully created configuration profile with transaction id : $transaction->id \n";
} catch (Exception $e) {
die('Error in creating configuration profile : ' . $e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment