aws php环境变量,在 Amazon CloudWatch 中 AWS SDK for PHP 发布自定义指标与 版本 3 - 适用于 PHP 的 AWS 开发工具包...

AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门。

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

在 Amazon CloudWatch 中 AWS SDK for PHP 发布自定义指标与 版本 3

指标是关于您的系统性能的数据。 警报会在您指定的时间段内监控一个指标。它在多个时间段内根据相对于给定阈值的指标值,执行一项或多项操作。

以下示例演示如何:

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

Credentials

发布指标数据

导入

require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;

use Aws\Exception\AwsException;

示例代码

function putMetricData($cloudWatchClient, $cloudWatchRegion, $namespace,

$metricData)

{

try {

$result = $cloudWatchClient->putMetricData([

'Namespace' => $namespace,

'MetricData' => $metricData

]);

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

{

if ($result['@metadata']['effectiveUri'] ==

'https://monitoring.' . $cloudWatchRegion . '.amazonaws.com')

{

return 'Successfully published datapoint(s).';

} else {

return 'Could not publish datapoint(s).';

}

} else {

return 'Error: Could not publish datapoint(s).';

}

} catch (AwsException $e) {

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

}

}

function putTheMetricData()

{

$namespace = 'MyNamespace';

$metricData = [

[

'MetricName' => 'MyMetric',

'Timestamp' => 1589228818, // 11 May 2020, 20:26:58 UTC.

'Dimensions' => [

[

'Name' => 'MyDimension1',

'Value' => 'MyValue1'

],

[

'Name' => 'MyDimension2',

'Value' => 'MyValue2'

]

],

'Unit' => 'Count',

'Value' => 1

]

];

$cloudWatchRegion = 'us-east-1';

$cloudWatchClient = new CloudWatchClient([

'profile' => 'default',

'region' => $cloudWatchRegion,

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

]);

echo putMetricData($cloudWatchClient, $cloudWatchRegion, $namespace,

$metricData);

}

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

// putTheMetricData();

创建警报

导入

require 'vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient;

use Aws\Exception\AwsException;

示例代码

function putMetricAlarm($cloudWatchClient, $cloudWatchRegion,

$alarmName, $namespace, $metricName,

$dimensions, $statistic, $period, $comparison, $threshold,

$evaluationPeriods)

{

try {

$result = $cloudWatchClient->putMetricAlarm([

'AlarmName' => $alarmName,

'Namespace' => $namespace,

'MetricName' => $metricName,

'Dimensions' => $dimensions,

'Statistic' => $statistic,

'Period' => $period,

'ComparisonOperator' => $comparison,

'Threshold' => $threshold,

'EvaluationPeriods' => $evaluationPeriods

]);

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

{

if ($result['@metadata']['effectiveUri'] ==

'https://monitoring.' . $cloudWatchRegion . '.amazonaws.com')

{

return 'Successfully created or updated specified alarm.';

} else {

return 'Could not create or update specified alarm.';

}

} else {

return 'Could not create or update specified alarm.';

}

} catch (AwsException $e) {

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

}

}

function putTheMetricAlarm()

{

$alarmName = 'my-ec2-resources';

$namespace = 'AWS/Usage';

$metricName = 'ResourceCount';

$dimensions = [

[

'Name' => 'Type',

'Value' => 'Resource'

],

[

'Name' => 'Resource',

'Value' => 'vCPU'

],

[

'Name' => 'Service',

'Value' => 'EC2'

],

[

'Name' => 'Class',

'Value' => 'Standard/OnDemand'

]

];

$statistic = 'Average';

$period = 300;

$comparison = 'GreaterThanThreshold';

$threshold = 1;

$evaluationPeriods = 1;

$cloudWatchRegion = 'us-east-1';

$cloudWatchClient = new CloudWatchClient([

'profile' => 'default',

'region' => $cloudWatchRegion,

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

]);

echo putMetricAlarm($cloudWatchClient, $cloudWatchRegion,

$alarmName, $namespace, $metricName,

$dimensions, $statistic, $period, $comparison, $threshold,

$evaluationPeriods);

}

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

// putTheMetricAlarm();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AWS CloudWatch是一项监控和管理AWS资源和应用程序的服务。它可以收集来自AWS资源和应用程序的指标和日志数据,以及监控AWS资源的状态和性能。以下是AWS CloudWatch的详细说明: 1. 监控:CloudWatch可以监控AWS资源(例如EC2实例,RDS数据库实例等)的状态和性能,并生成指标数据。这些指标数据可以用来观察系统运行状况,识别和解决问题。 2. 日志:CloudWatch Logs可以收集、监控和存储应用程序和系统日志。用户可以使用CloudWatch Logs来分析日志数据,查找故障和安全事件,以及生成指标数据。 3. 仪表板:用户可以使用CloudWatch仪表板创建自定义可视化仪表板,以查看指标数据和日志数据。用户可以将多个指标数据和日志数据放在同一个仪表板上,以便更轻松地监控系统状态和性能。 4. 报警:用户可以使用CloudWatch报警创建自定义报警,以在系统状态或性能出现异常时接收通知。用户可以定义报警触发条件,并选择通知方式(例如电子邮件、SMS等)。 5. 事件:CloudWatch Events可以监视AWS资源的状态和性能,并在特定事件发生时触发自定义操作。例如,用户可以配置CloudWatch Events在EC2实例启动时自动运行脚本。 总之,AWS CloudWatchAWS资源和应用程序的全面监控和管理服务,它可以收集、监控和存储指标数据和日志数据,并帮助用户识别和解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值