riemann-tools 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/riemann-health CHANGED
@@ -2,31 +2,36 @@
2
2
 
3
3
  # Reports current CPU, disk, load average, and memory use to riemann.
4
4
 
5
- require 'trollop'
6
- require 'riemann/client'
7
-
8
- class Riemann::Health
9
- def initialize(opts)
10
- @host = opts[:host]
11
- @port = opts[:port]
12
- @interval = opts[:interval]
5
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
6
+
7
+ class Riemann::Tools::Health
8
+ include Riemann::Tools
9
+
10
+ opt :cpu_warning, "CPU warning threshold (fraction of total jiffies)", :default => 0.9
11
+ opt :cpu_critical, "CPU critical threshold (fraction of total jiffies)", :default => 0.95
12
+ opt :disk_warning, "Disk warning threshold (fraction of space used)", :default => 0.9
13
+ opt :disk_critical, "Disk critical threshold (fraction of space used)", :default => 0.95
14
+ opt :load_warning, "Load warning threshold (load average / core)", :default => 3
15
+ opt :load_critical, "Load critical threshold (load average / core)", :default => 8
16
+ opt :memory_warning, "Memory warning threshold (fraction of RAM)", :default => 0.85
17
+ opt :memory_critical, "Memory critical threshold (fraction of RAM)", :default => 0.95
18
+
19
+ def initialize
13
20
  @limits = {
14
- cpu: {critical: opts[:cpu_critical], warning: opts[:cpu_warning]},
15
- disk: {critical: opts[:disk_critical], warning: opts[:disk_warning]},
16
- :load => {critical: opts[:load_critical], warning: opts[:load_warning]},
17
- memory: {critical: opts[:memory_critical], warning: opts[:memory_warning]}
21
+ :cpu => {:critical => opts[:cpu_critical], :warning => opts[:cpu_warning]},
22
+ :disk => {:critical => opts[:disk_critical], :warning => opts[:disk_warning]},
23
+ :load => {:critical => opts[:load_critical], :warning => opts[:load_warning]},
24
+ :memory => {:critical => opts[:memory_critical], :warning => opts[:memory_warning]}
18
25
  }
19
-
20
- @client = Riemann::Client.new(:host => @host, :port => @port)
21
26
  end
22
27
 
23
28
  def alert(service, state, metric, description)
24
- @client << {
25
- service: service,
26
- state: state.to_s,
27
- metric: metric.to_f,
28
- description: description
29
- }
29
+ report(
30
+ :service => service,
31
+ :state => state.to_s,
32
+ :metric => metric.to_f,
33
+ :description => description
34
+ )
30
35
  end
31
36
 
32
37
  def cores
@@ -131,35 +136,11 @@ class Riemann::Health
131
136
  end
132
137
 
133
138
  def tick
134
- begin
135
- cpu
136
- memory
137
- load
138
- disk
139
- rescue => e
140
- $stderr.puts "#{e.class} #{e}\n#{e.backtrace.join "\n"}"
141
- sleep 10
142
- end
143
- end
144
-
145
- def run
146
- loop do
147
- tick
148
- sleep @interval
149
- end
139
+ cpu
140
+ memory
141
+ load
142
+ disk
150
143
  end
151
144
  end
152
145
 
