aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2026-02-26 18:13:08 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2026-02-26 18:13:08 +0000
commit59313925ddce08cbcadca8605fc5a3c1d5b6d439 (patch)
tree024af4b7a0dfc9eefb970c55e4f350f9b7fbb0e2
parent3fbc69f98f42e4a3fef5743f69845d2d95e50162 (diff)
downloadPingLogger-59313925ddce08cbcadca8605fc5a3c1d5b6d439.tar.gz
PingLogger-59313925ddce08cbcadca8605fc5a3c1d5b6d439.zip
Initial commit
l---------.dockerignore1
-rw-r--r--.gitignore2
-rw-r--r--Dockerfile9
-rw-r--r--config.env.example10
-rw-r--r--docker-compose.yml10
-rw-r--r--pingLogger.py58
-rw-r--r--requirements.txt2
7 files changed, 92 insertions, 0 deletions
diff --git a/.dockerignore b/.dockerignore
new file mode 120000
index 0000000..3e4e48b
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1 @@
+.gitignore \ No newline at end of file
diff --git a/.gitignore b/.gitignore
index b7faf40..ff29492 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
+config.env
+
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[codz]
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..90f147c
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,9 @@
+FROM reg.reaweb.uk/cron
+
+RUN apt update -y
+RUN apt install -y python3-pip iputils-ping
+COPY . /app
+WORKDIR /app
+RUN pip3 install -r requirements.txt
+
+RUN echo "*/1 * * * * root python3 /app/pingLogger.py > /proc/1/fd/1 2>/proc/1/fd/2" > /etc/crontab \ No newline at end of file
diff --git a/config.env.example b/config.env.example
new file mode 100644
index 0000000..9221f0d
--- /dev/null
+++ b/config.env.example
@@ -0,0 +1,10 @@
+DOCKER_INFLUXDB_INIT_MODE=setup
+DOCKER_INFLUXDB_INIT_USERNAME=eden
+DOCKER_INFLUXDB_INIT_PASSWORD=*****************
+DOCKER_INFLUXDB_INIT_ORG=poweredagay
+DOCKER_INFLUXDB_INIT_BUCKET=edenbucket
+DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=***************************************
+DOCKER_INFLUXDB_DB=power
+
+PING_HOST=8.8.8.8
+NUM_PINGS=10 \ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..ea57c08
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,10 @@
+services:
+ pinglogger:
+ build:
+ context: .
+ dockerfile: Dockerfile
+ container_name: ping_logger
+ image: reg.reaweb.uk/pinglogger
+ env_file:
+ - config.env
+ restart: unless-stopped \ No newline at end of file
diff --git a/pingLogger.py b/pingLogger.py
new file mode 100644
index 0000000..e114780
--- /dev/null
+++ b/pingLogger.py
@@ -0,0 +1,58 @@
+from influxdb_client import InfluxDBClient, Point, WritePrecision
+from influxdb_client.client.write_api import SYNCHRONOUS
+import subprocess
+import dotenv
+import json
+import re
+import os
+
+PING_INDEXES = ["rtt_min", "rtt_avg", "rtt_max", "rtt_mdev"]
+
+def do_ping(host, numpings):
+ proc = subprocess.Popen(["ping", host, "-c", str(numpings)], stdout = subprocess.PIPE)
+ out = []
+ while True:
+ line = proc.stdout.readline()
+ if not line:
+ break
+ line = line.rstrip().decode()
+ print(line)
+ out.append(line)
+
+ return {
+ "packet_loss_percent": int(re.findall(r"\d+", out[-2].split(", ")[2])[0]),
+ "total_time": int(re.findall(r"\d+", out[-2].split(", ")[3])[0])
+ } | {PING_INDEXES[i]: float(f) for i, f in enumerate(out[-1].split(" ")[3].split("/"), 0)}
+
+def influx_write(fields):
+ influxc = InfluxDBClient(
+ url = "http://%s:8086" % INFLUXDB_HOST,
+ token = os.environ["DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"],
+ org = os.environ["DOCKER_INFLUXDB_INIT_ORG"]
+ )
+ influxc.ping()
+
+ write_api = influxc.write_api(write_options = SYNCHRONOUS)
+ write_api.write(
+ os.environ["DOCKER_INFLUXDB_INIT_BUCKET"],
+ os.environ["DOCKER_INFLUXDB_INIT_ORG"],
+ [{
+ "measurement": "ping_logger",
+ "fields": fields
+ }],
+ write_precision = WritePrecision.S
+ )
+
+def main():
+ fields = do_ping(os.environ["PING_HOST"], os.environ["NUM_PINGS"])
+ print(json.dumps(fields, indent = 4))
+ influx_write(fields)
+
+if __name__ == "__main__":
+ env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.env")
+ if os.path.exists(env_path):
+ import dotenv
+ dotenv.load_dotenv(dotenv_path = env_path)
+ INFLUXDB_HOST = "192.168.69.5"
+
+ main() \ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..5bc1166
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+python-dotenv
+influxdb-client \ No newline at end of file