Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Sep 30, 2023
1 parent 1f81c22 commit c803763
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/heahmund/http_check.py → src/heahmund/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from app.util.logger import Logger
from heahmund.logger import Logger


class HttpCheck:
"""Http Check"""
class HTTP:
"""HTTP Check"""

def __init__(self, name):
self.name = name
Expand Down
34 changes: 20 additions & 14 deletions src/heahmund/udp_check.py → src/heahmund/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,30 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from app.util.logger import Logger
import logging


class UdpCheck:
"""UDP Check"""
class Logger:
"""Logger Class
def __init__(self, name, hostname="example.com", port=443, timeout=30):
self.name = name
self.hostname = hostname
self.port = port
self.timeout = timeout
self.logger = Logger().get_logger(__name__)
Attributes:
loggers: A dict of loggers
"""

def run(self):
"""
Run The Check
loggers = {}

def get_logger(self, name=__name__):
"""Get logger instance by name
Args:
name: logger identifier
Returns:
The Check Result
An instance of logging.Logger
"""
return {"name": self.name, "status": "OK"}
if name in self.loggers:
return self.loggers[name]

self.loggers[name] = logging.getLogger(name)

return self.loggers[name]
4 changes: 2 additions & 2 deletions src/heahmund/ping_check.py → src/heahmund/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from app.util.logger import Logger
from heahmund.logger import Logger


class PingCheck:
class Ping:
"""Ping Check"""

def __init__(self, name, hostname="example.com"):
Expand Down
6 changes: 3 additions & 3 deletions src/heahmund/certificate_check.py → src/heahmund/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from app.util.logger import Logger
from heahmund.logger import Logger


class CertificateCheck:
"""Certificate Check"""
class SSL:
"""SSL Check"""

def __init__(self, name, hostname="example.com", days=10, port=443, timeout=30):
self.name = name
Expand Down
43 changes: 40 additions & 3 deletions src/heahmund/tcp_check.py → src/heahmund/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from app.util.logger import Logger
import socket

from heahmund.logger import Logger
from heahmund.status import Status

class TcpCheck:

class TCP:
"""TCP Check"""

def __init__(self, name, hostname="example.com", port=443, timeout=30):
Expand All @@ -40,4 +43,38 @@ def run(self):
Returns:
The Check Result
"""
return {"name": self.name, "status": "OK"}
status = Status.OK
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self.timeout)

try:
sock.connect((self.hostname, self.port))

self.logger.debug(
"Server with hostname {} with port {}.".format(self.hostname, self.port)
)

status = Status.OK

except socket.timeout:
self.logger.debug(
"Server with hostname {} with port {} timeout error.".format(
hostname, port
)
)

status = Status.NOT_OK

except socket.error as e:
self.logger.debug(
"Server with hostname {} with port {} error raised {}.".format(
hostname, port, str(e)
)
)

status = Status.ERROR

finally:
sock.close()

return {"name": self.name, "status": status}
81 changes: 81 additions & 0 deletions src/heahmund/udp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# MIT License
#
# Copyright (c) 2023 Clivern
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import socket

from heahmund.logger import Logger
from heahmund.status import Status


class UDP:
"""UDP Check"""

def __init__(self, name, hostname="example.com", port=443, timeout=30):
self.name = name
self.hostname = hostname
self.port = port
self.timeout = timeout
self.logger = Logger().get_logger(__name__)

def run(self):
"""
Run The Check
Returns:
The Check Result
"""
status = Status.OK
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(self.timeout)

try:
sock.sendto(b"", (self.hostname, self.port))
data, addr = sock.recvfrom(1024)

self.logger.debug(
"Server with hostname {} with port {}.".format(self.hostname, self.port)
)

status = Status.OK

except socket.timeout:
self.logger.debug(
"Server with hostname {} with port {} timeout error.".format(
self.hostname, self.port
)
)

status = Status.NOT_OK

except socket.error as e:
self.logger.debug(
"Server with hostname {} with port {} error raised {}.".format(
self.hostname, self.port, str(e)
)
)

status = Status.ERROR

finally:
sock.close()

return {"name": self.name, "status": status}

0 comments on commit c803763

Please sign in to comment.