You can send custom metrics to AWS CloudWatch like disk/memory usage
- Send custom metrics to AWS CloudWatch
- Add new metrics what you need by metric plugins
- Configure different cron time for each metric plugin
- PHP 5.5+
- aws/aws-sdk-php
- mtdowling/cron-expression
- Create php file like 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
}
// ...
- Add to cron like :
*/10 * * * * /path/to/php /path/to/awscw-agent.php
For AWS EC2 instances, some meta-data can be obtained from system like instance-id :
/usr/bin/wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
While creating Sender object, if you dont give instance-id param, class tries to find instance-id using above cmd.
Each metric plugin can be configured to run at specified time. Time can be defined at crontab format. For more info: 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
}
// ...
You can use Composer to install :
composer require fustundag/awscw-custom-metrics
NOT TESTED.86% Coverage100% Coverage- MORE PLUGINS.
You can contribute by forking the repo and creating pull requests. You can also create issues or feature requests.
Your AWS CloudWatch usage my be charged. Please check CloudWatch pricing page : https://aws.amazon.com/cloudwatch/pricing/
This project is licensed under the MIT license. LICENSE
file can be found in this repository.