argos-httping/ping.1m+.py

81 lines
3.6 KiB
Python
Raw Normal View History

2018-06-13 07:59:34 +02:00
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
""" Simulates httping for `argos <https://github.com/p-e-w/argos>`"""
2018-06-13 08:46:34 +02:00
# <bitbar.title>httping</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Tobias Schmidl</bitbar.author>
# <bitbar.author.github>schtobia</bitbar.author.github>
# <bitbar.desc>Short description of what your plugin does.</bitbar.desc>
# <bitbar.image></bitbar.image>
# <bitbar.dependencies>python,requests</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/schtobia/argos-httping/blob/master/ping.1m+.py</bitbar.abouturl>
2018-06-13 07:59:34 +02:00
import sys #for redirecting print to stderr
import datetime #for the whole ms → s and vice versa stuff
import requests #for the central HTTP HEAD request
import statistics #for median and stdev
__author__ = "Tobias Schmidl"
__copyright__ = "Copyright 2018, Tobias Schmidl"
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "Tobias Schmidl"
2018-06-13 08:46:34 +02:00
max_ping = datetime.timedelta(milliseconds = 1000) # in seconds
targets = ["https://www.google.de", "https://www.bing.com", "https://www.wikipedia.org"]
2018-06-13 07:59:34 +02:00
max_ping_in_ms = max_ping.total_seconds() * 1000
class Themes:
2018-06-13 08:46:34 +02:00
# Themes are from http://colorbrewer2.org/
2018-06-13 07:59:34 +02:00
purple_green = ["#762a83", "#9970ab","#c2a5cf","#a6dba0","#5aae61","#1b7837"]
red_green = ["#d73027", "#fc8d59","#fee08b","#d9ef8b","#91cf60","#1a9850"]
original = ["#acacac","#ff0101","#cc673b","#ce8458","#6bbb15","#0ed812"]
selected_colors = red_green
def colorize(response_time):
# panda's "cut" functon would be an alternative here, but this would be overkill, to include numpy just for this
if response_time < max_ping_in_ms / 5:
return Themes.selected_colors[5]
elif response_time >= max_ping_in_ms / 5 and response_time < 2 * max_ping_in_ms / 5:
return Themes.selected_colors[4]
elif response_time >= 2 * max_ping_in_ms / 5 and response_time < 3 * max_ping_in_ms / 5:
return Themes.selected_colors[3]
elif response_time >= 3 * max_ping_in_ms / 5 and response_time < 4 * max_ping_in_ms / 5:
return Themes.selected_colors[2]
elif response_time >= 4 * max_ping_in_ms / 5 and response_time < max_ping_in_ms:
return Themes.selected_colors[1]
else:
return Themes.selected_colors[0]
def httping(targets):
responses = {}
for target in targets:
try:
responses[target] = requests.head(target, timeout = max_ping.total_seconds()).elapsed.total_seconds() * 1000
except requests.exceptions.RequestException as ex:
print(str(ex), file=sys.stderr)
responses[target] = None
response_times = [value for key, value in responses.items() if isinstance(value, float)]
if len(response_times) > 1:
return responses, statistics.median(response_times), statistics.stdev(response_times)
elif len(response_times) > 0:
return responses, response_times[0], 0
else:
return responses, None, None
if __name__ == "__main__":
response_times, median, stddev = httping(targets)
if isinstance(median, float) and median != max_ping_in_ms:
print("⦿ " + str(round(median,2)) + "±" + str(round(stddev, 2)) + " | color=" + Themes.colorize(median))
else:
print("☠ | color=" + Themes.selected_colors[0]);
print("---")
for target, response_time in response_times.items():
if isinstance(response_time, float):
print(target + ": " + str(round(response_time, 2)) + " | color=" + Themes.colorize(response_time))
else:
print(target + ": ☠ | color=" + Themes.selected_colors[0])