Skip to content

Instantly share code, notes, and snippets.

@tchellomello
Last active July 27, 2020 21:57
Show Gist options
  • Save tchellomello/7eb0e280f3627d183fecd7a9971f9651 to your computer and use it in GitHub Desktop.
Save tchellomello/7eb0e280f3627d183fecd7a9971f9651 to your computer and use it in GitHub Desktop.
ookla-speedtest on k8s
FROM centos:8
LABEL MAINTAINER Tchello Mello <tchello.mello@gmail.com>
# install main packages:
RUN yum -y update && \
yum -y install wget python38 && \
wget https://bintray.com/ookla/rhel/rpm -O /etc/yum.repos.d/bintray-ookla-rhel.repo && \
yum -y install speedtest && \
yum clean all
ADD start.py /
RUN chmod +x /start.py
RUN ln -sf /proc/1/fd/1 /tmp/speedtest.out
CMD ["/start.py"]
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: ookla-speedtest-cronjob
spec:
schedule: "37 * * * *"
concurrencyPolicy: Replace
successfulJobsHistoryLimit: 16
failedJobsHistoryLimit: 5
jobTemplate:
spec:
template:
metadata:
labels:
jobgroup: ookla-speedtest
spec:
restartPolicy: OnFailure
containers:
- name: ookla-speedtest
image: registry.tatu.home/tekton/ookla-speedtest:latest
imagePullPolicy: Always
env:
- name: DOWNLOAD_THRESHOLD
value: "500"
#!/usr/bin/env python3
import os
import smtplib
from subprocess import Popen, PIPE
from email.message import EmailMessage
DOWNLOAD_THRESHOLD = float(os.environ.get('DOWNLOAD_THRESHOLD', 600))
SMTP_PORT = os.environ.get('SMTP_PORT', 25)
SMTP_SERVER = os.environ.get('SMTP_SERVER', 'smtp.example.home')
EMAIL_FROM = os.environ.get('EMAIL_FROM', 'me@email.com')
EMAIL_TO = os.environ.get('EMAIL_TO', 'me@email.com')
DEBUG = os.environ.get('DEBUG', 'False')
SPEEDTEST_OUTPUT = '/tmp/speedtest.out'
msg = EmailMessage()
msg['From'] = EMAIL_FROM
msg['To'] = EMAIL_TO
msg['Subject'] = '[Toca K8S] - Speedtest Notification by Ookla'
# run command
p = Popen(
["/usr/bin/speedtest", '--accept-license', '--progress', 'no'],
stdout=PIPE, stderr=PIPE, universal_newlines=True
)
stdout, stderr = p.communicate()
for line in stdout.splitlines():
if "Download:" in line:
try:
download = float(line.split(' (')[0].replace(' ','').replace('Download:','').replace('Mbps',''))
if download >= DOWNLOAD_THRESHOLD:
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.ehlo()
msg.set_content(stdout)
server.send_message(msg)
# save output to file
# print to stdout
with open(SPEEDTEST_OUTPUT, 'w') as fd:
fd.write(stdout)
except:
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment