|
10 | 10 | CRITICAL = 2
|
11 | 11 | UNKNOWN = 3
|
12 | 12 |
|
| 13 | + |
13 | 14 | status = ['OK', 'WARN', 'CRIT', 'UNKN']
|
14 | 15 |
|
15 | 16 | @proxies = []
|
16 | 17 | @errors = []
|
17 | 18 | @perfdata = []
|
18 | 19 | exit_code = OK
|
| 20 | +http_error_critical = false |
19 | 21 |
|
20 | 22 | options = OpenStruct.new
|
21 | 23 | options.proxies = []
|
|
53 | 55 | options.critical = v
|
54 | 56 | end
|
55 | 57 |
|
| 58 | + opts.on( '-s', '--ssl', 'Enable TLS/SSL' ) do |
| 59 | + require 'openssl' |
| 60 | + end |
| 61 | + |
| 62 | + 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 |
| 69 | + end |
| 70 | + |
| 71 | + opts.on( '--http-error-critical', 'Throw critical when connection to HAProxy is refused or returns error code' ) do |
| 72 | + http_error_critical = true |
| 73 | + end |
| 74 | + |
56 | 75 | opts.on( '-h', '--help', 'Display this screen' ) do
|
57 | 76 | puts opts
|
58 | 77 | exit 3
|
|
85 | 104 | exit UNKNOWN
|
86 | 105 | end
|
87 | 106 |
|
| 107 | + |
| 108 | +tries = 2 |
| 109 | + |
88 | 110 | begin
|
89 | 111 | f = open(options.url, :http_basic_authentication => [options.user, options.password])
|
90 | 112 | rescue OpenURI::HTTPError => e
|
91 | 113 | puts "ERROR: #{e.message}"
|
92 |
| - exit UNKNOWN |
| 114 | + http_error_critical ? exit CRITICAL : exit UNKNOWN |
93 | 115 | rescue Errno::ECONNREFUSED => e
|
94 | 116 | puts "ERROR: #{e.message}"
|
95 |
| - exit UNKNOWN |
| 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 |
| 125 | + end |
96 | 126 | end
|
97 | 127 |
|
| 128 | + |
98 | 129 | f.each do |line|
|
99 | 130 |
|
100 | 131 | if line =~ /^# /
|
|
115 | 146 | perf_id = "#{row['pxname']}".downcase
|
116 | 147 |
|
117 | 148 | if row['svname'] == 'FRONTEND'
|
118 |
| - session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i |
| 149 | + if row['slim'].to_i == 0 |
| 150 | + session_percent_usage = 0 |
| 151 | + else |
| 152 | + session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i |
| 153 | + end |
119 | 154 | @perfdata << "#{perf_id}_sessions=#{session_percent_usage}%;#{options.warning ? options.warning : ""};#{options.critical ? options.critical : ""};;"
|
120 | 155 | @perfdata << "#{perf_id}_rate=#{row['rate']};;;;#{row['rate_max']}"
|
121 | 156 | if options.critical && session_percent_usage > options.critical.to_i
|
|
159 | 194 | @errors << message
|
160 | 195 | exit_code = WARNING if exit_code == OK || exit_code == UNKNOWN
|
161 | 196 | else
|
162 |
| - session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i |
| 197 | + if row['slim'].to_i == 0 |
| 198 | + session_percent_usage = 0 |
| 199 | + else |
| 200 | + session_percent_usage = row['scur'].to_i * 100 / row['slim'].to_i |
| 201 | + end |
163 | 202 | @perfdata << "#{perf_id}-#{row['svname']}_sessions=#{session_percent_usage}%;;;;"
|
164 | 203 | @perfdata << "#{perf_id}-#{row['svname']}_rate=#{row['rate']};;;;#{row['rate_max']}"
|
165 | 204 | end
|
|
0 commit comments