Class: Heartbeat

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/heartbeat-client.rb

Class Method Summary collapse

Class Method Details

.cpu_usages(cpu_usage, str) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/heartbeat-client.rb', line 97

def self.cpu_usages(cpu_usage, str)
  cpu = str.split(':')
  if cpu and cpu[0] and cpu[0].include?('CPU usage')
    cpu[1].split(',').each do |cp|
      cpu_usage['user'] = cp.split(' ')[0].strip.to_f if cp.include?('user')
      cpu_usage['sys'] = cp.split(' ')[0].strip.to_f if cp.include?('sys')
      cpu_usage['idle'] = cp.split(' ')[0].strip.to_f if cp.include?('idle')
    end
  end
end

.create(apikey) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/heartbeat-client.rb', line 23

def self.create(apikey)
  procs = {'total' => 0, 'running' => 0, 'stuck' => 0, 'sleeping' => 0, 'threads' => 0}
  load_avg = []
  cpu_usage = {'user' => 0, 'sys' => 0, 'idle' => 0}
  processes = []

  if self.is_linux?
    `top -b -n1 > /tmp/top.out`
  else
    `top -l 1 > /tmp/top.out`
  end

  if File.exists?('/tmp/top.out')
    counter = 0; proc_count = 0
    File.open("/tmp/top.out", "r") do |infile|
      while (line = infile.gets)
        processes(procs, line) if line.include?('Processes')
        load_averages(load_avg, line) if line.include?('Load Avg')
        cpu_usages(cpu_usage, line) if line.include?('CPU usage')
        proc_count = counter + 1 if line.include?('PID') and line.include?('COMMAND')
        process(processes, line) if proc_count > 0 and counter >= proc_count
        counter += 1
      end
    end
    
    options = {
      :body => {
        :heartbeat => {
          :apikey => apikey,
          :host => `hostname`.chomp,
          :timestamp => Time.now.to_i,
          :values => {
            :process_stats => procs,
            :load_avg => load_avg,
            :cpu_usage => cpu_usage,
            :processes => processes
          }
        }
      }
    }

    # puts procs.inspect
    # puts load_avg.inspect
    # puts cpu_usage.inspect
    # puts processes.inspect

    pp Heartbeat.post('/heartbeat', options)
  else
    put "No top output found."
  end
end

.is_linux?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/heartbeat-client.rb', line 15

def self.is_linux?
  RUBY_PLATFORM.downcase.include?("linux")
end

.is_mac?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/heartbeat-client.rb', line 11

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

.load_averages(load_avg, str) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/heartbeat-client.rb', line 88

def self.load_averages(load_avg, str)
  avg = str.split(':')
  if avg and avg[0] and avg[0].include?('Load Avg')
    avg[1].split(',').each do |a|
      load_avg << a.strip.to_f
    end
  end
end

.logObject



19
20
21
# File 'lib/heartbeat-client.rb', line 19

def self.log
  Logger.new('/tmp/heartbeat.log')
end

.process(processes, line) ⇒ Object



108
109
110
111
112
113
# File 'lib/heartbeat-client.rb', line 108

def self.process(processes, line)
  procs = line.split(' ')
  if procs and procs.size > 0
    processes << {'pid' => procs[0].strip.to_i, 'command' => procs[1].strip, 'cpu' => procs[2].strip.to_f}
  end
end

.processes(procs, str) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/heartbeat-client.rb', line 75

def self.processes(procs, str)
  proc = str.split(':')
  if proc and proc[0] and proc[0].include?('Processes')
    proc[1].split(',').each do |pr|
      procs['total'] = pr.split(' ')[0].strip.to_i if pr.include?('total')
      procs['running'] = pr.split(' ')[0].strip.to_i if pr.include?('running')
      procs['stuck'] = pr.split(' ')[0].strip.to_i if pr.include?('stuck')
      procs['sleeping'] = pr.split(' ')[0].strip.to_i if pr.include?('sleeping')
      procs['threads'] = pr.split(' ')[0].strip.to_i if pr.include?('threads')
    end
  end
end