Skip to content

Instantly share code, notes, and snippets.

Created Oct 10, 2019
What would you like to do?
Monitor packet loss to a particular host and send statistics to Cloudwatch
import threading
import time
import logging
logging.basicConfig(level=logging.INFO, format='[%(levelname)s %(asctime)s %(name)s] %(message)s')
logger = logging.getLogger('pingmon')
import boto3
from pythonping.executor import Communicator
target = ''
timeout = 1
client = Communicator(target, None, timeout)
seq = 1
identifier = client.seed_id
attempts = 0
lost = 0
dump_requested = False
session = boto3.Session(profile_name='monitoring')
cloudwatch = session.client('cloudwatch')
def monitor():
global attempts
global lost
while True:
loss = round(100 * lost / attempts)'{} packets sent {} packets lost {}% packet loss'.format(attempts, lost, loss))
attempts = 0
lost = 0
'MetricName': 'PacketLoss',
'Unit': 'Percent',
'Value': loss
], Namespace='Home')
thread = threading.Thread(target=monitor, args=())
thread.daemon = True
thread.start()'pingmon is monitoring {}'.format(target))
while True:
attempts += 1
client.send_ping(client.seed_id, seq, b'a')
res = client.listen_for(identifier, timeout)
if not res.message:
lost += 1
seq = client.increase_seq(seq)
Copy link

keeth commented Oct 10, 2019

Ping a host every second. Every minute send packet loss percentage metric to Amazon CloudWatch.

I wrote the script so that I could monitor my upstream internet connection via Raspberry Pi. Sending to Cloudwatch meant that I did not need to log the data to disk and worry about wearing out my SD card.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment