def setup(self, item):
        """ Set up instance vars and check item values.
        Passed in item dictionary keys:
            key             Full 'itemtype_tag' key value from config file line
            tag             'tag' portion only from 'itemtype_tag' from config file line
            user_host_port  'local' or 'user@hostname[:port]' from config file line
            host            'local' or 'hostname' from config file line
            critical        True if 'CRITICAL' is in the config file line
            rest_of_line    Remainder of line after the 'user_host' from the config file line
        Returns True if all good, else False
        """

        # Construct item type specifics and check validity
        self.key = item["key"]  # vvvv These items don't need to be modified
        self.key_padded = self.key.ljust(globvars.keylen)
        self.tag = item["tag"]
        self.user_host_port = item["user_host_port"]
        self.host = item["host"]
        self.host_padded = self.host.ljust(globvars.hostlen)
        if item["critical"]:
            self.failtype = RTN_CRITICAL
            self.failtext = "CRITICAL"
        else:
            self.failtype = RTN_FAIL
            self.failtext = "FAIL"  # ^^^^ These items don't need to be modified

        try:
            xx = item["rest_of_line"].split(maxsplit=1)
            self.maxage_sec, self.units = convert_time(
                xx[0])  # Exception on conversion error
            self.url = xx[1] + "/Info.htm"
        except Exception as e:
            logging.error(
                f"  ERROR:  <{self.key}> INVALID LINE SYNTAX <{item['rest_of_line']}>\n  {e}"
            )
            return RTN_FAIL

        return RTN_PASS
Exemple #2
0
    def renotif(self):
        """ Periodically send a consolidated notification with all current critical events
        if renotif time passed then
            if there are active criticals then
                send consolidated renotif message
            else
                set renotif time = now, which allows next critical to be notified immediately
        """
        logging.debug (f"Entering: {HANDLER_NAME}.renotif")
        if (self.next_renotif < datetime.datetime.now()):
            logging.debug (f"self.next_renotif:        {self.next_renotif}")
            logging.debug (f"datetime.datetime.now():  {datetime.datetime.now()}")

            if self.are_criticals():
                criticals = ""
                for event in self.events:
                    if self.events[event]["criticality"] == RTN_CRITICAL:
                        criticals += f"  {self.events[event]['message']}\n"
                snd_notif (subj=NOTIF_SUBJ, msg=criticals, log=True)
                self.next_renotif = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta(seconds=convert_time(getcfg("CriticalReNotificationInterval"))[0])
                logging.info(f"Next critical renotification:  {self.next_renotif}")
            else:
                self.next_renotif = datetime.datetime.now().replace(microsecond=0)
Exemple #3
0
    def log_event (self, dict):
        """ Handle any logging for each event status type
        Passed in dict keys:
            notif_key   - Corresponds to the monitortype_key in the config file
            rslt
                RTN_PASS     - Clears any prior logged WARNING / FAIL / CRITICAL events
                RTN_WARNING  - Logged and included in summary, but no notification.
                RTN_FAIL     - Logged & notified
                RTN_CRITICAL - Logged & notified, with periodic renotification
            message     - Message text from the monitor plugin
        """

        if dict["rslt"] == RTN_PASS:
            logging.info(dict["message"])
            if dict["notif_key"] in self.events:
                del self.events[dict["notif_key"]]
                logging.warning(f"  Event {dict['notif_key']} now passing.  Removed from events log.")
            return

        if dict["rslt"] == RTN_WARNING:
            if dict["notif_key"] not in self.events:
                logging.warning(dict["message"])

        else:       # RTN_FAIL and RTN_CRITICAL cases
            if dict["rslt"] == RTN_CRITICAL:
                # if there are no prior active criticals, then set renotif time to now + renotif value
                if self.next_renotif < datetime.datetime.now()  and  not self.are_criticals():
                    self.next_renotif = datetime.datetime.now().replace(microsecond=0) + datetime.timedelta(seconds=convert_time(getcfg("CriticalReNotificationInterval"))[0])
                    if not globvars.args.once:
                        logging.info(f"Next critical renotification:  {self.next_renotif}")
            if dict["notif_key"] not in self.events  and  not globvars.args.once:
                snd_notif (subj=NOTIF_SUBJ, msg=dict["message"], log=True)
            if dict["notif_key"] not in self.events  or  globvars.args.once  or  getcfg("LoggingLevel", 30) < 30:
                logging.warning(dict["message"])

        self.events[dict["notif_key"]] = {"message": dict["message"], "criticality": dict["rslt"]}