-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_throughput
executable file
·59 lines (47 loc) · 1.71 KB
/
check_throughput
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/ruby
require 'socket'
require 'timeout'
class CheckThroughput
attr_accessor :host, :interface, :direction, :throughput, :ranges
def initialize(args)
@host, @interface, @direction, @ranges = args[:host], args[:interface], args[:direction], args[:ranges]
Timeout::timeout(2) {
TCPSocket.open("127.0.0.1", "2000") { | client | client.puts "#{@host} #{@interface} #{@direction}"; @throughput = client.gets.to_f / 102.4 }
}
end
def critical?
"THROUGHPUT CRITICAL: last measured at #{@throughput}" if check_warning(:critical)
end
def warning?
"THROUGHPUT WARNING: last measured at #{@throughput}" if check_warning(:warning)
end
private
def check_warning(level)
!@ranges[level].nil? && (@ranges[level].begin > @throughput || @ranges[level].end < @throughput)
end
end
if ARGV.length == 0
puts "Usage: check_throughput [host] [community] [in|out] [interface #] [warning max:warning min] [critical max|critical min]"
exit 0
else
@ranges = {}
if ARGV[4] # warning
ARGV[4] =~ /([\-0-9]+):?([\-0-9]+)?/
@ranges[:warning] = ($2.to_i..$1.to_i||999999)
end
if ARGV[5] # critical
ARGV[5] =~ /([\-0-9]+):?([\-0-9]+)?/
@ranges[:critical] = ($2.to_i..$1.to_i||999999)
end
@check_throughput = CheckThroughput.new(:host => ARGV[0], :interface => ARGV[3], :direction => ARGV[2], :ranges => @ranges)
if @check_throughput.critical?
puts "THROUGHPUT CRITICAL: #{sprintf('%1.2f', @check_throughput.throughput)} k/s"
exit 2
elsif @check_throughput.warning?
puts "THROUGHPUT WARNING: #{sprintf('%1.2f', @check_throughput.throughput)} k/s"
exit 1
else
puts "THROUGHPUT OK: #{sprintf('%1.2f', @check_throughput.throughput)} k/s"
exit 0
end
end