From f3c1003c957efee83f7eb44d5e06a40a04407ee3 Mon Sep 17 00:00:00 2001 From: Phil Owen <19691521+PhillipsOwen@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:42:50 -0400 Subject: [PATCH] adding slack, tidying up log messages --- src/archiver/archiver.py | 17 +++++++++++++---- src/common/general_utils.py | 10 +++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/archiver/archiver.py b/src/archiver/archiver.py index 3503709..9c9eb79 100644 --- a/src/archiver/archiver.py +++ b/src/archiver/archiver.py @@ -81,7 +81,13 @@ def run(self) -> bool: rule_def_version = rule_defs['rule_definition_version'] self.logger.info('<---------- New Run: %s ---------->', infile) - self.logger.info('APSViz Archiver start. Name: %s, Version: %s', rule_def_name, rule_def_version) + + # create a start message + start_msg = f'APSViz Archiver start. Name: {rule_def_name}, Version: {rule_def_version}' + + # send/log the start message + self.utils.send_slack_msg(start_msg, 'slack_status_channel') + self.logger.info(start_msg) # process the rule set for rule_set in rule_defs['rule_sets']: @@ -90,17 +96,20 @@ def run(self) -> bool: # no failures get a short message if run_stats['failed'] == 0: - final_msg = 'status: Success.' + final_msg = f"Status: {len(rule_set['rules'])} rule(s) succeeded." # set the success flag ret_val = True else: # show all results on failure - final_msg = f"Failures detected: {len(rule_set['rules'])} rule(s) in set, {run_stats['moved']} Move rule(s), " \ + final_msg = f"Status: Failures detected - {len(rule_set['rules'])} rule(s) in set, {run_stats['moved']} Move rule(s), " \ f"{run_stats['copied']} copy rule(s), {run_stats['removed']} remove rule(s), " \ f"{run_stats['swept']} sweep rule(s), {run_stats['failed']} failed rule(s)." - self.logger.info("Rule set complete. Run %s", final_msg) + self.logger.info("APSVix-Archiver Rule set %s complete. Run %s", rule_set['rule_set_name'], final_msg) + + # send out a slack of the details + self.utils.send_slack_msg(final_msg, 'slack_status_channel') self.logger.info('<---------- Run complete: %s ---------->\n', infile) except Exception: diff --git a/src/common/general_utils.py b/src/common/general_utils.py index 270a0af..1e14790 100644 --- a/src/common/general_utils.py +++ b/src/common/general_utils.py @@ -49,9 +49,9 @@ def send_slack_msg(self, msg, channel, debug_mode=False): """ sends a msg to the Slack channel - :param msg: the msg tpo be sent + :param msg: the msg to be sent :param channel: the Slack channel to post the message to - :param debug_mode: mode to indicate that this is a + :param debug_mode: mode to indicate that this is a no-op :return: nothing """ # init the final msg @@ -63,17 +63,17 @@ def send_slack_msg(self, msg, channel, debug_mode=False): # send the message to Slack if not in debug mode and not running locally if not debug_mode and self.system in ['Dev', 'Prod', 'AWS/EKS']: # determine the client based on the channel - if channel == self.slack_channels['slack_status_channel']: + if channel == 'slack_status_channel': client = WebClient(token=os.getenv('SLACK_STATUS_TOKEN')) else: client = WebClient(token=os.getenv('SLACK_ISSUES_TOKEN')) try: # send the message - client.chat_postMessage(channel=channel, text=final_msg) + client.chat_postMessage(channel=self.slack_channels[channel], text=final_msg) except SlackApiError: # log the error - self.logger.exception('Slack %s messaging failed. msg: %s', channel, final_msg) + self.logger.exception('Slack %s messaging failed. msg: %s', self.slack_channels[channel], final_msg) @staticmethod def load_rule_definition_file(infile: str) -> dict: