Skip to content

Commit f2a7daa

Browse files
committed
cleanups
1 parent 9b1eb2d commit f2a7daa

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

cloudwatchmon/cli/put_instance_stats.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def __init__(self, mount, file_system, total, used, avail):
9898

9999

100100
class Metrics:
101-
def __init__(self, region, instance_id, instance_type, image_id, aggregated,
102-
autoscaling_group_name):
101+
def __init__(self, region, instance_id, instance_type, image_id,
102+
aggregated, autoscaling_group_name):
103103
self.names = []
104104
self.units = []
105105
self.values = []
@@ -144,20 +144,22 @@ def send(self, verbose):
144144
boto_debug = 2 if verbose else 0
145145

146146
# TODO add timeout
147-
conn = boto.ec2.cloudwatch.connect_to_region(self.region, debug=boto_debug)
147+
conn = boto.ec2.cloudwatch.connect_to_region(self.region,
148+
debug=boto_debug)
148149

149150
if not conn:
150-
raise IOError('Could not establish connection to CloudWatch service')
151-
151+
raise IOError('Could not establish connection to CloudWatch')
152152

153153
size = len(self.names)
154154

155-
for idx in xrange(0, size, AWS_LIMIT_METRICS_SIZE):
156-
response = conn.put_metric_data('System/Linux', self.names[idx:idx + AWS_LIMIT_METRICS_SIZE],
157-
self.values[idx:idx + AWS_LIMIT_METRICS_SIZE],
155+
for idx_start in xrange(0, size, AWS_LIMIT_METRICS_SIZE):
156+
idx_end = idx_start + AWS_LIMIT_METRICS_SIZE
157+
response = conn.put_metric_data('System/Linux',
158+
self.names[idx_start:idx_end],
159+
self.values[idx_start:idx_end],
158160
datetime.datetime.utcnow(),
159-
self.units[idx:idx + AWS_LIMIT_METRICS_SIZE],
160-
self.dimensions[idx:idx + AWS_LIMIT_METRICS_SIZE])
161+
self.units[idx_start:idx_end],
162+
self.dimensions[idx_start:idx_end])
161163

162164
if not response:
163165
raise ValueError('Could not send data to CloudWatch - '
@@ -345,6 +347,17 @@ def add_disk_metrics(args, metrics):
345347
disk.mount, disk.file_system)
346348

347349

350+
def add_static_file_metrics(args, metrics):
351+
with open(args.from_file[0]) as f:
352+
for line in f.readlines():
353+
try:
354+
(label, unit, value) = [x.strip() for x in line.split(',')]
355+
metrics.add_metric(label, unit, value)
356+
except ValueError:
357+
print 'Ignore unparseable metric: "' + line + '"'
358+
pass
359+
360+
348361
@FileCache
349362
def get_autoscaling_group_name(region, instance_id, verbose):
350363
boto_debug = 2 if verbose else 0
@@ -353,7 +366,7 @@ def get_autoscaling_group_name(region, instance_id, verbose):
353366
conn = boto.ec2.autoscale.connect_to_region(region, debug=boto_debug)
354367

355368
if not conn:
356-
raise IOError('Could not establish connection to CloudWatch service')
369+
raise IOError('Could not establish connection to CloudWatch')
357370

358371
autoscaling_instances = conn.get_all_autoscaling_instances([instance_id])
359372

@@ -438,15 +451,8 @@ def main():
438451
args.aggregated,
439452
autoscaling_group_name)
440453

441-
# Add metrics from file
442454
if args.from_file:
443-
lines = open(args.from_file[0]).readlines()
444-
for line in lines:
445-
try:
446-
(label, unit, value) = [x.strip() for x in line.split(',')]
447-
metrics.add_metric(label, unit, value)
448-
except:
449-
pass
455+
add_static_file_metrics(args, metrics)
450456

451457
if report_mem_data:
452458
add_memory_metrics(args, metrics)

0 commit comments

Comments
 (0)