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
|
"""Takes the ec2din log files from ec2watch.sh and summarises them
based on key and instance type.
"""
import sys
import re
import collections
import datetime
Entry = collections.namedtuple('Entry', 'stamp id key type')
def main():
instances = {}
starts = {}
# Sample step in hours. Used to catch the missing final half
# hour.
step = 0.5
for arg in sys.argv[1:]:
# Split the date and time out of the filename
match = re.match(r'.+(20\d+)-(\d+)-(\d+)-(\d+)-(\d+)', arg)
parts = [int(x) for x in match.groups()]
stamp = datetime.datetime(*parts)
with open(arg) as f:
# Pull out all of the instance lines
rows = [x.split() for x in f if x.startswith('INSTANCE')]
entries = [Entry(stamp, x[1], x[6], x[9]) for x in rows]
for entry in entries:
if entry.id not in starts:
starts[entry.id] = stamp
instances[entry.id] = entry
# Make a list of all instances to start time
s = [(x, y) for x, y in starts.items()]
s.sort(key=lambda x: x[1])
bins = {}
for id, start in s:
# Pull out the final record. Should be the last seen
v = instances[id]
runtime = (v.stamp-starts[v.id]).total_seconds()/3600
runtime += step
row = start, v.id, v.key, v.type, '%.1f' % runtime
# Print this entry
print '\t'.join('%s' % x for x in row)
# Bin them based on user and instance type
key = '%s %s' % (v.key, v.type)
bins[key] = bins.get(key, 0) + runtime
for key in sorted(bins):
print '%s\t%.1f' % (key, bins[key])
if __name__ == '__main__':
main()
|