Created
October 11, 2011 18:15
-
-
Save joakimk/1278898 to your computer and use it in GitHub Desktop.
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
class CPU | |
def self.current_usage | |
process_usages = `ps -eo pcpu` | |
total_usage = process_usages.split("\n").inject(0) { |sum, usage| sum += usage.strip.to_f } | |
(total_usage / count).to_i | |
end | |
def self.count | |
case RUBY_PLATFORM | |
when /darwin/ | |
`hwprefs cpu_count`.to_i | |
when /linux/ | |
`cat /proc/cpuinfo | grep processor | wc -l`.to_i | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class CPU
def self.current_usage
process_usages =
ps -eo pcpu
total_usage = process_usages.split("\n").inject(0) { |sum, usage| sum += usage.strip.to_f }
(total_usage / 2).to_i
end
def self.count
case RUBY_PLATFORM
when /darwin/
hwprefs cpu_count
.to_iwhen /linux/
cat /proc/cpuinfo | grep processor | wc -l
.to_iend
end
end
require 'dboard'
user=""
password=""
url= ""
Dboard::Api::Client.basic_auth(user, password)
Dboard::Api::Client.base_uri(url)
loop do
usage = CPU.current_usage
Dboard::Publisher.publish(:cpu, {:load => usage})
puts usage
sleep 1
end