Skip to content

Commit 60a2e3e

Browse files
committed
cleanups and prepared python 3 compatibility (work in progress)
1 parent e3e0a72 commit 60a2e3e

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

cloudwatchmon/cli/get_instance_stats.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919

20+
from __future__ import print_function
2021
from cloudwatchmon.cloud_watch_client import *
2122

2223
import argparse
@@ -76,17 +77,17 @@ def print_metric_stats(region, instance_id, namespace, metric, title,
7677
['Average', 'Maximum', 'Minimum'],
7778
dims)
7879

79-
print title
80+
print(title)
8081

8182
if metrics:
8283
max_val = max(m['Maximum'] for m in metrics)
8384
min_val = min(m['Minimum'] for m in metrics)
8485
avg_val = sum(m['Average'] for m in metrics) / float(len(metrics))
8586

86-
print " Average: {0:.2f}%, Minimum: {1:.2f}%, Maximum: {2:.2f}%\n" \
87-
.format(avg_val, min_val, max_val)
87+
print(" Average: {0:.2f}%, Minimum: {1:.2f}%, Maximum: {2:.2f}%\n"
88+
.format(avg_val, min_val, max_val))
8889
else:
89-
print " Average: N/A, Minimum: N/A, Maximum: N/A\n"
90+
print(" Average: N/A, Minimum: N/A, Maximum: N/A\n")
9091

9192

