Skip to content

Commit

Permalink
πŸ“ Pics + readme, πŸ› http check_host
Browse files Browse the repository at this point in the history
  • Loading branch information
justadoll committed Aug 10, 2022
1 parent 0d88a69 commit c4a02e5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Pulse bot

Telegram bot to monitor up/down hosts via check-host.net API

![UP](https://github.com/justadoll/Pulse-bot/blob/main/pics/up.jpg?raw=true)

![Down](https://github.com/justadoll/Pulse-bot/blob/main/pics/down.jpg?raw=true)

- [x] Checkhost API
- [x] FP (False-positive) by ping if checkhost broken
- [ ] Add hosts oportunity for every user (with limits)
- [ ] Identify who add bot to group/channel
Binary file added pics/down.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/up.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ charset-normalizer==2.0.12
environs==9.5.0
frozenlist==1.3.0
greenlet==1.1.2
icmplib==3.0.3
idna==3.3
loguru==0.6.0
marshmallow==3.15.0
Expand Down
48 changes: 36 additions & 12 deletions tgbot/services/check_host.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from aiohttp import ClientSession
from icmplib import async_ping
from icmplib.exceptions import NameLookupError
from asyncio import sleep
from loguru import logger
from json import loads
Expand Down Expand Up @@ -30,16 +32,17 @@ async def count_checker(url:str, is_ping:bool) -> dict:
# logger.info(f"[+] PING: {node=} {x[0]}")
count[node] = count[node] + 1
else:
if i[2] == "OK":
# logger.info(f"[+] HTTP: {node=} {i[2]}")
# logger.info(f"[+] HTTP: {node=} {i[0]}")
# if not ping
if i[0] == 1:
count[node] = count[node] + 1
#else:
# logger.warning(f"[-] {node} is down")
# else:
# logger.warning(f"[-] {node} is down")
return count

def final_decision(hostname:str, results:list, active_counter:int, method:str) -> str:
async def final_decision(hostname:str, results:list, active_counter:int, method:str, perm_link:str) -> str:
actual_counter = 0
final_str = f"<b>{method.upper()}</b>\n\n"
final_str = f"<a href=\"{perm_link}\">{method.upper()}</a>\n\n"
# logger.debug(f"{results=}")
# logger.debug(f"{active_counter=}")
for host in results:
Expand All @@ -51,13 +54,21 @@ def final_decision(hostname:str, results:list, active_counter:int, method:str) -
actual_counter += results[host]
logger.info(f"{active_counter=} {actual_counter=}")
if active_counter == actual_counter:
logger.success("UP!")
logger.success(f"{hostname} is UP!")
final_str += "Status: <b>UP</b>!\n\n"
is_up = True
elif actual_counter == 0:
logger.error("DOWN!")
final_str += "Status: <b>DOWN</b>!\n\n"
is_up = False
own_ping_ok = await own_ping_check(hostname=hostname, count=5)
logger.error(f"{hostname} is DOWN!")
if own_ping_ok:
logger.info(f"{hostname} bot-ping decided it's ALIVE")
final_str += "πŸ€–Bot-ping says it's <b>ALIVE</b>\n"
is_up = True
else:
logger.warning(f"{hostname} bot-ping decided it's DEAD")
final_str += "πŸ€–Bot-ping says it's <b>DEAD</b>\n"
final_str += "Status: <b>DOWN</b>!\n\n"
else:
logger.warning("Not all UP or DOWN!")
final_str += "Status: <b>Not all UP or DOWN</b>!\n\n"
Expand All @@ -70,15 +81,28 @@ async def checker(hostname:str, method:str, nodes:int):
logger.info(f"Perm link: {perm_link}")
if perm_link:
logger.debug("Sleeping...")
await sleep(5)
await sleep(10)
if method == "ping":
active_counts = nodes * 4
is_ping=True
else:
active_counts = nodes
is_ping=False
count_results = await count_checker(url=perm_link, is_ping=is_ping)
return final_decision(hostname=hostname, results=count_results, active_counter=active_counts, method=method)
results = await final_decision(hostname=hostname, results=count_results, active_counter=active_counts, method=method, perm_link=perm_link)
return results

else:
logger.error(f"ERROR")
logger.error(f"ERROR")

async def own_ping_check(hostname:str, count:int) -> bool:
""" Sends ICMP packets to host from itself """
try:
res = await async_ping(hostname, count=count, interval=1, timeout=5, privileged=False)
except NameLookupError:
logger.warning(f"{hostname} dns lookup error")
else:
if res.packets_sent == res.packets_received:
return True
else:
return False

0 comments on commit c4a02e5

Please # to comment.