From a1e168f345bd2cadb1528d925a096a0788461839 Mon Sep 17 00:00:00 2001 From: Dan Hamik Date: Tue, 26 Nov 2024 13:22:46 -0600 Subject: [PATCH] initial commit --- .gitea/workflows/check-build.yml | 29 ++++++++++++++++++++++ .gitea/workflows/push-build.yml | 41 ++++++++++++++++++++++++++++++++ Dockerfile | 12 ++++++++++ docker-compose.yml | 9 +++++++ init.sh | 2 ++ logspeedtest.py | 35 +++++++++++++++++++++++++++ ookla_speedtest_cli.list | 5 ++++ requirements.txt | 5 ++++ 8 files changed, 138 insertions(+) create mode 100644 .gitea/workflows/check-build.yml create mode 100644 .gitea/workflows/push-build.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 init.sh create mode 100644 logspeedtest.py create mode 100644 ookla_speedtest_cli.list create mode 100644 requirements.txt diff --git a/.gitea/workflows/check-build.yml b/.gitea/workflows/check-build.yml new file mode 100644 index 0000000..fc98f06 --- /dev/null +++ b/.gitea/workflows/check-build.yml @@ -0,0 +1,29 @@ +Name: Commit Checks +run-name: ${{ gitea.actor }} +on: + push: + branches: + - "**" + - "!main" + +jobs: + Plan: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./ + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 +# + - name: List Contents + id: ls + run: ls -al + + + - name: Deploy + run: | + echo "${{ secrets.GITEASECRET_TOKEN }}" | docker login git.hamik.net -u paradizelost --password-stdin + docker build -t git.hamik.net/paradizelost/speedtest:dev . + docker push git.hamik.net/paradizelost/speedtest:dev \ No newline at end of file diff --git a/.gitea/workflows/push-build.yml b/.gitea/workflows/push-build.yml new file mode 100644 index 0000000..c96248d --- /dev/null +++ b/.gitea/workflows/push-build.yml @@ -0,0 +1,41 @@ +Name: Container Build Push +run-name: ${{ gitea.actor }} +on: + pull_request: + types: [closed] + +jobs: + Plan: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: List Contents + id: ls + run: ls -al + + - name: Deploy + run: | + echo "${{ secrets.GITEASECRET_TOKEN }}" | docker login git.hamik.net -u paradizelost --password-stdin + docker build -t git.hamik.net/paradizelost/speedtest:latest . + docker push git.hamik.net/paradizelost/speedtest:latest + Deploy: + needs: [Plan] + runs-on: speedtest + defaults: + run: + working-directory: /docker/speedtest + steps: + - name: Destroy Container + id: down + run: docker compose down + + - name: Pull Updates + id: pull + run: docker compose pull + + - name: Start Containers + id: start + run: docker compose up -d \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2591770 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM debian:bullseye-slim +# +COPY ookla_speedtest_cli.list /etc/apt/sources.list.d/ +RUN apt update && apt install python3 python3-pip iputils-ping speedtest -y +COPY logspeedtest.py /usr/local/bin/ +COPY requirements.txt / +COPY init.sh / +RUN chmod +x /init.sh +RUN chmod +x /usr/local/bin/logspeedtest.py +RUN pip3 install -r /requirements.txt +RUN apt clean +CMD ["/init.sh"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a369ac6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + speedtest: + image: git.hamik.net/paradizelost/speedtest:latest + container_name: speedtest + restart: always + environment: + - INFLUX_USER=${INFLUX_USER} + - INFLUX_PASSWORD=${INFLUX_PASSWORD} + - INFLUX_URI=${INFLUX_URI} \ No newline at end of file diff --git a/init.sh b/init.sh new file mode 100644 index 0000000..032f165 --- /dev/null +++ b/init.sh @@ -0,0 +1,2 @@ +#!/bin/bash +/usr/local/bin/logspeedtest.py \ No newline at end of file diff --git a/logspeedtest.py b/logspeedtest.py new file mode 100644 index 0000000..97177f9 --- /dev/null +++ b/logspeedtest.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 +import os +import subprocess as sp +import requests +import time +import json +def addinfluxrecord(downspeed,upspeed,pktloss,jitter,latencyavg,minlatency,maxlatency): + data = f"Down value={downspeed}\nUp value={upspeed}\nPacketLoss value={pktloss}\nJitter value={jitter}\nLatencyAvg value={latencyavg}\nLatencyLow value={minlatency}\nLatencyHigh value={maxlatency}" + print(data) + headers = {'Content-Type': 'application/x-www-form-urlencoded'} + response = requests.post( + influxuri, + data=data, + headers=headers, + auth=(infdbuser,infdbpass) + ) + return response + +infdbuser = os.environ["INFLUX_USER"] +infdbpass = os.environ["INFLUX_PASSWORD"] +influxuri = os.environ["INFLUX_URI"] +while(True): + response = sp.getstatusoutput(f"speedtest --format=json")[1] + respjson = json.loads(response.split("\n")[1]) + print(response) + downspeed = respjson['download']['bandwidth'] * 8 / 1024 / 1024 + upspeed = respjson['upload']['bandwidth'] * 8 / 1024 / 1024 + pktloss = respjson['packetLoss'] + jitter = respjson['ping']['jitter'] + latencyavg=respjson['ping']['latency'] + minlatency=respjson['ping']['low'] + maxlatency=respjson['ping']['high'] + result = addinfluxrecord(downspeed,upspeed,pktloss,jitter,latencyavg,minlatency,maxlatency) + print(result) + time.sleep(300) diff --git a/ookla_speedtest_cli.list b/ookla_speedtest_cli.list new file mode 100644 index 0000000..38322ef --- /dev/null +++ b/ookla_speedtest_cli.list @@ -0,0 +1,5 @@ +# this file was generated by packagecloud.io for +# the repository at https://packagecloud.io/ookla/speedtest-cli + +deb [signed-by=/etc/apt/keyrings/ookla_speedtest-cli-archive-keyring.gpg] https://packagecloud.io/ookla/speedtest-cli/debian/ bookworm main +deb-src [signed-by=/etc/apt/keyrings/ookla_speedtest-cli-archive-keyring.gpg] https://packagecloud.io/ookla/speedtest-cli/debian/ bookworm main \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e6593da --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +boto==2.49.0 +boto3==1.26.27 +botocore==1.29.27 +requests==2.28.1 +tabulate==0.8.9 \ No newline at end of file