basic functionality

This commit is contained in:
Christoph J. Scherr 2023-05-22 14:57:31 +02:00
parent d0eeb46822
commit 5b203904db
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
5 changed files with 55 additions and 1 deletions

View File

@ -1,3 +1,3 @@
# moninet
monitor over time for internet outages
monitor your network for internet outages

0
requirements.txt Normal file
View File

0
src/__init__.py Normal file
View File

39
src/main.py Normal file
View File

@ -0,0 +1,39 @@
import http.client
from time import sleep
import datetime
from socket import socket
TIMEOUT: float = 1
TESTDOMAINS: list[str] = ["cscherr.de", "debian.org", "cloudflare.com"]
def check_net() -> bool:
overall: bool = True
for domain in TESTDOMAINS:
status, msg = reachable(domain)
overall &= status
print(f"{domain:60s} {msg}")
return overall
def reachable(domain: str) -> tuple[bool, str]:
try:
conn = http.client.HTTPSConnection(domain, timeout=TIMEOUT)
conn.request("GET", "/")
r = conn.getresponse()
conn.close()
return True, r.status
except Exception as e:
return False, e.__str__()
def monitor():
while True:
try:
b = check_net()
if b: print(":SUCCESS:")
else: print(":FAILURE:")
sleep(4)
except Exception as e:
print(e)
if __name__ == "__main__":
monitor()

15
src/moninet.service Normal file
View File

@ -0,0 +1,15 @@
[Unit]
Description=Monitor network for internet outage
After=syslog.target
[Service]
Type=simple
User=moninet
Group=moninet
WorkingDirectory=/srv/moninet
ExecStart=/srv/moninet/.venv/bin/python3 -u /srv/moninet/src/main.py
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target