From f564068ea586d835c8e10758d0dba615aae7d217 Mon Sep 17 00:00:00 2001 From: john Date: Sat, 18 May 2024 23:22:53 -0700 Subject: [PATCH] Create web server with exported data for Prometheus. --- Dockerfile | 18 ++++++++++++++++++ requirements.txt | 3 +++ tmobile_monitor.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 Dockerfile create mode 100644 requirements.txt create mode 100644 tmobile_monitor.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..263b339 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Use the official Python image from the Docker Hub +FROM python:3.9-slim + +# Set the working directory inside the container +WORKDIR /app + +# Copy the requirements file and the script into the container +COPY requirements.txt . +COPY tmobile_monitor.py . + +# Install the Python dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Expose the port that the Prometheus client will use +EXPOSE 8000 + +# Run the script +CMD ["python", "tmobile_monitor.py"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..65af412 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +requests~=2.31.0 +beautifulsoup4~=4.12.3 +prometheus_client~=0.20.0 \ No newline at end of file diff --git a/tmobile_monitor.py b/tmobile_monitor.py new file mode 100644 index 0000000..607110a --- /dev/null +++ b/tmobile_monitor.py @@ -0,0 +1,39 @@ +import requests +from bs4 import BeautifulSoup +from prometheus_client import start_http_server, Gauge +import time + +metrics = { + 'snr': Gauge('snr', 'Signal to Noise Ratio'), + 'internetStatusRSRP': Gauge('rsrp', 'Reference Signal Received Power'), + 'internetStatusRSRQ': Gauge('rsrq', 'Reference Signal Received Quality') +} + + +def collect_metrics(): + url = 'http://192.168.1.1' + + try: + response = requests.get(url, verify=False) + response.raise_for_status() + soup = BeautifulSoup(response.text, 'html.parser') + + for key in metrics: + element = soup.find('div', id=key) + if element: + value = float(element.text.strip().split()[0]) + metrics[key].set(value) + else: + print(f"{key.upper()} value not found on the page.") + + except requests.RequestException as e: + print(f"Error fetching data from {url}: {e}") + + +if __name__ == '__main__': + start_http_server(8000) + print("HTTP server started on port 8000") + + while True: + collect_metrics() + time.sleep(10)