Nagio/NRPE_NT VBS Script to check the size of file(s) inside a Folder.
IMPORTANT, From nagios you Must pass double slashes
Usage:
check_filesize -p "path" -f "file(s)" -w -c
Example1(script localy using cscript ):
check_filesize -p "E:mdbdata" -f priv1.edb,priv1.stm,pub1.edb,pub1.stm -w 5000 -c 10000
Sample Output:
STATUS OK - priv1.edb = 21.690,00(MB) - priv1.stm = 13.680,00(MB) - pub1.edb = 735,00(MB) - pub1.stm = 6,00(MB)
Exemple2 (from nagios server,after defining in nrpe.cfg):
#./check_nrpe -H server11 -c check_filesize -a C:windowssystem32 notepad.exe 1 2
It accepts multiple file checks, the Warning/Critical thresholds (-w,-c) should be passed in Megabytes.
The alarm will be raised if any files reach the threshold.
Also the path can be in UNIX format or Windows format / the script will accept both.

EASY HOWTO:

1-Place the script
Copy the script to “c:nrpe_ntpluginsv2” or whatever location your NRPE_NT .vbs are at.
2-Set nrpe_nt.cfg
Inside the NRPE_NT Client Server add the command line in the file nrpe_nt.cfg:
command[check_filesize]=cscript.exe //nologo //T:60 c:nrpe_ntpluginsv2check_filesize.vbs -p “$ARG1$” -f “$ARG2$” -w “$ARG3$” -c “$ARG4$”
3-Restart the nrpe_nt service
net stop nrpe_nt
net start nrpe_nt
4-Define the command inside nagios
$USER1$/check_nrpe -H $HOSTADDRESS$ -c check_filesize -a $ARG1$ $ARG2$ $ARG3$ $ARG4$

'Usage: check_filesize -p "path" -f "file(s)" -w  -c 
'By Felipe Ferreira 10/2008 www.felipeferreira.net Version 2.0 for NAGIOS
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
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,debug
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@@@@@@@@@@@@@@@
'@@@@@@@@@@@HANDLES THE ARGUMENTS@@@@@@@@@@@@@@@
debug =  1    ' SET 0 FOR SILENT MODE!
'@@@@@@@@@@@HANDLES THE ARGUMENTS@@@@@@@@@@@@@@@
GetArgs()
if argcountcommand = 0 then
	help()
elseif ((UCase(wscript.arguments(0))="-H") Or (UCase(wscript.arguments(0))="--HELP")) then
	help()
