fustundag/awscw-custom-metrics

将自定义指标数据发送到AWS CloudWatch(PHP版)

2.1.3 2017-09-10 10:23 UTC

This package is auto-updated.

Last update: 2024-08-28 16:48:47 UTC


README

您可以发送自定义指标到AWS CloudWatch,例如磁盘/内存使用情况

特性

  • 将自定义指标发送到AWS CloudWatch
  • 通过指标插件添加您需要的新的指标
  • 为每个指标插件配置不同的cron时间

要求

  • PHP 5.5+
  • aws/aws-sdk-php
  • mtdowling/cron-expression

使用方法

基本使用方法

  • 创建一个php文件,例如awscw-agent.php
<?php

use AWSCustomMetric\DI;
use AWSCustomMetric\Logger\DefaultLogger;
use AWSCustomMetric\Sender as CWSender;
use AWSCustomMetric\CommandRunner;

try {
$diObj = new DI();
$diObj->setCommandRunner(new CommandRunner());
//Optional
//$diObj->setLogger(new DefaultLogger());

// Create the Sender
$cwSender = new CWSender("AWS_KEY", "AWS_SECRET", "AWS_REGION", new CommandRunner());
$cwSender->setNamespace('Custom/System');
$cwSender->addPlugin([
    new DiskUsage($diObj),
    new MemoryUsage($diObj)
]);
$cwSender->run();
} catch (\Exception $e) {
    //Error handling
}

// ...
  • 添加到cron中
*/10 * * * * /path/to/php /path/to/awscw-agent.php

自动发现InstanceId

对于AWS EC2实例,可以从系统获取一些元数据,如instance-id

/usr/bin/wget -q -O - http://169.254.169.254/latest/meta-data/instance-id

在创建Sender对象时,如果您没有提供instance-id参数,类将尝试使用上述命令找到instance-id。

指标插件的cron

每个指标插件都可以配置在指定的时间运行。时间可以定义为crontab格式。更多信息:[https://github.com/mtdowling/cron-expression](https://github.com/mtdowling/cron-expression)

<?php

use AWSCustomMetric\DI;
use AWSCustomMetric\Logger\DefaultLogger;
use AWSCustomMetric\Sender as CWSender;
use AWSCustomMetric\CommandRunner;

try {
$diObj = new DI();
$diObj->setCommandRunner(new CommandRunner());
//Optional
//$diObj->setLogger(new DefaultLogger());

//metric will be sent at every sender->run calls
$diskPlugin = new DiskUsage($diObj, 'Appname/System', '* * * * *');

//metric will be sent at every hour
$memoryPlugin = new MemoryUsage($diObj, 'Appname/System', '0 * * * *');

// Create the Sender
$cwSender = new CWSender("AWS_KEY", "AWS_SECRET", "AWS_REGION", new CommandRunner());
$cwSender->setNamespace('Custom/System');
$cwSender->addPlugin([$diskPlugin, $memoryPlugin]);
$cwSender->run();
} catch (\Exception $e) {
    //Error handling
}

// ...

安装

您可以使用Composer来安装

composer require fustundag/awscw-custom-metrics

待办事项

  • 未测试。 86%覆盖率 100%覆盖率
  • 更多插件。

贡献

您可以通过fork仓库并创建pull请求来贡献。您还可以创建问题或功能请求。

免责声明

您的AWS CloudWatch使用可能需要付费。请检查CloudWatch定价页面:[https://aws.amazon.com/cloudwatch/pricing/](https://aws.amazon.com/cloudwatch/pricing/)

许可证

此项目采用MIT许可证。LICENSE文件可以在本仓库中找到。