_validMetrics, true)) { throw new Zend_Service_Amazon_Ec2_Exception('Invalid Metric Type: ' . $options['MeasureName']); } if(!isset($options['Statistics'])) { $options['Statistics'][] = 'Average'; } elseif(!is_array($options['Statistics'])) { $options['Statistics'][] = $options['Statistics']; } foreach($options['Statistics'] as $k=>$s) { if(!in_array($s, $this->_validStatistics, true)) continue; $options['Statistics.member.' . ($k+1)] = $s; $_usedStatistics[] = $s; } unset($options['Statistics']); if(isset($options['StartTime'])) { if(!is_numeric($options['StartTime'])) $options['StartTime'] = strtotime($options['StartTime']); $options['StartTime'] = gmdate('c', $options['StartTime']); } else { $options['StartTime'] = gmdate('c', strtotime('-1 hour')); } if(isset($options['EndTime'])) { if(!is_numeric($options['EndTime'])) $options['EndTime'] = strtotime($options['EndTime']); $options['EndTime'] = gmdate('c', $options['EndTime']); } else { $options['EndTime'] = gmdate('c'); } if(isset($options['Dimensions'])) { $x = 1; foreach($options['Dimensions'] as $dimKey=>$dimVal) { if(!in_array($dimKey, $this->_validDimensionsKeys, true)) continue; $options['Dimensions.member.' . $x . '.Name'] = $dimKey; $options['Dimensions.member.' . $x . '.Value'] = $dimVal; $x++; } unset($options['Dimensions']); } $params = array_merge($params, $options); $response = $this->sendRequest($params); $response->setNamespace($this->_xmlNamespace); $xpath = $response->getXPath(); $nodes = $xpath->query('//ec2:GetMetricStatisticsResult/ec2:Datapoints/ec2:member'); $return = array(); $return['label'] = $xpath->evaluate('string(//ec2:GetMetricStatisticsResult/ec2:Label/text())'); foreach ( $nodes as $node ) { $item = array(); $item['Timestamp'] = $xpath->evaluate('string(ec2:Timestamp/text())', $node); $item['Unit'] = $xpath->evaluate('string(ec2:Unit/text())', $node); $item['Samples'] = $xpath->evaluate('string(ec2:Samples/text())', $node); foreach($_usedStatistics as $us) { $item[$us] = $xpath->evaluate('string(ec2:' . $us . '/text())', $node); } $return['datapoints'][] = $item; unset($item, $node); } return $return; } /** * Return the Metrics that are aviable for your current monitored instances * * @param string $nextToken The NextToken parameter is an optional parameter * that allows you to retrieve the next set of results * for your ListMetrics query. * @return array */ public function listMetrics($nextToken = null) { $params = array(); $params['Action'] = 'ListMetrics'; if (!empty($nextToken)) { $params['NextToken'] = $nextToken; } $response = $this->sendRequest($params); $response->setNamespace($this->_xmlNamespace); $xpath = $response->getXPath(); $nodes = $xpath->query('//ec2:ListMetricsResult/ec2:Metrics/ec2:member'); $return = array(); foreach ( $nodes as $node ) { $item = array(); $item['MeasureName'] = $xpath->evaluate('string(ec2:MeasureName/text())', $node); $item['Namespace'] = $xpath->evaluate('string(ec2:Namespace/text())', $node); $item['Deminsions']['name'] = $xpath->evaluate('string(ec2:Dimensions/ec2:member/ec2:Name/text())', $node); $item['Deminsions']['value'] = $xpath->evaluate('string(ec2:Dimensions/ec2:member/ec2:Value/text())', $node); if (empty($item['Deminsions']['name'])) { $item['Deminsions'] = array(); } $return[] = $item; unset($item, $node); } return $return; } }