5
5
# stdlib
6
6
from datetime import datetime
7
7
import os .path
8
+ from os import environ
8
9
import re
9
10
import socket
10
11
import ssl
@@ -150,9 +151,11 @@ class HTTPCheck(NetworkCheck):
150
151
def __init__ (self , name , init_config , agentConfig , instances ):
151
152
self .ca_certs = init_config .get ('ca_certs' , get_ca_certs_path ())
152
153
proxy_settings = get_proxy (agentConfig )
153
- if not proxy_settings :
154
- self .proxies = None
155
- else :
154
+ self .proxies = {
155
+ "http" : None ,
156
+ "https" : None ,
157
+ }
158
+ if proxy_settings :
156
159
uri = "{host}:{port}" .format (
157
160
host = proxy_settings ['host' ],
158
161
port = proxy_settings ['port' ])
@@ -161,10 +164,15 @@ def __init__(self, name, init_config, agentConfig, instances):
161
164
user = proxy_settings ['user' ],
162
165
password = proxy_settings ['password' ],
163
166
uri = uri )
164
- self .proxies = {
165
- 'http' : "http://{uri}" .format (uri = uri ),
166
- 'https' : "https://{uri}" .format (uri = uri )
167
- }
167
+ self .proxies ['http' ] = "http://{uri}" .format (uri = uri )
168
+ self .proxies ['https' ] = "https://{uri}" .format (uri = uri )
169
+ else :
170
+ self .proxies ['http' ] = environ .get ('HTTP_PROXY' , None )
171
+ self .proxies ['https' ] = environ .get ('HTTPS_PROXY' , None )
172
+
173
+ self .proxies ['no' ] = environ .get ('no_proxy' ,
174
+ environ .get ('NO_PROXY' , None )
175
+ )
168
176
169
177
NetworkCheck .__init__ (self , name , init_config , agentConfig , instances )
170
178
@@ -189,15 +197,16 @@ def _load_conf(self, instance):
189
197
instance_ca_certs = instance .get ('ca_certs' , self .ca_certs )
190
198
weakcipher = _is_affirmative (instance .get ('weakciphers' , False ))
191
199
ignore_ssl_warning = _is_affirmative (instance .get ('ignore_ssl_warning' , False ))
200
+ skip_proxy = _is_affirmative (instance .get ('no_proxy' , False ))
192
201
193
202
return url , username , password , http_response_status_code , timeout , include_content ,\
194
203
headers , response_time , content_match , tags , ssl , ssl_expire , instance_ca_certs ,\
195
- weakcipher , ignore_ssl_warning
204
+ weakcipher , ignore_ssl_warning , skip_proxy
196
205
197
206
def _check (self , instance ):
198
207
addr , username , password , http_response_status_code , timeout , include_content , headers ,\
199
208
response_time , content_match , tags , disable_ssl_validation ,\
200
- ssl_expire , instance_ca_certs , weakcipher , ignore_ssl_warning = self ._load_conf (instance )
209
+ ssl_expire , instance_ca_certs , weakcipher , ignore_ssl_warning , skip_proxy = self ._load_conf (instance )
201
210
start = time .time ()
202
211
203
212
service_checks = []
@@ -208,18 +217,33 @@ def _check(self, instance):
208
217
self .warning ("Skipping SSL certificate validation for %s based on configuration"
209
218
% addr )
210
219
220
+ instance_proxy = self .proxies .copy ()
221
+
222
+ # disable proxy if necessary
223
+ if skip_proxy :
224
+ instance_proxy .pop ('http' )
225
+ instance_proxy .pop ('https' )
226
+ else :
227
+ for url in self .proxies ['no' ].replace (';' ,',' ).split ("," ):
228
+ if url in parsed_uri .netloc :
229
+ instance_proxy .pop ('http' )
230
+ instance_proxy .pop ('https' )
231
+
232
+ self .log .debug ("Proxies used for %s - %s" , addr , instance_proxy )
233
+
211
234
auth = None
212
235
if username is not None and password is not None :
213
236
auth = (username , password )
214
237
215
238
sess = requests .Session ()
239
+ sess .trust_env = False
216
240
if weakcipher :
217
241
base_addr = '{uri.scheme}://{uri.netloc}/' .format (uri = parsed_uri )
218
242
sess .mount (base_addr , WeakCiphersAdapter ())
219
243
self .log .debug ("Weak Ciphers will be used for {0}. Suppoted Cipherlist: {1}" .format (
220
244
base_addr , WeakCiphersHTTPSConnection .SUPPORTED_CIPHERS ))
221
245
222
- r = sess .request ('GET' , addr , auth = auth , timeout = timeout , headers = headers , proxies = self . proxies ,
246
+ r = sess .request ('GET' , addr , auth = auth , timeout = timeout , headers = headers , proxies = instance_proxy ,
223
247
verify = False if disable_ssl_validation else instance_ca_certs )
224
248
225
249
except (socket .timeout , requests .exceptions .ConnectionError , requests .exceptions .Timeout ) as e :
0 commit comments