riemann-babbler 0.0.2 → 0.0.3

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/README.md ADDED
@@ -0,0 +1,70 @@
1
+ ### Установка
2
+ ```
3
+ gem install riemann-babbler
4
+ ````
5
+
6
+ ### Использование
7
+ ```
8
+ $ riemann-babbler --help
9
+ Riemann-babbler is plugin manager for riemann-tools.
10
+
11
+ Usage:
12
+ riemann-babbler [options]
13
+ where [options] are:
14
+ --config, -c <s>: Config file (default: /etc/riemann-babbler/config.yml)
15
+ --version, -v: Print version and exit
16
+ --help, -h: Show this message
17
+ ```
18
+
19
+ ### Описание конфига
20
+ Bubbler имеет собственные конифиги, значения полученные через --config будут смерджены
21
+ ```yaml
22
+ riemann:
23
+ host: riemann.host # хост riemann куда слать сообщения
24
+ port: 5555 # порт
25
+ tags: # таги которые будут сообшатся
26
+ - prod
27
+ - web
28
+ suffix: ".testing" # окончание `hostname`
29
+
30
+ plugins:
31
+ dirs:
32
+ - /etc/riemann/plugins # загружает все плагины из указаной дирректории
33
+ files:
34
+ - /var/lib/riemann-plugins/test.rb # подгружает плагин по указаному пути
35
+ ```
36
+ ##### Настройки конкретного плагина
37
+ ```yaml
38
+ plugins:
39
+ awesome_plugin:
40
+ service: some critical service # описание сервиса для поста на riemann
41
+ interval: 1 # как часто дергать плагин (в сек)
42
+ states:
43
+ warning: 80 # какой стейт давать плагину когда метрика перевалит за указанное значение
44
+ critical: 90 # соответственно стейт critical
45
+ some_parametr: "pgsql://username:password@database" # например необходимая настройка для плагина
46
+ ```
47
+
48
+ ### Написание собственного плагина
49
+ ```ruby
50
+ class Riemann::Babbler::Awesomeplugins
51
+ include Riemann::Babbler
52
+
53
+ # быстрый доступ к конфигу
54
+ def plugin
55
+ options.plugins.awesome_plugin
56
+ end
57
+
58
+ # то что будет вызыватся таймером по указаному interval
59
+ def tick
60
+ status = {
61
+ :service => plugin.service,
62
+ :state => 'ok'
63
+ }
64
+ report status
65
+ end
66
+
67
+ end
68
+ # обязательный вызов
69
+ Riemann::Babbler::Awesomeplugins.run
70
+ ```
@@ -60,6 +60,12 @@ unless configatron.plugins.dirs.nil?
60
60
  end
61
61
  end
62
62
 
63
+ unless configatron.plugins.files.nil?
64
+ configatron.plugins.files.each do |file|
65
+ plugins << file
66
+ end
67
+ end
68
+
63
69
  # start plugins
64
70
  Parallel.each( plugins, :in_threads => plugins.count ) do |plugin|
65
71
  require "#{plugin}"
data/config.yml CHANGED
@@ -1,16 +1,46 @@
1
1
  riemann:
2
- host: riemann-testing-01.undev.cc
2
+ host: localhost
3
3
  port: 5555
4
- tags:
5
- - prod
6
- - nptv
4
+
5
+ logger:
6
+ level: DEBUG
7
7
 
8
8
  plugins:
9
- dirs:
10
- - /etc/riemann-babbler/plugins
11
- dummy:
12
- service: dummy service
9
+
10
+ babbler:
11
+ service: keep alive
12
+ interval: 1
13
+
14
+ la:
15
+ service: load avverage
13
16
  interval: 1
14
17
  states:
15
- warning: 70
16
- critical: 90
18
+ warning: 4
19
+ critical: 10
20
+
21
+ disk:
22
+ service: disk usage
23
+ interval: 1
24
+ states:
25
+ warning: 0.7
26
+ critical: 0.85
27
+
28
+ cpu:
29
+ service: cpu usage
30
+ interval: 1
31
+ states:
32
+ warning: 0.7
33
+ critical: 0.85
34
+
35
+ memory:
36
+ service: memory ussage
37
+ interval: 1
38
+ report_free: true
39
+ report_total: true
40
+ states:
41
+ warning: 0.6
42
+ critical: 0.8
43
+
44
+ net:
45
+ service: net ussage
46
+ interval: 1
@@ -27,10 +27,18 @@ module Riemann
27
27
  alias :opts :options
28
28
 
29
29
  def report(event)
30
- event[:tags] unless options.riemann.tags.nil?
30
+ event[:tags] = options.riemann.tags unless options.riemann.tags.nil?
31
+ event[:host] = host
32
+ log.debug "Report status: #{event.inspect}"
31
33
  riemann << event
32
34
  end
33
35
 
36
+ def host
37
+ hostname = `hostname`.chomp.downcase
38
+ hostname += options.riemann.suffix unless options.riemann.suffix.nil?
39
+ hostname
40
+ end
41
+
34
42
  def riemann
35
43
  @riemann ||= Riemann::Client.new(
36
44
  :host => options.riemann.host,
@@ -0,0 +1,18 @@
1
+ class Riemann::Babbler::Dummy
2
+ include Riemann::Babbler
3
+
4
+ def plugin
5
+ options.plugins.babbler
6
+ end
7
+
8
+ def tick
9
+ status = {
10
+ :service => plugin.service,
11
+ :state => 'ok'
12
+ }
13
+ report status
14
+ end
15
+
16
+ end
17
+
18
+ Riemann::Babbler::Dummy.run
@@ -0,0 +1,38 @@
1
+ class Riemann::Babbler::Cpu
2
+ include Riemann::Babbler
3
+
4
+ def plugin
5
+ options.plugins.cpu
6
+ end
7
+
8
+ def cpu
9
+ cpu = File.read('/proc/stat')
10
+ cpu[/cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/]
11
+ u2, n2, s2, i2 = [$1, $2, $3, $4].map { |e| e.to_i }
12
+
13
+ if @old_cpu
14
+ u1, n1, s1, i1 = @old_cpu
15
+ used = (u2+n2+s2) - (u1+n1+s1)
16
+ total = used + i2-i1
17
+ fraction = used.to_f / total
18
+ end
19
+
20
+ @old_cpu = [u2, n2, s2, i2]
21
+ log.error fraction
22
+ fraction
23
+ end
24
+
25
+ def tick
26
+ current_state = cpu
27
+ # при первом проходе у нас будет nil
28
+ next unless current_state
29
+ report({
30
+ :service => plugin.service,
31
+ :state => state(current_state),
32
+ :metric => current_state
33
+ })
34
+ end
35
+
36
+ end
37
+
38
+ Riemann::Babbler::Cpu.run
@@ -0,0 +1,32 @@
1
+ class Riemann::Babbler::Disk
2
+ include Riemann::Babbler
3
+
4
+ def plugin
5
+ options.plugins.disk
6
+ end
7
+
8
+ def disk
9
+ disk = {}
10
+ `df -P`.split(/\n/).each do |r|
11
+ f = r.split(/\s+/)
12
+ next unless f[0] =~ /^\//
13
+ next if f[0] == 'Filesystem'
14
+ x = f[4].to_f/100
15
+ disk.merge!({f[5] => x})
16
+ end
17
+ return disk
18
+ end
19
+
20
+ def tick
21
+ disk.each do |point, free|
22
+ report({
23
+ :service => plugin.service + " on #{point}",
24
+ :state => state(free),
25
+ :metric => free
26
+ })
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ Riemann::Babbler::Disk.run
@@ -0,0 +1,24 @@
1
+ class Riemann::Babbler::La
2
+ include Riemann::Babbler
3
+
4
+ def plugin
5
+ options.plugins.la
6
+ end
7
+
8
+ def la
9
+ File.read('/proc/loadavg').split(/\s+/)[2].to_f
10
+ end
11
+
12
+ def tick
13
+ current_state = la
14
+ status = {
15
+ :service => plugin.service,
16
+ :state => state(current_state),
17
+ :metric => current_state
18
+ }
19
+ report status
20
+ end
21
+
22
+ end
23
+
24
+ Riemann::Babbler::La.run
@@ -0,0 +1,43 @@
1
+ class Riemann::Babbler::Memory
2
+ include Riemann::Babbler
3
+
4
+ def plugin
5
+ options.plugins.memory
6
+ end
7
+
8
+ def memory
9
+ m = File.read('/proc/meminfo').split(/\n/).inject({}) { |info, line|
10
+ x = line.split(/:?\s+/)
11
+ # Assume kB...
12
+ info[x[0]] = x[1].to_i
13
+ info
14
+ }
15
+ free = m['MemFree'].to_i + m['Buffers'].to_i + m['Cached'].to_i
16
+ total = m['MemTotal'].to_i
17
+ fraction = 1 - (free.to_f / total)
18
+ return {:free => (free.to_f/1024), :fraction => fraction, :total => (total.to_f/1024)}
19
+ end
20
+
21
+ def tick
22
+ current_state = memory
23
+ # соотношение свободной/занятой
24
+ report({
25
+ :service => plugin.service,
26
+ :state => state(current_state[:fraction]),
27
+ :metric => current_state[:fraction]
28
+ })
29
+ # постим сообщение о свобоной памяти
30
+ report({
31
+ :service => plugin.service,
32
+ :metric => current_state[:free]
33
+ }) if plugin.report_free
34
+ # постим сообение о том сколько у нас вообще памяти
35
+ report({
36
+ :service => plugin.service,
37
+ :metric => current_state[:total]
38
+ }) if plugin.report_total
39
+ end
40
+
41
+ end
42
+
43
+ Riemann::Babbler::Memory.run
@@ -0,0 +1,51 @@
1
+ class Riemann::Babbler::Net
2
+ include Riemann::Babbler
3
+
4
+ def plugin
5
+ options.plugins.net
6
+ end
7
+
8
+ def net
9
+ f = File.read('/proc/net/dev')
10
+ net = f.split("\n").inject({}) do |s, line|
11
+ if line =~ /\s*(\w+?):\s*([\s\d]+)\s*/
12
+ iface = $1
13
+
14
+ ['rx bytes',
15
+ 'rx packets',
16
+ 'rx errs',
17
+ 'rx drop',
18
+ 'rx fifo',
19
+ 'rx frame',
20
+ 'rx compressed',
21
+ 'rx multicast',
22
+ 'tx bytes',
23
+ 'tx packets',
24
+ 'tx drops',
25
+ 'tx fifo',
26
+ 'tx colls',
27
+ 'tx carrier',
28
+ 'tx compressed'].map do |service|
29
+ "#{iface} #{service}"
30
+ end.zip(
31
+ $2.split(/\s+/).map { |str| str.to_i }
32
+ ).each do |service, value|
33
+ s[service] = value
34
+ end
35
+ end
36
+
37
+ puts s
38
+ end
39
+ end
40
+
41
+ def tick
42
+ status = {
43
+ :service => plugin.service,
44
+ :state => 'ok'
45
+ }
46
+ report status
47
+ end
48
+
49
+ end
50
+
51
+ Riemann::Babbler::Net.run
@@ -1,3 +1,3 @@
1
1
  module Babbler
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riemann-babbler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -95,19 +95,25 @@ description: Some plugins mannager for riemann.
95
95
  email:
96
96
  - vadv.mkn@gmail.com
97
97
  executables:
98
- - riemann-babbler.rb
98
+ - riemann-babbler
99
99
  extensions: []
100
100
  extra_rdoc_files: []
101
101
  files:
102
102
  - .gitignore
103
103
  - Gemfile
104
104
  - Gemfile.lock
105
+ - README.md
105
106
  - Rakefile
106
- - bin/riemann-babbler.rb
107
+ - bin/riemann-babbler
107
108
  - config.yml
108
109
  - lib/deep_merge.rb
109
110
  - lib/riemann/babbler/plugin.rb
110
- - lib/riemann/babbler/plugins/dummy.rb
111
+ - lib/riemann/babbler/plugins/babbler.rb
112
+ - lib/riemann/babbler/plugins/cpu.rb
113
+ - lib/riemann/babbler/plugins/disk.rb
114
+ - lib/riemann/babbler/plugins/la.rb
115
+ - lib/riemann/babbler/plugins/memory.rb
116
+ - lib/riemann/babbler/plugins/net.rb
111
117
  - lib/riemann/version.rb
112
118
  - riemann-babbler.gemspec
113
119
  homepage: https://github.com/vadv/riemann-babbler
@@ -123,9 +129,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
129
  - - ! '>='
124
130
  - !ruby/object:Gem::Version
125
131
  version: '0'
126
- segments:
127
- - 0
128
- hash: -110392361130597825
129
132
  required_rubygems_version: !ruby/object:Gem::Requirement
130
133
  none: false
131
134
  requirements:
@@ -1,25 +0,0 @@
1
- class Riemann::Babbler::Dummy
2
- include Riemann::Babbler
3
-
4
- def plugin
5
- options.plugins.dummy
6
- end
7
-
8
- def status
9
- Random.rand(100)
10
- end
11
-
12
- def tick
13
- current_status = status
14
- status = {
15
- :service => plugin.service,
16
- :metric => current_status,
17
- :state => state(current_status)
18
- }
19
- log.debug "Report status: #{status.inspect}"
20
- report status
21
- end
22
-
23
- end
24
-
25
- Riemann::Babbler::Dummy.run