Skip to content

Instantly share code, notes, and snippets.

@shevron
Last active April 28, 2020 02:28
Show Gist options
  • Select an option

  • Save shevron/6204349 to your computer and use it in GitHub Desktop.

Select an option

Save shevron/6204349 to your computer and use it in GitHub Desktop.

Revisions

  1. shevron revised this gist May 19, 2015. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,27 @@
    Copyright (c) 2015, Shahar Evron
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation and/or
    other materials provided with the distribution.

    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    3. Neither the name of the copyright holder nor the names of its contributors
    may be used to endorse or promote products derived from this software without
    specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
  2. shevron revised this gist May 19, 2015. 2 changed files with 16 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    Copyright (c) 2015, Shahar Evron
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    4 changes: 4 additions & 0 deletions cw-monitor-memusage.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,10 @@
    a boto credentials file and rely on it instead.
    Original idea based on https://github.com/colinbjohnson/aws-missing-tools
    (c) 2015 Shahar Evron, all rights reserved;
    You are free to use, modify and redistribute this software in any form
    under the conditions described in the LICENSE file included.
    '''

    import sys
  3. shevron revised this gist Nov 5, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions cw-monitor-memusage.py
    Original file line number Diff line number Diff line change
    @@ -44,9 +44,13 @@ def send_multi_metrics(instance_id, region, metrics, namespace='EC2/Memory',

    mem_free = mem_usage['MemFree'] + mem_usage['Buffers'] + mem_usage['Cached']
    mem_used = mem_usage['MemTotal'] - mem_free
    swap_used = mem_usage['SwapTotal'] - mem_usage['SwapFree'] - mem_usage['SwapCached']
    if mem_usage['SwapTotal'] != 0 :
    swap_used = mem_usage['SwapTotal'] - mem_usage['SwapFree'] - mem_usage['SwapCached']
    swap_percent = swap_used / mem_usage['SwapTotal'] * 100
    else:
    swap_percent = 0

    metrics = {'MemUsage': mem_used / mem_usage['MemTotal'] * 100,
    'SwapUsage': swap_used / mem_usage['SwapTotal'] * 100 }
    'SwapUsage': swap_percent }

    send_multi_metrics(instance_id, region, metrics)
  4. shevron revised this gist Sep 16, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions cw-monitor-memusage.py
    Original file line number Diff line number Diff line change
    @@ -42,8 +42,9 @@ def send_multi_metrics(instance_id, region, metrics, namespace='EC2/Memory',
    region = metadata['placement']['availability-zone'][0:-1]
    mem_usage = collect_memory_usage()

    mem_used = mem_usage['MemTotal'] - mem_usage['MemFree']
    swap_used = mem_usage['SwapTotal'] - mem_usage['SwapFree']
    mem_free = mem_usage['MemFree'] + mem_usage['Buffers'] + mem_usage['Cached']
    mem_used = mem_usage['MemTotal'] - mem_free
    swap_used = mem_usage['SwapTotal'] - mem_usage['SwapFree'] - mem_usage['SwapCached']

    metrics = {'MemUsage': mem_used / mem_usage['MemTotal'] * 100,
    'SwapUsage': swap_used / mem_usage['SwapTotal'] * 100 }
  5. shevron revised this gist Aug 11, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion cw-monitor-memusage.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,6 @@
    import sys
    import re
    from boto.ec2 import cloudwatch
    from boto.ec2 import get_region
    from boto.utils import get_instance_metadata

    def collect_memory_usage():
  6. shevron revised this gist Aug 11, 2013. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions cw-monitor-memusage.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #!/usr/bin/env python
    '''
    Send memory usage metrics to Amazon CloudWatch
    @@ -8,6 +9,7 @@
    Original idea based on https://github.com/colinbjohnson/aws-missing-tools
    '''

    import sys
    import re
    from boto.ec2 import cloudwatch
    from boto.ec2 import get_region
    @@ -21,11 +23,11 @@ def collect_memory_usage():
    match = pattern.match(line)
    if match:
    # For now we don't care about units (match.group(3))
    meminfo[match.group(1)] = int(match.group(2))
    meminfo[match.group(1)] = float(match.group(2))
    return meminfo

    def send_multi_metrics(instance_id, region, metrics, namespace='EC2/Memory',
    unit='Kilobytes'):
    unit='Percent'):
    '''
    Send multiple metrics to CloudWatch
    metrics is expected to be a map of key -> value pairs of metrics
    @@ -40,8 +42,11 @@ def send_multi_metrics(instance_id, region, metrics, namespace='EC2/Memory',
    instance_id = metadata['instance-id']
    region = metadata['placement']['availability-zone'][0:-1]
    mem_usage = collect_memory_usage()
    metrics = {'MemFree': mem_usage['MemFree'],
    'SwapTotal': mem_usage['SwapTotal'],
    'SwapFree': mem_usage['SwapFree']}

    mem_used = mem_usage['MemTotal'] - mem_usage['MemFree']
    swap_used = mem_usage['SwapTotal'] - mem_usage['SwapFree']

    metrics = {'MemUsage': mem_used / mem_usage['MemTotal'] * 100,
    'SwapUsage': swap_used / mem_usage['SwapTotal'] * 100 }

    send_multi_metrics(instance_id, region, metrics)
  7. shevron revised this gist Aug 11, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion cw-monitor-memusage.py
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,6 @@
    Original idea based on https://github.com/colinbjohnson/aws-missing-tools
    '''

    import sys
    import re
    from boto.ec2 import cloudwatch
    from boto.ec2 import get_region
  8. shevron created this gist Aug 11, 2013.
    48 changes: 48 additions & 0 deletions cw-monitor-memusage.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    '''
    Send memory usage metrics to Amazon CloudWatch
    This is intended to run on an Amazon EC2 instance and requires an IAM
    role allowing to write CloudWatch metrics. Alternatively, you can create
    a boto credentials file and rely on it instead.
    Original idea based on https://github.com/colinbjohnson/aws-missing-tools
    '''

    import sys
    import re
    from boto.ec2 import cloudwatch
    from boto.ec2 import get_region
    from boto.utils import get_instance_metadata

    def collect_memory_usage():
    meminfo = {}
    pattern = re.compile('([\w\(\)]+):\s*(\d+)(:?\s*(\w+))?')
    with open('/proc/meminfo') as f:
    for line in f:
    match = pattern.match(line)
    if match:
    # For now we don't care about units (match.group(3))
    meminfo[match.group(1)] = int(match.group(2))
    return meminfo

    def send_multi_metrics(instance_id, region, metrics, namespace='EC2/Memory',
    unit='Kilobytes'):
    '''
    Send multiple metrics to CloudWatch
    metrics is expected to be a map of key -> value pairs of metrics
    '''
    cw = cloudwatch.connect_to_region(region)
    cw.put_metric_data(namespace, metrics.keys(), metrics.values(),
    unit=unit,
    dimensions={"InstanceId": instance_id})

    if __name__ == '__main__':
    metadata = get_instance_metadata()
    instance_id = metadata['instance-id']
    region = metadata['placement']['availability-zone'][0:-1]
    mem_usage = collect_memory_usage()
    metrics = {'MemFree': mem_usage['MemFree'],
    'SwapTotal': mem_usage['SwapTotal'],
    'SwapFree': mem_usage['SwapFree']}

    send_multi_metrics(instance_id, region, metrics)