Last active
December 24, 2015 14:29
-
-
Save maxencehenneron/6812863 to your computer and use it in GitHub Desktop.
Get ram usage in % and load average
This file contains 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
def get_load_average | |
load_avg = nil | |
File.open('/proc/loadavg') {|f| | |
load_avg = Float(f.readline.split(' ')[2]) | |
} | |
load_avg | |
end | |
def get_ram_usage | |
mem_total = mem_free = mem_cached = nil; | |
File.foreach('/proc/meminfo'){|l| | |
case l | |
when /MemTotal/ | |
mem_total = Float(l.split(' ')[1]) | |
when /MemFree/ | |
mem_free = Float(l.split(' ')[1]) | |
when %r{(?<!Swap)Cached} | |
mem_cached = Float(l.split(' ')[1]) | |
end | |
} | |
100 - ((mem_free + mem_cached) / mem_total * 100).to_i | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment