diff --git a/README.md b/README.md index a57c9af..9237661 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,16 @@ Start | End | Duration 2024-10-19 16:33:24 | 2024-10-19 16:34:27 | 63.17s (1.05 minutes) ``` +You can also add your own hosts to ping by using the `--extra-hosts` or `e` flag when +it's ran in the command line, giving each host's address and name. +For example, + +```python uptime.py --extra-hosts 8.8.4.4 GoogleDNS 1.0.0.1 "Another Host"``` + +```python uptime.py -e 8.8.4.4 GoogleDNS``` + +```uptime.exe -e 8.8.4.4 GoogleDNS 208.67.222.222 "Another Host"``` + Note that if using Windows 10 or below, the emojis are replaced by [✓] and [X]. @@ -123,5 +133,3 @@ If you encounter issues: ## License This project is licensed under the Apache NON-AI License, Version 2.0 - see the [LICENSE](LICENSE.txt) file for details - - diff --git a/uptime.exe b/uptime.exe index 3b17d6c..4a76b60 100644 Binary files a/uptime.exe and b/uptime.exe differ diff --git a/uptime.py b/uptime.py index c354b2d..30ee4f8 100644 --- a/uptime.py +++ b/uptime.py @@ -1,12 +1,15 @@ -""" +r""" Can be run for days to see how often the internet is up. + +Compiled into exe with `pyinstaller -F .\uptime.py` """ +import argparse +import os +import platform import subprocess +import sys import time from dataclasses import dataclass, field -import sys -import platform -import os from datetime import datetime sys.stdout.reconfigure(encoding='utf-8') @@ -30,7 +33,6 @@ checkmark = '[√]' x = '[X]' - if os_is_windows: ping_arg = "-n" else: @@ -66,6 +68,7 @@ def duration(self) -> float: @dataclass(slots=True) class Host: address: str + name: str = '' success_pings: int = 0 failed_pings: int = 0 total_pings: int = 0 @@ -76,6 +79,10 @@ class Host: uptime_percentage: float = 0 avg_response_time: float = 0 + def __post_init__(self): + if not self.name: + name = self.address + def update_response_time(self, time_: float): self.response_times.append(time_) self.total_response_time += time_ @@ -91,8 +98,31 @@ def calculate_stats(self): def main() -> None: - all_host_ips = ["9.9.9.9", "1.1.1.1", "8.8.8.8"] # Quad9, Cloudflare, Google DNS servers (could do 208.67.222.222) - all_hosts: list[Host] = [Host(address=host_ip) for host_ip in all_host_ips] + host_quad9 = Host(address='9.9.9.9', name='Quad9') + host_cloudflare = Host(address='1.1.1.1', name='Cloudflare') + host_google = Host(address='8.8.8.8', name='Google') + all_hosts: list[Host] = [host_quad9, host_cloudflare, host_google] + + parser = argparse.ArgumentParser(description="Add extra hosts.") + parser.add_argument( + '-e', '--extra-hosts', + nargs='*', + metavar=('ADDRESS', 'NAME'), + help="Add extra hosts in the form of address and name pairs. Example: --extra-hosts 8.8.4.4 GoogleDNS" + ) + + args = parser.parse_args() + + if args.extra_hosts: + extra_hosts = args.extra_hosts + for i in range(0, len(extra_hosts), 2): + try: + address = extra_hosts[i] + name = extra_hosts[i + 1] + all_hosts.append(Host(address=address, name=name)) + except IndexError: + print("Error: Each extra host requires an address and a name.") + return ping_interval_seconds = 3 info_print_interval_seconds = 40 @@ -103,8 +133,9 @@ def main() -> None: outages: list[Outage] = [] start_time = 0.0 - print_and_log(" " * 5 + "\nMonitoring internet uptime by pinging DNS servers:\n" + "-" * 60 + "\n") - print_and_log(" " * 22 + "Quad9 Cloudflare Google") + formatted_names = " " * 22 + ''.join([host.name.ljust(17) for host in all_hosts]) + print_and_log("\nMonitoring internet uptime by pinging DNS servers:\n" + "-" * len(formatted_names) + "\n") + print_and_log(formatted_names) script_start = time.time() try: @@ -144,7 +175,8 @@ def main() -> None: outage = Outage(outage_start, outage_end) outages.append(outage) outage_start = 0.0 - print_and_log(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {' '.join(ping_result_output)}"[:-2]) + print_and_log( + f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {' '.join(ping_result_output)}"[:-2]) if all_hosts[0].total_pings % info_print_interval_seconds == 0: # Every x seconds for host in all_hosts: @@ -155,10 +187,11 @@ def main() -> None: if outages: print_outage_info(outages) elif outage_start: - print_and_log(f"\nOngoing outage since {datetime.fromtimestamp(start_time).strftime('%Y-%m-%d %H:%M:%S')}") + print_and_log( + f"\nOngoing outage since {datetime.fromtimestamp(start_time).strftime('%Y-%m-%d %H:%M:%S')}") else: print_and_log("\nNo outages") - print_and_log("\n Quad9 Cloudflare Google") + print_and_log("\n" + formatted_names) time.sleep(ping_interval_seconds) @@ -236,6 +269,5 @@ def print_and_log(output: str) -> None: file.write(output + '\n') - if __name__ == '__main__': main()