9293
def print_filesystem_stats(region, instance_id, namespace, metric, title,
@@ -117,21 +118,21 @@ def main():
117118
args = parser.parse_args()
118119

119120
if args.version:
120-
print CLIENT_NAME + ' version ' + VERSION
121+
print(CLIENT_NAME + ' version ' + VERSION)
121122
return 0
122123

123124
try:
124125
metadata = get_metadata()
125126

126127
if args.verbose:
127-
print 'Instance metadata: ' + str(metadata)
128+
print('Instance metadata: ' + str(metadata))
128129

129130
region = metadata['placement']['availability-zone'][:-1]
130131
instance_id = metadata['instance-id']
131132

132133
unit = 'hours' if args.recent_hours > 1 else 'hour'
133-
print 'Instance {0} statistics for the last {1} {2}.\n'\
134-
.format(instance_id, args.recent_hours, unit)
134+
print('Instance {0} statistics for the last {1} {2}.\n'
135+
.format(instance_id, args.recent_hours, unit))
135136

136137
print_metric_stats(region, instance_id,
137138
'AWS/EC2',

cloudwatchmon/cli/put_instance_stats.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919

20+
from __future__ import print_function
2021
from cloudwatchmon.cloud_watch_client import *
2122

2223
import argparse
@@ -182,7 +183,7 @@ def send(self, verbose):
182183

183184
size = len(self.names)
184185

185-
for idx_start in xrange(0, size, AWS_LIMIT_METRICS_SIZE):
186+
for idx_start in range(0, size, AWS_LIMIT_METRICS_SIZE):
186187
idx_end = idx_start + AWS_LIMIT_METRICS_SIZE
187188
response = conn.put_metric_data('System/Linux',
188189
self.names[idx_start:idx_end],
@@ -281,7 +282,6 @@ def config_parser():
281282
action='store_true',
282283
help='Report load averages for 1min, 5min and 15min divided by the number of CPU cores.')
283284

284-
285285
disk_group = parser.add_argument_group('disk metrics')
286286
disk_group.add_argument('--disk-path',
287287
metavar='PATH',
@@ -306,7 +306,6 @@ def config_parser():
306306
action='store_true',
307307
help='Reports disk inode utilization in percentages.')
308308

309-
310309
exclusive_group = parser.add_mutually_exclusive_group()
311310
exclusive_group.add_argument('--from-cron',
312311
action='store_true',
@@ -356,6 +355,7 @@ def add_memory_metrics(args, metrics):
356355
metrics.add_metric('SwapUsed', mem_unit_name,
357356
mem.swap_used() / mem_unit_div)
358357

358+
359359
def add_loadavg_metrics(args, metrics):
360360
loadavg = LoadAverage()
361361
if args.loadavg:
@@ -381,8 +381,8 @@ def get_disk_info(args):
381381
used = int(line[2]) * 1024
382382
avail = int(line[3]) * 1024
383383
disks.append(Disk(mount, file_system, total, used, avail, 0))
384-
385-
#Gather inode utilization if it is requested
384+
385+
# Gather inode utilization if it is requested
386386
if not args.disk_inode_util:
387387
return disks
388388

@@ -394,7 +394,7 @@ def get_disk_info(args):
394394
used = float(line[2])
395395
total = float(line[1])
396396
inode_util_val = 100.0 * used / total if total > 0 else 0
397-
disks_inode_util.append(inode_util_val)
397+
disks_inode_util.append(inode_util_val)
398398

399399
for index, disk in enumerate(disks):
400400
disk.inode_util = disks_inode_util[index]
@@ -420,7 +420,6 @@ def add_disk_metrics(args, metrics):
420420
if args.disk_inode_util:
421421
metrics.add_metric('InodeUtilization', 'Percent',
422422
disk.inode_util, disk.mount, disk.file_system)
423-
424423

425424

426425
def add_static_file_metrics(args, metrics):
@@ -430,7 +429,7 @@ def add_static_file_metrics(args, metrics):
430429
(label, unit, value) = [x.strip() for x in line.split(',')]
431430
metrics.add_metric(label, unit, value)
432431
except ValueError:
433-
print 'Ignore unparseable metric: "' + line + '"'
432+
print('Ignore unparseable metric: "' + line + '"')
434433
pass
435434

436435

@@ -473,8 +472,8 @@ def validate_args(args):
473472
raise ValueError('Metrics to report disk space are provided but '
474473
'disk path is not specified.')
475474

476-
if not report_mem_data and not report_disk_data and not args.from_file and \
477-
not report_loadavg_data:
475+
if not report_mem_data and not report_disk_data and \
476+
not args.from_file and not report_loadavg_data:
478477
raise ValueError('No metrics specified for collection and '
479478
'submission to CloudWatch.')
480479

@@ -492,24 +491,25 @@ def main():
492491
args = parser.parse_args()
493492

494493
if args.version:
495-
print CLIENT_NAME + ' version ' + VERSION
494+
print(CLIENT_NAME + ' version ' + VERSION)
496495
return 0
497496

498497
try:
499-
report_disk_data, report_mem_data, report_loadavg_data = validate_args(args)
498+
report_disk_data, report_mem_data, report_loadavg_data = \
499+
validate_args(args)
500500

501501
# avoid a storm of calls at the beginning of a minute
502502
if args.from_cron:
503503
time.sleep(random.randint(0, 19))
504504

505505
if args.verbose:
506-
print 'Working in verbose mode'
507-
print 'Boto-Version: ' + boto.__version__
506+
print('Working in verbose mode')
507+
print('Boto-Version: ' + boto.__version__)
508508

509509
metadata = get_metadata()
510510

511511
if args.verbose:
512-
print 'Instance metadata: ' + str(metadata)
512+
print('Instance metadata: ' + str(metadata))
513513

514514
region = metadata['placement']['availability-zone'][:-1]
515515
instance_id = metadata['instance-id']
@@ -520,7 +520,7 @@ def main():
520520
args.verbose)
521521

522522
if args.verbose:
523-
print 'Autoscaling group: ' + autoscaling_group_name
523+
print('Autoscaling group: ' + autoscaling_group_name)
524524

525525
metrics = Metrics(region,
526526
instance_id,
@@ -542,16 +542,16 @@ def main():
542542
add_disk_metrics(args, metrics)
543543

544544
if args.verbose:
545-
print 'Request:\n' + str(metrics)
545+
print('Request:\n' + str(metrics))
546546

547547
if args.verify:
548548
if not args.from_cron:
549-
print 'Verification completed successfully. ' \
550-
'No actual metrics sent to CloudWatch.'
549+
print('Verification completed successfully. '
550+
'No actual metrics sent to CloudWatch.')
551551
else:
552552
metrics.send(args.verbose)
553553
if not args.from_cron:
554-
print 'Successfully reported metrics to CloudWatch.'
554+
print('Successfully reported metrics to CloudWatch.')
555555
except Exception as e:
556556
log_error(str(e), args.from_cron)
557557
return 1

cloudwatchmon/cloud_watch_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18+
from __future__ import print_function
1819
import boto
1920
import boto.utils
2021
import hashlib
@@ -56,7 +57,7 @@ def __call__(self, *args, **kwargs):
5657

5758
tmp = self.fnc(*args, **kwargs)
5859
with open(filename, 'wb') as f:
59-
os.chmod(filename, 0600)
60+
os.chmod(filename, 0o600)
6061
pickle.dump(tmp, f)
6162

6263
return tmp
@@ -66,7 +67,7 @@ def log_error(message, use_syslog):
6667
if use_syslog:
6768
syslog.syslog(syslog.LOG_ERR, message)
6869
else:
69-
print >> sys.stderr, 'ERROR: ' + message
70+
print('ERROR: ' + message, file=sys.stderr)
7071

7172

7273
@FileCache

0 commit comments

Comments
 (0)