|
17 | 17 | @errors = []
|
18 | 18 | @perfdata = []
|
19 | 19 | exit_code = OK
|
20 |
| -http_error_critical = false |
21 |
| - |
22 | 20 | options = OpenStruct.new
|
23 | 21 | options.proxies = []
|
24 | 22 |
|
25 | 23 | op = OptionParser.new do |opts|
|
26 | 24 | opts.banner = 'Usage: check_haproxy.rb [options]'
|
27 | 25 |
|
28 |
| - opts.separator "" |
29 |
| - opts.separator "Specific options:" |
| 26 | + opts.separator '' |
| 27 | + opts.separator 'Specific options:' |
30 | 28 |
|
31 | 29 | # Required arguments
|
32 | 30 | opts.on("-u", "--url URL", "Statistics URL to check (eg. http://demo.1wt.eu/)") do |v|
|
|
55 | 53 | options.critical = v
|
56 | 54 | end
|
57 | 55 |
|
58 |
| - opts.on( '-s', '--ssl', 'Enable TLS/SSL' ) do |
59 |
| - require 'openssl' |
60 |
| - end |
61 |
| - |
62 | 56 | opts.on( '-k', '--insecure', 'Allow insecure TLS/SSL connections' ) do
|
63 |
| - require 'openssl' |
64 |
| - |
65 |
| - # allows https with invalid certificate on ruby 1.8+ |
66 |
| - # |
67 |
| - # src: also://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl |
68 |
| - OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE |
| 57 | + options.insecure_ssl = true |
69 | 58 | end
|
70 | 59 |
|
71 | 60 | opts.on( '--http-error-critical', 'Throw critical when connection to HAProxy is refused or returns error code' ) do
|
72 |
| - http_error_critical = true |
| 61 | + options.http_error_critical = true |
73 | 62 | end
|
74 | 63 |
|
75 | 64 | opts.on( '-h', '--help', 'Display this screen' ) do
|
|
80 | 69 |
|
81 | 70 | op.parse!
|
82 | 71 |
|
83 |
| -if !options.url |
84 |
| - puts "ERROR: URL is required" |
| 72 | +unless options.url |
| 73 | + puts 'ERROR: URL is required' |
85 | 74 | puts op
|
86 | 75 | exit UNKNOWN
|
87 | 76 | end
|
88 | 77 |
|
89 |
| -if options.warning && ! options.warning.to_i.between?(0, 100) |
90 |
| - puts "ERROR: warning should be between 0 and 100" |
| 78 | +if options.warning && !options.warning.to_i.between?(0, 100) |
| 79 | + puts 'ERROR: warning must be between 0 and 100' |
91 | 80 | puts op
|
92 | 81 | exit UNKNOWN
|
93 | 82 | end
|
94 | 83 |
|
95 |
| -if options.critical && ! options.critical.to_i.between?(0, 100) |
96 |
| - puts "ERROR: critical should be between 0 and 100" |
| 84 | +if options.critical && !options.critical.to_i.between?(0, 100) |
| 85 | + puts 'ERROR: critical must be between 0 and 100' |
97 | 86 | puts op
|
98 | 87 | exit UNKNOWN
|
99 | 88 | end
|
100 | 89 |
|
101 | 90 | if options.warning && options.critical && options.warning.to_i > options.critical.to_i
|
102 |
| - puts "ERROR: warning should be below critical" |
| 91 | + puts 'ERROR: warning must be below critical' |
103 | 92 | puts op
|
104 | 93 | exit UNKNOWN
|
105 | 94 | end
|
106 | 95 |
|
| 96 | +def open_options(options) |
| 97 | + open_opts = { |
| 98 | + :http_basic_authentication => [options.user, options.password] |
| 99 | + } |
107 | 100 |
|
108 |
| -tries = 2 |
109 |
| - |
110 |
| -begin |
111 |
| - f = open(options.url, :http_basic_authentication => [options.user, options.password]) |
112 |
| -rescue OpenURI::HTTPError => e |
113 |
| - puts "ERROR: #{e.message}" |
114 |
| - http_error_critical ? exit CRITICAL : exit UNKNOWN |
115 |
| -rescue Errno::ECONNREFUSED => e |
116 |
| - puts "ERROR: #{e.message}" |
117 |
| - http_error_critical ? exit CRITICAL : exit UNKNOWN |
118 |
| -rescue Exception => e |
119 |
| - if e.message =~ /redirection forbidden/ |
120 |
| - options.url = e.message.gsub(/.*-> (.*)/, '\1') # extract redirect URL |
121 |
| - retry if (tries -= 1) > 0 |
122 |
| - raise |
123 |
| - else |
124 |
| - exit UNKNOWN |
| 101 | + # allows https with invalid certificate on ruby 1.9 |
| 102 | + # src: http://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl |
| 103 | + if options.insecure_ssl && RUBY_VERSION =~ /1\.9/ |
| 104 | + open_opts[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE |
125 | 105 | end
|
| 106 | + |
| 107 | + open_opts |
126 | 108 | end
|
127 | 109 |
|
| 110 | +def haproxy_response(options) |
| 111 | + tries = 2 |
128 | 112 |
|
129 |
| -f.each do |line| |
| 113 | + if options.url =~ /https/ |
| 114 | + require 'openssl' |
| 115 | + # allows https with invalid certificate on ruby 1.8 |
| 116 | + # src: http://snippets.aktagon.com/snippets/370-hack-for-using-openuri-with-ssl |
| 117 | + if options.insecure_ssl && RUBY_VERSION =~ /1\.8/ |
| 118 | + OpenSSL::SSL.const_set :VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + begin |
| 123 | + open(options.url, open_options(options)) |
| 124 | + rescue OpenURI::HTTPError => e |
| 125 | + puts "ERROR: #{e.message}" |
| 126 | + options.http_error_critical ? exit(CRITICAL) : exit(UNKNOWN) |
| 127 | + rescue Errno::ECONNREFUSED => e |
| 128 | + puts "ERROR: #{e.message}" |
| 129 | + options.http_error_critical ? exit(CRITICAL) : exit(UNKNOWN) |
| 130 | + rescue RuntimeError => e |
| 131 | + if e.message =~ /redirection forbidden/ |
| 132 | + options.url = e.message.gsub(/.*-> (.*)/, '\1') # extract redirect URL |
| 133 | + retry if (tries -= 1) > 0 |
| 134 | + raise |
| 135 | + else |
| 136 | + exit UNKNOWN |
| 137 | + end |
| 138 | + end |
| 139 | +end |
130 | 140 |
|
| 141 | +haproxy_response(options).each do |line| |
131 | 142 | if line =~ /^# /
|
132 | 143 | HAPROXY_COLUMN_NAMES = line[2..-1].split(',')
|
133 | 144 | next
|
134 |
| - elsif ! defined? HAPROXY_COLUMN_NAMES |
| 145 | + elsif !defined? HAPROXY_COLUMN_NAMES |
135 | 146 | puts "ERROR: CSV header is missing"
|
136 | 147 | exit UNKNOWN
|
137 | 148 | end
|
|
0 commit comments