Newer
Older

Stéphane Diemer
committed
Criticality: High
Tests that all webserver services (vhosts) are available and reachable.
try:
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
except ImportError:
requests.packages.urllib3.disable_warnings()
sys.path.append(str(Path(__file__).parents[1].resolve()))
# pylint: disable=wrong-import-position
from envsetup import utils as u # noqa: E402
This script checks for all enabled vhosts in Nginx conf that:
* The response status code is 200, 401 or 403.
* The host is resolved as 127.0.0.1.
* The Wowza response is correct on /streaming/ (only for mediaserver vhosts).
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def get_configs(path: str) -> list:
configs_dir = Path(path)
configs = [c.resolve() for c in configs_dir.glob("*.conf")]
return configs
def get_vhosts(config: Path) -> list:
# remove comments and blank lines
sanitize = re.compile(r"(?:\s*#\s*.*)|(?:^\s*)", re.M)
# capture server blocks
servers = re.compile(r"^server\s+{(?:\s*(?!server\s{).)+", re.M)
with open(config) as config_fo:
config_content = sanitize.sub(r"", config_fo.read())
vhosts = servers.findall(config_content)
return vhosts
def get_hostnames(vhost: str) -> list:
# extract hostname(s) from server_name values
server_names = re.compile(r"^\s*server_name\s+(.*);$")
hostnames = []
for line in vhost.splitlines():
if server_names.match(line):
hostnames.extend(server_names.match(line)[1].split())
return hostnames
def get_ports(vhost: str) -> list:
# extract port(s) from listen values
listens = re.compile(r"^\s*listen\s+(?:.*:)?(\d+)\s*(ssl)?.*;$")
ports = []
for line in vhost.splitlines():
if listens.match(line):
ports.append(
(listens.match(line)[1], "https" if listens.match(line)[2] else "http")
)
return ports
ports_info=None,
domains=None,
resolution_ignored=None,
celerity_conf="",
nginx_file=None,
wowza_dir=None,
tested=0,
errors = 0
warnings = 0
if not re.search(r"https?://%s" % domain, celerity_conf):
u.warning("url '%s' not found in celerity conf" % url)
warnings += 1
# test domain IP
ip_error = None
ip_warning = None
try:
ip = socket.gethostbyname(domain)
except Exception as e:
ip_warning = "domain: resolve to %s instead of 127.0.0.1" % ip
if ip_error:
if resolution_ignored and domain in resolution_ignored:
ip_error = None
else:
elif ip_warning:
if resolution_ignored and domain in resolution_ignored:
ip_warning = None
else:
# test url
req_error = False
try:
req = requests.get(
url, verify=False, proxies={"http": "", "https": ""}, timeout=30
)
req_time = int(1000 * req.elapsed.total_seconds())
except Exception as e:
code = str(e)
req_time = 0
code = req.status_code
if (
domain != "localhost"
and code not in (200, 401, 403)
or domain == "localhost"
and code not in (200, 401, 403, 404)
):
req_error = True
if req_time > 10000:
# test /streaming url
try:
req = requests.get(
url + "/streaming/",
verify=False,
proxies={"http": "", "https": ""},
timeout=30,
)
req_time = int(1000 * req.elapsed.total_seconds())
except Exception as e:
code = str(e)
req_time = 0
else:
code = req.status_code
if code != 200:
u.error("streaming: %s, %sms" % (code, req_time))
req_error = True
elif req_time > 10000:
u.warning("streaming: %s, %sms" % (code, req_time))
warnings += 1
u.success("streaming: %s, %sms" % (code, req_time))
if ip_warning:
warnings += 1
if ip_error or req_error:
errors += 1
return tested, warnings, errors
print("Check that nginx vhosts are well configured:")
# check that Nginx dir exists
u.error("nginx dir does not exists ('%s')." % nginx_dir)
# check that Wowza is installed
u.info("wowza is not installed ('%s' does not exist)." % wowza_dir)
wowza_dir = None
else:
u.info("wowza is installed, /streaming/ will be tested on mediaserver vhosts.")
# get envsetup conf
# get celerity conf
celerity_conf = fo.read()
# get enabled vhosts
resolution_ignored = conf.get("TESTER_VHOST_RESOLUTION_IGNORED", "").split(",")
errors = 0
warnings = 0
nginx_confs = get_configs(nginx_dir)
for nginx_conf in nginx_confs:
tested = 0
vhosts = get_vhosts(nginx_conf)
for vhost in vhosts:
hostnames = get_hostnames(vhost)
ports = get_ports(vhost)
t, w, e = test_vhost(
ports,
hostnames,
resolution_ignored,
celerity_conf,
nginx_conf,
wowza_dir,
tested,
)
tested += t
warnings += w
errors += e
elif warnings:
if not tested:
u.error("no url found in nginx sites-enabled dir")