elseif(3 < 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:
pt "Warning = " & intWarningN & " - " & varType(intWarningN)
pt "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:
pt "TESTCRIT: " & toMegaBytes(intFileSize) & " > "&  toMegaBytes(intCriticalN) & " = " & (intFileSize > intCriticalN)
pt "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
if debug = 1 then
	pt " To have clean response turn the variable debug to 0 inside the script"
end if
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
	pt "1Error Folder " & objFolder & " was not founded!"
		if instr(UCase(objfolder), "MDBDATA") then
		'search for mdbdata folder
			pt "Searching in Drive E: "
 			FindFolder "e:\",strFile 'if not found try
			if foldermdbdata = "notfound" then
			pt "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&"cscript "& "cscript check_filesize.vbs -p c:\ -f vtapi.dll -w20 -c 30"&vbCrlF
  		str=str&vbCrlF
  		str=str&"-h [--help]                 Help."&vbCrlF
  		str=str&"-p path                     Path where files are."&vbCrlF
		str=str&"-f file1,file2,file3        File(s) to check size."&vbCrlF
		str=str&"-w Warnining                Filze size to warn."&vbCrlF
		str=str&"-c Critical                 Filze size to warn Critical."&vbCrlF
  		str=str&vbCrlF
  		str=str&"By Felipe Ferreira October 2008, version 1.0." & vbCrlF
  		wscript.echo str
		wscript.quit
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 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
	pt oSubFolder.Name
	FindFolder oSubFolder.Path,strFile2
           If instr(UCase(oSubFolder.Path), "MDBDATA") then
		foldermdbdata = oSubFolder.Path
		pt "FOUND: " & oSubFolder.Path
		CheckFolder oSubFolder.Path,strFile2
	   end if
   Next
End Function
function pt(txt)
if debug = 1 then
	wscript.echo txt
end if
end function

Tags: , , , , , , , , , ,

24 thoughts on “CheckFileSize VBS for Nagios

  1. There’s a typo in the download. On line 116, you had:
    End FunctionFunction GetArgs()
    s/b
    End Function
    Function GetArgs()

  2. THanks for noticing that.
    I have uploaded a new version of the script.
    Also a second version that is customized for Microsfot Exchange Database files.

  3. Hi Felipe,
    I’ve installed your ‘filesize’ script on nagios and it works perfectly on server 2003 thanks! However when I use Centreon 2, I’ve got a problem.
    Indeed, on monitoring tab, filesize statute is up and I have no information, there is nothing.
    Do you have any idea?
    Thanks.
    Best regards

  4. Hi Felipe,
    I’ve found the problem! Actually, there is a problem with command arguments on Centreon 2 so I’ve written directly the arguments and not $ARG1$ £ARG2$ etc.. It works perfectly now!
    Thanks.
    Glenn

  5. Hola Felipe:
    He instalado tu script en mi sistema Nagios 3.0.6 pero no logro que funcione. Al ejecutar el comando:
    check_nrpe -H 10.3.1.14 -c check_filesize -a “E:PersonalesCarpetas Outlook” *.pst 1536 1945
    recibo el error:
    scriptscheck_filesize.vbs(103, 17) Error de Microsoft VBScript en tiempo de ejecuci¢n: No coinciden los tipos: ‘Megabytes’
    ¿Cual puede ser el problema?
    Gracias por anticipado.
    Diego.
    ***********In english************
    Sorry for my very bad english.
    Hi Felipe,
    I had installed your check_filesize script in my Nagios 3.0.6 but not work.
    If I execute:
    check_nrpe -H 10.3.1.14 -c check_filesize -a “E:PersonalesCarpetas Outlook” *.pst 1536 1945
    the Nagios system said:
    scriptscheck_filesize.vbs(103, 17) Error de Microsoft VBScript en tiempo de ejecuci¢n: No coinciden los tipos: ‘Megabytes’
    Thank you for your help.
    Diego.

  6. Hola Diego,
    The syntax is :
    check_nrpe -H -c check_filesize -a “E:PersonalesCarpetas Outlook” file.pst 1536 1945
    Inside Nagios I have defined like
    !”E:/Program files/Exchsrvr/mdbdata”!priv1.edb,priv1.stm,pub1.edb,pub1.stm!40000!65000
    The * will not work and you will need double slashes. you can pass multiple filenames using “,”.
    I just tested from cmd line:
    #./check_nrpe -H server11 -c check_filesize -a C:windowssystem32 notepad.exe 1 2
    it works.
    saludos,
    Felipe

  7. Hi felipe, the problem Type mismatch: ‘Megabytes’ still appearing to me, can you help with this problem? and anotrhe think, your script still have the problem ‘Function’ on line 139.
    thanks for help.

  8. RM :There’s a typo in the download. On line 116, you had:End FunctionFunction GetArgs()
    s/bEnd FunctionFunction GetArgs()
    UN:F [1.7.5_995]

    please wait…
    Rating: 0.0/5 (0 votes cast)

    Today the same type is in there (again?)
    Also I get:
    check_filesize.vbs(126, 17) Microsoft VBScript runtime error: Type mismatch: ‘MegaBytes’
    Pls. advise,
    Ed

  9. Opa Felipe, arrumei o erro no script e mesmo assim tá dando erro pra mim.
    C:>cscript.exe //NoLogo //T:40 “C:Program FilesNSClient++scriptscheck_filesize.vbs”
    C:Program FilesNSClient++scriptscheck_filesize.vbs(29, 1) Microsoft VBScript runtime error: Subscript out of range

  10. Hi Felipe,
    Please provide us with the fix for the error (Type mismatch: ‘Megabytes’) or link to the corrected code.
    Thanks,
    Shaheen

  11. Hi Felipe,
    The actual issue was not with the code but with the entry in nsc.ini. There shouldnt be any quotes around the ARGs entry and this works perfectly.
    command[check_file_size]=cscript //nologo //t:60 “C:Program FilesNSClient++Additionscheck_filesize.vbs” -p $ARG1$ -f $ARG2$ -w $ARG3$ -c $ARG4$
    Cheers,
    Shaheen

  12. @shaheen,
    doesnt work for me.
    i got the “Type mismatch: ‘Megabytes'” failure.
    with, and without the quotes.

  13. Update:
    This helped
    check_command nrpe_checkfilesize!’d:exchsrvrmdbdata’!priv1.edb!50000!60000

  14. @Frankstar
    Hi did you ever get a resolution to the megabytes error in this script. I have tried quotes and no quotes and double slashes and that does result in any return at all. many thanks in advance.

  15. @Macca
    I too faced this problem. I got around it by hardcoding the warning and critical levels in NSC.ini, like so:
    check_ex_filesize=cscript.exe //nologo “scriptscheck_ex_filesize.vbs” -p “D:ExchsrvrMDBDATA” -f priv1.edb,priv1.stm,pub1.edb,pub1.stm -w 50000 -c 60000
    Now, I simply call check_ex_filesize from Nagios without any arguments and it works.
    Hope this helps!

  16. Any news on fixing the links for these scripts? Or can you email them to me?

  17. Hello, i’m using your script in op5 and it works fine except charts. When it goes over 1 GB it stops drawing chart and drops to 0 and stays there. Any tips?

Leave a Reply

Your email address will not be published. Required fields are marked *