Skip to content

Commit

Permalink
Merge pull request #14 from fealone/supports_tcp
Browse files Browse the repository at this point in the history
Supports for tcp endpoint.
  • Loading branch information
fealone authored Aug 13, 2021
2 parents 3219b4e + ab9a6f5 commit 7071d26
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/monitapi/monitoring/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ async def monitor_stun(target: MonitoringTarget) -> MonitoringResult:
response="")


async def monitor_tcp(target: MonitoringTarget) -> MonitoringResult:
err = Exception()
for i in range(target.retry):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host, port = target.url.split("tcp:")[1].split(":")
sock.settimeout(target.timeout)
sock.connect((host, int(port)))
sock.close()
break
except Exception as e:
logger.warning(("Monitor failed. "
f"Target: {target.url}"))
err = e
time.sleep(target.retry_wait)
else:
return MonitoringResult(
expected_status_code=0,
status_code=0,
state=False,
url=target.url,
response=str(err))
return MonitoringResult(
expected_status_code=0,
status_code=0,
state=True,
url=target.url,
response="")


async def watch(f: IO = None) -> None:
if f is None:
f = open("targets.yaml")
Expand All @@ -140,6 +170,8 @@ async def watch(f: IO = None) -> None:
task = asyncio.ensure_future(monitor_http(target))
elif target.url.startswith("stun:"):
task = asyncio.ensure_future(monitor_stun(target))
elif target.url.startswith("tcp:"):
task = asyncio.ensure_future(monitor_tcp(target))
else:
logger.warning(f"Unsupported target type. target: {target.url}")
monitors.append(task)
Expand Down
2 changes: 1 addition & 1 deletion src/monitapi/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "0.5.0"
version = "0.6.0"
2 changes: 1 addition & 1 deletion website/docs/definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ monitor_targets:
- HEAD, GET, POST, PUT, DELETE, OPTIONS, PATCH
* monitoring-url
- URL to monitor
- Supports for [http://, https://, stun:]
- Supports for [http://, https://, stun:, tcp:]
* header-name, header-value
- HTTP headers
* expected-status-code
Expand Down

0 comments on commit 7071d26

Please sign in to comment.