Last active
August 29, 2015 14:07
-
-
Save ross-nordstrom/a21c53472624c9bf128d to your computer and use it in GitHub Desktop.
Get CPU usage
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 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