Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 80ca4c0

Browse files
committedOct 1, 2023
update
1 parent 0072f01 commit 80ca4c0

File tree

6 files changed

+115
-5
lines changed

6 files changed

+115
-5
lines changed
 

‎src/heahmund/http.py

+73-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,62 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
import requests
24+
from http import HTTPStatus
25+
26+
from heahmund.status import Status
2327
from heahmund.logger import Logger
2428

2529

26-
class HTTP:
27-
"""HTTP Check"""
30+
class GET:
31+
"""GET Check"""
32+
33+
def __init__(self, name, url, headers={}, params={}):
34+
self.name = name
35+
self.url = url
36+
self.headers = headers
37+
self.params = params
38+
self.logger = Logger().get_logger(__name__)
39+
40+
def run(self):
41+
"""
42+
Run The Check
43+
44+
Returns:
45+
The Check Result
46+
"""
47+
48+
try:
49+
response = requests.get(self.url, headers=self.headers, params=self.params)
50+
51+
if response.status_code == HTTPStatus.OK:
52+
return {"name": self.name, "status": Status.OK}
53+
else:
54+
return {"name": self.name, "status": Status.NOT_OK}
55+
56+
except requests.exceptions.Timeout as e:
57+
self.logger.debug(
58+
"Get request to url {} throw timeout error {}.".format(self.url, str(e))
59+
)
60+
61+
return {"name": self.name, "status": Status.ERROR}
62+
63+
except Exception as e:
64+
self.logger.debug(
65+
"Get request to url {} throw error {}.".format(self.url, str(e))
66+
)
67+
68+
return {"name": self.name, "status": Status.ERROR}
69+
70+
71+
class HEAD:
72+
"""HEAD Check"""
2873

29-
def __init__(self, name):
74+
def __init__(self, name, url, headers={}, params={}):
3075
self.name = name
76+
self.url = url
77+
self.headers = headers
78+
self.params = params
3179
self.logger = Logger().get_logger(__name__)
3280

3381
def run(self):
@@ -37,4 +85,25 @@ def run(self):
3785
Returns:
3886
The Check Result
3987
"""
40-
return {"name": self.name, "status": "OK"}
88+
89+
try:
90+
response = requests.head(self.url, headers=self.headers, params=self.params)
91+
92+
if response.status_code == HTTPStatus.OK:
93+
return {"name": self.name, "status": Status.OK}
94+
else:
95+
return {"name": self.name, "status": Status.NOT_OK}
96+
97+
except requests.exceptions.Timeout as e:
98+
self.logger.debug(
99+
"Get request to url {} throw timeout error {}.".format(self.url, str(e))
100+
)
101+
102+
return {"name": self.name, "status": Status.ERROR}
103+
104+
except Exception as e:
105+
self.logger.debug(
106+
"Get request to url {} throw error {}.".format(self.url, str(e))
107+
)
108+
109+
return {"name": self.name, "status": Status.ERROR}

‎src/heahmund/ping.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def run(self):
4141
Returns:
4242
The Check Result
4343
"""
44+
4445
response = subprocess.run(
4546
["ping", "-c", "3", self.hostname], capture_output=True
4647
)

‎src/heahmund/ssl.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
import ssl
24+
import datetime
25+
import socket
26+
2327
from heahmund.logger import Logger
28+
from heahmund.status import Status
2429

2530

2631
class SSL:
@@ -41,4 +46,36 @@ def run(self):
4146
Returns:
4247
The Check Result
4348
"""
44-
return {"name": self.name, "status": "OK"}
49+
50+
context = ssl.create_default_context()
51+
52+
try:
53+
# Connects to the server using the SSL context
54+
with socket.create_connection(
55+
(self.hostname, self.port), timeout=self.timeout
56+
) as sock:
57+
sock.settimeout(self.timeout)
58+
59+
with context.wrap_socket(sock, server_hostname=self.hostname) as ssock:
60+
cert = ssock.getpeercert()
61+
62+
# Get the expiration date of the certificate
63+
expiry_date = datetime.datetime.strptime(
64+
cert["notAfter"], "%b %d %H:%M:%S %Y %Z"
65+
)
66+
67+
# Get the current date
68+
current_date = datetime.datetime.now()
69+
70+
# Calculate the number of days until the certificate expires
71+
days_until_expiry = (expiry_date - current_date).days
72+
73+
# Check if the certificate is valid for the specified number of days
74+
if days_until_expiry > days:
75+
return {"name": self.name, "status": Status.OK}
76+
else:
77+
return {"name": self.name, "status": Status.NOT_OK}
78+
79+
except socket.timeout:
80+
# Handle the timeout
81+
return {"name": self.name, "status": Status.TIMEOUT}

‎src/heahmund/tcp.py

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def run(self):
4343
Returns:
4444
The Check Result
4545
"""
46+
4647
status = Status.OK
4748
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4849
sock.settimeout(self.timeout)

‎src/heahmund/udp.py

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def run(self):
4343
Returns:
4444
The Check Result
4545
"""
46+
4647
status = Status.OK
4748
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
4849
sock.settimeout(self.timeout)

‎src/heahmund/utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ def url_to_hostname(url="https://example.com"):
3333
Returns:
3434
The hostname
3535
"""
36+
3637
return urllib.parse.urlparse(url).netloc

0 commit comments

Comments
 (0)
Please sign in to comment.