def __init__(self): self._logger = get_logger() kafka_config = get_kafka_config() self._kafka_producer = KafkaProducer( bootstrap_servers=kafka_config["service_uri"], security_protocol="SSL", ssl_cafile=kafka_config["ssl_cafile"], ssl_certfile=kafka_config["ssl_certfile"], ssl_keyfile=kafka_config["ssl_keyfile"], )
def __init__(self): self._logger = get_logger() kafka_config = get_kafka_config() self._kafka_consumer = KafkaConsumer( "pinga-events", bootstrap_servers=kafka_config["service_uri"], client_id=f"pinga-consumer-{uuid4()}", group_id="pinga-consumer-group", auto_offset_reset='earliest', security_protocol="SSL", ssl_cafile=kafka_config["ssl_cafile"], ssl_certfile=kafka_config["ssl_certfile"], ssl_keyfile=kafka_config["ssl_keyfile"], ) self._db_conn = get_db_conn() create_events_table(self._db_conn)
def check_site(site_url: str) -> dict: """ Checks if a given site is up or now by sending and HTTP request to the given URL :param site_url: the event to be sent. It must UTF-8 encodable. :returns: the result of the HTTP request as dict """ logger = get_logger() try: response = requests.get(site_url) status = "up" if response.ok else "down" result = { "url": site_url, "status": status, "httpStatus": response.status_code, "responseTimeSeconds": response.elapsed.total_seconds() } logger.info(result) except requests.RequestException as exc: result = {"url": site_url, "status": "error", "errorMessage": str(exc)} logger.error(result) return result
from pinga.checker import check_site from pinga.config import get_sites_list from pinga.events.producer import Producer from pinga.log import get_logger def _check_site_thread(producer, site): while True: result = check_site(site) producer.emit(json.dumps(result)) gevent.sleep(5) if __name__ == "__main__": logger = get_logger() group = Group() producer = Producer() sites_list = get_sites_list()["sites"] logger.info(f"List of sites to be checked by Pinga {sites_list}") for site in sites_list: group.add(gevent.spawn(_check_site_thread, producer, site)) try: logger.info("Starting Pinga producer...") group.join() except KeyboardInterrupt: logger.info("Pinga producer interrupted. Goodbye.")