153
- Riemann::Health.new(Trollop.options do
154
- opt :host, "Host", :default => '127.0.0.1'
155
- opt :port, "Port", :default => 5555
156
- opt :interval, "Seconds between updates", :default => 5
157
- opt :cpu_warning, "CPU warning threshold (fraction of total jiffies)", :default => 0.9
158
- opt :cpu_critical, "CPU critical threshold (fraction of total jiffies)", :default => 0.95
159
- opt :disk_warning, "Disk warning threshold (fraction of space used)", :default => 0.9
160
- opt :disk_critical, "Disk critical threshold (fraction of space used)", :default => 0.95
161
- opt :load_warning, "Load warning threshold (load average / core)", :default => 3
162
- opt :load_critical, "Load critical threshold (load average / core)", :default => 8
163
- opt :memory_warning, "Memory warning threshold (fraction of RAM)", :default => 0.85
164
- opt :memory_critical, "Memory critical threshold (fraction of RAM)", :default => 0.95
165
- end).run
146
+ Riemann::Tools::Health.run
data/bin/riemann-munin ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Gathers munin statistics and submits them to Riemann.
4
+
5
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
6
+
7
+ class Riemann::Tools::Munin
8
+ include Riemann::Tools
9
+ require 'munin-ruby'
10
+
11
+ def initialize
12
+ @munin = ::Munin::Node.new
13
+ end
14
+
15
+ def tick
16
+ services = opts[:services] || @munin.list
17
+ services.each do |service|
18
+ @munin.fetch(service).each do |service, parts|
19
+ parts.each do |part, metric|
20
+ report(
21
+ :service => "#{service} #{part}",
22
+ :metric => metric.to_f,
23
+ :state => 'ok',
24
+ :tags => ['munin']
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ opt :munin_host, "Munin hostname", :default => 'localhost'
32
+ opt :munin_port, "Munin port", :default => 4949
33
+ opt :services, "Munin services to translate (if not specified, all services are relayed)", :type => :strings
34
+ end
35
+
36
+ Riemann::Tools::Munin.run
data/bin/riemann-net ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Gathers munin statistics and submits them to Riemann.
4
+
5
+ require File.expand_path('../../lib/riemann/tools', __FILE__)
6
+
7
+ class Riemann::Tools::Net
8
+ include Riemann::Tools
9
+
10
+ opt :munin_host, "Munin hostname", :default => 'localhost'
11
+ opt :munin_port, "Munin port", :default => 4949
12
+ opt :services, "Munin services to translate (if not specified, all services are relayed)", :type => :strings
13
+
14
+ def initialize
15
+ @old_state = nil
16
+ end
17
+
18
+ def state
19
+ f = File.read('/proc/net/dev')
20
+ state = f.split("\n").inject({}) do |s, line|
21
+ if line =~ /\s*(\w+?):\s*([\s\d]+)\s*/
22
+ iface = $1
23
+ ['rx bytes',
24
+ 'rx packets',
25
+ 'rx errs',
26
+ 'rx drop',
27
+ 'rx fifo',
28
+ 'rx frame',
29
+ 'rx compressed',
30
+ 'rx multicast',
31
+ 'tx bytes',
32
+ 'tx packets',
33
+ 'tx drops',
34
+ 'tx fifo',
35
+ 'tx colls',
36
+ 'tx carrier',
37
+ 'tx compressed'].map do |service|
38
+ "#{iface} #{service}"
39
+ end.zip(
40
+ $2.split(/\s+/).map { |str| str.to_i }
41
+ ).each do |service, value|
42
+ s[service] = value
43
+ end
44
+ end
45
+
46
+ s
47
+ end
48
+ end
49
+
50
+ def tick
51
+ state = self.state
52
+
53
+ if @old_state
54
+ state.each do |service, metric|
55
+ delta = metric - @old_state[service]
56
+ svc_state = case service
57
+ when /drop$/
58
+ if metric > 0
59
+ 'warning'
60
+ else
61
+ 'ok'
62
+ end
63
+ when /errs$/
64
+ if metric > 0
65
+ 'warning'
66
+ else
67
+ 'ok'
68
+ end
69
+ else
70
+ 'ok'
71
+ end
72
+
73
+ report(
74
+ :service => service,
75
+ :metric => (delta / opts[:interval]),
76
+ :state => svc_state
77
+ )
78
+ end
79
+ end
80
+
81
+ @old_state = state
82
+ end
83
+ end
84
+
85
+ Riemann::Tools::Net.run
data/lib/riemann/tools.rb CHANGED
@@ -1,4 +1,83 @@
1
1
  module Riemann
2
2
  module Tools
3
+ require 'rubygems'
4
+ require 'trollop'
5
+ require 'riemann/client'
6
+
7
+ def self.included(base)
8
+ base.instance_eval do
9
+ def run
10
+ new.run
11
+ end
12
+
13
+ def opt(*a)
14
+ a.unshift :opt
15
+ @opts ||= []
16
+ @opts << a
17
+ end
18
+
19
+ def options
20
+ p = Trollop::Parser.new
21
+ @opts.each do |o|
22
+ p.send *o
23
+ end
24
+ Trollop::with_standard_exception_handling(p) do
25
+ p.parse ARGV
26
+ end
27
+ end
28
+
29
+ opt :host, "Riemann host", :default => '127.0.0.1'
30
+ opt :port, "Riemann port", :default => 5555
31
+ opt :interval, "Seconds between updates", :default => 5
32
+ end
33
+ end
34
+
35
+ def initialize
36
+ super
37
+ end
38
+
39
+ def tool_options
40
+ {}
41
+ end
42
+
43
+ # Returns parsed options (cached) from command line.
44
+ def options
45
+ @options ||= self.class.options
46
+ end
47
+ alias :opts :options
48
+
49
+ # Add a new command line option
50
+ def opt(*a)
51
+ @option_parser.opt *a
52
+ end
53
+
54
+ def report(event)
55
+ riemann << event
56
+ end
57
+
58
+ def riemann
59
+ @riemann ||= Riemann::Client.new(
60
+ :host => options[:host],
61
+ :port => options[:port]
62
+ )
63
+ end
64
+ alias :r :riemann
65
+
66
+ def run
67
+ t0 = Time.now
68
+ loop do
69
+ begin
70
+ tick
71
+ rescue => e
72
+ $stderr.puts "#{e.class} #{e}\n#{e.backtrace.join "\n"}"
73
+ end
74
+
75
+ # Sleep.
76
+ sleep(options[:interval] - ((Time.now - t0) % options[:interval]))
77
+ end
78
+ end
79
+
80
+ def tick
81
+ end
3
82
  end
4
83
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riemann-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-24 00:00:00.000000000 Z
12
+ date: 2012-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: riemann-client
16
- requirement: &8635900 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: 0.0.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *8635900
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.4
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: trollop
27
- requirement: &8635300 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,16 +37,41 @@ dependencies:
32
37
  version: 1.16.2
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *8635300
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.16.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: munin-ruby
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.2.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.1
36
62
  description:
37
63
  email: aphyr@aphyr.com
38
64
  executables:
65
+ - riemann-net
66
+ - riemann-munin
39
67
  - riemann-bench
40
68
  - riemann-health
41
69
  extensions: []
42
70
  extra_rdoc_files: []
43
71
  files:
44
72
  - lib/riemann/tools.rb
73
+ - bin/riemann-net
74
+ - bin/riemann-munin
45
75
  - bin/riemann-bench
46
76
  - bin/riemann-health
47
77
  - LICENSE
@@ -66,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
96
  version: '0'
67
97
  requirements: []
68
98
  rubyforge_project: riemann-tools
69
- rubygems_version: 1.8.10
99
+ rubygems_version: 1.8.22
70
100
  signing_key:
71
101
  specification_version: 3
72
102
  summary: HTTP dashboard for the distributed event system Riemann.