'Script to get Last Time Exchange DB was backuped and report answer to Nagios NRPE_NT plugin
'Author: Felipe Ferreira fel.h2o(at)gmail.com
'Version: 1.0 09/02/2009

'Todo: 
'Should report only SGs, no need of Public Folders
'Will check How old is the backup, allow you to set Warn/Critc Levels.
'Ex. If exchange SGs backup is older then 5 days report critical, if older then 2 days report WARNING

'----------NAGIOS VARs
Const intOK = 0
Const intWarning = 1
Const intCritical = 2
Const intError = 3
'----------ARGUMENT VALUES
Dim argcountcommand
Dim arg(10)
'Should be Dynamic From the ARGs
Dim intWarn : intWarn = 3  '(default, but will get from Argument)
Dim intCrit : intCrit = 5  '(default, but will get from Argument)


call CheckArg()
call Main()

'###############################FUNCTIONS############################
function Main()
'Looks the AD attribute about LastBackup Date
	set conn = createobject("ADODB.Connection")
	set mdbobj = createobject("CDOEXM.MailboxStoreDB")
	set pdbobj = createobject("CDOEXM.PublicStoreDB")
	set com = createobject("ADODB.Command")
	Set iAdRootDSE = GetObject("LDAP://RootDSE")
	strNameingContext = iAdRootDSE.Get("configurationNamingContext")
	Conn.Provider = "ADsDSOObject"
	Conn.Open "ADs Provider"
	mbQuery = "<LDAP://" & strNameingContext & ">;(objectCategory=msExchPrivateMDB);name,distinguishedName;subtree"
	'pfQuery = "<LDAP://" & strNameingContext & ">;(objectCategory=msExchPublicMDB);name,distinguishedName;subtree"
	Com.ActiveConnection = Conn
	Com.CommandText = mbQuery
	Set Rs = Com.Execute

	While Not Rs.EOF
			mdbobj.datasource.open "LDAP://" & Rs.Fields("distinguishedName")
			'Wscript.echo Rs.Fields("name") & " Last Backed Up : " & mdbobj.LastFullBackupTime
	'CALL function to compare date
			if (mdbobj.LastFullBackupTime) <> "" then
				checkdate(mdbobj.LastFullBackupTime)			
			end if
			Rs.MoveNext

	Wend
	'BUG, it will never get here
	Rs.Close
	Conn.Close
	set mdbobj = Nothing
	set pdbobj = Nothing
	Set Rs = Nothing
	Set Rs1 = Nothing
	Set Com = Nothing
	Set Conn = Nothing
end function 

function checkdate(dateB)
'Check if backup was done more then X days ago
	'wscript.echo "BACKUP = "& dateB & " X TODAY = " & now() 
	'wscript.echo "Critical = "& intCrit & ", WARNING = " & intWarn
	datex = DateAdd("d",daysold,Now()) 

	'Check how many day difference from Today and LastBackup
	Difference = DateDiff("d",dateB,now()) 

	if Difference >= intCrit then 'Call Critical
		wscript.echo "CRITICAl, Hace " & Difference & " dias sin backup!"
		wscript.quit(intCritical)
	elseif (Difference >= intWarn) and (Diffrence <= intCrit) then
		wscript.echo "WARNING, Hace " & Difference &" dias sin backup!"
		wscript.quit(intWARNING)
	else
		wscript.echo "OK, Ultimo Backup " & dateB & " ,hace " & Difference &" dias." 
		wscript.quit(intOK)
	end if	
end function

function CheckArg()
GetArgs()
If argcountcommand = 4 then
	intWarn = CInt(GetOneArg("-w"))
	intCrit = CInt(GetOneArg("-c"))
else 
	help()
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)
	'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 Help()
'Prints out help 	
		Dim str
  		str="Check Last Exchange Backup Date, report if Last backup was done in X days."&vbCrlF&vbCrlF
  		str=str&"cscript "& strScriptFile &" -w <days> -c <days>" &vbCrlF
  		str=str&vbCrlF  		
		str=str&"-w Warning                  Warning Size in KB, ex. 2000 ."&vbCrlF  
		str=str&"-c Critical                 Critical Size in KN, ex. 3000 ."&vbCrlF
		str=str&"--help			     This help !."&vbCrlF	
		str=str&"                            Examples -w 2 -c 5 ."&vbCrlF  
  		str=str&vbCrlF
  		str=str&"By Felipe Ferreira February 2009, version 1.0 (For Nagios)." & vbCrlF
  		wscript.echo str		
End Function