亚马逊获取订单aws PHP,从 Amazon CloudWatch 中获取指标与 AWS SDK for PHP 版本 3 - 适用于 PHP 的 AWS 开发工具包...

这篇博客介绍了如何利用AWS SDK for PHP v3从Amazon CloudWatch获取系统性能指标,包括列举指标、描述指标警报以及获取指标统计数据。示例代码展示了如何监控Amazon EC2实例和自定义应用程序的详细性能数据。
摘要由CSDN通过智能技术生成

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

从 Amazon CloudWatch 中获取指标与 AWS SDK for PHP 版本 3

指标是关于您的系统性能的数据。您可以启用对某些资源 (例如 Amazon EC2 实例) 或您自己的应用程序指标的详细监控。

以下示例演示如何:

版本 3 的所有示例代码在 AWS SDK for PHP 上的此处提供。GitHub

Credentials

列出指标

导入

require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;

use Aws\Exception\AwsException;

示例代码

function listMetrics($cloudWatchClient)

{

try {

$result = $cloudWatchClient->listMetrics();

$message = '';

if (isset($result['@metadata']['effectiveUri']))

{

$message .= 'For the effective URI at ' .

$result['@metadata']['effectiveUri'] . ":\n\n";

if ((isset($result['Metrics'])) and

(count($result['Metrics']) > 0))

{

$message .= "Metrics found:\n\n";

foreach($result['Metrics'] as $metric)

{

$message .= 'For metric ' . $metric['MetricName'] .

' in namespace ' . $metric['Namespace'] . ":\n";

if ((isset($metric['Dimensions'])) and

(count($metric['Dimensions']) > 0))

{

$message .= "Dimensions:\n";

foreach ($metric['Dimensions'] as $dimension)

{

$message .= 'Name: ' . $dimension['Name'] .

', Value: ' . $dimension['Value'] . "\n";

}

$message .= "\n";

} else {

$message .= "No dimensions.\n\n";

}

}

} else {

$message .= 'No metrics found.';

}

} else {

$message .= 'No metrics found.';

}

return $message;

} catch (AwsException $e) {

return 'Error: ' . $e->getAwsErrorMessage();

}

}

function listTheMetrics()

{

$cloudWatchClient = new CloudWatchClient([

'profile' => 'default',

'region' => 'us-east-1',

'version' => '2010-08-01'

]);

echo listMetrics($cloudWatchClient);

}

// Uncomment the following line to run this code in an AWS account.

// listTheMetrics();

检索指标的警报

导入

require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;

use Aws\Exception\AwsException;

示例代码

function describeAlarmsForMetric($cloudWatchClient, $metricName,

$namespace, $dimensions)

{

try {

$result = $cloudWatchClient->describeAlarmsForMetric([

'MetricName' => $metricName,

'Namespace' => $namespace,

'Dimensions' => $dimensions

]);

$message = '';

if (isset($result['@metadata']['effectiveUri']))

{

$message .= 'At the effective URI of ' .

$result['@metadata']['effectiveUri'] . ":\n\n";

if ((isset($result['MetricAlarms'])) and

(count($result['MetricAlarms']) > 0))

{

$message .= 'Matching alarms for ' . $metricName . ":\n\n";

foreach ($result['MetricAlarms'] as $alarm)

{

$message .= $alarm['AlarmName'] . "\n";

}

} else {

$message .= 'No matching alarms found for ' . $metricName . '.';

}

} else {

$message .= 'No matching alarms found for ' . $metricName . '.';

}

return $message;

} catch (AwsException $e) {

return 'Error: ' . $e->getAwsErrorMessage();

}

}

function describeTheAlarmsForMetric()

{

$metricName = 'BucketSizeBytes';

$namespace = 'AWS/S3';

$dimensions = [

[

'Name' => 'StorageType',

'Value'=> 'StandardStorage'

],

[

'Name' => 'BucketName',

'Value' => 'my-bucket'

]

];

$cloudWatchClient = new CloudWatchClient([

'profile' => 'default',

'region' => 'us-east-1',

'version' => '2010-08-01'

]);

echo describeAlarmsForMetric($cloudWatchClient, $metricName,

$namespace, $dimensions);

}

// Uncomment the following line to run this code in an AWS account.

// describeTheAlarmsForMetric();

获取指标统计数据

导入

require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;

use Aws\Exception\AwsException;

示例代码

function getMetricStatistics($cloudWatchClient, $namespace, $metricName,

$dimensions, $startTime, $endTime, $period, $statistics, $unit)

{

try {

$result = $cloudWatchClient->getMetricStatistics([

'Namespace' => $namespace,

'MetricName' => $metricName,

'Dimensions' => $dimensions,

'StartTime' => $startTime,

'EndTime' => $endTime,

'Period' => $period,

'Statistics' => $statistics,

'Unit' => $unit

]);

$message = '';

if (isset($result['@metadata']['effectiveUri']))

{

$message .= 'For the effective URI at ' .

$result['@metadata']['effectiveUri'] . "\n\n";

if ((isset($result['Datapoints'])) and

(count($result['Datapoints']) > 0))

{

$message .= "Datapoints found:\n\n";

foreach($result['Datapoints'] as $datapoint)

{

foreach ($datapoint as $key => $value)

{

$message .= $key . ' = ' . $value . "\n";

}

$message .= "\n";

}

} else {

$message .= 'No datapoints found.';

}

} else {

$message .= 'No datapoints found.';

}

return $message;

} catch (AwsException $e) {

return 'Error: ' . $e->getAwsErrorMessage();

}

}

function getTheMetricStatistics()

{

// Average number of Amazon EC2 vCPUs every 5 minutes within

// the past 3 hours.

$namespace = 'AWS/Usage';

$metricName = 'ResourceCount';

$dimensions = [

[

'Name' => 'Service',

'Value' => 'EC2'

],

[

'Name' => 'Resource',

'Value'=> 'vCPU'

],

[

'Name' => 'Type',

'Value' => 'Resource'

],

[

'Name' => 'Class',

'Value'=> 'Standard/OnDemand'

]

];

$startTime = strtotime('-3 hours');

$endTime = strtotime('now');

$period = 300; // Seconds. (5 minutes = 300 seconds.)

$statistics = array('Average');

$unit = 'None';

$cloudWatchClient = new CloudWatchClient([

'profile' => 'default',

'region' => 'us-east-1',

'version' => '2010-08-01'

]);

echo getMetricStatistics($cloudWatchClient, $namespace, $metricName,

$dimensions, $startTime, $endTime, $period, $statistics, $unit);

// Another example: average number of bytes of standard storage in the

// specified Amazon S3 bucket each day for the past 3 days.

/*

$namespace = 'AWS/S3';

$metricName = 'BucketSizeBytes';

$dimensions = [

[

'Name' => 'StorageType',

'Value'=> 'StandardStorage'

],

[

'Name' => 'BucketName',

'Value' => 'my-bucket'

]

];

$startTime = strtotime('-3 days');

$endTime = strtotime('now');

$period = 86400; // Seconds. (1 day = 86400 seconds.)

$statistics = array('Average');

$unit = 'Bytes';

$cloudWatchClient = new CloudWatchClient([

'profile' => 'default',

'region' => 'us-east-1',

'version' => '2010-08-01'

]);

echo getMetricStatistics($cloudWatchClient, $namespace, $metricName,

$dimensions, $startTime, $endTime, $period, $statistics, $unit);

*/

}

// Uncomment the following line to run this code in an AWS account.

// getTheMetricStatistics();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值