Created
November 9, 2017 23:10
-
-
Save DennisTT/c0b919840b49dcaaf38964cd0a912ef9 to your computer and use it in GitHub Desktop.
Collectd - Mac Memory Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Send more accurate memory readings from Mac OS X than the default plugin | |
# Calculations from: http://badrit.com/blog/2014/5/1/how-to-calc-memory-usage-in-mavericks-programmatically | |
import collectd | |
import subprocess | |
import re | |
PAGE_SIZE = 4096 # From the top of `vm_stat` | |
def read_func(): | |
collectd.info('mac_memory plugin: Read function') | |
phys_mem_str = subprocess.check_output(["sysctl", "hw.memsize"]) | |
phys_mem = int(phys_mem_str.split(' ')[1]) | |
vm_stat = subprocess.check_output(["vm_stat"]) | |
app_memory_match = re.search(r"Anonymous pages:(\s+)(\d+)\.", vm_stat) | |
app_memory_pages = app_memory_match.group(2) | |
app_memory_bytes = int(app_memory_pages) * PAGE_SIZE | |
wired_memory_match = re.search(r"Pages wired down:(\s+)(\d+)\.", vm_stat) | |
wired_memory_pages = wired_memory_match.group(2) | |
wired_memory_bytes = int(wired_memory_pages) * PAGE_SIZE | |
compressed_memory_match = re.search(r"Pages occupied by compressor:(\s+)(\d+)\.", vm_stat) | |
compressed_memory_pages = compressed_memory_match.group(2) | |
compressed_memory_bytes = int(compressed_memory_pages) * PAGE_SIZE | |
memory_used_bytes = app_memory_bytes + wired_memory_bytes + compressed_memory_bytes | |
free_bytes = phys_mem - memory_used_bytes | |
val = collectd.Values(type='gauge', plugin='mac_memory', plugin_instance='memory_total') | |
val.dispatch(values=[phys_mem]) | |
val = collectd.Values(type='gauge', plugin='mac_memory', plugin_instance='app_memory') | |
val.dispatch(values=[app_memory_bytes]) | |
val = collectd.Values(type='gauge', plugin='mac_memory', plugin_instance='wired_memory') | |
val.dispatch(values=[wired_memory_bytes]) | |
val = collectd.Values(type='gauge', plugin='mac_memory', plugin_instance='compressed_memory') | |
val.dispatch(values=[compressed_memory_bytes]) | |
val = collectd.Values(type='gauge', plugin='mac_memory', plugin_instance='memory_used') | |
val.dispatch(values=[memory_used_bytes]) | |
val = collectd.Values(type='gauge', plugin='mac_memory', plugin_instance='memory_free') | |
val.dispatch(values=[free_bytes]) | |
collectd.register_read(read_func) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment