' -- Nagios constants Const intOK = 0 Const intWarning = 1 Const intCritical = 2 Const intUnknown = 3 ' -- Default values intMetric = "days" intDate = "modification" If Wscript.Arguments.Named.Exists("h") Then Wscript.Echo "Usage: check_file_age.vbs /file:""C:\Program Files\Tivoli\Console\Report.htm"" /date:modification /metric:days /warning:1 /critical:2" Wscript.Echo "" Wscript.Echo "/file: - Full path to the file that shall be checked" Wscript.Echo " Note: if the full path contains spaces, enclose the full path" Wscript.Echo " within quote marks!" Wscript.Echo "" Wscript.Echo "/date: - File attribute to check" Wscript.Echo " Possible values are: creation, modification, access" Wscript.Echo " Defaults to modification." Wscript.Echo "" Wscript.Echo "/metric: - Interval to be used to calculate the age of the file." Wscript.Echo " Possible values: s (seconds), n (minutes), h (hours)," Wscript.Echo " d (days), m (months), q (quarters)" Wscript.Echo " Defaults to days." Wscript.Echo "" Wscript.Echo "/w: - Amount of intervals before WARNING." Wscript.Echo "/c: - Amount of intervals before CRITICAL." Wscript.Quit(intOK) End If If Wscript.Arguments.Named.Exists("file") and Wscript.Arguments.Named("file") <> "" Then intFile = Wscript.Arguments.Named("file") Else Wscript.Echo "WARNING: No file specified!" Wscript.Quit(intUnknown) End If If Wscript.Arguments.Named.Exists("date") and Wscript.Arguments.Named("date") <> "" Then intDate = Cstr(Wscript.Arguments.Named("date")) End If If Wscript.Arguments.Named.Exists("metric") and Wscript.Arguments.Named("metric") <> "" Then intMetric = Wscript.Arguments.Named("metric") End If If Wscript.Arguments.Named.Exists("w") and Wscript.Arguments.Named("w") <> "" Then intWarn = CInt(Wscript.Arguments.Named("w")) End If If Wscript.Arguments.Named.Exists("c") and Wscript.Arguments.Named("c") <> "" Then intCrit = CInt(Wscript.Arguments.Named("c")) End If Set objFSO = CreateObject("Scripting.FileSystemObject") if Not objFSO.FileExists(intFile) Then Wscript.Echo "CRITICAL: " & intFile & " does not exist!" Wscript.Quit(intCrit) End If Set objFile = objFSO.GetFile(intFile) objFileCreated = objFile.DateCreated objFileModified = objFile.DateLastModified objFileAccessed = objFile.DateLastAccessed ' Switch between objFileDates based on the /date: type passed to the script. Select Case intDate Case "creation" objDate = objFileCreated Case "modification" objDate = objFileModified Case "access" objDate = objFileAccessed End Select ' Switch between the metric based on /metric passed on to the script. Select Case intMetric Case "seconds", "s" objMetric = "s" strMetric = "seconds" Case "minutes", "n" objMetric = "n" strMetric = "minutes" Case "hours", "h" objMetric = "h" strMetric = "hours" Case "days", "d" objMetric = "d" strMetric = "days" Case "months", "m" objMetric = "m" strMetric = "months" Case "quarters", "q" objMetric = "q" strMetric = "quarters" End Select ' Return the difference between current date and the requested ' file time, using /intMetric as value objFileOffset = DateDiff(objMetric, objDate, Now()) ' -- Need a way to convert the intWarn/intCrit/objFileOffset value into seconds ' based on the value of intMetric Function convert_result(Metric, Value) Select Case Metric Case "q" convert_result = Value * 10368000 Case "m" convert_result = Value * 2592000 Case "d" convert_result = Value * 86400 Case "h" convert_result = Value * 3600 Case "n" convert_result = Value * 60 End Select End Function ' Compare the difference to the supplied intWarn and intCrit thresholds If ( objFileOffset > intWarn ) and ( objFileOffset < intCrit ) Then ' Wscript.Echo "WARNING: " & intFile & " is older than " & intWarn & " " & strMetric Wscript.Echo "WARNING: " & intFile & " is older than " & intWarn & " " & strMetric & "| 'check_file_age'=" & convert_result(Cstr(objMetric), objFileOffset) & "s;" & convert_result(Cstr(objMetric), intWarn) & ";" & convert_result(Cstr(objMetric), intCrit) & ";" Wscript.Quit(intWarning) ElseIf ( objFileOffset > intCrit ) Then ' Wscript.Echo "CRITICAL: " & intFile & " is older than " & intCrit & " " & strMetric Wscript.Echo "CRITICAL: " & intFile & " is older than " & intCrit & " " & strMetric & "| 'check_file_age'=" & convert_result(Cstr(objMetric), objFileOffset) & "s;" & convert_result(Cstr(objMetric), intWarn) & ";" & convert_result(Cstr(objMetric), intCrit) & ";" Wscript.Quit(intCritical) ElseIf objFileOffset <= intWarning Then ' Wscript.Echo "OK: " & intFile & " is not older than " & intWarn & " " & strMetric Wscript.Echo "OK: " & intFile & " is not older than " & intWarn & " " & strMetric & "| 'check_file_age'=" & objFileOffset_Seconds & "s;" & intWarn & ";" & intCrit & ";" Wscript.Quit(intOK) End If ' vim: set filetype=vb :