Skip to content

Commit

Permalink
Merge pull request #5 from levpen/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Antony-Sk committed Apr 29, 2024
2 parents bc46894 + 32dc8f2 commit d518107
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
python -m pip install --upgrade pip
python -m pip install poetry
poetry install
apt install -y inetutils-ping nmap
- name: Run Ruff
run: |
poetry run ruff check
- name: Test
run: |
poetry run coverage run -m pytest
poetry run coverage run -m pytest tests
poetry run coverage report --fail-under=60
3 changes: 3 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

[lint.per-file-ignores]
"tests/test_collect_data.py" = ["S101"]
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM duffn/python-poetry:3.11-slim

EXPOSE 8501
WORKDIR /app
COPY poetry.lock pyproject.toml /app/
COPY backend/*.py /app/backend/
RUN apt update && \
apt install -y nmap iputils-ping && \
poetry install

CMD ["(poetry run python3 backend/main.py &) &&", "poetry run streamlit run", "backend/app.py"]
Binary file removed backend/__pycache__/collect_data.cpython-310.pyc
Binary file not shown.
10 changes: 7 additions & 3 deletions backend/collect_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ def get_loss_and_latency(host: str, n_packet: int = 5) -> (float, float):
"""Do getting packet loss and latency of resource, using ICMP port via ping."""
process = Popen(['/bin/sh', '-c', f'ping -c {n_packet} {host}'], stdout=PIPE)
stdout, _ = process.communicate()
if stdout.decode('utf-8').find("Name or service not known") != -1:
return (100.0, 0.0)
packetloss = float(next(x for x in stdout.decode('utf-8').split('\n')
if x.find('packet loss') != -1).split('%')[0].split(' ')[-1])
latency = float(stdout.decode('utf-8').split('\n')[-3].split(' ')[-1][:-2])/n_packet

return (packetloss, latency)


def get_accessibility(host: str, port: int) -> str:
"""Do checks of accessibility of given resource via nmap util."""
process = Popen(['/bin/sh', '-c', f'nmap -p {port} {host}'], stdout=PIPE)
stdout, _ = process.communicate()
acc = stdout.decode('utf-8').split('\n')[-4].split(' ')[1]

try:
acc = stdout.decode('utf-8').split('\n')[-4].split(' ')[1]
except IndexError:
acc = "unreachable"
return acc


Expand Down
32 changes: 32 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Daemon that collect data from hosts."""
from persistence import MetricsRepository
from collect_data import collect_data
from time import sleep
from threading import Thread

DB_PATH = "metrics.db"

def collect_and_append_data(host: str, records: []) -> None:
"""Do to run in thread."""
record = collect_data(host, 1)
records.append(record)

if __name__ == "__main__":

with MetricsRepository(DB_PATH) as metrics_repo:
while True:
hosts = ['.'.join(item) for item in metrics_repo.get_hosts()]
records = []
threads = []
for host in hosts:
thread = Thread(target=collect_and_append_data, args=(host, records))
threads.append(thread)
thread.start()

sleep(1)
for thread in threads:
thread.join()

for host, record in zip(hosts, records, strict=False):
metrics_repo.save_record(host, record)

37 changes: 37 additions & 0 deletions tests/test_collect_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Tests for collect_data.py."""
from backend.collect_data import collect_data,get_loss_and_latency,get_accessibility

def test_collect_data() -> None:
"""Do call of stable service to test."""
host = "ya.ru" # Assume super stable
expected_result = {
"loss": 0.0,
"latency": 0.0,
"accessibility": [
("HTTP", "open"),
("HTTPS", "open"),
("IMAP", "filtered"),
("SMTP", "filtered"),
("SSH", "filtered"),
("DNS", "filtered")
]
}
result = collect_data(host)
for ex_status, status in zip(expected_result["accessibility"],
result["accessibility"], strict=False):
assert ex_status == status

def test_get_acc() -> None:
"""Do call services to test."""
assert get_accessibility("ya.ru", 80) == "open"
assert get_accessibility("ya.ru", 43) == "filtered"
assert get_accessibility("ru.ya", 80) == "unreachable"


def test_ping() -> None:
"""Do ping services to test."""
assert get_loss_and_latency("ya.ru", 1)[0] == 100.0
assert get_loss_and_latency("ya.ru", 1)[1] >= 0.0
assert get_loss_and_latency("ru.ya", 1)[0] == 100.0


0 comments on commit d518107

Please sign in to comment.