heartbeat-client 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.0
@@ -1,7 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
1
3
  require 'rubygems'
2
4
  require 'daemons'
3
5
  require 'logger'
4
- require File.dirname(__FILE__) + '/heartbeat-client'
6
+ require File.dirname(__FILE__) + '/../lib/heartbeat-client'
5
7
 
6
8
  begin
7
9
  puts "Using config in your home directory"
@@ -15,9 +17,13 @@ unless @config['apikey']
15
17
  exit
16
18
  end
17
19
 
18
- Daemons.run_proc('heartbeat-client.rb') do
19
- loop do
20
- Heartbeat.create(@config['apikey'])
21
- sleep(30)
20
+ if ARGV and ARGV.size == 1
21
+ Daemons.run_proc('heartbeat-client.rb') do
22
+ loop do
23
+ Heartbeat.create(@config['apikey'])
24
+ sleep(30)
25
+ end
22
26
  end
27
+ else
28
+ puts "Please provide a command to hbc (start|stop|run)!"
23
29
  end
@@ -21,29 +21,94 @@ class Heartbeat
21
21
  end
22
22
 
23
23
  def self.create(apikey)
24
- cpu = 0; load_average = 0; memory_used = 0; memory_free = 0
24
+ procs = {'total' => 0, 'running' => 0, 'stuck' => 0, 'sleeping' => 0, 'threads' => 0}
25
+ load_avg = []
26
+ cpu_usage = {'user' => 0, 'sys' => 0, 'idle' => 0}
27
+ processes = []
25
28
 
26
29
  if self.is_linux?
27
- cpus = `iostat -c`.split
28
- cpu = 100 - cpus.last.to_i
30
+ `top -b -n1 > /tmp/top.out`
31
+ else
32
+ `top -l 1 > /tmp/top.out`
29
33
  end
30
34
 
31
- options = {
32
- :body => {
33
- :heartbeat => {
34
- :apikey => apikey,
35
- :host => `hostname`.chomp,
36
- :timestamp => Time.now.to_i,
37
- :values => {
38
- :cpu => cpu,
39
- :load_average => load_average,
40
- :memory_used => memory_used,
41
- :memory_free => memory_free
35
+ if File.exists?('/tmp/top.out')
36
+ counter = 0; proc_count = 0
37
+ File.open("/tmp/top.out", "r") do |infile|
38
+ while (line = infile.gets)
39
+ processes(procs, line) if line.include?('Processes')
40
+ load_averages(load_avg, line) if line.include?('Load Avg')
41
+ cpu_usages(cpu_usage, line) if line.include?('CPU usage')
42
+ proc_count = counter + 1 if line.include?('PID') and line.include?('COMMAND')
43
+ process(processes, line) if proc_count > 0 and counter >= proc_count
44
+ counter += 1
45
+ end
46
+ end
47
+
48
+ options = {
49
+ :body => {
50
+ :heartbeat => {
51
+ :apikey => apikey,
52
+ :host => `hostname`.chomp,
53
+ :timestamp => Time.now.to_i,
54
+ :values => {
55
+ :process_stats => procs,
56
+ :load_avg => load_avg,
57
+ :cpu_usage => cpu_usage,
58
+ :processes => processes
59
+ }
42
60
  }
43
61
  }
44
62
  }
45
- }
46
63
 
47
- pp Heartbeat.post('/heartbeat', options)
64
+ # puts procs.inspect
65
+ # puts load_avg.inspect
66
+ # puts cpu_usage.inspect
67
+ # puts processes.inspect
68
+
69
+ pp Heartbeat.post('/heartbeat', options)
70
+ else
71
+ put "No top output found."
72
+ end
73
+ end
74
+
75
+ def self.processes(procs, str)
76
+ proc = str.split(':')
77
+ if proc and proc[0] and proc[0].include?('Processes')
78
+ proc[1].split(',').each do |pr|
79
+ procs['total'] = pr.split(' ')[0].strip.to_i if pr.include?('total')
80
+ procs['running'] = pr.split(' ')[0].strip.to_i if pr.include?('running')
81
+ procs['stuck'] = pr.split(' ')[0].strip.to_i if pr.include?('stuck')
82
+ procs['sleeping'] = pr.split(' ')[0].strip.to_i if pr.include?('sleeping')
83
+ procs['threads'] = pr.split(' ')[0].strip.to_i if pr.include?('threads')
84
+ end
85
+ end
86
+ end
87
+
88
+ def self.load_averages(load_avg, str)
89
+ avg = str.split(':')
90
+ if avg and avg[0] and avg[0].include?('Load Avg')
91
+ avg[1].split(',').each do |a|
92
+ load_avg << a.strip.to_f
93
+ end
94
+ end
95
+ end
96
+
97
+ def self.cpu_usages(cpu_usage, str)
98
+ cpu = str.split(':')
99
+ if cpu and cpu[0] and cpu[0].include?('CPU usage')
100
+ cpu[1].split(',').each do |cp|
101
+ cpu_usage['user'] = cp.split(' ')[0].strip.to_f if cp.include?('user')
102
+ cpu_usage['sys'] = cp.split(' ')[0].strip.to_f if cp.include?('sys')
103
+ cpu_usage['idle'] = cp.split(' ')[0].strip.to_f if cp.include?('idle')
104
+ end
105
+ end
106
+ end
107
+
108
+ def self.process(processes, line)
109
+ procs = line.split(' ')
110
+ if procs and procs.size > 0
111
+ processes << {'pid' => procs[0].strip.to_i, 'command' => procs[1].strip, 'cpu' => procs[2].strip.to_f}
112
+ end
48
113
  end
49
114
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heartbeat-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 5
10
- version: 0.1.5
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Oliver Kiessler
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-22 00:00:00 Z
18
+ date: 2012-01-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  version_requirements: &id001 !ruby/object:Gem::Requirement
@@ -135,8 +135,8 @@ dependencies:
135
135
  requirement: *id008
136
136
  description: Heartbeat Client in Ruby
137
137
  email: kiessler@inceedo.com
138
- executables: []
139
-
138
+ executables:
139
+ - hbc
140
140
  extensions: []
141
141
 
142
142
  extra_rdoc_files:
@@ -150,8 +150,8 @@ files:
150
150
  - README.rdoc
151
151
  - Rakefile
152
152
  - VERSION
153
+ - bin/hbc
153
154
  - lib/heartbeat-client.rb
154
- - lib/heartbeat_proc.rb
155
155
  - test/helper.rb
156
156
  - test/test_heartbeat-client.rb
157
157
  homepage: http://github.com/okiess/heartbeat-client