Skip to content

Commit

Permalink
Merge pull request #13 from crccheck/plumbum
Browse files Browse the repository at this point in the history
add utility to help me generate config files
  • Loading branch information
crccheck committed Oct 27, 2014
2 parents c9ea831 + e26e626 commit 50e21c1
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ test:
.PHONY: build
build:
python setup.py sdist bdist_wheel upload


# makes it easier to test setup.py's entry points
reinstall:
pip uninstall cloudwatch-to-graphite --yes
pip install .
20 changes: 20 additions & 0 deletions config.yaml.j2.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{# This is a Jinja2 comment
To use this template, you'd run something like:
python plumbum.py config.yaml.j2.example ec2
#}
# Sample config.yaml
Metrics:
{% for instance in resources %}
- Namespace: "AWS/EC2"
MetricName: "CPUUtilization"
Statistics:
- "Maximum"
- "Average"
Unit: "Percent"
Dimensions:
InstanceId: "{{ instance.id }}"
Options:
{# I'm assuming my tag names are safe to use as metric names here #}
Formatter: 'cloudwatch.%(Namespace)s.{{ instance.tags['Name'] }}.%(MetricName)s.%(statistic)s.%(Unit)s'
Period: 5
{% endfor %}
114 changes: 114 additions & 0 deletions plumbum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# -*- coding: UTF-8 -*-
"""
Usage:
plumbum <template> <namespace> [options]...
Examples:
plumbum elb.yaml.j2 elb
plumbum ec2.yaml.j2 ec2 environment=production
Outputs to stdout.
"""
from __future__ import unicode_literals

import sys

import boto.ec2
import boto.ec2.elb
import boto.rds
import jinja2


def get_property_func(key):
"""
Get the accessor function for an instance to look for `key`.
Look for it as an attribute, and if that does not work, look to see if it
is a tag.
"""
def get_it(obj):
try:
return getattr(obj, key)
except AttributeError:
return obj.tags.get(key)
return get_it


def filter_key(filter_args):
def filter_instance(instance):
return all([value == get_property_func(key)(instance)
for key, value in filter_args.items()])
return filter_instance


def lookup(instances, filter_by=None):
if filter_by:
return filter(filter_key(filter_by), instances)
return instances


def get_options(input_args):
filter_by_kwargs = {}
for arg in input_args:
if arg.startswith('-'):
# ignore options
continue
if '=' in arg:
key, value = arg.split('=', 2)
filter_by_kwargs[key] = value
return filter_by_kwargs


def list_ec2(filter_by_kwargs):
conn = boto.ec2.connect_to_region('us-east-1') # XXX magic constant
instances = conn.get_only_instances()
return lookup(instances, filter_by=filter_by_kwargs)


def list_elb(filter_by_kwargs):
conn = boto.ec2.elb.connect_to_region('us-east-1') # XXX magic constant
instances = conn.get_all_load_balancers()
return lookup(instances, filter_by=filter_by_kwargs)


def list_rds(filter_by_kwargs):
conn = boto.rds.connect_to_region('us-east-1')
instances = conn.get_all_dbinstances()
return lookup(instances, filter_by=filter_by_kwargs)


def main():
if len(sys.argv) < 3:
print __doc__
sys.exit()
options = sys.argv[1:]
template = options[0]
namespace = options[1].lower()
filters = get_options(options[2:])

# get the template first so this can fail before making a network request
loader = jinja2.FileSystemLoader('.')
jinja2_env = jinja2.Environment(loader=loader)
template = jinja2_env.get_template(template)

# should I be using ARNs?
if namespace in ('ec2', 'aws/ec2'):
resources = list_ec2(filters)
elif namespace in ('elb', 'aws/elb'):
resources = list_elb(filters)
elif namespace in ('rds', 'aws/rds'):
resources(filters)
else:
# TODO
sys.exit(1)

print template.render({
'filters': filters,
'resources': resources,
})


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
boto==2.33.0
PyYAML==3.11
docopt==0.6.2
Jinja2==2.7.3

# tests
tox==1.8.0
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
author='Chris Chang',
author_email='[email protected]',
url='https://github.com/crccheck/cloudwatch-to-graphite',
py_modules=['leadbutt'],
py_modules=['leadbutt', 'plumbum'],
entry_points={
'console_scripts': [
'leadbutt = leadbutt:main',
'plumbum = plumbum:main',
],
},
install_requires=[
'boto',
'PyYAML',
'docopt',
'Jinja2',
],
license='Apache License, Version 2.0',
long_description=open('README.rst').read(),
Expand Down

0 comments on commit 50e21c1

Please sign in to comment.