Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Oct 1, 2023
1 parent 0072f01 commit 80ca4c0
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
77 changes: 73 additions & 4 deletions src/heahmund/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,62 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import requests
from http import HTTPStatus

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


class HTTP:
"""HTTP Check"""
class GET:
"""GET Check"""

def __init__(self, name, url, headers={}, params={}):
self.name = name
self.url = url
self.headers = headers
self.params = params
self.logger = Logger().get_logger(__name__)

def run(self):
"""
Run The Check
Returns:
The Check Result
"""

try:
response = requests.get(self.url, headers=self.headers, params=self.params)

if response.status_code == HTTPStatus.OK:
return {"name": self.name, "status": Status.OK}
else:
return {"name": self.name, "status": Status.NOT_OK}

except requests.exceptions.Timeout as e:
self.logger.debug(
"Get request to url {} throw timeout error {}.".format(self.url, str(e))
)

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

except Exception as e:
self.logger.debug(
"Get request to url {} throw error {}.".format(self.url, str(e))
)

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


class HEAD:
"""HEAD Check"""

def __init__(self, name):
def __init__(self, name, url, headers={}, params={}):
self.name = name
self.url = url
self.headers = headers
self.params = params
self.logger = Logger().get_logger(__name__)

def run(self):
Expand All @@ -37,4 +85,25 @@ def run(self):
Returns:
The Check Result
"""
return {"name": self.name, "status": "OK"}

try:
response = requests.head(self.url, headers=self.headers, params=self.params)

if response.status_code == HTTPStatus.OK:
return {"name": self.name, "status": Status.OK}
else:
return {"name": self.name, "status": Status.NOT_OK}

except requests.exceptions.Timeout as e:
self.logger.debug(
"Get request to url {} throw timeout error {}.".format(self.url, str(e))
)

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

except Exception as e:
self.logger.debug(
"Get request to url {} throw error {}.".format(self.url, str(e))
)

return {"name": self.name, "status": Status.ERROR}
1 change: 1 addition & 0 deletions src/heahmund/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def run(self):
Returns:
The Check Result
"""

response = subprocess.run(
["ping", "-c", "3", self.hostname], capture_output=True
)
Expand Down
39 changes: 38 additions & 1 deletion src/heahmund/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import ssl
import datetime
import socket

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


class SSL:
Expand All @@ -41,4 +46,36 @@ def run(self):
Returns:
The Check Result
"""
return {"name": self.name, "status": "OK"}

context = ssl.create_default_context()

try:
# Connects to the server using the SSL context
with socket.create_connection(
(self.hostname, self.port), timeout=self.timeout
) as sock:
sock.settimeout(self.timeout)

with context.wrap_socket(sock, server_hostname=self.hostname) as ssock:
cert = ssock.getpeercert()

# Get the expiration date of the certificate
expiry_date = datetime.datetime.strptime(
cert["notAfter"], "%b %d %H:%M:%S %Y %Z"
)

# Get the current date
current_date = datetime.datetime.now()

# Calculate the number of days until the certificate expires
days_until_expiry = (expiry_date - current_date).days

# Check if the certificate is valid for the specified number of days
if days_until_expiry > days:
return {"name": self.name, "status": Status.OK}
else:
return {"name": self.name, "status": Status.NOT_OK}

except socket.timeout:
# Handle the timeout
return {"name": self.name, "status": Status.TIMEOUT}
1 change: 1 addition & 0 deletions src/heahmund/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def run(self):
Returns:
The Check Result
"""

status = Status.OK
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(self.timeout)
Expand Down
1 change: 1 addition & 0 deletions src/heahmund/udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def run(self):
Returns:
The Check Result
"""

status = Status.OK
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(self.timeout)
Expand Down
1 change: 1 addition & 0 deletions src/heahmund/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ def url_to_hostname(url="https://example.com"):
Returns:
The hostname
"""

return urllib.parse.urlparse(url).netloc

0 comments on commit 80ca4c0

Please sign in to comment.