Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions cloudwatchmon/cli/get_instance_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import print_function
from cloudwatchmon.cloud_watch_client import *

import argparse
Expand Down Expand Up @@ -70,23 +72,23 @@ def print_metric_stats(region, instance_id, namespace, metric, title,
start_time = end_time - datetime.timedelta(hours=recent_hours)
dims = {'InstanceId': instance_id}
if xdims:
dims = dict(dims.items() + xdims.items())
dims.update(xdims)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, if this may result in the same problem as below...

metrics = conn.get_metric_statistics(300, start_time, end_time,
metric, namespace,
['Average', 'Maximum', 'Minimum'],
dims)

print title
print(title)

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

print " Average: {0:.2f}%, Minimum: {1:.2f}%, Maximum: {2:.2f}%\n" \
.format(avg_val, min_val, max_val)
print(" Average: {0:.2f}%, Minimum: {1:.2f}%, Maximum: {2:.2f}%\n" \
.format(avg_val, min_val, max_val))
else:
print " Average: N/A, Minimum: N/A, Maximum: N/A\n"
print(" Average: N/A, Minimum: N/A, Maximum: N/A\n")


def print_filesystem_stats(region, instance_id, namespace, metric, title,
Expand Down Expand Up @@ -117,21 +119,21 @@ def main():
args = parser.parse_args()

if args.version:
print CLIENT_NAME + ' version ' + VERSION
print(CLIENT_NAME + ' version ' + VERSION)
return 0

try:
metadata = get_metadata()

if args.verbose:
print 'Instance metadata: ' + str(metadata)
print('Instance metadata: ' + str(metadata))

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

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

print_metric_stats(region, instance_id,
'AWS/EC2',
Expand Down
29 changes: 17 additions & 12 deletions cloudwatchmon/cli/put_instance_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import print_function
from cloudwatchmon.cloud_watch_client import *

import argparse
Expand All @@ -29,6 +31,7 @@
import re
import sys
import time
from six.moves import range

CLIENT_NAME = 'CloudWatch-PutInstanceData'
FileCache.CLIENT_NAME = CLIENT_NAME
Expand Down Expand Up @@ -167,7 +170,9 @@ def __add_metric_dimensions(self, name, unit, value, common_dims, dims):
self.names.append(name)
self.units.append(unit)
self.values.append(value)
self.dimensions.append(dict(common_dims.items() + dim.items()))
final_dims = common_dims.copy()
final_dims.update(dims)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in a runtime error:
ERROR: dictionary update sequence element #0 has length 1; 2 is required

self.dimensions.append(final_dims)

def send(self, verbose):
boto_debug = 2 if verbose else 0
Expand All @@ -181,7 +186,7 @@ def send(self, verbose):

size = len(self.names)

for idx_start in xrange(0, size, AWS_LIMIT_METRICS_SIZE):
for idx_start in range(0, size, AWS_LIMIT_METRICS_SIZE):
idx_end = idx_start + AWS_LIMIT_METRICS_SIZE
response = conn.put_metric_data('System/Linux',
self.names[idx_start:idx_end],
Expand Down Expand Up @@ -403,7 +408,7 @@ def add_static_file_metrics(args, metrics):
(label, unit, value) = [x.strip() for x in line.split(',')]
metrics.add_metric(label, unit, value)
except ValueError:
print 'Ignore unparseable metric: "' + line + '"'
print('Ignore unparseable metric: "' + line + '"')
pass


Expand Down Expand Up @@ -465,7 +470,7 @@ def main():
args = parser.parse_args()

if args.version:
print CLIENT_NAME + ' version ' + VERSION
print(CLIENT_NAME + ' version ' + VERSION)
return 0

try:
Expand All @@ -476,13 +481,13 @@ def main():
time.sleep(random.randint(0, 19))

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

metadata = get_metadata()

if args.verbose:
print 'Instance metadata: ' + str(metadata)
print('Instance metadata: ' + str(metadata))

region = metadata['placement']['availability-zone'][:-1]
instance_id = metadata['instance-id']
Expand All @@ -493,7 +498,7 @@ def main():
args.verbose)

if args.verbose:
print 'Autoscaling group: ' + autoscaling_group_name
print('Autoscaling group: ' + autoscaling_group_name)

metrics = Metrics(region,
instance_id,
Expand All @@ -515,16 +520,16 @@ def main():
add_disk_metrics(args, metrics)

if args.verbose:
print 'Request:\n' + str(metrics)
print('Request:\n' + str(metrics))

if args.verify:
if not args.from_cron:
print 'Verification completed successfully. ' \
'No actual metrics sent to CloudWatch.'
print('Verification completed successfully. ' \
'No actual metrics sent to CloudWatch.')
else:
metrics.send(args.verbose)
if not args.from_cron:
print 'Successfully reported metrics to CloudWatch.'
print('Successfully reported metrics to CloudWatch.')
except Exception as e:
log_error(str(e), args.from_cron)
return 1
Expand Down
10 changes: 6 additions & 4 deletions cloudwatchmon/cloud_watch_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import print_function
import boto
import boto.utils
import hashlib
Expand Down Expand Up @@ -43,9 +45,9 @@ def __init__(self, fnc):
def __call__(self, *args, **kwargs):
sig = ":".join([VERSION, str(self.fnc.__name__), str(args), str(kwargs)])

hashed = hashlib.md5(sig.encode('utf-8')).hexdigest()
filename = os.path.join(META_DATA_CACHE_DIR, '{0}-{1}.bin'
.format(self.CLIENT_NAME,
hashlib.md5(sig).hexdigest()))
.format(self.CLIENT_NAME, hashed))

if os.path.exists(filename):
mtime = os.path.getmtime(filename)
Expand All @@ -56,7 +58,7 @@ def __call__(self, *args, **kwargs):

tmp = self.fnc(*args, **kwargs)
with open(filename, 'wb') as f:
os.chmod(filename, 0600)
os.chmod(filename, 0o600)
pickle.dump(tmp, f)

return tmp
Expand All @@ -66,7 +68,7 @@ def log_error(message, use_syslog):
if use_syslog:
syslog.syslog(syslog.LOG_ERR, message)
else:
print >> sys.stderr, 'ERROR: ' + message
print('ERROR: ' + message, file=sys.stderr)


@FileCache
Expand Down
10 changes: 8 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ def readme():
keywords="monitoring cloudwatch amazon web services aws ec2",
zip_safe=True,
packages=find_packages(),
install_requires=['boto>=2.33.0','argparse'],
install_requires=['boto>=2.33.0','argparse', 'six'],
entry_points={'console_scripts': [
'mon-get-instance-stats.py=cloudwatchmon.cli.get_instance_stats:main',
'mon-put-instance-stats.py=cloudwatchmon.cli.put_instance_stats:main',
]
},
classifiers=[
'Programming Language :: Python :: 2 :: Only',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'License :: OSI Approved :: Apache Software License',
'Topic :: System :: Monitoring'
]
Expand Down