'Script to Monitor Exchange DB file Sizes
'Usage: check_filesize -p "path" -f "file(s)" -w <number> -c <critical>
'By Felipe Ferreira 10/2008 www.felipeferreira.net Version 2.0 for NAGIOS


'TODO:
'1. dynamic find the mdbdata folder in drive E:\ then in drive D:\ - OK

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Const intError = 3
Const intUnknown = 3
Dim intFileSize : intFileSize = 0
dim intWarningN,intCriticalN
Dim sFile
Dim outputmsg,outputmsgstats
Dim intExit
dim strScriptFile : strScriptFile = WScript.ScriptFullname
Dim argcountcommand
Dim arg(25)
Dim path   		'get from arg -p
dim foldermdbdata : foldermdbdata = "notfound"
'Dim arrFiles(10)	'should be populated by arg -f files

'@@@@@@@@@@@HANDLES THE ARGUMENTS@@@@@@@@@@@@@@@
GetArgs()
if ((UCase(wscript.arguments(0))="-H") Or (UCase(wscript.arguments(0))="--HELP")) and (argcountcommand=1) then
	help()
elseif(1 < argcountcommand < 6) then
	path = GetOneArg("-p")
	If instr(path,"/") then
	  path = replace(path,"/","\")
	end if
	strFiles = GetOneArg("-f")
	  arrFiles = split(strFiles,",")
	intWarningN = toBytes(GetOneArg("-w"))
	intCriticalN = toBytes(GetOneArg("-c"))
end if


'DEBUGGING:
'wscript.echo "Warning = " & intWarningN & " - " & varType(intWarningN)
'wscript.echo "Critical = " & intCriticalN & " - " & varType(intCriticalN)

'@@@@@@@@@@@HANDLES THE WARN AND CRITI OUTPUT@@@@@@@@@@@@@@@
for each sFile in arrFiles
	if instr(Ucase(foldermdbdata),"MDBDATA") then 
		CheckFolder foldermdbdata, sFile
	else
		CheckFolder path, sFile
	end if	
	if intFileSize <> 0 then
		outputmsg = outputmsg & " - "  & sFile & " = " & toMegaBytes(intFileSize) & "(MB)"	
'Shuold be Warning or Critical if even just on file is high
	if intExit <> 2 and intExit <> 1 then
'DEBUGGING:
'wscript.echo "TESTCRIT: " & toMegaBytes(intFileSize) & " > "&  toMegaBytes(intCriticalN) & " = " & (intFileSize > intCriticalN)
'wscript.echo "TESTWARN: " & toMegaBytes(intFileSize) & " > "&  toMegaBytes(intWarningN) & " = " & (intFileSize > intWarningN)
		If (intFileSize > intWarningN) and (intFileSize < intCriticalN) Then
			outputmsgstats = "WARNING "		
			intExit = intWarning
		Elseif (intFileSize > intCriticalN)  Then
			outputmsgstats = "CRITICAL "			
			intExit = intCritical
		Elseif intFileSize <= intWarningN Then
			outputmsgstats = "OK "
			intExit = intOK
		end if  
	end if  'warn or critc
	end if ' FileSize condition
	intFileSize = 0
next

wscript.echo outputmsgstats & outputmsg
wscript.quit(intExit)

Function CheckFolder(objFolder,strFile) 
'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.FolderExists(objFolder) = False Then
	'wscript.echo "1Error Folder " & objFolder & " was not founded!"        
		if instr(UCase(objfolder), "MDBDATA") then
		'search for mdbdata folder			
			'wscript.echo "Searching in Drive E: "
 			FindFolder "e:\",strFile 'if not found try
			if foldermdbdata = "notfound" then
			'wscript.echo "Searching in Drive D: "
 			 FindFolder "d:\",strFile
			end if
		else
			wscript.echo "Error Folder " & objFolder & " was not founded!"        
			wscript.quit(intError)	
		end if
		
		
	else
		Set oFolder = oFSO.GetFolder(objFolder)
  		Set oFileCollection = oFolder.Files        'gets all files of current folder
	       'Walk through each file in this folder collection.
		For each oFile in oFileCollection 'Gets its size based on the name.
                If Ucase(oFile.name) = Ucase(strFile) Then
		    intFileSize = oFile.size
		end if	
		next
	End If	
end function

Function toGigaBytes(bytes) 
                toGigabytes = int(((bytes / 1024) /1024) / 1024) 
End Function 
Function toMegaBytes(bytes) 
                toMegabytes = int ((bytes / 1024) /1024) 
		toMegabytes = FormatNumber(toMegaBytes,2)
End Function
Function toBytes(Megabytes) 
                toBytes = int ((Megabytes * 1024) *1024) 
End Function
Function Help()
'Prints out help 	
		Dim str
  		str="Check Size of File(s) in supplied Path."&vbCrlF&vbCrlF
  		str=str&"cscript "& strScriptFile &" -p Path -f file(s) -w warningValue(MB) -c criticalValue(MB)"&vbCrlF
  		str=str&vbCrlF
  		str=str&"-h [--help]                 Help."&vbCrlF
  		str=str&"-p path                     Path where files are."&vbCrlF  
  		str=str&vbCrlF
  		str=str&"By Felipe Ferreira October 2008, version 1.0." & vbCrlF
  		wscript.echo str		
End FunctionFunction 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)
'wscript.echo 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 GetMArg(strName)
'Get multiples arguments, must get by comma delimeterd way
'	On Error Resume Next
	Dim i
	Dim j : j = 0
	Dim k : k = 1
	Dim x : x = argcountcommand-7 '(gets number of args between -f and -w)
	for i=0 to argcountcommand-5

		if (Ucase(arg(i))=Ucase(strName)) then
'need to know total number of arguments passed to calculate, dynamic populate the array		
		While j < x 		
wscript.echo i & "==" & arg(i)
			if not arg(i) = "," then
				arrFiles(j)=arg(i+k)				
				j = j + 1
				k = k + 2
			end if
		wend		
end if
	next		
End Function


Function FindFolder(objFolderMdbdata,strFile2) 
'Searches for the MDBDATA folder, only once (after finding defines the foldermdbdata variable)
if instr(Ucase(foldermdbdata),"MDBDATA") then 
'	wscript.echo "exit function"
	exit function
end if
'Checks where is the mdbdata folder
    Dim oFSO        'FileSystemObject
    Dim oFolder        'Handle to the folder
    Dim oSubFolders    'Handle to subfolders collection
    Dim oSubFolder    'Handle to current subfolder in collection
'Connect to folder object and files
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFSO2 = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(objFolderMdbdata)
    Set oSubFolders   =  oFolder.Subfolders

'    wscript.echo "Searching in " & objFolderMdbdata

'Checks if Folder exists
 	If oFSO.FolderExists(objFolderMdbdata) = False Then
		wscript.echo "NOT FOUND: " & objFolderMdbdata
        Exit function
    End If	
	
   For Each oSubFolder In oSubFolders
	
	'wscript.echo oSubFolder.Name
	FindFolder oSubFolder.Path,strFile2
           If instr(UCase(oSubFolder.Path), "MDBDATA") then 
		foldermdbdata = oSubFolder.Path	
		'wscript.echo "FOUND: " & oSubFolder.Path
		CheckFolder oSubFolder.Path,strFile2 	
		
	   end if
   Next   
End Function


