|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Service/Amazon/Ec2/Abstract.php'; 00027 00038 class Zend_Service_Amazon_Ec2_CloudWatch extends Zend_Service_Amazon_Ec2_Abstract 00039 { 00043 protected $_ec2Endpoint = 'monitoring.amazonaws.com'; 00044 00048 protected $_ec2ApiVersion = '2009-05-15'; 00049 00053 protected $_xmlNamespace = 'http://monitoring.amazonaws.com/doc/2009-05-15/'; 00054 00112 protected $_validMetrics = array('CPUUtilization', 'NetworkIn', 'NetworkOut', 00113 'DiskWriteOps', 'DiskReadBytes', 'DiskReadOps', 00114 'DiskWriteBytes', 'Latency', 'RequestCount', 00115 'HealthyHostCount', 'UnHealthyHostCount'); 00116 00153 protected $_validStatistics = array('Average', 'Maximum', 'Minimum', 'Samples', 'Sum'); 00154 00184 protected $_validDimensionsKeys = array('ImageId', 'AvailabilityZone', 'AutoScalingGroupName', 00185 'InstanceId', 'InstanceType', 'LoadBalancerName'); 00186 00231 public function getMetricStatistics(array $options) 00232 { 00233 $_usedStatistics = array(); 00234 00235 $params = array(); 00236 $params['Action'] = 'GetMetricStatistics'; 00237 00238 if (!isset($options['Period'])) { 00239 $options['Period'] = 60; 00240 } 00241 if (!isset($options['Namespace'])) { 00242 $options['Namespace'] = 'AWS/EC2'; 00243 } 00244 00245 if (!isset($options['MeasureName']) || !in_array($options['MeasureName'], $this->_validMetrics, true)) { 00246 throw new Zend_Service_Amazon_Ec2_Exception('Invalid Metric Type: ' . $options['MeasureName']); 00247 } 00248 00249 if(!isset($options['Statistics'])) { 00250 $options['Statistics'][] = 'Average'; 00251 } elseif(!is_array($options['Statistics'])) { 00252 $options['Statistics'][] = $options['Statistics']; 00253 } 00254 00255 foreach($options['Statistics'] as $k=>$s) { 00256 if(!in_array($s, $this->_validStatistics, true)) continue; 00257 $options['Statistics.member.' . ($k+1)] = $s; 00258 $_usedStatistics[] = $s; 00259 } 00260 unset($options['Statistics']); 00261 00262 if(isset($options['StartTime'])) { 00263 if(!is_numeric($options['StartTime'])) $options['StartTime'] = strtotime($options['StartTime']); 00264 $options['StartTime'] = gmdate('c', $options['StartTime']); 00265 } else { 00266 $options['StartTime'] = gmdate('c', strtotime('-1 hour')); 00267 } 00268 00269 if(isset($options['EndTime'])) { 00270 if(!is_numeric($options['EndTime'])) $options['EndTime'] = strtotime($options['EndTime']); 00271 $options['EndTime'] = gmdate('c', $options['EndTime']); 00272 } else { 00273 $options['EndTime'] = gmdate('c'); 00274 } 00275 00276 if(isset($options['Dimensions'])) { 00277 $x = 1; 00278 foreach($options['Dimensions'] as $dimKey=>$dimVal) { 00279 if(!in_array($dimKey, $this->_validDimensionsKeys, true)) continue; 00280 $options['Dimensions.member.' . $x . '.Name'] = $dimKey; 00281 $options['Dimensions.member.' . $x . '.Value'] = $dimVal; 00282 $x++; 00283 } 00284 00285 unset($options['Dimensions']); 00286 } 00287 00288 $params = array_merge($params, $options); 00289 00290 $response = $this->sendRequest($params); 00291 $response->setNamespace($this->_xmlNamespace); 00292 00293 $xpath = $response->getXPath(); 00294 $nodes = $xpath->query('//ec2:GetMetricStatisticsResult/ec2:Datapoints/ec2:member'); 00295 00296 $return = array(); 00297 $return['label'] = $xpath->evaluate('string(//ec2:GetMetricStatisticsResult/ec2:Label/text())'); 00298 foreach ( $nodes as $node ) { 00299 $item = array(); 00300 00301 $item['Timestamp'] = $xpath->evaluate('string(ec2:Timestamp/text())', $node); 00302 $item['Unit'] = $xpath->evaluate('string(ec2:Unit/text())', $node); 00303 $item['Samples'] = $xpath->evaluate('string(ec2:Samples/text())', $node); 00304 foreach($_usedStatistics as $us) { 00305 $item[$us] = $xpath->evaluate('string(ec2:' . $us . '/text())', $node); 00306 } 00307 00308 $return['datapoints'][] = $item; 00309 unset($item, $node); 00310 } 00311 00312 return $return; 00313 00314 } 00315 00324 public function listMetrics($nextToken = null) 00325 { 00326 $params = array(); 00327 $params['Action'] = 'ListMetrics'; 00328 if (!empty($nextToken)) { 00329 $params['NextToken'] = $nextToken; 00330 } 00331 00332 $response = $this->sendRequest($params); 00333 $response->setNamespace($this->_xmlNamespace); 00334 00335 $xpath = $response->getXPath(); 00336 $nodes = $xpath->query('//ec2:ListMetricsResult/ec2:Metrics/ec2:member'); 00337 00338 $return = array(); 00339 foreach ( $nodes as $node ) { 00340 $item = array(); 00341 00342 $item['MeasureName'] = $xpath->evaluate('string(ec2:MeasureName/text())', $node); 00343 $item['Namespace'] = $xpath->evaluate('string(ec2:Namespace/text())', $node); 00344 $item['Deminsions']['name'] = $xpath->evaluate('string(ec2:Dimensions/ec2:member/ec2:Name/text())', $node); 00345 $item['Deminsions']['value'] = $xpath->evaluate('string(ec2:Dimensions/ec2:member/ec2:Value/text())', $node); 00346 00347 if (empty($item['Deminsions']['name'])) { 00348 $item['Deminsions'] = array(); 00349 } 00350 00351 $return[] = $item; 00352 unset($item, $node); 00353 } 00354 00355 return $return; 00356 } 00357 }