class implementation_check_kind_file_state(interface_check_kind): ''' [implementation] ''' def parameters_schema(self): return { "type": "object", "additionalProperties": False, "properties": { "path": { "type": "string" }, "exist_mode": { "description": "whether the file is supposed to exist or not", "type": "boolean", "default": True, }, "exist_critical": { "description": "whether a violation of the extist state (parameter 'exist_mode') shall be considered as critical (true) or concerning (false)", "type": "boolean", "default": True, }, "age_threshold_concerning": { "description": "in seconds; ignored if 'exist_mode' is set to false", "type": ["null", "integer"], "exclusiveMinimum": 0, "default": None, }, "age_threshold_critical": { "description": "in seconds; ignored if 'exist_mode' is set to false", "type": ["null", "integer"], "exclusiveMinimum": 0, "default": None, }, "size_threshold_concerning": { "description": "in bytes; ignored if 'exist_mode' is set to false", "type": ["null", "integer"], "exclusiveMinimum": 0, "default": None, }, "size_threshold_critical": { "description": "in bytes; ignored if 'exist_mode' is set to false", "type": ["null", "integer"], "exclusiveMinimum": 0, "default": None, }, # deprecated "strict": { "deprecated": True, "description": "", "type": "boolean", "default": True, }, "exist": { "deprecated": True, "description": "", "type": "boolean", "default": True, }, "age_threshold": { "deprecated": True, "description": "", "type": ["null", "integer"], "exclusiveMinimum": 0, "default": None, }, "size_threshold": { "deprecated": True, "description": "", "type": ["null", "integer"], "exclusiveMinimum": 0, "default": None, }, }, "required": [ "path", ] } ''' [implementation] ''' def normalize_order_node(self, node): version = ( "v1" if (not ("exist_mode" in node)) else "v2" ) if (version == "v1"): if ("path" not in node): raise ValueError("missing mandatory field 'path'") else: node_ = dict_merge( { "critical": True, "exist": True, "age_threshold": None, "size_threshold": None, }, node ) return { "exist_mode": node_["exist"], "exist_critical": node_["strict"], "age_threshold_concerning": ( None if node_["strict"] else node_["age_threshold"] ), "age_threshold_critical": ( node_["age_threshold"] if node_["strict"] else None ), "size_threshold_concerning": ( None if node_["strict"] else node_["age_threshold"] ), "size_threshold_critical": ( node_["age_threshold"] if node_["strict"] else None ), } elif (version == "v2"): if ("path" not in node): raise ValueError("missing mandatory field 'path'") else: node_ = dict_merge( { "exist_mode": True, "exist_critical": True, "age_threshold_concerning": None, "age_threshold_critical": None, "size_threshold_concerning": None, "size_threshold_critical": None, }, node ) return node_ else: raise ValueError("unhandled") ''' [implementation] ''' def run(self, parameters): condition = enum_condition.ok faults = [] data = {} exists = _os.path.exists(parameters["path"]) if (not parameters["exist_mode"]): if (exists): condition = ( enum_condition.critical if parameters["exist_critical"] else enum_condition.concerning ) faults.append(translation_get("checks.file_state.exists")) else: pass else: if (not exists): condition = ( enum_condition.critical if parameters["exist_critical"] else enum_condition.concerning ) faults.append(translation_get("checks.file_state.missing")) else: stat = _os.stat(parameters["path"]) ## age if True: timestamp_this = get_current_timestamp() timestamp_that = int(stat.st_atime) age = (timestamp_this - timestamp_that) if (age < 0): condition = enum_condition.critical faults.append(translation_get("checks.file_state.timestamp_implausible")) else: if ( (parameters["age_threshold_critical"] is not None) and (age > parameters["age_threshold_critical"]) ): condition = enum_condition.critical faults.append(translation_get("checks.file_state.too_old")) else: if ( (parameters["age_threshold_concerning"] is not None) and (age > parameters["age_threshold_concerning"]) ): condition = enum_condition.concerning faults.append(translation_get("checks.file_state.too_old")) else: pass data = dict_merge( data, { "timestamp_of_checking_instance": timestamp_this, "timestamp_of_file": timestamp_that, "age_value_in_seconds": age, "age_threshold_in_seconds_concerning": parameters["age_threshold_concerning"], "age_threshold_in_seconds_concerning": parameters["age_threshold_critical"], } ) ## size if True: size = stat.st_size if (size < 0): condition = enum_condition.critical faults.append(translation_get("checks.file_state.size_implausible")) else: if ( (parameters["size_threshold_critical"] is not None) and (size > parameters["size_threshold_critical"]) ): condition = enum_condition.critical faults.append(translation_get("checks.file_state.too_big")) else: if ( (parameters["size_threshold_concerning"] is not None) and (size > parameters["size_threshold_concerning"]) ): condition = enum_condition.concerning faults.append(translation_get("checks.file_state.too_big")) else: pass data = dict_merge( data, { "size_value_in_bytes": size, "size_threshold_in_bytes": parameters["size_threshold"], } ) return { "condition": condition, "info": { "path": parameters["path"], "faults": faults, "data": data, } }