def __init__(self, name="unnamed", config_options=None): """What's that coming over the hill? Is a monitor?""" if config_options is None: config_options = {} self.name = name self.monitor_logger = logging.getLogger('simplemonitor.monitor-' + self.name) self.set_dependencies(Monitor.get_config_option( config_options, 'depend', required_type='[str]', default=list() )) self.set_urgency(Monitor.get_config_option( config_options, 'urgent', required_type='bool', default=True )) self.set_notify(Monitor.get_config_option( config_options, 'notify', required_type='bool', default=True )) self.set_group(Monitor.get_config_option( config_options, 'group', default='default' )) self.set_tolerance(Monitor.get_config_option( config_options, 'tolerance', required_type='int', default=0, minimum=0 )) self.set_remote_alerting(Monitor.get_config_option( config_options, 'remote_alert', required_type='bool', default=False )) self.set_recover_command(Monitor.get_config_option( config_options, 'recover_command' )) self.set_gap(Monitor.get_config_option( config_options, 'gap', required_type='int', minimum=0, default=0 )) self.running_on = short_hostname() self.was_skipped = False
def __init__(self, name="unnamed", config_options=None): """What's that coming over the hill? Is a monitor?""" if config_options is None: config_options = {} self.name = name self.monitor_logger = logging.getLogger('simplemonitor.monitor-' + self.name) self.set_dependencies( Monitor.get_config_option(config_options, 'depend', required_type='[str]', default=list())) self.set_urgency( Monitor.get_config_option(config_options, 'urgent', required_type='bool', default=True)) self.set_notify( Monitor.get_config_option(config_options, 'notify', required_type='bool', default=True)) self.set_group( Monitor.get_config_option(config_options, 'group', default='default')) self.set_tolerance( Monitor.get_config_option(config_options, 'tolerance', required_type='int', default=0, minimum=0)) self.set_remote_alerting( Monitor.get_config_option(config_options, 'remote_alert', required_type='bool', default=False)) self.set_recover_command( Monitor.get_config_option(config_options, 'recover_command')) self.set_gap( Monitor.get_config_option(config_options, 'gap', required_type='int', minimum=0, default=0)) self.running_on = short_hostname() self.was_skipped = False
def is_remote(self): """Check if we're running on this machine, or if we're a remote instance.""" if self.running_on == short_hostname(): return False return True
def process_batch(self): """Save the HTML file.""" ok_count = 0 fail_count = 0 old_count = 0 remote_count = 0 my_host = short_hostname() try: temp_file = tempfile.mkstemp() file_handle = os.fdopen(temp_file[0], "w") file_name = temp_file[1] except Exception: sys.stderr.write("Couldn't create temporary file for HTML output\n") return output_ok = StringIO() output_fail = StringIO() keys = list(self.batch_data.keys()) keys.sort() for entry in keys: if self.batch_data[entry]["age"] > 120: status = "OLD" old_count += 1 elif self.batch_data[entry]["status"]: status = "OK" ok_count += 1 else: status = "FAIL" fail_count += 1 if self.batch_data[entry]["host"] != my_host: remote_count += 1 try: monitor_name = entry.split("/")[1] except Exception: monitor_name = entry if status == "FAIL": output = output_fail else: output = output_ok output.write("<tr class=\"%srow\">" % status.lower()) output.write(""" <td class="monitor_name">%s</td> <td class="status %s">%s</td> <td>%s</td> <td>%s</td> """ % ( monitor_name, status.lower(), status, self.batch_data[entry]["host"], self.batch_data[entry]["fail_time"], ) ) if self.batch_data[entry]["fail_count"] == 0: output.write("<td class=\"vfc\"> </td>") else: output.write("<td class=\"vfc\">%s</td>" % self.batch_data[entry]["fail_count"]) try: output.write("<td>%d+%02d:%02d:%02d</td>" % (self.batch_data[entry]["downtime"][0], self.batch_data[entry]["downtime"][1], self.batch_data[entry]["downtime"][2], self.batch_data[entry]["downtime"][3])) except Exception: output.write("<td> </td>") output.write("<td>%s </td>" % (self.batch_data[entry]["fail_data"])) if self.batch_data[entry]["failures"] == 0: output.write("<td></td><td></td>") else: output.write("""<td>%s</td> <td>%s</td>""" % ( self.batch_data[entry]["failures"], format_datetime(self.batch_data[entry]["last_failure"]) ) ) if self.batch_data[entry]["host"] == my_host: output.write("<td></td>") else: output.write("<td>%d</td>" % self.batch_data[entry]["age"]) output.write("</tr>\n") count_data = "<div id=\"summary\"" if old_count > 0: cls = "old" elif fail_count > 0: cls = "fail" else: cls = "ok" count_data = count_data + " class=\"%s\">%s" % (cls, cls.upper()) self.count_data = count_data + "<div id=\"details\"><span class=\"ok\">%d OK</span> <span class=\"fail\">%d FAIL</span> <span class=\"old\">%d OLD</span> <span class=\"remote\">%d remote</span></div></div>" % (ok_count, fail_count, old_count, remote_count) self.status = cls.upper() with open(os.path.join(self.folder, self.header), "r") as file_input: file_handle.writelines(self.parse_file(file_input)) file_handle.write(output_fail.getvalue()) file_handle.write(output_ok.getvalue()) with open(os.path.join(self.folder, self.footer), "r") as file_input: file_handle.writelines(self.parse_file(file_input)) try: file_handle.flush() file_handle.close() os.chmod(file_name, stat.S_IREAD | stat.S_IWRITE | stat.S_IRGRP | stat.S_IROTH) shutil.move(file_name, os.path.join(self.folder, self.filename)) except Exception: self.logger_logger.exception("problem closing temporary file for HTML output")