-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.php
157 lines (144 loc) · 5.43 KB
/
client.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
require 'vendor/autoload.php';
use Aws\CloudWatch\CloudWatchClient;
$key = "your_access_key";
$secret = "your_access_secret";
$region = "us-west-2";
$version = "latest";
$interval = 15;
// Users/rajamani/Downloads/devserver/bill/vendor/aws/aws-sdk-php/src
// Use the us-west-2 region and latest version of each client.
$sharedConfig = [
'region' => $region,
'version' => $version,
'credentials' => array(
'key' => $key,
'secret' => $secret,
),
'key' => $key,
'secret' => $secret,
];
// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);
$client = $sdk->createCloudWatch();
function grabber($client, $tablename, $metric) {
$output = array();
date_default_timezone_set('America/Los_Angeles');
$results = $client->getMetricStatistics(array(
'Namespace' => 'AWS/ECS',
'MetricName' => $metric,
'Dimensions' => array(
array(
'Name' => 'TableName',
'Value' => $tablename,
),
),
'StartTime' => strtotime('-1 days'), //'-'.$interval.' minutes'),
'EndTime' => strtotime('now'),
'Period' => 300,
'Statistics' => array('Minimum', 'Maximum', 'Average', 'Sum'),
));
//echo 'GRABBER='.serialize($results);
print "-------------------------------------------\n";
print " $metric\n";
print "-------------------------------------------\n";
$output = array('key'=>$metric, 'min'=>1, 'max'=>5, 'avg'=>2, 'sum'=>2, 'cumulate'=>true, 'units'=>'count');
/*
foreach ($results as $result){
//echo 'RESULT='.serialize($result);
if (is_array($result) && array_key_exists('Datapoints', $result)){
foreach ( $result['Datapoints'] as $item ) {
$min = $item['Minimum'];
$max = $item['Maximum'];
$avg = $item['Average'];
$sum = $item['Sum'];
$time = $item['Timestamp'];
//print "$metric -- min $min, max $max, avg $avg, sum $sum\n";
array_push($output, array('key'=>$metric, 'min'=>$min, 'max'=>$max, 'avg'=>$avg, 'sum'=>$sum, 'cumulate'=>true, 'units'=>'count'));
// echo 'OUTPUT='.serialize($output);
}
}
}
*/
//echo 'GRABBER='.serialize($output);
return $output;
}
function grab_stats($client, $tablename) {
print "\n==========================================================\n";
print " $tablename\n";
print "==========================================================\n";
$output = array();
$result = $client->listMetrics(array(
));
$result = (array)$result;
foreach ($result as $item){
foreach ($item as $metrics){
foreach ($metrics as $metric){
//echo 'METRIC='.serialize($metric);
if (is_array($metric) &&
array_key_exists('MetricName', $metric) &&
array_key_exists('Dimensions', $metric)){
//echo $metric['MetricName'] . ' - '
// . $metric['Dimensions'][0]['Name'] . ' - '
// . $metric['Dimensions'][0]['Value'] . "\n";
$output[$metric['MetricName']] = grabber($client, $tablename, $metric['MetricName']);
}
}
}
}
//echo 'grab_STATS='.serialize($output);
return $output;
//$cpuOutput = grabber($client, $tablename, 'CPUCreditBalance'); // per instance
//$volOutput = grabber($client, $tablename, 'VolumeWriteOps'); // per volume
//return array('cpu'=>$cpuOutput, 'vol'=>$volOutput);
}
function addCurrentToCumulative($current, $cumulative){
if (!is_array($cumulative)) return array();
//echo 'CURRENT='.serialize($current);
// echo 'CUMULATIVE='.serialize($cumulative)."\n";
foreach ( $current as $metric ){
//echo 'CUMMETRIC='.serialize($metric)."\n";
if (is_array($cumulative) && array_key_exists($metric['key'], $cumulative) == false){
$cumulative[$metric['key']] = array('key'=>$metric[key], 'value' => 0, 'units' => $metric['count']);
}
if (is_array($cumulative) && array_key_exists('cumulate', $metric) && $metric['cumulate']){
if (array_key_exists($metric['key'], $cumulative)){
//echo 'adding '.$metric['key'].' values old:'.$cumulative[$metric['key']]['value'].' and new:'.$metric['sum'];
$cumulative[$metric['key']]['value'] += $metric['sum'];
}else{
//echo 'adding '.$metric['key'].' values old:'.$cumulative[$metric['key']]['value'].' and new reset:'.$metric['sum'];
$cumulative[$metric['key']]['value'] = $metric['sum'];
}
}else{
$cumulative[$metric['key']]['value'] = $metric['avg'];
}
}
return $cumulative;
}
function saveOrPrintCumulative($cumulative){
print "\n==========================================================\n";
print " Aggregated Metrics\n";
print "==========================================================\n";
//echo 'CUMULATIVE='.serialize($cumulative);
foreach($cumulative as $item){
$key = $item['key'];
$value = $item['value'];
$units = $item['units'];
print "Metric -- $key $value $units \n";
}
}
// after every $interval minutes
// cumulate by adding current to total
// update current with grab_stats
$tablename = "Metrics";
$current = grab_stats($client, $tablename); //array();
$cumulative = array();
$cumulative = addCurrentToCumulative($current, $cumulative);
echo 'CUMULATIVE='.serialize($cumulative)."\n";
$current = grab_stats($client, $tablename);
//Sleep($interval*60); // seconds
$cumulative = addCurrentToCumulative($current, $cumulative);
echo 'CUMULATIVE='.serialize($cumulative)."\n";
saveOrPrintCumulative($cumulative);
//$current = grab_stats($client, $tablename);
?>