-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmetrics.php
74 lines (63 loc) · 2.42 KB
/
metrics.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
64
65
66
67
68
69
70
71
72
73
74
<?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");
}
foreach ($conf->metrics as $metrics) {
foreach ($metrics as $metricName => $metric) {
$pluginName = isset($metric->{'plugin'}) === true ? $metric->{'plugin'} : $metricName;
$className = "CloudWatchScript\\Plugins\\" . $pluginName . "Monitoring";
$metricController = new $className($metric, $metricName);
if (!array_key_exists($metric->namespace, $metricsToPush)) {
$metricsToPush[$metric->namespace] = array();
}
$metrics = $metricController->getMetric();
if (is_array($metrics)) {
$units = $metricController->getUnit();
foreach ($metrics as $metricId => $value) {
$metricsToPush[$metric->namespace][] = array(
'MetricName' => $metric->name . " " . $metricId,
'Timestamp' => time(),
'Value' => $value,
'Unit' => $units[$metricId],
'Dimensions' => array(
array('Name' => 'InstanceId', 'Value' => $instanceId),
array('Name' => 'Metrics', 'Value' => $metricName)
)
);
}
} else {
$metricsToPush[$metric->namespace][] = array(
'MetricName' => $metric->name,
'Timestamp' => time(),
'Value' => $metrics,
'Unit' => $metricController->getUnit(),
'Dimensions' => array(
array('Name' => 'InstanceId', 'Value' => $instanceId),
array('Name' => 'Metrics', 'Value' => $metricName)
)
);
}
}
}
$client = getCloudWatchClient($conf);
foreach ($metricsToPush as $namespace => $metricData) {
$client->putMetricData(array(
'Namespace' => $namespace,
'MetricData' => $metricData
));
}