'Script to check how old the file is compare to today (checks by LastModified Date)
'Example of my use: get Last Time Antivirus MCAfee Enterprise (Version 8.0i) was Updated
'Author: Felipe Ferreira fel.h2o(at)gmail.com
'Version: 1.0 01/04/2009

'Command example: check_av.vbs -f  "C:\Program Files\Common Files\Network Associates\Engine\scan.dat" -w 10 -c 7
'check_av.vbs -f <File path and Location> -w <warning for days old> -c <Critical for days old>

'Todo: 

'----------NAGIOS VARs
Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Const intError = 3
Dim intWarn : intWarn = 10  '(default, but will get from Argument)
Dim intCrit : intCrit = 7  '(default, but will get from Argument)

'----------ARGUMENT VALUES VARIABLES
Dim argcountcommand
Dim arg(10)
'Should be Dynamic From the ARGs
Dim DateDif,Date1,Date2

'----------GENERAL VARIABLES
Dim verbose : verbose = 0       'Output Verbose = 1, = 0 disable 
Dim strFile			'PATH AND FILE TO CHECK
Dim outputmsgstats		'Msg to be printed, NOTE: edit according to your language
dim strScriptFile : strScriptFile = WScript.ScriptFullname
'----------MAIN CALLS
call CheckArg()
dif = Cint(getFileDate())

'DEBUG
pt "Warning = " & intWarn
pt "Critical = " & intCrit
pt "Diferencia = " & dif

' CheckAge compare with arguments
If (dif > intWarn) and (dif < intCrit) Then
	outputmsgstats = "WARNING "		
	intExit = intWarning
Elseif (dif > intCrit)  Then
	outputmsgstats = "CRITICAL "			
	intExit = intCritical
Elseif dif <= intWarn Then
	outputmsgstats = "OK "
	intExit = intOK
end if	
outputmsgstats = outputmsgstats & "Hace " & dif & " dias sin actualizar."

'OUTPUT TO STD OUT AND QUIT WITH ERROR CODE
wscript.echo outputmsgstats 
wscript.quit(intExit)


'-----------------FUNCTIONS 

Function getFileDate() 
'Check the size of file requested and returns it to intFileSize global var
on error resume next
    Dim oFSO           'FileSystemObject
    Dim oFolder        'Handle to the folder
    Dim oSubFolders    'Handle to subfolders collection
    Dim oFileCollection 'All files of the folder
'Connect to folder object and files
    Set oFSO = CreateObject("Scripting.FileSystemObject")
'Checks if Folder exists
	If oFSO.FileExists(strFile) = False Then
		wscript.echo "Error File " & strFile & " was not founded!"        
		wscript.quit(intError)
	end if	
	pt "Checking file = " & strFile
	Set oFile = oFSO.GetFile(LCase(strFile))
	Date1 = Now()
	Date2 = oFile.DateLastModified 
	pt "Today: " &  Date1
	pt "File: " & Date2

	DateDif =  DateDiff("d",Date2,Date1)
	getFileDate = DateDif

end Function

Function CheckArg()
on error resume next
	GetArgs()
	if ((UCase(wscript.arguments(0))="-H") Or (UCase(wscript.arguments(0))="--HELP")) and (argcountcommand=1) then
		help()
		wscript.quit
	elseif(3 < argcountcommand <= 6) then
		strFile = GetOneArg("-f")
		 If instr(path,"/") then
		  strFile = replace(path,"/","\") 'WIN X LINUX Path conversion
		 end if
		intWarn = Cint(GetOneArg("-w"))
		intCrit = CInt(GetOneArg("-c"))
		verbose = GetOneArg("-v")
	end if
end function

Function GetArgs()
'Get ALL arguments passed to the script
	On Error Resume Next		
	Dim i		
	argcountcommand=WScript.Arguments.Count		
	for i=0 to argcountcommand-1
		arg(i)=WScript.Arguments(i)
                'pt i & " - " & arg(i)
	next		
End Function

Function GetOneArg(strName)
	On Error Resume Next
	Dim i
	for i=0 to argcountcommand-1
		if (Ucase(arg(i))=Ucase(strName)) then
			GetOneArg=arg(i+1)
			Exit Function
		end if
	next		
End Function

Function Help()
'Prints out help 	
		Dim str
  		str="Check and compare file LastModified day to today, and reports in Nagios Format."&vbCrlF&vbCrlF
  		
  		str=str&vbCrlF
		str=str&"-f Path+Filename            Complete path and filename." &vbCrlF    		
		str=str&"-w Warning                  Days old to set as Warning." &vbCrlF  
		str=str&"-c Critical                 Days old to set as Critical." &vbCrlF  
		str=str&"-v 1 			     Set verbose mode. " &vbCrlF  	
		str=str&"--help			     This help !."&vbCrlF	
  		str=str&vbCrlF
		str=str&"EXample: cscript "& strScriptFile &" -f c:\boot.ini -w 7 -c 10" &vbCrlF
  		str=str&"By Felipe Ferreira, February 2009, version 1.0 (For Nagios)." & vbCrlF
  		str=str&"www.felipeferreira.net"&vbCrlF

  		wscript.echo str		
End Function

Function pt(strMsg)
	if verbose = 1 then
	 wscript.echo strMsg
	end if
end function
