warcorrespondent 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,54 @@
1
1
  = warcorrespondent
2
2
 
3
- warcorrespondent reports to the warroom.
3
+ <em>warcorrespondent reports to the warroom.</em>
4
+
5
+ warcorrespondent is a tool that reports measured data in json format to a web application.
6
+ At the moment the functionality is only available in a standalone binary.
7
+
8
+ == Data Format
9
+ The data is subitted to the web app via POST, setting <em>secret</em> and <em>data</em>.
10
+ <em>secret</em> is set to the string specified in the configuration file and is meant as a simple authentication method.
11
+ <em>data</em> is a json structure and looks like this:
12
+ [
13
+ {
14
+ identifier:"hosts:example:users",
15
+ timestamp: 1271146141,
16
+ type:"integer",
17
+ value:12
18
+ },
19
+ {
20
+ identifier: "apps:example:dontations",
21
+ timestamp: 1271146141,
22
+ type:"money",
23
+ unit:"EUR",
24
+ value:100
25
+ }
26
+ ]
27
+
28
+
29
+ == Configuration
30
+
31
+ The main configuration lies in /etc/warcorrespondent/warcorrespondent.yml by default.
32
+ Example:
33
+ url: http://localhost:3000/graphs
34
+ secret: adsfdsafdsfa
35
+
36
+ The reporters are by default defined in /etc/warcorrespondent/reporters/*.rb
37
+ WarCorrespondent::Reporter.new(:timeout => 300, :identifier => 'hosts:example:users') do
38
+ count = 0
39
+ IO.popen('who') do |p|
40
+ count = p.readlines.size
41
+ end
42
+ {:value => count, :type => "integer"}
43
+ end
44
+ The block may either return one hash or an array of hashes.
45
+
46
+
47
+
48
+ To start/stop the daemon use:
49
+ warcorrespondent start
50
+ or
51
+ warcorrespondent stop
4
52
 
5
53
  == Note on Patches/Pull Requests
6
54
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/bin/warcorrespondent CHANGED
@@ -7,5 +7,10 @@ rescue LoadError
7
7
  require 'warcorrespondent'
8
8
  end
9
9
 
10
- WarCorrespondent::setup
11
- WarCorrespondent::run
10
+ require 'daemons'
11
+
12
+ Daemons.run_proc('warcorrespondent.rb') do
13
+ WarCorrespondent::setup
14
+ WarCorrespondent::run
15
+ end
16
+
@@ -3,7 +3,7 @@ module WarCorrespondent
3
3
  attr_accessor :timeout
4
4
  attr_accessor :identifier
5
5
  def initialize(args, &block)
6
- @timeout = 5
6
+ @timeout = 300
7
7
  [:timeout, :identifier].each do |key|
8
8
  if args[key] && self.respond_to?("#{key}=")
9
9
  self.send("#{key}=",args[key])
@@ -17,10 +17,12 @@ module WarCorrespondent
17
17
  data = @block.call
18
18
  data = [data] if data.class == Hash
19
19
  data.map! do |e|
20
- e[:identifier] = "#{identifier}#{e[:identifier] ? (':' + e[:identifier]) : ''}".gsub(/:+/, ':')
20
+ e = {:timestamp => Time.now.to_i}.merge(e)
21
+ e[:identifier] = identifier + (e[:identifier] ? (':' + e[:identifier]) : '')
22
+ e[:identifier].gsub!(/:+/, ':')
21
23
  e
22
24
  end
23
- WarCorrespondent::update({:payload => data, :timestamp => Time.now.to_i})
25
+ WarCorrespondent::update(data)
24
26
  end
25
27
 
26
28
  def run
@@ -36,7 +38,7 @@ module WarCorrespondent
36
38
 
37
39
 
38
40
  end
39
- puts Dir.pwd
41
+
40
42
  Dir.glob(File.dirname(__FILE__) + "/reporters/*.rb") do |i|
41
43
  require i
42
44
  end
@@ -0,0 +1,42 @@
1
+ module WarCorrespondent
2
+ module Reporters
3
+ module Linux
4
+ class CPU < WarCorrespondent::Reporter
5
+ def initialize(args)
6
+ @block = Proc.new do
7
+ get_data = Proc.new do
8
+ data = nil
9
+ File.open('/proc/stat','r') do |f|
10
+ f.readlines.select{|l| l =~ /^cpu /}.each do |l|
11
+ data = l.split
12
+ data.shift
13
+ end
14
+ end
15
+ data
16
+ end
17
+ if !@prev_data
18
+ @prev_data = get_data.call
19
+ sleep 5
20
+ end
21
+ @new_data = get_data.call
22
+ difference = @prev_data.zip(@new_data).map{|i| i[1].to_f - i[0].to_f}
23
+ sum = difference.inject{|a,b| a+b}
24
+ difference.map!{|i| i/sum}
25
+ @prev_data = @new_data
26
+ [
27
+ {:identifier => "cpu:user", :type => "float", :value => difference[0]},
28
+ {:identifier => "cpu:nice", :type => "float", :value => difference[1]},
29
+ {:identifier => "cpu:system", :type => "float", :value => difference[2]},
30
+ {:identifier => "cpu:idle", :type => "float", :value => difference[3]},
31
+ {:identifier => "cpu:running", :type => "float", :value => difference[4]},
32
+ {:identifier => "cpu:iowait", :type => "float", :value => difference[5]},
33
+ {:identifier => "cpu:irq", :type => "float", :value => difference[6]},
34
+ {:identifier => "cpu:softirq", :type => "float", :value => difference[7]}
35
+ ]
36
+ end
37
+ super(args)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ module WarCorrespondent
2
+ module Reporters
3
+ module Linux
4
+ class LoadAvg < WarCorrespondent::Reporter
5
+ def initialize(args)
6
+
7
+ @block = Proc.new do
8
+ File.open('/proc/loadavg','r') do |f|
9
+ line = f.readline
10
+ load_one_min, load_five_min, load_fivteen_min, processes = line.split
11
+ processes_running, processes_total = processes.split('/')
12
+ [
13
+ {:identifier => "load:1", :type => "float", :value => load_one_min},
14
+ {:identifier => "load:5", :type => "float", :value => load_five_min},
15
+ {:identifier => "load:15", :type => "float", :value => load_fivteen_min},
16
+ {:identifier => "processes:running", :type => "integer", :value => processes_running},
17
+ {:identifier => "processes:running", :type => "integer", :value => processes_total}
18
+ ]
19
+ end
20
+ end
21
+ super(args)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ module WarCorrespondent
2
+ module Reporters
3
+ module Linux
4
+ class Memory < WarCorrespondent::Reporter
5
+ def initialize(args)
6
+ @block = Proc.new do
7
+ items = {
8
+ 'mem:free' => 'MemFree',
9
+ 'mem:cached' => 'Cached',
10
+ 'mem:buffers' => 'Buffers',
11
+ 'mem:total' => 'MemTotal',
12
+ 'swap:total' => 'SwapTotal',
13
+ 'swap:free' => 'SwapFree'
14
+
15
+ }
16
+ File.open('/proc/meminfo','r') do |f|
17
+ f.readlines.each do |l|
18
+ items.each_pair do |i, k|
19
+ if matches = Regexp.new("#{k}: +([0-9]+)").match(l)
20
+ items[i] = matches[1].to_i
21
+ end
22
+ end
23
+ end
24
+ items['mem:used'] = items['mem:total'] - items['mem:free'] - items['mem:cached'] - items['mem:buffers']
25
+ items['swap:used'] = items['mem:total'] - items['mem:free']
26
+ end
27
+ items.map{|k,v|
28
+ {:identifier => k, :value => v, :type => "integer", :unit => "kB"}
29
+ }
30
+ end
31
+ super(args)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ module WarCorrespondent
2
+ module Reporters
3
+ module Linux
4
+ class Net < WarCorrespondent::Reporter
5
+ def initialize(args)
6
+ @block = Proc.new do
7
+ get_data = Proc.new do
8
+ delta = nil
9
+ data = {}
10
+ File.open('/proc/net/dev','r') do |f|
11
+ f.readlines.select{|l| l =~ /:/}.each do |l|
12
+ l.gsub!(/ +/,' ').strip!
13
+ id, body = l.split(':')
14
+ body = body.strip.split
15
+ data[id] = [body[0],body[1],body[8],body[9]]
16
+ end
17
+ end
18
+ data
19
+ end
20
+ if !@prev_data
21
+ @prev_data = get_data.call
22
+ delta = 5
23
+ sleep 5
24
+ end
25
+ delta ||= timeout
26
+ @new_data = get_data.call
27
+ difference ={}
28
+ @prev_data.keys.each{|k|
29
+ difference[k] = @prev_data[k].zip(@new_data[k]).map{|i| (i[1].to_f - i[0].to_f)/delta}
30
+ }
31
+ @prev_data = @new_data
32
+ result=[]
33
+ difference.each{|k,body|
34
+ result << {:identifier => "net:#{k}:rbytes", :value => body[0], :type => "integer"}
35
+ result << {:identifier => "net:#{k}:rpackets", :value => body[1], :type => "integer"}
36
+ result << {:identifier => "net:#{k}:sbytes", :value => body[2], :type => "integer"}
37
+ result << {:identifier => "net:#{k}:spackets", :value => body[3], :type => "integer"}
38
+ }
39
+ result
40
+ end
41
+ super(args)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,4 @@
1
1
  require 'net/http'
2
- require 'pp'
3
2
  module WarCorrespondent
4
3
  class Uplink
5
4
 
@@ -16,11 +15,16 @@ module WarCorrespondent
16
15
 
17
16
  def sync
18
17
  return if @@messages.size == 0
19
- begin
20
- message = @@messages.shift
21
- post(encode(message))
22
- rescue
23
- add(message)
18
+ buffer = []
19
+ 10.times do
20
+ buffer << @@messages.shift if @@messages.size > 0
21
+ end
22
+ buffer.each do |message|
23
+ begin
24
+ post(encode(message))
25
+ rescue
26
+ add(message)
27
+ end
24
28
  end
25
29
  end
26
30
 
@@ -38,9 +42,7 @@ module WarCorrespondent
38
42
  end
39
43
 
40
44
  def post(message)
41
- pp({'secret' => secret, 'data' => message})
42
- res = Net::HTTP.post_form(URI.parse(url),
43
- {'secret'=>secret, 'data'=>message})
45
+ res = Net::HTTP.post_form(URI.parse(url), {'secret'=>secret, 'data'=>message})
44
46
  raise if res.code == 200
45
47
  end
46
48
 
@@ -1,7 +1,6 @@
1
1
  require 'warcorrespondent/reporter.rb'
2
2
  require 'warcorrespondent/uplink.rb'
3
3
  require 'json'
4
- require 'pp'
5
4
 
6
5
  module WarCorrespondent
7
6
  CONFIG_FILE = '/etc/warcorrespondent/warcorrespondent.yml'
@@ -14,7 +13,7 @@ module WarCorrespondent
14
13
  @@uplink.url = @@config['url']
15
14
  @@uplink.secret = @@config['secret']
16
15
  rescue
17
- puts "Could not load config file #{CONFIG_FILE}"
16
+ raise "Could not load config file #{CONFIG_FILE}"
18
17
  exit
19
18
  end
20
19
  Dir.glob("#{REPORTERS_DIRECTORIES}/*.rb").each do |i|
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Stefan Maier
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-12 00:00:00 +02:00
17
+ date: 2010-04-17 00:00:00 +02:00
18
18
  default_executable: warcorrespondent
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -48,6 +48,10 @@ files:
48
48
  - bin/warcorrespondent
49
49
  - lib/warcorrespondent.rb
50
50
  - lib/warcorrespondent/reporter.rb
51
+ - lib/warcorrespondent/reporters/linux/cpu.rb
52
+ - lib/warcorrespondent/reporters/linux/loadavg.rb
53
+ - lib/warcorrespondent/reporters/linux/memory.rb
54
+ - lib/warcorrespondent/reporters/linux/net.rb
51
55
  - lib/warcorrespondent/reporters/users.rb
52
56
  - lib/warcorrespondent/uplink.rb
53
57
  - test/helper.rb