Skip to content

Instantly share code, notes, and snippets.

@ross-nordstrom
Last active August 29, 2015 14:07
Show Gist options
  • Save ross-nordstrom/786d30c269965655838d to your computer and use it in GitHub Desktop.
Save ross-nordstrom/786d30c269965655838d to your computer and use it in GitHub Desktop.
Get mem usage
#!/usr/bin/env ruby
require 'json'
memstr = `free`
memPieces = memstr.split("\n")[1].split(" ")
memData = {
:available => (100*memPieces[1].to_f/1000).round/100.0,
:used => (100*memPieces[2].to_f/1000).round/100.0,
:free => (100*memPieces[3].to_f/1000).round/100.0,
:shared => (100*memPieces[4].to_f/1000).round/100.0,
:buffers => (100*memPieces[5].to_f/1000).round/100.0,
:cached => (100*memPieces[6].to_f/1000).round/100.0
}
totalUsed = memData[:available] - memData[:free]
usedPct = totalUsed.to_f / memData[:available].to_f
memData[:used_state] = usedPct >= 0.80 ? 3 : (usedPct >= 0.50 ? 2 : 1)
puts memData.to_json.gsub(/"/, "'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment