def check_root_keys(config): printin(1, "Checking root level keys") unrecognised_key = check_keys(config, accepted_root_keys) if unrecognised_key != None: printin(2, "Unrecognised key", '"' + unrecognised_key + '"') shutdown_error() printin(2, "Okay")
def self_check_hard_fail(self): print_w_time("CPU monitor self check with hard fail") temp = self.get_temperature() if temp == None: printin(1, "Failed to get temperature") shutdown_error() else: printin(1, "Okay") printin(2, "Temperature :", temp)
def check_config(config): print_w_time("Checking configuration file") try: check_root_keys(config) check_ecsbx_stores(config) check_cpu_health(config) check_disk_health(config) check_delay_before_sched_sec(config) except KeyError as e: printin(1, "Key", str(e), "misisng") shutdown_error() except Exception as e: printin(1, str(e)) shutdown_error()
def check_delay_before_sched_sec(config): try: printin(1, "Checking delay_before_sched_sec") if "delay_before_sched_sec" in config: delay_before_sched_sec = config["delay_before_sched_sec"] if not isinstance(delay_before_sched_sec, int): raise Exception("delay_before_sched_sec should be an integer") if delay_before_sched_sec < 0: raise Exception( "delay_before_sched_sec should be greater than or equal to 0" ) printin(2, "Okay") else: printin(2, "Value not specified") except Exception as e: printin(2, str(e)) shutdown_error()
def self_check_hard_fail(self): print_w_time("Disk controller self check with hard fail, disk :", self.disk) if self.smart_enabled: temp = self.get_temperature() reallocated_sector_count = self.get_reallocated_sector_count() if temp == None: printin(1, "Failed to get temperature") shutdown_error() elif reallocated_sector_count == None: printin(1, "Failed to get reallocated sector count") shutdown_error() else: printin(1, "Okay") printin(2, "Temperature :", temp) printin(2, "Reallocated sector count :", reallocated_sector_count) else: printin(1, "SMART monitoring not enabled, check skipped")
def load_file(self, file_path): print_w_time("Loading configuration file") try: with open(file_path) as f: config = yaml.safe_load(f.read()) check_config(config) self.__config = config if "ecsbx_stores" in config: self.__ecsbx_stores = map( lambda d: ECSBXStoreConfig(name=d["name"], partition=d["partition"], mount_dir=d["mount_dir"], smart_enabled=d[ "smart_enabled"]), config["ecsbx_stores"]) if "cpu_health" in config: cpu_health = config["cpu_health"] self.__cpu_health = CPUHealthConfig( warn_temperature=cpu_health["warn_temperature"], shutdown_temperature=cpu_health["shutdown_temperature"] ) if "disk_health" in config: disk_health = config["disk_health"] self.__disk_health = DiskHealthConfig( warn_temperature=disk_health["warn_temperature"], shutdown_temperature=disk_health[ "shutdown_temperature"]) if "delay_before_sched_sec" in config: self.__delay_before_sched_sec = config[ "delay_before_sched_sec"] except IsADirectoryError: printin( 1, "Configuration file " + '"' + file_path + '"' + " is a directory") shutdown_error() except FileNotFoundError: printin( 1, "Configuration file " + '"' + file_path + '"' + " does not exist") shutdown_error() except yaml.YAMLError as e: printin( 1, "Failed to parse configuration file " + '"' + file_path + '"') printin(1, "Error :") print(indent_str(2, str(e))) shutdown_error()
def check_disk_health(config): try: printin(1, "Checking disk health section") if "disk_health" in config: disk_health = config["disk_health"] if not isinstance(disk_health, dict): raise Exception( "Value following key disk_health should be key value pairs" ) unrecognised_key = check_keys( disk_health, ["warn_temperature", "shutdown_temperature"]) if unrecognised_key != None: printin(2, "Unrecognised key", '"' + unrecognised_key + '"') shutdown_error() warn_temperature = disk_health["warn_temperature"] if not isinstance(warn_temperature, int): raise Exception("warn_temperature should be an integer") if warn_temperature < 0: raise Exception("warn_temperature should be positive") shutdown_temperature = disk_health["shutdown_temperature"] if not isinstance(shutdown_temperature, int): raise Exception("shutdown_temperature should be an integer") if shutdown_temperature < 0: raise Exception( "shutdown_temperature should be greater than or equal to 0" ) printin(2, "Okay") else: printin(2, "Section not specified") except KeyError as e: printin(2, "Key", str(e), "misisng") shutdown_error() except Exception as e: printin(2, str(e)) shutdown_error()
def main(): parser = argparse.ArgumentParser(prog=sys_info["acronym"]) parser.add_argument("--config", metavar="CONFIG", default="daams.config", help="configuration file to use") parser.add_argument("--acronym", action="version", help="show acronym", version=sys_info["acronym"]) parser.add_argument("--full-name", action="version", help="show full name", version=sys_info["full_name"]) parser.add_argument("--version", action="version", help="show acrynym and version number", version=sys_info["acronym"] + " " + sys_info["daams_version"]) parser.add_argument("--version-long", action="version", help="show full name and version number", version=sys_info["full_name"] + " " + sys_info["daams_version"]) parser.add_argument("--check-only", action="store_true", help="complete all initial checks then exit") args = parser.parse_args() system_diagnostics.print_start_up_message() system_diagnostics.check_system_rights() system_diagnostics.check_dependencies() system_diagnostics.print_system_info() config = Config() config.load_file(args.config) warning_board = WarningBoard() cpu_monitor = CPUMonitor(config.cpu_health(), warning_board) cpu_monitor.self_check_hard_fail() ecsbx_stores = [ ECSBXStore(x, config.disk_health(), warning_board) for x in config.ecsbx_stores() ] for ecsbx_store in ecsbx_stores: ecsbx_store.self_check_hard_fail() if args.check_only: print("All initial checks completed") shutdown_normal() try: delay_before_sched_sec = config.delay_before_sched_sec() print_w_time("Waiting for", str(delay_before_sched_sec), "seconds before scheduling tasks") time.sleep(delay_before_sched_sec) print_w_time("Scheduling tasks") scheduler = sched.scheduler(time.time, time.sleep) init_tasks(ecsbx_stores) schedule_tasks(scheduler, cpu_monitor, ecsbx_stores, warning_board) scheduler.run() except KeyboardInterrupt: print() for ecsbx_store in ecsbx_stores: ecsbx_store.unmount() shutdown_normal() except OSShutdownRequest: for ecsbx_store in ecsbx_stores: ecsbx_store.unmount() shutdown_error()
def check_ecsbx_stores(config): try: printin(1, "Checking ECSBX stores section") if "ecsbx_stores" in config: ecsbx_stores = config["ecsbx_stores"] if not isinstance(ecsbx_stores, list): raise Exception( "Value following key ecsbx_stores should be a list") names = [] partitions = [] mount_dirs = [] for store in ecsbx_stores: if not isinstance(store, dict): raise Exception( "ECSBX store specification should be key value pairs") unrecognised_key = check_keys( store, ["name", "partition", "mount_dir", "smart_enabled"]) if unrecognised_key != None: printin(2, "Unrecognised key", '"' + unrecognised_key + '"') shutdown_error() name = store["name"] partition = store["partition"] mount_dir = store["mount_dir"] smart_enabled = store["smart_enabled"] names.append(name) partitions.append(partition) mount_dirs.append(mount_dir) try: mode = os.stat(partition).st_mode if not stat.S_ISBLK(mode): raise Exception('"' + partition + '"' + " is not a block device") except FileNotFoundError: raise Exception("Partition " + partition + " not found") if os.path.isfile(mount_dir): raise Exception(mount_dir + " is not a directory") elif os.path.isdir(mount_dir): pass else: raise Exception(mount_dir + " does not exist") if not isinstance(smart_enabled, bool): raise Exception("smart_enabled should be a boolean") duplicate_names = misc_utils.collect_duplicates(names) if duplicate_names != []: raise Exception("Following names are used more than once : " + ", ".join(duplicate_names)) duplicate_partitions = misc_utils.collect_duplicates(partitions) if duplicate_partitions != []: raise Exception( "Following partitions are used more than once : " + ", ".join(duplicate_partitions)) duplicate_mount_dirs = misc_utils.collect_duplicates(mount_dirs) if duplicate_mount_dirs != []: raise Exception( "Following mount directories are used more than once : " + ", ".join(duplicate_mount_dirs)) printin(2, "Okay") else: printin(2, "Section not specified") except KeyError as e: printin(2, "Key", str(e), "misisng") shutdown_error() except Exception as e: printin(2, str(e)) shutdown_error()