Sha256: e7c37adcfcc9648174a8596cd51e5053915ae0ac9517c444386432a1188a0295

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'rubygems'
gem 'httparty'
require 'httparty'
require 'pp'
require 'logger'

class Heartbeat
  include HTTParty
  base_uri 'http://heartbeat-server.herokuapp.com'

  def self.is_mac?
    RUBY_PLATFORM.downcase.include?("darwin")
  end

  def self.is_linux?
    RUBY_PLATFORM.downcase.include?("linux")
  end
  
  def self.log
    Logger.new('/tmp/heartbeat.log')
  end
  
  def self.create(apikey)
    cpu = 0; load_average = 0; memory_used = 0; memory_free = 0
    output = is_mac? ? `top -l 1`.chomp : `top -n 1`.chomp

    # puts output.split('\n').first.inspect
    # TODO analyse output

    options = {
      :body => {
        :heartbeat => {
          :apikey => apikey,
          :host => `hostname`.chomp,
          :timestamp => Time.now.to_i,
          :values => {
            :cpu => cpu,
            :load_average => load_average,
            :memory_used => memory_used,
            :memory_free => memory_free 
          }
        }
      }
    }

    pp Heartbeat.post('/heartbeat', options)    
    # log.info("Heartbeat sent...")
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
heartbeat-client-0.1.2 lib/heartbeat-client.rb
heartbeat-client-0.1.1 lib/heartbeat-client.rb
heartbeat-client-0.1.0 lib/heartbeat-client.rb