Import file age checker
[infodrom/icinga-plugins] / check_fileage
1 #!/usr/bin/python
2
3 import os, sys, datetime, stat, time
4
5 from stat import *
6
7 version = "16th April 2007"
8 #########################################################################################
9 #  Author:  Craig Rayner
10 #  28th February 2007
11 #########################################################################################
12
13
14 def getopts(argv):
15
16         global version
17                 
18         opts = {}
19         while argv:
20                 x = len(argv) - 1
21                 if argv[0] == '-f':
22                         opts[argv[0]] = argv[1]
23                         argv = argv[2:]
24                 elif argv[0] == '--file':
25                         opts['-f'] = argv[1]
26                         argv = argv[2:]
27                 elif argv[0] == '-w':
28                         opts[argv[0]] = argv[1]
29                         argv = argv[2:]
30                 elif argv[0] == '--warning':
31                         opts['-w'] = argv[1]
32                         argv = argv[2:]
33                 elif argv[0] == '-c':
34                         opts[argv[0]] = argv[1]
35                         argv = argv[2:]
36                 elif argv[0] == '--critical':
37                         opts['-c'] = argv[1]
38                         argv = argv[2:]
39                 elif argv[0] == '-d':
40                         opts[argv[0]] = argv[1]
41                         argv = argv[2:]
42                 elif argv[0] == '--datetype':
43                         opts['-d'] = argv[1]
44                         argv = argv[2:]
45                 elif argv[0] == '-h':
46                         PrintHelp()
47                         argv = argv[1:]
48                 elif argv[0] == '--help':
49                         PrintHelp()
50                         argv = argv[1:]
51                 elif argv[0] == '-V':
52                         print 'Version: ' + str(version)
53                         argv = argv[1:]
54                 elif argv[0] == '--version':
55                         print 'Version: ' + str(version)
56                         argv = argv[1:]
57                 else:
58                         argv = argv[1:]
59         return opts
60
61 def PrintHelp():
62
63         global version
64
65         print "Version: " + version
66         print """
67         Usage: check_fileage -f /mount/path/file.ext -w nnnn -c nnnn [-d A|C|M] [-V] [-h]
68
69         Options:
70          -h, --help
71             Print detailed help screen
72          -d, --datetype
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).
77                     The default is M.
78          -f, --file
79             The absolute path and file name.
80          -w, --warning
81             The age of the file in minutes to generate a warning notification.
82          -c, --critical
83             The age of the file in minutes to generate a critical notification.
84          -V, --version
85             State the Version
86
87         Examples:
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.
90         """
91
92
93 if __name__ == '__main__':
94         from sys import argv
95         myargs = getopts(argv)
96         if myargs.has_key('-V'):
97                 print 'Version: ' + str(version)
98         datetype = 'M'
99         if myargs.has_key('-d'):
100                 datetype = myargs['-d']
101                 if datetype not in 'ACM':
102                     datetype = 'M'
103         if myargs.has_key('-w'):
104                 warning = int(myargs['-w']) * 60
105         else:
106                 print 'The warning time is not set.  See check_fileage.py --help'
107                 PrintHelp()
108                 sys.exit(3)
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'
113                         sys.exit(3)
114         else:
115                 print 'The critical time is not set.  See check_fileage.py --help'
116                 PrintHelp()
117                 sys.exit(3)
118         if myargs.has_key('-f'):
119                 try:
120                         filestat = os.stat(myargs['-f'])
121                         if datetype == 'A':
122                                 filedate = filestat.st_atime
123                                 descriptive = 'last access'
124                         elif datetype == 'C':
125                                 filedate = filestat.st_ctime
126                                 descriptive = 'created (NOT *NIX)'
127                         else:
128                                 filedate = filestat.st_mtime
129                                 descriptive = 'modified'
130                         today = time.mktime(time.localtime())
131                         exitstate = 0
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:
140                                 exitstate = 1
141                                 exitmessage = 'Warning: ' + exitmessage[4:]
142                         if today > filedate + critical:
143                                 exitstate = 2
144                                 exitmessage = 'Critical: ' + exitmessage[9:]
145                         print exitmessage
146                         sys.exit(exitstate)
147                 except OSError:
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
154                         sys.exit(3)
155         else:
156                 print 'The file name was not set.  See check_fileage.py --help'
157                 PrintHelp()
158                 sys.exit(3)