nginx-lens 0.2.1__py3-none-any.whl → 0.2.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- commands/health.py +9 -4
- {nginx_lens-0.2.1.dist-info → nginx_lens-0.2.3.dist-info}/METADATA +1 -1
- {nginx_lens-0.2.1.dist-info → nginx_lens-0.2.3.dist-info}/RECORD +8 -8
- upstream_checker/checker.py +74 -7
- {nginx_lens-0.2.1.dist-info → nginx_lens-0.2.3.dist-info}/WHEEL +0 -0
- {nginx_lens-0.2.1.dist-info → nginx_lens-0.2.3.dist-info}/entry_points.txt +0 -0
- {nginx_lens-0.2.1.dist-info → nginx_lens-0.2.3.dist-info}/licenses/LICENSE +0 -0
- {nginx_lens-0.2.1.dist-info → nginx_lens-0.2.3.dist-info}/top_level.txt +0 -0
commands/health.py
CHANGED
|
@@ -10,14 +10,15 @@ console = Console()
|
|
|
10
10
|
def health(
|
|
11
11
|
config_path: str = typer.Argument(..., help="Путь к nginx.conf"),
|
|
12
12
|
timeout: float = typer.Option(2.0, help="Таймаут проверки (сек)"),
|
|
13
|
-
retries: int = typer.Option(1, help="Количество попыток")
|
|
13
|
+
retries: int = typer.Option(1, help="Количество попыток"),
|
|
14
|
+
mode: str = typer.Option("tcp", help="Режим проверки: tcp или http", case_sensitive=False),
|
|
14
15
|
):
|
|
15
16
|
"""
|
|
16
17
|
Проверяет доступность upstream-серверов, определённых в nginx.conf. Выводит таблицу.
|
|
17
18
|
|
|
18
19
|
Пример:
|
|
19
20
|
nginx-lens health /etc/nginx/nginx.conf
|
|
20
|
-
nginx-lens health /etc/nginx/nginx.conf --timeout 5 --retries 3
|
|
21
|
+
nginx-lens health /etc/nginx/nginx.conf --timeout 5 --retries 3 --mode http
|
|
21
22
|
"""
|
|
22
23
|
try:
|
|
23
24
|
tree = parse_nginx_config(config_path)
|
|
@@ -27,14 +28,18 @@ def health(
|
|
|
27
28
|
except Exception as e:
|
|
28
29
|
console.print(f"[red]Ошибка при разборе {config_path}: {e}[/red]")
|
|
29
30
|
return
|
|
31
|
+
|
|
30
32
|
upstreams = tree.get_upstreams()
|
|
31
|
-
results = check_upstreams(upstreams, timeout=timeout, retries=retries)
|
|
33
|
+
results = check_upstreams(upstreams, timeout=timeout, retries=retries, mode=mode.lower())
|
|
34
|
+
|
|
32
35
|
table = Table(show_header=True, header_style="bold blue")
|
|
33
36
|
table.add_column("upstream_name")
|
|
34
37
|
table.add_column("upstream_status")
|
|
38
|
+
|
|
35
39
|
for name, servers in results.items():
|
|
36
40
|
for srv in servers:
|
|
37
41
|
status = "Healthy" if srv["healthy"] else "Unhealthy"
|
|
38
42
|
color = "green" if srv["healthy"] else "red"
|
|
39
43
|
table.add_row(srv["address"], f"[{color}]{status}[/{color}]")
|
|
40
|
-
|
|
44
|
+
|
|
45
|
+
console.print(table)
|
|
@@ -15,7 +15,7 @@ commands/analyze.py,sha256=W6begSgXNjgKJGoGeguR3WKgHPLkClWXxxpDcqvsJdc,8343
|
|
|
15
15
|
commands/cli.py,sha256=9HwDJ-po5al0ceb4Wkyw5F2wzqxbJTo0CbHQ2AQ8obo,722
|
|
16
16
|
commands/diff.py,sha256=C7gRIWh6DNWHzjiQBPVTn-rZ40m2KCY75Zd6Q4URJIE,2076
|
|
17
17
|
commands/graph.py,sha256=xB6KjXBkLmm5gII3e-5BMRGO7WeTwc7EFxRGzYgnme4,5947
|
|
18
|
-
commands/health.py,sha256=
|
|
18
|
+
commands/health.py,sha256=EV2M2Gt_LnA8WI_0gOBlRYr2u-fxpjq6FBd-tlQOSuI,1830
|
|
19
19
|
commands/include.py,sha256=5PTYG5C00-AlWfIgpQXLq9E7C9yTFSv7HrZkM5ogDps,2224
|
|
20
20
|
commands/logs.py,sha256=RkPUdIpbO9dOVL56lemreYRuAjMjcqqMxRCKOFv2gC4,3691
|
|
21
21
|
commands/route.py,sha256=-x_71u6ENl3iO-oxK3bdE8v5eZKf4xRCydeUyXMFVrY,3163
|
|
@@ -25,13 +25,13 @@ exporter/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
25
25
|
exporter/graph.py,sha256=WYUrqUgCaK6KihgxAcRHaQn4oMo6b7ybC8yb_36ZIsA,3995
|
|
26
26
|
exporter/html.py,sha256=uquEM-WvBt2aV9GshgaI3UVhYd8sD0QQ-OmuNtvYUdU,798
|
|
27
27
|
exporter/markdown.py,sha256=_0mXQIhurGEZ0dO-eq9DbsuKNrgEDIblgtL3DAgYNo8,724
|
|
28
|
-
nginx_lens-0.2.
|
|
28
|
+
nginx_lens-0.2.3.dist-info/licenses/LICENSE,sha256=g8QXKdvZZC56rU8E12vIeYF6R4jeTWOsblOnYAda3K4,1073
|
|
29
29
|
parser/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
30
30
|
parser/nginx_parser.py,sha256=Sa9FtGAkvTqNzoehBvgLUWPJHLLIZYWH9ugSHW50X8s,3699
|
|
31
31
|
upstream_checker/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
32
|
-
upstream_checker/checker.py,sha256=
|
|
33
|
-
nginx_lens-0.2.
|
|
34
|
-
nginx_lens-0.2.
|
|
35
|
-
nginx_lens-0.2.
|
|
36
|
-
nginx_lens-0.2.
|
|
37
|
-
nginx_lens-0.2.
|
|
32
|
+
upstream_checker/checker.py,sha256=5aTU3-ejUlEOKX9lYxD8PuGTBYyQENwiDknSh168lFE,2336
|
|
33
|
+
nginx_lens-0.2.3.dist-info/METADATA,sha256=ue5Bb9dQNf6RSCxxS7k718glb6-ujAY8yqLlTU7b27w,520
|
|
34
|
+
nginx_lens-0.2.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
35
|
+
nginx_lens-0.2.3.dist-info/entry_points.txt,sha256=qEcecjSyLqcJjbIVlNlTpqAhPqDyaujUV5ZcBTAr3po,48
|
|
36
|
+
nginx_lens-0.2.3.dist-info/top_level.txt,sha256=mxLJO4rZg0rbixVGhplF3fUNFs8vxDIL25ronZNvRy4,51
|
|
37
|
+
nginx_lens-0.2.3.dist-info/RECORD,,
|
upstream_checker/checker.py
CHANGED
|
@@ -1,12 +1,79 @@
|
|
|
1
|
+
# upstream_checker/checker.py
|
|
2
|
+
|
|
1
3
|
import socket
|
|
4
|
+
import time
|
|
5
|
+
import http.client
|
|
2
6
|
from typing import Dict, List
|
|
3
7
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
|
|
9
|
+
def check_tcp(address: str, timeout: float, retries: int) -> bool:
|
|
10
|
+
"""
|
|
11
|
+
Проверка доступности сервера по TCP.
|
|
12
|
+
Ignores extra upstream options like 'max_fails' or 'fail_timeout'.
|
|
13
|
+
"""
|
|
14
|
+
# Берем только host:port, игнорируем параметры
|
|
15
|
+
host_port = address.split()[0]
|
|
16
|
+
host, port = host_port.split(":")
|
|
17
|
+
port = int(port)
|
|
18
|
+
|
|
19
|
+
for _ in range(retries):
|
|
20
|
+
try:
|
|
21
|
+
with socket.create_connection((host, port), timeout=timeout):
|
|
22
|
+
return True
|
|
23
|
+
except (socket.timeout, ConnectionRefusedError, OSError):
|
|
24
|
+
time.sleep(0.2)
|
|
25
|
+
return False
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def check_http(address: str, timeout: float, retries: int) -> bool:
|
|
29
|
+
"""
|
|
30
|
+
Проверка доступности сервера по HTTP (GET /).
|
|
31
|
+
Ignores extra upstream options like 'max_fails' or 'fail_timeout'.
|
|
32
|
+
"""
|
|
33
|
+
host_port = address.split()[0]
|
|
34
|
+
host, port = host_port.split(":")
|
|
35
|
+
port = int(port)
|
|
36
|
+
|
|
37
|
+
for _ in range(retries):
|
|
38
|
+
try:
|
|
39
|
+
conn = http.client.HTTPConnection(host, port, timeout=timeout)
|
|
40
|
+
conn.request("GET", "/")
|
|
41
|
+
resp = conn.getresponse()
|
|
42
|
+
healthy = resp.status < 500
|
|
43
|
+
conn.close()
|
|
44
|
+
if healthy:
|
|
45
|
+
return True
|
|
46
|
+
except Exception:
|
|
47
|
+
time.sleep(0.2)
|
|
48
|
+
continue
|
|
49
|
+
return False
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def check_upstreams(
|
|
53
|
+
upstreams: Dict[str, List[str]],
|
|
54
|
+
timeout: float = 2.0,
|
|
55
|
+
retries: int = 1,
|
|
56
|
+
mode: str = "tcp"
|
|
57
|
+
) -> Dict[str, List[dict]]:
|
|
58
|
+
"""
|
|
59
|
+
Проверяет доступность upstream-серверов.
|
|
60
|
+
mode: "tcp" (по умолчанию) или "http"
|
|
61
|
+
|
|
62
|
+
Возвращает:
|
|
63
|
+
{
|
|
64
|
+
"backend": [
|
|
65
|
+
{"address": "127.0.0.1:8080", "healthy": True},
|
|
66
|
+
...
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
"""
|
|
70
|
+
results = {}
|
|
8
71
|
for name, servers in upstreams.items():
|
|
9
|
-
|
|
72
|
+
results[name] = []
|
|
10
73
|
for srv in servers:
|
|
11
|
-
|
|
12
|
-
|
|
74
|
+
if mode.lower() == "http":
|
|
75
|
+
healthy = check_http(srv, timeout, retries)
|
|
76
|
+
else:
|
|
77
|
+
healthy = check_tcp(srv, timeout, retries)
|
|
78
|
+
results[name].append({"address": srv, "healthy": healthy})
|
|
79
|
+
return results
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|