Commit 5913ab0d authored by Rafael Monnerat's avatar Rafael Monnerat

networkbench: Include ping6 and TTFB support

  Replace urlib by pycurl to get the 'Time To First Byte' responses.
  Include ping6 support
  Verbose how much package lost the connection had in case of package lost.
parent 1cc6e6a6
...@@ -50,6 +50,7 @@ setup(name=name, ...@@ -50,6 +50,7 @@ setup(name=name,
'zodbpack': ['ZODB3'], # needed to play with ZODB 'zodbpack': ['ZODB3'], # needed to play with ZODB
'agent': ['erp5.util'], 'agent': ['erp5.util'],
'flask_auth' : ["Flask-Auth"], 'flask_auth' : ["Flask-Auth"],
'networkbench' : ['pycurl'],
'check_web_page_http_cache_hit' : ['pycurl'], # needed for check_web_page_http_cache_hit module 'check_web_page_http_cache_hit' : ['pycurl'], # needed for check_web_page_http_cache_hit module
}, },
zip_safe=False, # proxy depends on Flask, which has issues with zip_safe=False, # proxy depends on Flask, which has issues with
...@@ -91,4 +92,4 @@ setup(name=name, ...@@ -91,4 +92,4 @@ setup(name=name,
'networkbench = slapos.networkbench:main' 'networkbench = slapos.networkbench:main'
] ]
}, },
) )
\ No newline at end of file
...@@ -10,6 +10,8 @@ import sys ...@@ -10,6 +10,8 @@ import sys
import shutil import shutil
import netifaces import netifaces
import random import random
import pycurl
from StringIO import StringIO
botname = socket.gethostname() botname = socket.gethostname()
...@@ -40,40 +42,69 @@ def _test_dns(name): ...@@ -40,40 +42,69 @@ def _test_dns(name):
resolving_time = time.time() - begin resolving_time = time.time() - begin
return ('DNS', name, resolution, resolving_time, status) return ('DNS', name, resolution, resolving_time, status)
def _test_ping(host, timeout=10): def _test_ping(host, timeout=10, protocol="4"):
proc = subprocess.Popen(('ping', '-c', '4', '-w', str(timeout), host), if protocol == '4':
ping_bin = 'ping'
test_title = 'PING'
elif protocol == '6':
ping_bin = 'ping6'
test_title = 'PING6'
proc = subprocess.Popen((ping_bin, '-c', '10', '-w', str(timeout), host),
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate() out, err = proc.communicate()
packet_loss_line, summary_line = (out.splitlines() or [''])[-2:] packet_loss_line, summary_line = (out.splitlines() or [''])[-2:]
m = ping_re.match(summary_line) m = ping_re.match(summary_line)
info_list = ('PING', host, '600', 'failed', "Cannot ping host") match = re.search('(\d*)% packet loss', packet_loss_line)
if ' 0% packet loss' in packet_loss_line: packet_lost_ratio = match.group(1)
info_list = (test_title, host, '600', 'failed', packet_lost_ratio, "Cannot ping host")
if packet_lost_ratio != 0:
if m: if m:
info_list = ('PING', host, '200', m.group('avg'), info_list = (test_title, host, '200', m.group('avg'), packet_lost_ratio,
'min %(min)s max %(max)s avg %(avg)s' % m.groupdict()) 'min %(min)s max %(max)s avg %(avg)s' % m.groupdict())
else:
info_list = (test_title, host, '600', 'failed', packet_lost_ratio,
"You have package Lost")
return info_list return info_list
def _test_url_request(url, request): def _test_ping6(host, timeout=10):
return _test_ping(host, timeout=10, protocol='6')
def _test_url_request(url):
begin = time.time() begin = time.time()
buffer = StringIO()
curl = pycurl.Curl()
curl.setopt(curl.URL, url)
curl.setopt(curl.CONNECTTIMEOUT, 10)
curl.setopt(curl.TIMEOUT, 300)
curl.setopt(curl.WRITEDATA, buffer)
try: try:
response = urllib2.urlopen(request) curl.perform()
if url != response.url: except:
raise RuntimeError, 'Redirected to %s' % response.url import traceback
response.read() traceback.print_exc(file=sys.stderr)
rendering_time = time.time() - begin sys.stderr.flush()
info_list = ('GET', url, response.code, rendering_time, "OK")
except urllib2.HTTPError, e: # 40x errors body = buffer.getvalue()
e.fp.read()
rendering_time = time.time() - begin rendering_time = "%s/%s/%s/%s/%s" % \
info_list = ('GET', url, e.code, rendering_time, "HTTPError") (curl.getinfo(curl.NAMELOOKUP_TIME),
except urllib2.URLError, e: # DNS failure curl.getinfo(curl.CONNECT_TIME),
rendering_time = time.time() - begin curl.getinfo(curl.PRETRANSFER_TIME),
info_list = ('GET', url, '600', rendering_time, "DNS Failure") curl.getinfo(curl.STARTTRANSFER_TIME),
except RuntimeError, e: curl.getinfo(curl.TOTAL_TIME))
info_list = ('GET', url, '600', 'failed')
response_code = curl.getinfo(pycurl.HTTP_CODE)
curl.close()
info_list = ('GET', url, response_code, rendering_time, "OK")
return info_list return info_list
def is_rotate_log(log_file_path): def is_rotate_log(log_file_path):
...@@ -146,6 +177,7 @@ def main(): ...@@ -146,6 +177,7 @@ def main():
name_list = config.get("network_bench", "dns") name_list = config.get("network_bench", "dns")
url_list = config.get("network_bench", "url") url_list = config.get("network_bench", "url")
ping_list = config.get("network_bench", "ping") ping_list = config.get("network_bench", "ping")
ping6_list = config.get("network_bench", "ping6")
logger = create_logger("info", log_folder) logger = create_logger("info", log_folder)
...@@ -161,9 +193,13 @@ def main(): ...@@ -161,9 +193,13 @@ def main():
for host in ping_list.split(): for host in ping_list.split():
info_list = _test_ping(host) info_list = _test_ping(host)
logger.info(';'.join(str(x) for x in info_list)) logger.info(';'.join(str(x) for x in info_list))
for host in ping6_list.split():
info_list = _test_ping6(host)
logger.info(';'.join(str(x) for x in info_list))
# http # http
for url in url_list.split(): for url in url_list.split():
info_list = _test_url_request(url, urllib2.Request(url)) info_list = _test_url_request(url)
logger.info(';'.join(str(x) for x in info_list)) logger.info(';'.join(str(x) for x in info_list))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment