core/source/logic/checks/file_timestamp.py

115 lines
2.8 KiB
Python

class implementation_check_kind_file_timestamp(interface_check_kind):
'''
[implementation]
'''
def parameters_schema(self):
return {
"type": "object",
"additionalProperties": False,
"properties": {
"path": {
"type": "string"
},
"warning_age": {
"type": "integer",
"exclusiveMinimum": 0,
"default": (60 * 60)
},
"critical_age": {
"type": "integer",
"exclusiveMinimum": 0,
"default": (60 * 60 * 24)
},
"condition_on_missing": {
"description": "which condition to report if file is missing",
"type": "string",
"enum": [
"unknown",
"ok",
"warning",
"critical"
],
"default": "warning"
},
"condition_on_implausible": {
"description": "which condition to report if the age is negative, i.e. the file is apparently from the future",
"type": "string",
"enum": [
"unknown",
"ok",
"warning",
"critical"
],
"default": "warning"
}
},
"required": [
"path"
]
}
'''
[implementation]
'''
def normalize_conf_node(self, node):
if ("path" not in node):
raise ValueError("missing mandatory field 'path'")
else:
return dict_merge(
{
"warning_age": (60 * 60),
"critical_age": (60 * 60 * 24),
"condition_on_missing": condition_encode(enum_condition.warning),
"condition_on_implausible": condition_encode(enum_condition.warning),
},
node
)
'''
[implementation]
'''
def run(self, parameters):
if (not _os.path.exists(parameters["path"])):
return {
"condition": condition_decode(parameters["condition_on_missing"]),
"info": {
"path": parameters["path"],
"flaw": translation_get("checks.file_timetsamp.missing"),
}
}
else:
result = _os.stat(parameters["path"])
timestamp = get_current_timestamp()
age = (timestamp - result.st_atime)
if (age < 0):
return {
"condition": condition_decode(parameters["condition_on_implausible"]),
"info": {
"path": parameters["path"],
"flaw": translation_get("checks.file_timetsamp.implausible"),
"timestamp_of_checking_instance": timestamp,
"timestamp_of_file": result.st_atime,
},
}
else:
if ((age > 0) and (age <= parameters["warning_age"])):
condition = enum_condition.ok
elif ((age > parameters["warning_age"]) and (age <= parameters["critical_age"])):
condition = enum_condition.warning
elif (age > parameters["critical_age"]):
condition = enum_condition.critical
else:
raise ValueError("impossible")
return {
"condition": condition,
"info": {
"path": parameters["path"],
"flaw": translation_get("checks.file_timetsamp.too_old"),
"age_in_seconds": ("%u" % age),
},
}