Skip to content

Instantly share code, notes, and snippets.

@akshendra
Last active June 12, 2018 13:36
Show Gist options
  • Save akshendra/578e3ff285f471777e971becde347873 to your computer and use it in GitHub Desktop.
Save akshendra/578e3ff285f471777e971becde347873 to your computer and use it in GitHub Desktop.
Send machine related into to Cloudwatch, like RAM, disk etc
#!/usr/bin/env python3
# Machine info on EC2 linux machines
import re
import os
import boto3
machine = os.environ['MACHINE_NAME']
region = os.environ['AWS_REGION']
cloudwatch = boto3.client('cloudwatch')
monitor_paths = os.environ['MONITOR_DISKS']
def make_matrix(name, value, unit):
return {
'MetricName': name,
'Dimensions': [
{
'Name': 'Machine',
'Value': machine
},
],
'Value': value,
'Unit': unit,
}
def send_multi_metrics(metrics, unit='Count',
namespace='EC2/Info'):
data = []
for key, value in metrics.items():
data.append(make_matrix(key, value, unit))
cloudwatch.put_metric_data(
Namespace=namespace,
MetricData=data
)
def gather_mem_info():
"""Gather information about memory usage"""
mem_info = {}
pattern = re.compile(r'^(?P<key>\S*):\s*(?P<value>\d*)\s*kB')
with open('/proc/meminfo') as f:
for line in f:
match = pattern.match(line)
if match:
key, value = match.groups(['key', 'value'])
mem_info[key] = int(value) * 1024
total_memory = mem_info['MemTotal']
free_memory = mem_info['MemFree']
cached_memory = mem_info['Cached']
buffered_memory = mem_info['Buffers']
used_memory = total_memory - free_memory
used_memory_uncached = total_memory - free_memory - cached_memory - buffered_memory
utilized_memory = 100 * (used_memory / total_memory)
utilized_memory_uncached = 100 * (used_memory_uncached / total_memory)
total_swap = mem_info['SwapTotal']
free_swap = mem_info['SwapFree']
used_swap = total_swap - free_swap
utilized_swap = ((100 * (used_swap / total_swap))
if total_swap != 0 else 0)
info = {
'TotalMemory': total_memory,
'FreeMemory': free_memory,
'UsedMemory': used_memory,
'UtilizedMemory': utilized_memory,
'UtilizedMemoryUncached': utilized_memory_uncached,
'CachedMemory': mem_info['Cached'],
'Buffers': mem_info['Buffers'],
'TotalSwap': total_swap,
'FreeSwap': free_swap,
'UsedSwap': used_swap,
'UtilizedSwap': utilized_swap,
}
return info
def gather_load_info():
"""Gather infomration about CPU load usage"""
loadavg_info = {}
with open('/proc/loadavg') as loadavg:
parsed = loadavg.read().split(' ')
loadavg_info['1minLoad'] = float(parsed[0])
loadavg_info['5minLoad'] = float(parsed[1])
loadavg_info['15minLoad'] = float(parsed[2])
with open('/proc/cpuinfo') as cpuinfo:
cpu_count = cpuinfo.read().count('processor\t:')
loadavg_info['PerCPU1minLoad'] = loadavg_info['1minLoad'] / cpu_count
loadavg_info['PerCPU5minLoad'] = loadavg_info['5minLoad'] / cpu_count
loadavg_info['PerCPU15minLoad'] = loadavg_info['15minLoad'] / cpu_count
return loadavg_info
def gather_cpu_usage():
"""Get the cpu used"""
return {
'UtilizedCPU': psutil.cpu_percent()
}
def make_disk_info(mount, file_system, total, used, avail):
return {
'UsedDisk_{}'.format(mount): used,
'FreeDisk_{}'.format(mount): avail,
'UtilizedDisk-{}'.format(mount):
100.0 * used / total if total > 0 else 0
}
def gather_disk_info(paths):
disks = []
for path in paths.split(','):
df_out = [s.split() for s in
os.popen('/bin/df -k -P {}'.format(path)).read().splitlines()]
for line in df_out[1:]:
mount = line[5]
file_system = line[0]
total = int(line[1]) * 1024
used = int(line[2]) * 1024
avail = int(line[3]) * 1024
disks.append(make_disk_info(mount, file_system, total, used, avail))
return disks
metrics = [];
ram = gather_mem_info()
metrics.append(ram)
load = gather_load_info()
metrics.append(load)
disks = gather_disk_info(monitor_paths)
for disk in disks:
metrics.append(disk);
# have to send 19 at a time
length = len(metrics)
index = 0
step = 19
while index < length:
start = index
end = index + step if index + step <= length else length
data = metrics[start:end]
info = {}
for metric in data:
info.update(metric)
send_multi_metrics(info)
index = index + step
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment