3 import os, sys, datetime, stat, time
7 version = "16th April 2007"
8 #########################################################################################
11 #########################################################################################
22 opts[argv[0]] = argv[1]
24 elif argv[0] == '--file':
28 opts[argv[0]] = argv[1]
30 elif argv[0] == '--warning':
34 opts[argv[0]] = argv[1]
36 elif argv[0] == '--critical':
40 opts[argv[0]] = argv[1]
42 elif argv[0] == '--datetype':
48 elif argv[0] == '--help':
52 print 'Version: ' + str(version)
54 elif argv[0] == '--version':
55 print 'Version: ' + str(version)
65 print "Version: " + version
67 Usage: check_fileage -f /mount/path/file.ext -w nnnn -c nnnn [-d A|C|M] [-V] [-h]
71 Print detailed help screen
73 Date Type: Return the date as defined:
74 A: (time of most recent access),
75 M: (time of most recent content modification),
76 C: (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows).
79 The absolute path and file name.
81 The age of the file in minutes to generate a warning notification.
83 The age of the file in minutes to generate a critical notification.
88 check_fileage -f /path/to/file -w 1440 -C 2880
89 Returns OK if the file is less than the warning time in age.
93 if __name__ == '__main__':
95 myargs = getopts(argv)
96 if myargs.has_key('-V'):
97 print 'Version: ' + str(version)
99 if myargs.has_key('-d'):
100 datetype = myargs['-d']
101 if datetype not in 'ACM':
103 if myargs.has_key('-w'):
104 warning = int(myargs['-w']) * 60
106 print 'The warning time is not set. See check_fileage.py --help'
109 if myargs.has_key('-c'):
110 critical = int(myargs['-c']) * 60
111 if critical <= warning:
112 print 'The critical time must be older than the warning time. See check_fileage.py --help'
115 print 'The critical time is not set. See check_fileage.py --help'
118 if myargs.has_key('-f'):
120 filestat = os.stat(myargs['-f'])
122 filedate = filestat.st_atime
123 descriptive = 'last access'
124 elif datetype == 'C':
125 filedate = filestat.st_ctime
126 descriptive = 'created (NOT *NIX)'
128 filedate = filestat.st_mtime
129 descriptive = 'modified'
130 today = time.mktime(time.localtime())
132 fileage = time.strftime('%a %d %b/%Y %H:%M', time.localtime(filedate))
133 filename = myargs['-f']
134 if filename[1] == ":":
135 filename = filename[2:]
136 filename = filename.replace("\\", "/")
137 filename = os.path.basename(filename)
138 exitmessage = 'OK: ' + filename + ' has a ' + descriptive + ' date of ' + fileage + '\n'
139 if today > filedate + warning:
141 exitmessage = 'Warning: ' + exitmessage[4:]
142 if today > filedate + critical:
144 exitmessage = 'Critical: ' + exitmessage[9:]
148 filename = myargs['-f']
149 if filename[1] == ":":
150 filename = filename[2:]
151 filename = filename.replace("\\", "/")
152 filename = os.path.basename(filename)
153 print 'Unknown: System Error - Unable to access the file ' + filename
156 print 'The file name was not set. See check_fileage.py --help'