Skip to content

Commit

Permalink
env updates
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaspcs committed Apr 12, 2023
1 parent 9206b34 commit 1f990a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
10 changes: 0 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
FROM python:3.9-slim-buster
# Set the environment variables
ENV MINUTES_INTERVAL=1
ENV INFLUXDB_BUCKET=my-bucket
ENV INFLUXDB_ORG=my-org
ENV INFLUXDB_TOKEN=my-token
ENV INFLUXDB_URL=http://localhost:8086
ENV PING_HOST=8.8.8.8
ENV PING_PACKETS=5
ENV PING_INTERVAL=0.5
ENV NOTIFY_ALWAYS=True

# Set the working directory
WORKDIR /app
Expand Down
57 changes: 28 additions & 29 deletions telepinger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@
logger.info("Starting Telepinger")
logger.info('Getting environment variables')

bucket = os.environ.get('INFLUXDB_BUCKET')
org = os.environ.get('INFLUXDB_ORG')
token = os.environ.get('INFLUXDB_TOKEN')
url = os.environ.get('INFLUXDB_URL')
bucket = os.environ.get('INFLUXDB_BUCKET', 'telepinger-no-bucket')
org = os.environ.get('INFLUXDB_ORG', 'my_org')
token = os.environ.get('INFLUXDB_TOKEN', 'my-token')
url = os.environ.get('INFLUXDB_URL', 'http://localhost:8086')
notify_always = os.environ.get('NOTIFY_ALWAYS', 'True')
notify_always = notify_always.lower() in ['true', '1', 'yes']

print(f'Bucket: {bucket}, Org: {org}, Token: {token}, URL: {url}, Notify Always: {notify_always}')

if not all([bucket, org, token, url]):
print("Missing InfluxDB configuration. Please check environment variables.")
logger.info('Missing InfluxDB configuration. Please check environment variables.')
exit(1)

hostname = socket.gethostname()

os_name = platform.system()
Expand Down Expand Up @@ -72,28 +67,32 @@
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:
logger.info('Sending to InfluxDB')
if packet_loss > 0:
logger.warning('Packet loss detected!')
# open Influx
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
)

write_api = client.write_api(write_options=SYNCHRONOUS)

p = influxdb_client.Point("ping") \
.tag("host", hostname) \
.tag("dest", args.host) \
.field("trx", packets_sent) \
.field("rcx", packets_received) \
.field("loss", packet_loss) \
.field("min", min_ms) \
.field("max", max_ms) \
.field("avg", avg_ms)
write_api.write(bucket=bucket, org=org, record=p)

if bucket!='telepinger-no-bucket':
# open Influx
logger.info('Sending to InfluxDB')
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
)

write_api = client.write_api(write_options=SYNCHRONOUS)

p = influxdb_client.Point("ping") \
.tag("host", hostname) \
.tag("dest", args.host) \
.field("trx", packets_sent) \
.field("rcx", packets_received) \
.field("loss", packet_loss) \
.field("min", min_ms) \
.field("max", max_ms) \
.field("avg", avg_ms)
write_api.write(bucket=bucket, org=org, record=p)
else:
logger.warning('No bucket specified, not sending to InfluxDB')

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

Expand Down

0 comments on commit 1f990a7

Please sign in to comment.