Skip to content

Instantly share code, notes, and snippets.

@ross-nordstrom
Last active August 29, 2015 14:07
Show Gist options
  • Save ross-nordstrom/a21c53472624c9bf128d to your computer and use it in GitHub Desktop.
Save ross-nordstrom/a21c53472624c9bf128d to your computer and use it in GitHub Desktop.
Get CPU usage
#!/usr/bin/env ruby
require 'json'
cpu = `mpstat -P ALL`
cpuLines = cpu.split("\n")[3..-1]
max = 0
cpuData = cpuLines.map{|s| [s.split(" ")[2], s.split(" ")[3]]}.reduce({}) do |acc, s|
key = s[0] == 'all' ? 'total' : "cpu#{s[0]}"
acc[key] = s[1].to_f unless key == 'total'
max = [max, acc[key]].max unless key == 'total'
acc
end
cpuData['max'] = max
puts cpuData.to_json.gsub(/"/, "'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment