def __post_message(self, text, threads, settings): new_threads = [] rooms = (settings.get("notification", {}).get("google", {}).get("rooms", [GOOGLE_DEFAULT_CHAT_ROOM])) for index, room in enumerate(rooms): try: thread = self.__get_thread(threads, index) data = {"text": text} if thread: data["thread"] = thread response = post(room, data=json.dumps(data), headers=HEADERS).json() self.__log_response(response) new_threads.append(response["thread"]) except Exception as error: logger.error( "error on post message: %s", str(error), extra={"traceback": traceback.format_exc()}, ) new_threads.append(None) return new_threads
def execute_validation(validation): try: result = VALIDATIONS[validation]["ref"]() result.last_execution = datetime.now() return make_json_response(ValidationResponseEncoder().encode(result)) except Exception: logger.error( "error on execute validation %s", validation, extra={"traceback": traceback.format_exc()}, ) return json.dumps({"error": traceback.format_exc()})
def start_scheduler(): configure_validations() while True: time.sleep(1) try: schedule.run_pending() except Exception as exception: logger.error( "error on execute scheduler %s", str(exception), extra={"traceback": traceback.format_exc()}, )
def wrapped(*args, **kwargs): try: response = function(*args, **kwargs) if isinstance(response, Response): return treat_response(response) return response except Exception as error: logger.error( "error on render dashboard index: %s", str(error), extra={"traceback": traceback.format_exc()}, )
def recover_settings(): """ Get settings.py from root of project """ logger.info("lifeguard directory %s", LIFEGUARD_DIRECTORY) sys.path.append(LIFEGUARD_DIRECTORY) logger.info("path: %s", sys.path) lifeguard_settings = "lifeguard_settings" logger.info("loading %s", lifeguard_settings) if importlib.util.find_spec(lifeguard_settings) is None: logger.error("lifeguard_settings.py not found!!!") sys.exit(-1) return __import__(lifeguard_settings)
def send_email(validation_response, settings): """ Send email action """ if validation_response.status in settings["email"].get( "remove_from_sent_list_when", [NORMAL] ) and (validation_response.validation_name in EMAIL_NOTIFICATIONS): EMAIL_NOTIFICATIONS.pop(validation_response.validation_name) if ( validation_response.status in settings["email"].get("send_in", [PROBLEM]) and validation_response.validation_name not in EMAIL_NOTIFICATIONS ): EMAIL_NOTIFICATIONS[validation_response.validation_name] = { "sent_at": datetime.now() } message = Message( settings["email"]["receivers"], settings["email"]["subject"], str(validation_response.details), LIFEGUARD_CONTEXT.alert_email_template, ) emails = [receiver["email"] for receiver in settings["email"]["receivers"]] logger.info("sending email %s to %s", message.render(), emails) try: session = smtplib.SMTP( LIFEGUARD_EMAIL_SMTP_SERVER, port=LIFEGUARD_EMAIL_SMTP_PORT ) session.starttls() session.login(LIFEGUARD_EMAIL_SMTP_USER, LIFEGUARD_EMAIL_SMTP_PASSWD) session.sendmail(LIFEGUARD_EMAIL_SMTP_USER, emails, message.render()) session.quit() except: logger.error( "error on send email %s to %s", message.render(), emails, extra={"traceback": traceback.format_exc()}, )
def build_global_status(complete=False): """ Build global status method """ try: response = {"status": NORMAL, "validations": []} repository = ValidationRepository() for validation in repository.fetch_all_validation_results(): response["status"] = change_status(response["status"], validation.status) if complete or validation.status != NORMAL: response["validations"].append(validation) return make_json_response(ValidationResponseEncoder().encode(response)) except Exception: logger.error( "error on execute get status", extra={"traceback": traceback.format_exc()}, ) return make_json_response(json.dumps({"error": traceback.format_exc()}))