Class: Heartbeat

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

Class Method Summary collapse

Class Method Details

.apache_status(apache, str) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/heartbeat-client.rb', line 263

def self.apache_status(apache, str)
  ap = str.split(':')
  if ap and ap[0]
    apache['requests'] = ap[1].strip.to_f if ap[0].include?('ReqPerSec')
    apache['busy_workers'] = ap[1].strip.to_i if ap[0].include?('BusyWorkers')
    apache['idle_workers'] = ap[1].strip.to_i if ap[0].include?('IdleWorkers')
  end
end

.cpu_usages(cpu_usage, str) ⇒ Object



225
226
227
228
229
230
231
232
233
234
# File 'lib/heartbeat-client.rb', line 225

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

.create(config, version = '0.0', gather_metrics = false) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/heartbeat-client.rb', line 29

def self.create(config, version = '0.0', gather_metrics = false)
  log.info("#create - Collecting data...")
  
  apikey = config['apikey']
  endpoint = config['endpoint']
  name = config['name']
  apache_status = config['apache_status']
  mongostat_arguments = config['mongostat_arguments']
  keen_project_token = config['keen_project_token']
  keen_api_key = config['keen_api_key']
  keen_collection = config['keen_collection']
  docker_command = config['docker_command']

  if gather_metrics
    procs = {'total' => 0, 'running' => 0, 'stuck' => 0, 'sleeping' => 0, 'threads' => 0, 'stopped' => 0, 'zombie' => 0}
    load_avg = []
    cpu_usage = {'user' => 0, 'sys' => 0, 'idle' => 0}
    processes = []
    memory = {'free' => 0, 'used' => 0}
    disks = {}
    swap = {'free' => 0, 'used' => 0}
    apache = {}
    mongodb = {}
    docker_containers = []

    log.debug("Dumping top output...")
    if is_linux?
      `top -b -n1 > /tmp/top.out`
    else
      `top -l 1 > /tmp/top.out`
    end

    log.debug("Dumping df output...")
    `df -m > /tmp/dfm.out`

    if apache_status
      log.debug("Dumping apache status output...")
      `curl #{apache_status} > /tmp/apache.out`  
    end

    if mongostat_arguments
      log.debug("Dumping mongostat output...")
      `mongostat #{mongostat_arguments} > /tmp/mongodb.out`
    end
    
    if docker_command
      log.debug("Dumping docker output...")
      `#{docker_command} > /tmp/docker.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)
          if is_linux?
            processes(procs, line) if line.include?('Task')
            load_averages(load_avg, line) if line.include?('load average')
            cpu_usages(cpu_usage, line) if line.include?('Cpu')
            memory_usage(memory, line) if line.include?('Mem')
            swap_usage(swap, line) if line.include?('Swap')
            proc_count = counter + 1 if line.include?('PID') and line.include?('COMMAND')
          else
            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')
            memory_usage(memory, line) if line.include?('PhysMem')
            proc_count = counter + 1 if line.include?('PID') and line.include?('COMMAND')
          end
          process(processes, line) if proc_count > 0 and counter >= proc_count
          counter += 1
        end
      end

      if File.exists?('/tmp/dfm.out')
        File.open("/tmp/dfm.out", "r") do |infile|
          counter = 0
          while (line = infile.gets)
            disk_usage(disks, line) if counter > 0
            counter += 1
          end
        end
      end

      if File.exists?('/tmp/apache.out')
        File.open("/tmp/apache.out", "r") do |infile|
          counter = 0; lines = []
          while (line = infile.gets)
            apache_status(apache, line)
            counter += 1
          end
        end
      end

      if File.exists?('/tmp/mongodb.out')
        File.open("/tmp/mongodb.out", "r") do |infile|
          counter = 0; lines = []
          while (line = infile.gets)
            lines << line
          end
          mongodb_status(mongodb, lines)
        end
      end
      
      if File.exists?('/tmp/docker.out')
        File.open("/tmp/docker.out", "r") do |infile|
          counter = 0; lines = []
          while (line = infile.gets)
            lines << line if counter > 0
            counter += 1
          end
          docker(docker_containers, lines)
        end
      end

      options = {
        :body => {
          :heartbeat => {
            :client_version => version,
            :apikey => apikey,
            :host => `hostname`.chomp,
            :macaddr => Mac.addr,
            :name => name,
            :timestamp => Time.now.to_i,
            :values => {
              :process_stats => procs,
              :load_avg => load_avg,
              :cpu_usage => cpu_usage,
              :processes => processes,
              :memory => memory,
              :disks => disks,
              :swap => swap,
              :apache_status => apache,
              :mongodb_status => mongodb,
              :docker_containers => docker_containers
            }
          }
        }
      }

      log.info("#create - Sending data to endpoint (with metrics)...")
      res = Heartbeat.post(endpoint + '/heartbeat', options)
      log.debug("Response: #{res.response.inspect}") if res

      if keen_project_token and keen_api_key and keen_collection
        keen = Keen::Client.new(:project_id => keen_project_token, :api_key => keen_api_key)
        data = options[:body][:heartbeat]
        keen.publish(keen_collection, {:host => data[:host], :cpu => data[:values][:cpu_usage]})
      end
    else
      log.error "No top output found."
    end
  else
    options = {
      :body => {
        :heartbeat => {
          :client_version => version,
          :apikey => apikey,
          :host => `hostname`.chomp,
          :macaddr => Mac.addr,
          :name => name,
          :timestamp => Time.now.to_i
        }
      }
    }

    log.info("#create - Sending data to endpoint (no metrics)...")
    res = Heartbeat.post(endpoint + '/heartbeat', options)
    log.debug("Response: #{res.response.inspect}") if res
  end
  log.info("Finished iteration.")
end

.disk_usage(disks, str) ⇒ Object



307
308
309
310
# File 'lib/heartbeat-client.rb', line 307

def self.disk_usage(disks, str)
  ds = str.split(' ')
  disks[ds[0].strip] = {'used' => ds[2].strip, 'available' => ds[3].strip}
end

.docker(docker_containers, lines) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/heartbeat-client.rb', line 295

def self.docker(docker_containers, lines)
  if lines and lines.size > 0
    lines.each do |line|
      container = {}
      parts = line.split(" ")
      container['name'] = parts[0]
      container['image'] = parts[1]
      docker_containers << container
    end
  end
end

.is_linux?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/heartbeat-client.rb', line 17

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

.is_mac?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/heartbeat-client.rb', line 13

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

.load_averages(load_avg, str) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/heartbeat-client.rb', line 216

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

.logObject



25
26
27
# File 'lib/heartbeat-client.rb', line 25

def self.log
  @@log
end

.log=(logger) ⇒ Object



21
22
23
# File 'lib/heartbeat-client.rb', line 21

def self.log=(logger)
  @@log = logger
end

.memory_usage(memory, str) ⇒ Object



243
244
245
246
247
248
249
250
251
# File 'lib/heartbeat-client.rb', line 243

def self.memory_usage(memory, str)
  mem = str.split(':')
  if mem and mem[0]
    mem[1].split(',').each do |m|
      memory['free'] = m.split(' ')[0].strip if m.include?('free')
      memory['used'] = m.split(' ')[0].strip if m.include?('used')
    end
  end
end

.mongodb_status(mongodb, lines) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/heartbeat-client.rb', line 272

def self.mongodb_status(mongodb, lines)
  if lines and lines.size == 3
    header = lines[1].gsub('%', '').gsub('miss', '').split(' '); mo = lines[2].split(' ')
    if header and mo and header.size == mo.size
      header.each_with_index do |h, index|
        mongodb['insert'] = mo[index].strip.to_i if h == 'insert'
        mongodb['query'] = mo[index].to_i if h == 'query'
        mongodb['update'] = mo[index].strip.to_i if h == 'update'
        mongodb['delete'] = mo[index].strip.to_i if h == 'delete'
        mongodb['getmore'] = mo[index].strip.to_i if h == 'getmore'
        mongodb['command'] = mo[index].strip.to_i if h == 'command'
        mongodb['flushes'] = mo[index].strip.to_i if h == 'flushes'
        mongodb['mapped'] = mo[index].strip if h == 'mapped'
        mongodb['vsize'] = mo[index].strip if h == 'vsize'
        mongodb['res'] = mo[index].strip if h == 'res'
        mongodb['netIn'] = mo[index].strip if h == 'netIn'
        mongodb['netOut'] = mo[index].strip if h == 'netOut'
        mongodb['conn'] = mo[index].strip if h == 'conn'
      end
    end
  end
end

.process(processes, str) ⇒ Object



236
237
238
239
240
241
# File 'lib/heartbeat-client.rb', line 236

def self.process(processes, str)
  procs = str.split(' ')
  if procs and procs.size > 0
    processes << (is_linux? ? procs[11].strip : procs[1].strip)
  end
end

.processes(procs, str) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/heartbeat-client.rb', line 201

def self.processes(procs, str)
  proc = str.split(':')
  if proc and proc[0]
    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')
      procs['stopped'] = pr.split(' ')[0].strip.to_i if pr.include?('stopped')
      procs['zombie'] = pr.split(' ')[0].strip.to_i if pr.include?('zombie')
    end
  end
end

.swap_usage(swap, str) ⇒ Object



253
254
255
256
257
258
259
260
261
# File 'lib/heartbeat-client.rb', line 253

def self.swap_usage(swap, str)
  sw = str.split(':')
  if sw and sw[0]
    sw[1].split(',').each do |s|
      swap['free'] = s.split(' ')[0].strip if s.include?('free')
      swap['used'] = s.split(' ')[0].strip if s.include?('used')
    end
  end
end