2022-11-30 10:26:27 +01:00
class implementation_check_kind_file_timestamp ( interface_check_kind ) :
2022-11-30 23:03:24 +01:00
'''
[ 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 "
]
}
2022-11-30 10:26:27 +01:00
'''
[ 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 ) ,
}
) ,
}