martinberlin / cale-integrations

CALE configurator to add APIs to your epaper screens - PHP/Symfony 4.4
https://cale.es
MIT License
6 stars 0 forks source link

99/99 Amazon Cloudwatch monitoring #15

Closed martinberlin closed 4 years ago

martinberlin commented 4 years ago

The intention of this Issue is to provide a way to bring AWS monitoring into our Screens.

The way is not yet 100% clear. After some research there are two ways to do it:

  1. To create a complex combination of Cloudformation -> Lambda -> API Gateway DISCARDED

  2. To retrieve metrics using GetMetricStatistics (AWS Sdk PHP) This has the benefit that we take the setup burden out from the user but increases the work in CALE side since we need to provide the charts. And the most important part here, not to be overlooked, is that I'm not sure if we can just render Javascript Charts. It all depends on how wkhtmltoimage takes it.

  3. Middle road: Use getMetricWidgetImage like this

    /**
     * @Route("/c", name="b_api_cloudwatch")
     */
    public function apiCloudwatch(CloudWatchClient $client) {

        try {
            $result = $client->getMetricWidgetImage([
                'MetricWidget' => '{
    "view": "timeSeries",
    "stacked": true,
    "metrics": [
        [ "AWS/EC2", "CPUUtilization", "InstanceId", "i-76ddb3b7" ],
        [ ".", "CPUCreditUsage", ".", "." ]
    ],
    "title": "CPU",
    "period": 300,
    "width": 400,
    "height": 200,
    "start": "-PT1H",
    "end": "P0D"}']);
        } catch (AwsException $e) {
            // output error message if fails
            error_log($e->getMessage());
        }

        if ($result instanceof \Aws\Result) {
            $image = $result->get('MetricWidgetImage');
            $response = new Response();
            $response->setContent($image);
            $response->headers->set('Content-Type', 'image/png');
            return $response;
        }
    }
martinberlin commented 4 years ago

Added and live. At the moment I'm the only one using it