Skip to content

Commit

Permalink
command setup and redone
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaspcs committed Apr 12, 2023
1 parent 82a6f38 commit aeef953
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
docker-compose.yml
config.py
__pycache__
6 changes: 1 addition & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ ENV MINUTES_INTERVAL=1
ENV PING_HOST=8.8.8.8
ENV PING_PACKETS=5
ENV PING_INTERVAL=0.5
ENV INFLUXDB_BUCKET=telepinger-no-bucket
ENV INFLUXDB_ORG=telepinger
ENV INFLUXDB_TOKEN=telepinger
ENV INFLUXDB_URL=http://localhost:8086
ENV NOTIFY_ALWAYS=True

# Set the working directory
WORKDIR /app
Expand All @@ -35,6 +30,7 @@ RUN /usr/local/bin/python -m pip install --no-cache-dir -r requirements.txt

# Copy the script into the container
COPY telepinger.py .
COPY config.py .

# Copy the entrypoint script into the container
COPY entrypoint.sh .
Expand Down
6 changes: 6 additions & 0 deletions config.sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Config file for telepinger
INFLUXDB_BUCKET="telepinger-no-bucket"
INFLUXDB_ORG="telepinger"
INFLUXDB_TOKEN="telepinger"
INFLUXDB_URL="http://localhost:8086"
NOTIFY_ALWAYS=True
5 changes: 0 additions & 5 deletions docker-compose.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ services:
restart: always
environment:
- MINUTES_INTERVAL=1
- INFLUXDB_BUCKET=my-bucket
- INFLUXDB_ORG=my-org
- INFLUXDB_TOKEN=my-token
- INFLUXDB_URL=http://localhost:8086
- PING_HOST=8.8.8.8
- PING_PACKETS=5
- PING_INTERVAL=1
- NOTIFY_ALWAYS=True
logging:
driver: "json-file"
options:
Expand Down
11 changes: 6 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/bin/sh

echo "Starting telepinger..."

echo "Current environment variables:"
echo "==================== Starting telepinger..."

echo "Current ping variables:"
env | grep MINUTES_
env | grep PING_
env | grep INFLUXDB_
env | grep NOTIFY_

echo "==================== Setting up cron..."

# Set up the cron job
echo "*/$MINUTES_INTERVAL * * * * /usr/local/bin/python /app/telepinger.py -c $PING_PACKETS -i $PING_INTERVAL $PING_HOST > /proc/1/fd/1 2>&1" | crontab -
crontab -l

echo "==================== Starting cron..."

# Start cron in the foreground
exec cron -f
45 changes: 20 additions & 25 deletions telepinger.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
import os
import subprocess
import platform
import re
import argparse
import influxdb_client
import socket
from config import INFLUXDB_BUCKET, INFLUXDB_ORG, INFLUXDB_TOKEN, INFLUXDB_URL, NOTIFY_ALWAYS
from influxdb_client.client.write_api import SYNCHRONOUS
from datetime import datetime

print("Starting Telepinger")
print('Getting environment variables')

print(os.environ)

bucket = os.environ.get('INFLUXDB_BUCKET')
org = os.environ.get('INFLUXDB_ORG')
token = os.environ.get('INFLUXDB_TOKEN')
url = os.environ.get('INFLUXDB_URL')
notify_always = os.environ.get('NOTIFY_ALWAYS', 'True')
notify_always = notify_always.lower() in ['true', '1', 'yes']


parser = argparse.ArgumentParser(description='Ping a host and return the results')
parser.add_argument('host', help='The host to ping')
parser.add_argument('-c', '--count', help='The number of packets to send', type=int, default=5)
parser.add_argument('-i', '--interval', help='The interval between packets', type=float, default=0.5)

args = parser.parse_args()

print(f'==================== Starting Telepinger to {args.host}...')

print(f'Bucket: {bucket}')
starttime = datetime.now()

hostname = socket.gethostname()

Expand All @@ -44,6 +32,7 @@

ping_result = subprocess.check_output(PING_CMD, shell=True, text=True)

# set defaults
packets_sent = 0
packets_received = 0
packet_loss = 0
Expand All @@ -66,19 +55,21 @@
max_ms = float(re.search(r'rtt min/avg/max/mdev = [\d.]+/([\d.]+)/', ping_result).group(1))
avg_ms = float(re.search(r'rtt min/avg/max/mdev = [\d.]+/[\d.]+/([\d.]+)/', ping_result).group(1))

if notify_always or packet_loss > 0:
print(f'Pinged {args.host}: {packets_received} packets back out of {packets_sent} sent with {packet_loss}% packet loss. Min: {min_ms}ms, Max: {max_ms}ms, Avg: {avg_ms}ms')

if NOTIFY_ALWAYS or packet_loss > 0:
if packet_loss > 0:
print('Packet loss detected!')

if bucket=='telepinger-no-bucket':
if INFLUXDB_BUCKET=='telepinger-no-bucket':
print('InfluxDB details have not been specified, not sending to InfluxDB')
else:
# open Influx
print(f'Sending to InfluxDB: {url}, {bucket}')
print(f'Sending to InfluxDB: {INFLUXDB_URL}, {INFLUXDB_BUCKET}')
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
url=INFLUXDB_URL,
token=INFLUXDB_TOKEN,
org=INFLUXDB_ORG
)

write_api = client.write_api(write_options=SYNCHRONOUS)
Expand All @@ -92,9 +83,13 @@
.field("min", min_ms) \
.field("max", max_ms) \
.field("avg", avg_ms)
write_api.write(bucket=bucket, org=org, record=p)
write_api.write(bucket=INFLUXDB_BUCKET, org=INFLUXDB_ORG, record=p)

endtime = datetime.now()

totaltime = endtime - starttime

print(f'Total time: {totaltime}')

current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
print('==================== Telepinger finished')

message = f'{current_time} - Pinged {args.host} from {hostname} and got {packets_received} packets back out of {packets_sent} sent with {packet_loss}% packet loss. Min: {min_ms}ms, Max: {max_ms}ms, Avg: {avg_ms}ms'
print(message)

0 comments on commit aeef953

Please sign in to comment.