Skip to content

Instantly share code, notes, and snippets.

@joakimk
Created October 11, 2011 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joakimk/1278898 to your computer and use it in GitHub Desktop.
Save joakimk/1278898 to your computer and use it in GitHub Desktop.
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
@soma
Copy link

soma commented Oct 11, 2011

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_i
when /linux/
cat /proc/cpuinfo | grep processor | wc -l.to_i
end
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment