riemann-tools 1.0.0 → 1.1.0

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +11 -0
  3. data/.github/workflows/ci.yml +13 -0
  4. data/.github/workflows/codeql-analysis.yml +72 -0
  5. data/.rubocop.yml +32 -0
  6. data/CHANGELOG.md +31 -2
  7. data/README.markdown +8 -24
  8. data/Rakefile +4 -2
  9. data/SECURITY.md +42 -0
  10. data/bin/riemann-apache-status +92 -78
  11. data/bin/riemann-bench +54 -49
  12. data/bin/riemann-cloudant +44 -40
  13. data/bin/riemann-consul +82 -76
  14. data/bin/riemann-dir-files-count +53 -47
  15. data/bin/riemann-dir-space +53 -47
  16. data/bin/riemann-diskstats +78 -75
  17. data/bin/riemann-fd +68 -48
  18. data/bin/riemann-freeswitch +108 -103
  19. data/bin/riemann-haproxy +46 -40
  20. data/bin/riemann-health +4 -343
  21. data/bin/riemann-kvminstance +18 -13
  22. data/bin/riemann-memcached +35 -29
  23. data/bin/riemann-net +4 -104
  24. data/bin/riemann-nginx-status +74 -67
  25. data/bin/riemann-ntp +4 -33
  26. data/bin/riemann-portcheck +40 -31
  27. data/bin/riemann-proc +96 -90
  28. data/bin/riemann-varnish +51 -45
  29. data/bin/riemann-zookeeper +38 -34
  30. data/lib/riemann/tools/health.rb +347 -0
  31. data/lib/riemann/tools/net.rb +104 -0
  32. data/lib/riemann/tools/ntp.rb +41 -0
  33. data/lib/riemann/tools/version.rb +1 -1
  34. data/lib/riemann/tools.rb +37 -40
  35. data/riemann-tools.gemspec +4 -1
  36. data/tools/riemann-aws/{Rakefile.rb → Rakefile} +2 -0
  37. data/tools/riemann-aws/bin/riemann-aws-billing +72 -66
  38. data/tools/riemann-aws/bin/riemann-aws-rds-status +55 -41
  39. data/tools/riemann-aws/bin/riemann-aws-sqs-status +37 -31
  40. data/tools/riemann-aws/bin/riemann-aws-status +63 -51
  41. data/tools/riemann-aws/bin/riemann-elb-metrics +149 -148
  42. data/tools/riemann-aws/bin/riemann-s3-list +70 -65
  43. data/tools/riemann-aws/bin/riemann-s3-status +85 -82
  44. data/tools/riemann-chronos/{Rakefile.rb → Rakefile} +2 -0
  45. data/tools/riemann-chronos/bin/riemann-chronos +136 -119
  46. data/tools/riemann-docker/{Rakefile.rb → Rakefile} +2 -0
  47. data/tools/riemann-docker/bin/riemann-docker +163 -174
  48. data/tools/riemann-elasticsearch/{Rakefile.rb → Rakefile} +2 -0
  49. data/tools/riemann-elasticsearch/bin/riemann-elasticsearch +155 -147
  50. data/tools/riemann-marathon/{Rakefile.rb → Rakefile} +2 -0
  51. data/tools/riemann-marathon/bin/riemann-marathon +138 -122
  52. data/tools/riemann-mesos/{Rakefile.rb → Rakefile} +2 -0
  53. data/tools/riemann-mesos/bin/riemann-mesos +125 -110
  54. data/tools/riemann-munin/{Rakefile.rb → Rakefile} +2 -0
  55. data/tools/riemann-munin/bin/riemann-munin +28 -22
  56. data/tools/riemann-rabbitmq/{Rakefile.rb → Rakefile} +2 -0
  57. data/tools/riemann-rabbitmq/bin/riemann-rabbitmq +226 -222
  58. data/tools/riemann-riak/{Rakefile.rb → Rakefile} +2 -0
  59. data/tools/riemann-riak/bin/riemann-riak +281 -289
  60. data/tools/riemann-riak/riak_status/riak_status.rb +39 -39
  61. metadata +65 -16
data/bin/riemann-health CHANGED
@@ -1,349 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
3
 
4
- # Reports current CPU, disk, load average, and memory use to riemann.
5
-
6
- require File.expand_path('../../lib/riemann/tools', __FILE__)
7
- require File.expand_path('../../lib/riemann/tools/utils', __FILE__)
8
-
9
- class Riemann::Tools::Health
10
- include Riemann::Tools
11
- include Riemann::Tools::Utils
12
-
13
- opt :cpu_warning, "CPU warning threshold (fraction of total jiffies)", :default => 0.9
14
- opt :cpu_critical, "CPU critical threshold (fraction of total jiffies)", :default => 0.95
15
- opt :disk_warning, "Disk warning threshold (fraction of space used)", :default => 0.9
16
- opt :disk_critical, "Disk critical threshold (fraction of space used)", :default => 0.95
17
- opt :load_warning, "Load warning threshold (load average / core)", :default => 3
18
- opt :load_critical, "Load critical threshold (load average / core)", :default => 8
19
- opt :memory_warning, "Memory warning threshold (fraction of RAM)", :default => 0.85
20
- opt :memory_critical, "Memory critical threshold (fraction of RAM)", :default => 0.95
21
- opt :checks, "A list of checks to run.", :type => :strings, :default => ['cpu', 'load', 'memory', 'disk']
22
-
23
- def initialize
24
- @limits = {
25
- :cpu => {:critical => opts[:cpu_critical], :warning => opts[:cpu_warning]},
26
- :disk => {:critical => opts[:disk_critical], :warning => opts[:disk_warning]},
27
- :load => {:critical => opts[:load_critical], :warning => opts[:load_warning]},
28
- :memory => {:critical => opts[:memory_critical], :warning => opts[:memory_warning]}
29
- }
30
- case (@ostype = `uname -s`.chomp.downcase)
31
- when 'darwin'
32
- @cores = `sysctl -n hw.ncpu`.to_i
33
- @cpu = method :darwin_cpu
34
- @disk = method :disk
35
- @load = method :darwin_load
36
- @memory = method :darwin_memory
37
- darwin_top
38
- when 'freebsd'
39
- @cores = `sysctl -n hw.ncpu`.to_i
40
- @cpu = method :freebsd_cpu
41
- @disk = method :disk
42
- @load = method :bsd_load
43
- @memory = method :freebsd_memory
44
- when 'openbsd'
45
- @cores = `sysctl -n hw.ncpu`.to_i
46
- @cpu = method :openbsd_cpu
47
- @disk = method :disk
48
- @load = method :bsd_load
49
- @memory = method :openbsd_memory
50
- when 'sunos'
51
- @cores = `mpstat -a 2>/dev/null`.split[33].to_i
52
- @cpu = method :sunos_cpu
53
- @disk = method :disk
54
- @load = method :bsd_load
55
- @memory = method :sunos_memory
56
- else
57
- @cores = `nproc`.to_i
58
- puts "WARNING: OS '#{@ostype}' not explicitly supported. Falling back to Linux" unless @ostype == "linux"
59
- @cpu = method :linux_cpu
60
- @disk = method :disk
61
- @load = method :linux_load
62
- @memory = method :linux_memory
63
- @supports_exclude_type = `df --help 2>&1 | grep -e "--exclude-type"` != ""
64
- end
65
-
66
- opts[:checks].each do |check|
67
- case check
68
- when "disk"
69
- @disk_enabled = true
70
- when "load"
71
- @load_enabled = true
72
- when "cpu"
73
- @cpu_enabled = true
74
- when "memory"
75
- @memory_enabled = true
76
- end
77
- end
78
- end
79
-
80
- def alert(service, state, metric, description)
81
- report(
82
- :service => service.to_s,
83
- :state => state.to_s,
84
- :metric => metric.to_f,
85
- :description => description
86
- )
87
- end
88
-
89
- def report_pct(service, fraction, report)
90
- if fraction
91
- if fraction > @limits[service][:critical]
92
- alert service, :critical, fraction, "#{sprintf("%.2f", fraction * 100)}% #{report}"
93
- elsif fraction > @limits[service][:warning]
94
- alert service, :warning, fraction, "#{sprintf("%.2f", fraction * 100)}% #{report}"
95
- else
96
- alert service, :ok, fraction, "#{sprintf("%.2f", fraction * 100)}% #{report}"
97
- end
98
- end
99
- end
100
-
101
- def linux_cpu
102
- new = File.read('/proc/stat')
103
- unless new[/cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/]
104
- alert 'cpu', :unknown, nil, "/proc/stat doesn't include a CPU line"
105
- return false
106
- end
107
- u2, n2, s2, i2 = [$1, $2, $3, $4].map { |e| e.to_i }
108
-
109
- if @old_cpu
110
- u1, n1, s1, i1 = @old_cpu
111
-
112
- used = (u2+n2+s2) - (u1+n1+s1)
113
- total = used + i2-i1
114
- fraction = used.to_f / total
115
-
116
- report_pct :cpu, fraction, "user+nice+system\n\n#{reverse_numeric_sort_with_header(`ps -eo pcpu,pid,comm`)}"
117
- end
118
-
119
- @old_cpu = [u2, n2, s2, i2]
120
- end
121
-
122
- def linux_load
123
- load = File.read('/proc/loadavg').split(/\s+/)[0].to_f / @cores
124
- if load > @limits[:load][:critical]
125
- alert "load", :critical, load, "1-minute load average/core is #{load}"
126
- elsif load > @limits[:load][:warning]
127
- alert "load", :warning, load, "1-minute load average/core is #{load}"
128
- else
129
- alert "load", :ok, load, "1-minute load average/core is #{load}"
130
- end
131
- end
132
-
133
- def linux_memory
134
- m = File.read('/proc/meminfo').split(/\n/).inject({}) { |info, line|
135
- x = line.split(/:?\s+/)
136
- # Assume kB...
137
- info[x[0]] = x[1].to_i
138
- info
139
- }
140
-
141
- free = m['MemFree'].to_i + m['Buffers'].to_i + m['Cached'].to_i
142
- total = m['MemTotal'].to_i
143
- fraction = 1 - (free.to_f / total)
144
-
145
- report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -eo pmem,pid,comm`)}"
146
- end
147
-
148
- def freebsd_cpu
149
- u2, n2, s2, t2, i2 = `sysctl -n kern.cp_time 2>/dev/null`.split.map{ |e| e.to_i } #FreeBSD has 5 cpu stats
150
-
151
- if @old_cpu
152
- u1, n1, s1, t1, i1 = @old_cpu
153
-
154
- used = (u2+n2+s2+t2) - (u1+n1+s1+t1)
155
- total = used + i2-i1
156
- fraction = used.to_f / total
157
-
158
- report_pct :cpu, fraction, "user+nice+sytem+interrupt\n\n#{reverse_numeric_sort_with_header(`ps -axo pcpu,pid,comm`)}"
159
- end
160
-
161
- @old_cpu = [u2, n2, s2, t2, i2]
162
- end
163
-
164
- def openbsd_cpu
165
- u2, n2, s2, t2, i2 = `sysctl -n kern.cp_time 2>/dev/null`.split(',').map{ |e| e.to_i } #OpenBSD separates with ,
166
-
167
- if @old_cpu
168
- u1, n1, s1, t1, i1 = @old_cpu
4
+ Process.setproctitle($PROGRAM_NAME)
169
5
 
170
- used = (u2+n2+s2+t2) - (u1+n1+s1+t1)
171
- total = used + i2-i1
172
- fraction = used.to_f / total
173
-
174
- report_pct :cpu, fraction, "user+nice+sytem+interrupt\n\n#{reverse_numeric_sort_with_header(`ps -axo pcpu,pid,comm`)}"
175
- end
176
-
177
- @old_cpu = [u2, n2, s2, t2, i2]
178
- end
179
-
180
- def sunos_cpu
181
- mpstats = `mpstat -a 2>/dev/null`.split
182
- u2 = mpstats[29].to_i
183
- s2 = mpstats[30].to_i
184
- t2 = mpstats[31].to_i
185
- i2 = mpstats[32].to_i
186
-
187
- if @old_cpu
188
- u1, s1, t1, i1 = @old_cpu
189
-
190
- used = (u2+s2+t2) - (u1+s1+t1)
191
- total = used + i2-i1
192
- if i2 == i1 && used == 0 #If the system is <1% used in both samples then total will be 0 + (99 - 99), avoid a div by 0
193
- fraction = 0
194
- else
195
- fraction = used.to_f / total
196
- end
197
-
198
- report_pct :cpu, fraction, "user+sytem+interrupt\n\n#{reverse_numeric_sort_with_header(`ps -ao pcpu,pid,comm`)}"
199
- end
200
-
201
- @old_cpu = [u2, s2, t2, i2]
202
- end
203
-
204
- def bsd_load
205
- m = `uptime`.split(':')[-1].chomp.gsub(/\s+/,'').split(',')
206
- load = m[0].to_f / @cores
207
- if load > @limits[:load][:critical]
208
- alert "load", :critical, load, "1-minute load average/core is #{load}"
209
- elsif load > @limits[:load][:warning]
210
- alert "load", :warning, load, "1-minute load average/core is #{load}"
211
- else
212
- alert "load", :ok, load, "1-minute load average/core is #{load}"
213
- end
214
- end
215
-
216
- def freebsd_memory
217
- meminfo = `sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_wire_count vm.stats.vm.v_active_count 2>/dev/null`.chomp.split
218
- fraction = (meminfo[1].to_f + meminfo[2].to_f) / meminfo[0].to_f
219
-
220
- report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -axo pmem,pid,comm`)}"
221
- end
222
-
223
- def openbsd_memory
224
- meminfo = `vmstat 2>/dev/null`.chomp.split
225
- fraction = meminfo[28].to_f / meminfo[29].to_f #The ratio of active to free memory unlike the others :(
226
-
227
- report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -axo pmem,pid,comm`)}"
228
- end
229
-
230
- def sunos_memory
231
- meminfo = `vmstat 2>/dev/null`.chomp.split
232
- total_mem = `prtconf | grep Memory`.split[2].to_f * 1024 # reports in GB but vmstat is in MB
233
- fraction = ( total_mem - meminfo[32].to_f ) / total_mem
234
-
235
- report_pct :memory, fraction, "used\n\n#{reverse_numeric_sort_with_header(`ps -ao pmem,pid,comm`)}"
236
- end
237
-
238
- def darwin_top
239
- raw = `top -l 1 | grep -i "^\\(cpu\\|physmem\\|load\\)"`.chomp
240
- @topdata = {:stamp => Time.now.to_i }
241
- raw.each_line do |ln|
242
- if ln.match(/Load Avg: [0-9.]+, [0-9.]+, ([0-9.])+/i)
243
- @topdata[:load] = $1.to_f
244
- elsif ln.match(/CPU usage: [0-9.]+% user, [0-9.]+% sys, ([0-9.]+)% idle/i)
245
- @topdata[:cpu] = 1 - ($1.to_f / 100)
246
- elsif mdat = ln.match(/PhysMem: ([0-9]+)([BKMGT]) wired, ([0-9]+)([BKMGT]) active, ([0-9]+)([BKMGT]) inactive, ([0-9]+)([BKMGT]) used, ([0-9]+)([BKMGT]) free/i)
247
- wired = mdat[1].to_i * (1024 ** "BKMGT".index(mdat[2]))
248
- active = mdat[3].to_i * (1024 ** "BKMGT".index(mdat[4]))
249
- inactive = mdat[5].to_i * (1024 ** "BKMGT".index(mdat[6]))
250
- used = mdat[7].to_i * (1024 ** "BKMGT".index(mdat[8]))
251
- free = mdat[9].to_i * (1024 ** "BKMGT".index(mdat[10]))
252
- @topdata[:memory] = (wired + active + used).to_f / (wired + active + used + inactive + free)
253
- # This is for OSX Mavericks which
254
- # uses a different format for top
255
- # Example: PhysMem: 4662M used (1328M wired), 2782M unused.
256
- elsif mdat = ln.match(/PhysMem: ([0-9]+)([BKMGT]) used \(([0-9]+)([BKMGT]) wired\), ([0-9]+)([BKMGT]) unused/i)
257
- used = mdat[1].to_i * (1024 ** "BKMGT".index(mdat[2]))
258
- wired = mdat[3].to_i * (1024 ** "BKMGT".index(mdat[4]))
259
- unused = mdat[5].to_i * (1024 ** "BKMGT".index(mdat[6]))
260
- @topdata[:memory] = (used).to_f / (used + unused)
261
- end
262
- end
263
- end
264
-
265
- def darwin_cpu
266
- darwin_top unless (Time.now.to_i - @topdata[:stamp]) < opts[:interval]
267
- unless @topdata[:cpu]
268
- alert 'cpu', :unknown, nil, "unable to get CPU stats from top"
269
- return false
270
- end
271
- report_pct :cpu, @topdata[:cpu], "usage\n\n#{reverse_numeric_sort_with_header(`ps -eo pcpu,pid,comm`)}"
272
- end
273
-
274
- def darwin_load
275
- darwin_top unless (Time.now.to_i - @topdata[:stamp]) < opts[:interval]
276
- unless @topdata[:load]
277
- alert 'load', :unknown, nil, "unable to get load ave from top"
278
- return false
279
- end
280
- metric = @topdata[:load] / @cores
281
- if metric > @limits[:load][:critical]
282
- alert "load", :critical, metric, "1-minute load average per core is #{metric}"
283
- elsif metric > @limits[:load][:warning]
284
- alert "load", :warning, metric, "1-minute load average per core is #{metric}"
285
- else
286
- alert "load", :ok, metric, "1-minute load average per core is #{metric}"
287
- end
288
- end
289
-
290
- def darwin_memory
291
- darwin_top unless (Time.now.to_i - @topdata[:stamp]) < opts[:interval]
292
- unless @topdata[:memory]
293
- alert 'memory', :unknown, nil, "unable to get memory data from top"
294
- return false
295
- end
296
- report_pct :memory, @topdata[:memory], "usage\n\n#{reverse_numeric_sort_with_header(`ps -eo pmem,pid,comm`)}"
297
- end
298
-
299
- def df
300
- case @ostype
301
- when 'darwin', 'freebsd', 'openbsd'
302
- `df -P -t noiso9660`
303
- when 'sunos'
304
- `df -P` # Is there a good way to exlude iso9660 here?
305
- else
306
- if @supports_exclude_type
307
- `df -P --exclude-type=iso9660 --exclude-type=nfs`
308
- else
309
- `df -P`
310
- end
311
- end
312
- end
313
-
314
- def disk
315
- df.split(/\n/).each do |r|
316
- f = r.split(/\s+/)
317
- next if f[0] == 'Filesystem'
318
- next unless f[0] =~ /\// # Needs at least one slash in the mount path
319
-
320
- # Calculate capacity
321
- x = f[4].to_f/100
322
-
323
- if x > @limits[:disk][:critical]
324
- alert "disk #{f[5]}", :critical, x, "#{f[4]} used"
325
- elsif x > @limits[:disk][:warning]
326
- alert "disk #{f[5]}", :warning, x, "#{f[4]} used"
327
- else
328
- alert "disk #{f[5]}", :ok, x, "#{f[4]} used"
329
- end
330
- end
331
- end
6
+ # Reports current CPU, disk, load average, and memory use to riemann.
332
7
 
333
- def tick
334
- if @cpu_enabled
335
- @cpu.call
336
- end
337
- if @memory_enabled
338
- @memory.call
339
- end
340
- if @disk_enabled
341
- @disk.call
342
- end
343
- if @load_enabled
344
- @load.call
345
- end
346
- end
347
- end
8
+ require 'riemann/tools/health'
348
9
 
349
10
  Riemann::Tools::Health.run
@@ -1,22 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
3
 
4
- require File.expand_path('../../lib/riemann/tools', __FILE__)
4
+ Process.setproctitle($PROGRAM_NAME)
5
5
 
6
- class Riemann::Tools::KVM
7
- include Riemann::Tools
6
+ require File.expand_path('../lib/riemann/tools', __dir__)
8
7
 
9
- def tick
8
+ module Riemann
9
+ module Tools
10
+ class KVM
11
+ include Riemann::Tools
10
12
 
11
- #determine how many instances I have according to libvirt
12
- kvm_instances = %x[LANG=C virsh list | grep -c running]
13
+ def tick
14
+ # determine how many instances I have according to libvirt
15
+ kvm_instances = `LANG=C virsh list | grep -c running`
13
16
 
14
- #submit them to riemann
15
- report(
16
- :service => "KVM Running VMs",
17
- :metric => kvm_instances.to_i,
18
- :state => "info"
19
- )
17
+ # submit them to riemann
18
+ report(
19
+ service: 'KVM Running VMs',
20
+ metric: kvm_instances.to_i,
21
+ state: 'info',
22
+ )
23
+ end
24
+ end
20
25
  end
21
26
  end
22
27
 
@@ -1,37 +1,43 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
+
4
+ Process.setproctitle($PROGRAM_NAME)
3
5
 
4
6
  # Gathers memcached STATS and submits them to Riemann.
5
7
 
6
- require File.expand_path('../../lib/riemann/tools', __FILE__)
7
-
8
- class Riemann::Tools::Memcached
9
- include Riemann::Tools
10
- require 'socket'
11
-
12
- opt :memcached_host, "Memcached hostname", :default => 'localhost'
13
- opt :memcached_port, "Memcached port", :default => 11211
14
-
15
- def tick
16
- sock = TCPSocket.new(opts[:memcached_host], opts[:memcached_port])
17
- sock.print("stats\r\n")
18
- sock.flush
19
- stats = sock.gets
20
-
21
- data = {}
22
- while true
23
- stats = sock.gets
24
- break if stats.strip == 'END'
25
- m = stats.match /STAT (\w+) (\S+)/
26
- report(
27
- :host => opts[:memcached_host].dup,
28
- :service => "memcached #{m[1]}",
29
- :metric => m[2].to_f,
30
- :state => 'ok',
31
- :tags => ['memcached']
32
- )
8
+ require File.expand_path('../lib/riemann/tools', __dir__)
9
+
10
+ module Riemann
11
+ module Tools
12
+ class Memcached
13
+ include Riemann::Tools
14
+ require 'socket'
15
+
16
+ opt :memcached_host, 'Memcached hostname', default: 'localhost'
17
+ opt :memcached_port, 'Memcached port', default: 11_211
18
+
19
+ def tick
20
+ sock = TCPSocket.new(opts[:memcached_host], opts[:memcached_port])
21
+ sock.print("stats\r\n")
22
+ sock.flush
23
+ stats = sock.gets
24
+
25
+ loop do
26
+ stats = sock.gets
27
+ break if stats.strip == 'END'
28
+
29
+ m = stats.match(/STAT (\w+) (\S+)/)
30
+ report(
31
+ host: opts[:memcached_host].dup,
32
+ service: "memcached #{m[1]}",
33
+ metric: m[2].to_f,
34
+ state: 'ok',
35
+ tags: ['memcached'],
36
+ )
37
+ end
38
+ sock.close
39
+ end
33
40
  end
34
- sock.close
35
41
  end
36
42
  end
37
43
 
data/bin/riemann-net CHANGED
@@ -1,110 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- Process.setproctitle($0)
2
+ # frozen_string_literal: true
3
3
 
4
- # Gathers network interface statistics and submits them to Riemann.
5
-
6
- require File.expand_path('../../lib/riemann/tools', __FILE__)
7
-
8
- class Riemann::Tools::Net
9
- include Riemann::Tools
10
-
11
- opt :interfaces, "Interfaces to monitor", :type => :strings, :default => nil
12
- opt :ignore_interfaces, "Interfaces to ignore", :type => :strings, :default =>['lo']
13
-
14
- def initialize
15
- @old_state = nil
16
- @interfaces = opts[:interfaces].map(&:dup) if opts[:interfaces]
17
- @ignore_interfaces = opts[:ignore_interfaces].map(&:dup)
18
- end
19
-
20
- def state
21
- f = File.read('/proc/net/dev')
22
- state = f.split("\n").inject({}) do |s, line|
23
- if line =~ /\s*(\w+?):\s*([\s\d]+)\s*/
24
- iface = $1
25
-
26
- ['rx bytes',
27
- 'rx packets',
28
- 'rx errs',
29
- 'rx drop',
30
- 'rx fifo',
31
- 'rx frame',
32
- 'rx compressed',
33
- 'rx multicast',
34
- 'tx bytes',
35
- 'tx packets',
36
- 'tx errs',
37
- 'tx drops',
38
- 'tx fifo',
39
- 'tx colls',
40
- 'tx carrier',
41
- 'tx compressed'].map do |service|
42
- "#{iface} #{service}"
43
- end.zip(
44
- $2.split(/\s+/).map { |str| str.to_i }
45
- ).each do |service, value|
46
- s[service] = value
47
- end
48
- end
49
-
50
- s
51
- end
4
+ Process.setproctitle($PROGRAM_NAME)
52
5
 
53
- # Filter interfaces
54
- if is = @interfaces
55
- state = state.select do |service, value|
56
- is.include? service.split(' ').first
57
- end
58
- end
59
-
60
- state = state.reject do |service, value|
61
- @ignore_interfaces.include? service.split(' ').first
62
- end
63
-
64
- state
65
- end
66
-
67
- def tick
68
- state = self.state
69
-
70
- if @old_state
71
- # Report services from `@old_state` that don't exist in `state` as expired
72
- @old_state.reject { |k| state.has_key?(k) }.each do |service, metric|
73
- report(:service => service.dup, :state => 'expired')
74
- end
75
-
76
- # Report delta for services that have values in both `@old_state` and `state`
77
- state.each do |service, metric|
78
- next unless @old_state.has_key?(service)
79
-
80
- delta = metric - @old_state[service]
81
- svc_state = case service
82
- when /drop$/
83
- if delta > 0
84
- 'warning'
85
- else
86
- 'ok'
87
- end
88
- when /errs$/
89
- if delta > 0
90
- 'warning'
91
- else
92
- 'ok'
93
- end
94
- else
95
- 'ok'
96
- end
6
+ # Gathers network interface statistics and submits them to Riemann.
97
7
 
98
- report(
99
- :service => service.dup,
100
- :metric => (delta.to_f / opts[:interval]),
101
- :state => svc_state
102
- )
103
- end
104
- end
8
+ require 'riemann/tools/net'
105
9
 
106
- @old_state = state
107
- end
108
- end
109
-
110
10
  Riemann::Tools::Net.run