public function __construct(array $options = array()) { if (!isset($options['default_cache_config'])) { $options['default_cache_config'] = 'cache/aws'; } if (!isset($options['key']) && Kwf_Config::getValue('aws.key')) { $options['key'] = Kwf_Config::getValue('aws.key'); } if (!isset($options['secret']) && Kwf_Config::getValue('aws.secret')) { $options['secret'] = Kwf_Config::getValue('aws.secret'); } parent::__construct($options); }
public function xGetMetricAction() { $amazonCloudWatch = AmazonCloudWatch::GetInstance($this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $this->getParam('region')); $res = $amazonCloudWatch->GetMetricStatistics($this->getParam('metricName'), strtotime($this->getParam('startTime')), strtotime($this->getParam('endTime')), array($this->getParam('type')), null, $this->getParam('period'), $this->getParam('namespace'), array($this->getParam('dValue') => $this->getParam('dType'))); $store = array(); ksort($res); foreach ($res as $time => $val) { if ($time != 'unit') { if ($this->getParam('Unit') == "MBytes" || $this->getParam('Unit') == "MBytes/Second") { $store[] = array('time' => date($this->getParam('dateFormat'), $time), 'value' => (double) round($val[$this->getParam('type')] / 1024, 2)); } else { if ($this->getParam('Unit') == "GBytes" || $this->getParam('Unit') == "GBytes/Second") { $store[] = array('time' => date($this->getParam('dateFormat'), $time), 'value' => (double) round($val[$this->getParam('type')] / 1024 / 1024, 2)); } else { $store[] = array('time' => date($this->getParam('dateFormat'), $time), 'value' => (double) round($val[$this->getParam('type')], 2)); } } } } $this->response->data(array('data' => $store)); }
/** * * @param string $AWSAccessKeyId * @param string $AWSAccessKey * @return AmazonCloudWatch */ public static function GetInstance($AWSAccessKeyId, $AWSAccessKey, $Region) { self::$Instance = new AmazonCloudWatch($AWSAccessKeyId, $AWSAccessKey, $Region); return self::$Instance; }
$endDate = $endDate_DT->format('Y-m-d'); // Get parameters $period = isset($_GET['period']) ? $_GET['period'] : 15; $start = isset($_GET['start']) ? $_GET['start'] : $startDate; $end = isset($_GET['end']) ? $_GET['end'] : $endDate; // Adjust parameters as needed $period *= 60; if ($debug < 1) { print "period = {$period}, start= {$start}, end={$end}\n"; exit(0); } // Create array of chart parameters, one interior array per chart $charts = array(array('M' => 'NetworkIn', 'U' => 'Bytes', 'L' => 'Network In (Bytes)'), array('M' => 'NetworkOut', 'U' => 'Bytes', 'L' => 'Network Out (Bytes)'), array('M' => 'CPUUtilization', 'U' => 'Percent', 'L' => 'CPU Utilization (Percent)'), array('M' => 'DiskReadBytes', 'U' => 'Bytes', 'L' => 'Disk Read Bytes'), array('M' => 'DiskReadOps', 'U' => 'Count', 'L' => 'Disk Read Operations/Second'), array('M' => 'DiskWriteBytes', 'U' => 'Bytes', 'L' => 'Disk Write Bytes'), array('M' => 'DiskWriteOps', 'U' => 'Count', 'L' => 'Disk Write Operations/Second')); // Create the CloudWatch access object // $cw = new AmazonCloudWatch(null, null, 'ap-southeast-1.monitoring.amazonaws.com'); $cw = new AmazonCloudWatch(); // $cw->set_region(REGION_US_E1); // Prepare to get metrics $opt = array('Namespace' => 'AWS/EC2', 'Period' => $period); $statistics = array('Average', 'Minimum', 'Maximum', 'Sum'); $chartImages = array(); // Generate one chart for each member of $charts foreach ($charts as &$chart) { $measure = $chart['M']; $unit = $chart['U']; $label = $chart['L']; // Get the metrics $res = $cw->get_metric_statistics($opt['Namespace'], $measure, $start, $end, $opt['Period'], $statistics, $unit); if ($debug < 2) { print_r($res); exit(0);
// Set default date values $startDate_DT = new DateTime('now'); $endDate_DT = new DateTime('now'); $startDate_DT->modify('-1 day'); $startDate = $startDate_DT->format('Y-m-d'); $endDate = $endDate_DT->format('Y-m-d'); // Get parameters $period = isset($_GET['period']) ? $_GET['period'] : 15; $start = isset($_GET['start']) ? $_GET['start'] : $startDate; $end = isset($_GET['end']) ? $_GET['end'] : $endDate; // Adjust parameters as needed $period *= 60; // Create array of chart parameters, one interior array per chart $charts = array(array('M' => 'NetworkIn', 'U' => 'Bytes', 'L' => 'Network In (Bytes)'), array('M' => 'NetworkOut', 'U' => 'Bytes', 'L' => 'Network Out (Bytes)'), array('M' => 'CPUUtilization', 'U' => 'Percent', 'L' => 'CPU Utilization (Percent)'), array('M' => 'DiskReadBytes', 'U' => 'Bytes', 'L' => 'Disk Read Bytes'), array('M' => 'DiskReadOps', 'U' => 'Count', 'L' => 'Disk Read Operations/Second'), array('M' => 'DiskWriteBytes', 'U' => 'Bytes', 'L' => 'Disk Write Bytes'), array('M' => 'DiskWriteOps', 'U' => 'Count', 'L' => 'Disk Write Operations/Second')); // Create the CloudWatch access object $cw = new AmazonCloudWatch(null, null, 'ap-southeast-1.monitoring.amazonaws.com'); // Prepare to get metrics $opt = array('Namespace' => 'AWS/EC2', 'Period' => $period); $statistics = array('Average', 'Minimum', 'Maximum', 'Sum'); $chartImages = array(); // Generate one chart for each member of $charts foreach ($charts as &$chart) { $measure = $chart['M']; $unit = $chart['U']; $label = $chart['L']; // Get the metrics $res = $cw->get_metric_statistics($measure, $statistics, $unit, $start, $end, $opt); if ($res->isOK()) { $datapoints = $res->body->GetMetricStatisticsResult->Datapoints->member; // Populate an array with the metrics for sorting $dataRows = array();
* of the License is located at * * http://aws.amazon.com/apache2.0/ * * or in the "license.txt" file accompanying this file. This file is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS * OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the * License. * * Modified by Jeffrey S. Haemer <*****@*****.**> */ error_reporting(E_ALL); require_once 'AWSSDKforPHP/sdk.class.php'; // Create the CloudWatch access object $cw = new AmazonCloudWatch(); // Get the metrics $res = $cw->list_metrics(); if ($res->isOK()) { $metrics = $res->body->ListMetricsResult->Metrics->member; $metricsRows = array(); // Build array of available metrics foreach ($metrics as $metric) { $metricsRows[] = array('MetricName' => (string) $metric->MetricName, 'Namespace' => (string) $metric->Namespace, 'Name' => (string) $metric->Dimensions->member->Name, 'Value' => (string) $metric->Dimensions->member->Value); } // Sort the metrics usort($metricsRows, 'CmpMetrics'); // Display a header and then the metrics printf("%-16s %-20s %-16s %-16s\n", "Namespace", "Metric Name", "Name", "Value"); printf("%-16s %-20s %-16s %-16s\n", "=========", "============", "====", "====="); foreach ($metricsRows as $metricsRow) {
<?php require_once 'AWSSDKforPHP/sdk.class.php'; define('AWS_KEY', getenv('EC2_KEY_ID')); define('AWS_SECRET_KEY', getenv('EC2_SECRET_KEY')); define('AWS_ACCOUNT_ID', getenv('AWS_ACCOUNT_ID')); $m = new Mongo(); $cw = new AmazonCloudWatch(); $cw->set_region('monitoring.' . getenv('EC2_REGION') . '.amazonaws.com'); $db = $m->admin; $local = $m->selectDB("local"); $replset = $local->selectCollection("system.replset"); $replica_set_conf = $replset->findOne(); $ismaster = $db->command(array('ismaster' => true)); $server_status = $db->command(array('serverStatus' => true)); $replica_set_status = $db->command(array('replSetGetStatus' => true)); if (isset($server_status['repl']['arbiterOnly']) && $server_status['repl']['arbiterOnly']) { $state = 'arbiter'; } else { if ($server_status['repl']['ismaster']) { $state = 'primary'; } else { $state = 'secondary'; } } switch ($state) { case 'primary': # here we do the replica set metrics add_replica_set_metrics($cw, $ismaster, $server_status, $replica_set_status, $replica_set_conf); case 'secondary': # and the metrics for primary & secondary
<?php require_once '/usr/share/php/AWSSDKforPHP/sdk.class.php'; define('AWS_KEY', 'AKIAJKI7FFIP2BHMXF7A'); define('AWS_SECRET_KEY', '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'); define('AWS_ACCOUNT_ID', '382146031153'); $cw = new AmazonCloudWatch(); $cw->set_region($cw::REGION_EU_W1); $measure = $_GET['measure']; $statistics = $_GET['statistics']; $unit = $_GET['unit']; $dimensions = explode('=', urldecode($_GET['dimensions'])); $namespace = $_GET['namespace']; $period = $_GET['period']; $threshold = (int) $_GET['threshold']; if (!isset($measure) || !isset($statistics) || !isset($unit) || !isset($dimensions) || !isset($namespace) || !isset($period) || !isset($threshold)) { exit("Usage: http://localhost/cloudwatch/breach.php?measure=CPUUtilization&statistics=Average&unit=Percent&dimensions=ImageId%3dami-30360344&namespace=EC2&period=900&threshold=20"); } $now = time(); $before = $now - (int) $period; $opt = array('Namespace' => 'AWS/' . $namespace, 'Period' => (int) $period); $opt = array_merge($opt, CFComplexType::map(array('Dimensions' => array('member' => array('Name' => $dimensions[0], 'Value' => $dimensions[1]))))); $response = $cw->get_metric_statistics($measure, $statistics, $unit, date("c", $before), date("c", $now), $opt); $measurement = $response->body->member(0)->{$statistics}(0); $breach = $measurement >= $threshold; echo $breach ? 1 : 0;
#!/usr/bin/php # This is pulled right out of the on-line documentation # http://docs.amazonwebservices.com/AWSSDKforPHP/latest/#m=AmazonCloudWatch/get_metric_statistics # <?php error_reporting(E_ALL); require_once 'AWSSDKforPHP/sdk.class.php'; // Get metrics $cw = new AmazonCloudWatch(); $opt = array(); $response = $cw->get_metric_statistics('AWS/EC2', 'CPUUtilization', '17 December 2010', '20 December 2010', 1800, 'Average', 'Percent'); // Success? var_dump($response->isOK()); print_r($response);