76.1.1
by Michael Hope
Add tools to watch the EC2 running instances on a cronjob and |
1 |
"""Takes the ec2din log files from ec2watch.sh and summarises them
|
2 |
based on key and instance type.
|
|
3 |
"""
|
|
4 |
||
5 |
import sys |
|
6 |
import re |
|
7 |
import collections |
|
8 |
import datetime |
|
9 |
||
10 |
Entry = collections.namedtuple('Entry', 'stamp id key type') |
|
11 |
||
12 |
def main(): |
|
13 |
instances = {} |
|
14 |
starts = {} |
|
15 |
# Sample step in hours. Used to catch the missing final half
|
|
16 |
# hour.
|
|
17 |
step = 0.5 |
|
18 |
||
19 |
for arg in sys.argv[1:]: |
|
20 |
# Split the date and time out of the filename
|
|
21 |
match = re.match(r'.+(20\d+)-(\d+)-(\d+)-(\d+)-(\d+)', arg) |
|
22 |
parts = [int(x) for x in match.groups()] |
|
23 |
stamp = datetime.datetime(*parts) |
|
24 |
||
25 |
with open(arg) as f: |
|
26 |
# Pull out all of the instance lines
|
|
27 |
rows = [x.split() for x in f if x.startswith('INSTANCE')] |
|
28 |
entries = [Entry(stamp, x[1], x[6], x[9]) for x in rows] |
|
29 |
||
30 |
for entry in entries: |
|
31 |
if entry.id not in starts: |
|
32 |
starts[entry.id] = stamp |
|
33 |
||
34 |
instances[entry.id] = entry |
|
35 |
||
36 |
# Make a list of all instances to start time
|
|
37 |
s = [(x, y) for x, y in starts.items()] |
|
38 |
s.sort(key=lambda x: x[1]) |
|
39 |
||
40 |
bins = {} |
|
41 |
||
42 |
for id, start in s: |
|
43 |
# Pull out the final record. Should be the last seen
|
|
44 |
v = instances[id] |
|
45 |
runtime = (v.stamp-starts[v.id]).total_seconds()/3600 |
|
46 |
runtime += step |
|
47 |
||
48 |
row = start, v.id, v.key, v.type, '%.1f' % runtime |
|
49 |
# Print this entry
|
|
50 |
print '\t'.join('%s' % x for x in row) |
|
51 |
||
52 |
# Bin them based on user and instance type
|
|
53 |
key = '%s %s' % (v.key, v.type) |
|
54 |
bins[key] = bins.get(key, 0) + runtime |
|
55 |
||
56 |
for key in sorted(bins): |
|
57 |
print '%s\t%.1f' % (key, bins[key]) |
|
58 |
||
59 |
if __name__ == '__main__': |
|
60 |
main() |