core/source/check_kinds/file_timestamp.py

66 lines
1.8 KiB
Python

class implementation_check_kind_file_timestamp(interface_check_kind):
'''
[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"]),
"output": "file is 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"]),
"output": string_coin(
"file is apparently from the future; timestamp of checking instance: {{timestamp_this}}; timestamp of file: {{timestamp_that}} (age in seconds: {{age}})",
{
"timestamp_this": timestamp,
"timestamp_that": result.st_atime,
"age": ("%u" % age),
}
),
}
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 state")
return {
"condition": condition,
"output": string_coin(
"age in seconds: {{age}}",
{
"age": ("%u" % age),
}
),
}