Last active
August 23, 2023 18:32
-
-
Save jonaslsaa/76e30bde83220fd4c699cce747da1ad0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# check ping and return ping | |
from pythonping import ping | |
import time | |
import re | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKCYAN = '\033[96m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
def ping_test(ip): | |
ret = ping(ip, verbose=False, count=1) | |
if "request timed out" in str(ret).lower(): | |
return "> 2000ms - timeout" | |
try: | |
return float(re.search(r"(\d+\.\d+|\d+)ms", str(ret)).group(1)) | |
except Exception as e: | |
print(e, ret, "\n") | |
return None | |
def ping_test_loop(ip, max_ping, interval): | |
ping_count = 0 | |
over_limit_count = 0 | |
while True: | |
ping = ping_test(ip) | |
ping_count += 1 | |
if ping is None: | |
continue | |
if isinstance(ping, str): | |
print(bcolors.FAIL + " * Crit:", ping, " ", bcolors.ENDC, end="\n") | |
over_limit_count += 1 | |
continue | |
color = bcolors.OKGREEN | |
if ping > max_ping: | |
color = bcolors.FAIL | |
over_limit_count += 1 | |
log_color = bcolors.WARNING | |
log_type = "Fail" | |
if ping > 350: | |
log_color = bcolors.FAIL | |
log_type = "Crit" | |
print(log_color + f" * {log_type}: ping over limit at", ping, "ms", " ", bcolors.ENDC, end="\n") | |
sucess_rate = (ping_count - over_limit_count) / ping_count * 100 | |
print(color, f"Ping: {ping}ms", f"({over_limit_count} fails/{ping_count} pings - {sucess_rate:.2f}%)", " ", end="\r" + bcolors.ENDC) | |
time.sleep(interval) | |
if __name__ == "__main__": | |
HOST = "84.214.104.76" | |
MAX_LIMIT_PING = 80 | |
INTERVAL = 0.08 | |
interval_ms = INTERVAL * 1000 | |
print(f"Starting ping test with {interval_ms}ms interval") | |
print(f"Pinging {HOST}") | |
ping_test_loop(HOST, MAX_LIMIT_PING, INTERVAL) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment