-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathalarmes.php
63 lines (54 loc) · 2.34 KB
/
alarmes.php
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
<?php
define('APPLICATION_PATH', realpath(dirname(__FILE__)));
include APPLICATION_PATH . '/vendor/autoload.php';
require_once 'lib.php';
use Aws\CloudWatch\CloudWatchClient;
// Load config file.
$conf = getConfigFile();
if ($conf === false) {
echo "Conf file is not valid";
die();
}
// Store metric by namespace in order to call "AWS Could Watch" one time per namespace
$metricsToPush = array();
// Get Instance Id
if ($conf->aws->instance) {
$instanceId = $conf->aws->instance;
} else {
$instanceId = file_get_contents("http://169.254.169.254/latest/meta-data/instance-id");
}
$client = getCloudWatchClient($conf);
foreach ($conf->metrics as $metrics) {
foreach ($metrics as $metricName => $metric) {
$pluginName = isset($metric->{'plugin'}) === true ? $metric->{'plugin'} : $metricName;
$className = "CloudWatchScript\\Plugins\\" . $pluginName . "Monitoring";
$period = isset($metric->period) === true ? $metric->period : 300;
$action = isset($metric->action) === true ? $metric->action : $conf->alarms->action;
$metricController = new $className($metric, $metric->name);
foreach ($metricController->getAlarms() as $key => $alarm) {
$client->putMetricAlarm(array(
'AlarmName' => $alarm["Name"],
'AlarmDescription' => $metric->description,
'ActionsEnabled' => true,
'OKActions' => array($action),
'AlarmActions' => array($action),
'InsufficientDataActions' => array($action),
'Dimensions' => array(
array('Name' => 'InstanceId', 'Value' => $instanceId),
array('Name' => 'Metrics', 'Value' => $metricName)
),
'MetricName' => $metricController->getMetricName($alarm["Name"]),
'Namespace' => $metric->namespace,
'Statistic' => 'Average',
'Period' => $period,
'Unit' => $metricController->getUnit($alarm["Name"]),
// EvaluationPeriods is required
'EvaluationPeriods' => 2,
// Threshold is required
'Threshold' => $alarm["Threshold"],
// ComparisonOperator is required
'ComparisonOperator' => $alarm["ComparisonOperator"]
));
}
}
}