Giter VIP home page Giter VIP logo

healthcheck-bot's Introduction

Healthcheck-bot

CI Badge codecov

logo

A simple Python script to verify if a service is up. Whenever the service falls, a message will be sent to a user/group/channel with Telegram

How to use?

Make a copy of the file config/settings.yaml.dist in the same directory and rename it to settings.yaml:

  • Set bot_token, your bot token
  • Set chat_ids, the ID(s) the bot will use to communicate any downtime. It's possible to set multiple IDs, semicolon separated without any space
  • Set the urls you'd like to check in urls.json

Telegram bots

This is bot is now able to contact other bots and check if their running correctly! In order to setup this module, obtain an api_id and api_hash following the Telegram guide, now add them your settings.yaml. The last step is to add the bot(s) to check in the same settings file, completing the value for each key:

  • Set username, the bot to be checked username without the prepending '@'
  • Set command, the command that will be sent to your bot
  • Set expected_response, the expected response from your bot or a substring. Any special character should be replaced with their equivalent character (a new line should be replaced with \n) Multiple bots could be added by added just by repeating the same structure of a sequence of objects in yaml.

Example

bot_token: "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" # Your bot token
chat_ids: ["10000000", "10000001", "10000002"]  # Single ID or Multiple IDs
api_id: 123456
api_hash: "123456:ABC-DEF1234ghIkl-zyx57W2v"
bots_to_check:
  - username: "WebpageBot"
    command: "/start"
    expected_response: "Hello, I'm the Webpage Bot!"

Run it every 5 minutes using crontab

  • Open crontab using:
$ crontab -e
  • Add the following line
*/5 * * * * cd /path/to/Healthcheck-bot/ && python3 src/main.py > checks.log 2> errors.log    

Write Unit Tests

In order to write Unit Tests, you have to put it inside tests. There are many possibilities,

Unit Tests without mocking

To add a new unit test without mocking, there are some examples at the beginning of tests, but let's inspect how they're implemented:

  • func: simply the function to mock
  • expected_res: the expected returned value of the function to test
  • arg: a tuple that contains the argument(s) of the function to test (the one previously defined in func). In case there are no passing arguments, just add an empty tuple object (tuple())
  • is_async: this is not a compulsory key, should be added and set to True only if the function is asynchronous

Unit Tests with mocking

If is necessary one or more mock(s), it's possible to append three more keys to the unit test. In case an async function needs to be mocked, its result can be wrapped in an AsyncMock and its return value set as a constructor element in the constructor. The test should have the same keys with three more keys:

  • mock_obj: it's a list of the objects in which there are the functions to mock. In our examples they often refer to the main object (the one imported from src.main)
  • mock_func: an array of strings, it indicates the functions to mock
  • mock_ret: a list of the returned values

healthcheck-bot's People

Contributors

domenicoblanco avatar helias avatar picred avatar salvo-polizzi avatar valeri0p avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

healthcheck-bot's Issues

pyrogram healthcheck bot

we should implement a pyrogram bot that speaks with our current bot and make a daily healthcheck

Check service availability

The bot should be able to request informations to a server.
In order to complete the assignment we should provide at least two alternatives:

  • Get request for a web server
  • Ping (a simple os execution of the ping command with the parsing of the last row)

Communicate with Telegram

The bot should be able to send POST request to Telegram API.

In order to achieve this issue, we should:

  • Set bot token as env variable
  • Being able to send a POST request with httpx
  • Being able to send a POST request with httpx and HTTP2

run every minute ?

if we run this every minute and a service is down, we will receive every minute via telegram that the service is down and it will be a bit annoying

we should find a way to make the healthcheck every minute but if a service has been already "alerted" we should not send any alert for at least 1 hour (or 24 hours?)

note: current workaround -> we'll run the healthcheck bot every 30 minutes to receive only 48 messages per day in the worst case ๐Ÿ˜…

replace try except generic with specific exception

if I close opis-api service the python script will crash saying "SSL error" without the try-except here 6391763 , ideally we should replace that generic except with a specific exception handling only the ssl error

how to reproduce the error:

  • close opis api service going to /srv/docker/opis-api/ -> docker-compose stop
  • write except e as Exception: print(str(e))
  • set the exact exception name and replace the generic one

Multiple tries

The bot should at least try 5 times to send a message

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.