Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CloudWatch.php « Aws « TestRunner « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 334d80442e6a4b7c01c82c93f1a114dd09f8ab2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\TestRunner\Aws;

use Aws\CloudWatch\CloudWatchClient;
use Aws\CloudWatch\Enum\ComparisonOperator;
use Aws\CloudWatch\Enum\Statistic;
use Aws\CloudWatch\Enum\Unit;

class CloudWatch
{
    /**
     * @var Config
     */
    private $config;

    /**
     * @var CloudWatchClient
     */
    private $client;

    public function __construct(Config $awsConfig)
    {
        $this->config = $awsConfig;
        $this->client = $this->getCloudWatchClient();
    }

    public function hasAssignedAlarms($instanceIds)
    {
        $result = $this->client->describeAlarmsForMetric(array(
            'MetricName' => 'CPUUtilization',
            'Namespace'  => $this->getNamespace(),
            'Dimensions' => $this->getDimensions($instanceIds)
        ));

        $metricAlarms = $result->getPath('MetricAlarms');

        return !empty($metricAlarms);
    }

    public function terminateInstanceIfIdleForTooLong($instanceIds)
    {
        $this->client->putMetricAlarm(array(
            'AlarmName' => 'TerminateInstanceBecauseIdle',
            'AlarmDescription' => 'Terminate instances if CPU is on average < 10% for 5 minutes in a row 8 times consecutively',
            'ActionsEnabled' => true,
            'OKActions' => array(),
            'AlarmActions' => $this->getAlarmActions(),
            'InsufficientDataActions' => array(),
            'MetricName' => 'CPUUtilization',
            'Namespace' => $this->getNamespace(),
            'Statistic' => Statistic::AVERAGE,
            'Dimensions' => $this->getDimensions($instanceIds),
            'Period' => 300,
            'Unit' => Unit::PERCENT,
            'EvaluationPeriods' => 8,
            'Threshold' => 10,
            'ComparisonOperator' => ComparisonOperator::LESS_THAN_THRESHOLD,
        ));

        $this->client->putMetricAlarm(array(
            'AlarmName' => 'TerminateInstanceIfStatusCheckFails',
            'AlarmDescription' => 'Terminate instances in case two status check fail within one minute',
            'ActionsEnabled' => true,
            'OKActions' => array(),
            'AlarmActions' => $this->getAlarmActions(),
            'InsufficientDataActions' => array(),
            'MetricName' => 'StatusCheckFailed',
            'Namespace' => $this->getNamespace(),
            'Statistic' => Statistic::AVERAGE,
            'Dimensions' => $this->getDimensions($instanceIds),
            'Period' => 60,
            'Unit' => Unit::PERCENT,
            'EvaluationPeriods' => 2,
            'Threshold' => 1,
            'ComparisonOperator' => ComparisonOperator::GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
        ));
    }

    private function getCloudWatchClient()
    {
        return CloudWatchClient::factory($this->getConnectionOptions());
    }

    private function getConnectionOptions()
    {
        return array(
            'key'    => $this->config->getAccessKey(),
            'secret' => $this->config->getSecretKey(),
            'region' => $this->config->getRegion()
        );
    }

    private function getDimensions($instanceIds)
    {
        $dimensions = array();

        foreach ($instanceIds as $instanceId) {
            $dimensions[] = array(
                'Name'  => 'InstanceId',
                'Value' => $instanceId,
            );
        }

        return $dimensions;
    }

    private function getNamespace()
    {
        return 'AWS/EC2';
    }

    private function getAlarmActions()
    {
        return array(
            'arn:aws:automate:' . $this->config->getRegion() . ':ec2:terminate',
            'arn:aws:sns:' . $this->config->getRegion() . ':682510200394:TerminateInstanceBecauseIdle'
        );
    }

}