Created
September 24, 2017 07:09
-
-
Save rsyring/304d0a04064fd325d4ac140a1dad4126 to your computer and use it in GitHub Desktop.
A python script that pings a host and uploads metrics to Librato
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import librato | |
from sh import ping, hostname | |
host = 'google.com' | |
# How many pings should we do before we process the results | |
ping_for = '30' | |
# How many times should we ping, process results, and send to Librato | |
repeat = 2 | |
liberato_token = '<censored>' | |
# Edit lines above, leave the rest. | |
i_am = hostname().strip() | |
def ping_and_send(): | |
result = ping('-w', ping_for, '-i', '1', host) | |
# `result` will have lines like "64 bytes from (172.217.0.142): icmp_seq=1 ttl=48 time=65.0 ms". | |
matches = re.findall('time=(\d+(.\d+)?) ms', str(result), re.MULTILINE) | |
# Parse values from "time=(...) ms" sections of the result and convert to floats. | |
times = [float(line_groups[0]) for line_groups in matches] | |
# Calculate values to send to librato. | |
ping_avg = sum(times) / len(times) | |
ping_min = min(times) | |
ping_max = max(times) | |
# Send to Librato. | |
tags = dict(from_host=i_am, to_host=host) | |
api = librato.connect('<censored>', liberato_token, tags=tags) | |
with api.new_queue() as q: | |
q.add('ping.min', ping_min) | |
q.add('ping.max', ping_max) | |
q.add('ping.avg', ping_avg) | |
print('sent stats') | |
for x in range(0, repeat): | |
ping_and_send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment