[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bbwin external for showing Resources open / Sessions active on Windows file servers



Asked a question a little while back about this on the bbwin user list
and thought I'd share in case anyone else might have a use for it.
See the attached file for explanation and such.  Save this to the
BBWIN\bin directory and add the appropriate line under the cfg file
(explained in the comments at the top of this script).

Note that this code assumes the compress.exe utility (from the Ms
Resource Tools kit) is present in the PATH, but it can be easily
altered to something else (this script backs up the info to the
BBWIN\logs directory - this functionality is easy enough to strip out,
see script for details).


Ben
'Option Explicit
'
' netfiles.vbs - hobbit/BB plugin to report on currently connected Sessions and Open file resources

' *******************************************************************************
' License:  GPLv2
' Copyright (C) 2008  Ben Trussell <azlobo73 atthe domain. gmail.com>

' This program is free software; you can redistribute it and/or
' modify it under the terms of the GNU General Public License
' as published by the Free Software Foundation; either version 2
' of the License, or (at your option) any later version.

' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
' *******************************************************************************
'
'  * Tested with Hobbit 4.3  and BBWin .10 
'  * Should work with 4.2, BBWin .10+, etc (test first)
'  * You will need to modify to make work with BB (See BBHOME, other variables below)
'  * default compression of logs (default location BBHOME\logs\netfiles_YYYYMMDDHHMMSS) is the compress utility from 
'      MS Resource Tools kit - edit the "compressCMD" variable to change this
' 
'  EXAMPLE line for the externals section of your bbwin.cfg file:
'	<load value="cscript /nologo netfiles.vbs HOSTNAME" timer="5m" />
' 
' Make sure to replace HOSTNAME with the nam of the Windows Host (used to enurmerate resources / sessions a la 
'		"WinNT://" & Host & "/lanmanserver,fileservice" laster so it must be a valid hostname to this end)


Dim TEST, BBHOME, BBEXTDIR, BBTMPDIR, DATAFILE, TMPOUTFILE, LOGDIR, color, DATEDAY, DATESEC, LOGFILE, LOGFILECAB, compressCMD
Dim tmpFile, objDataFile, netfilesInfo, today, runResult, DEBUG

DEBUG = 0

On Error Resume Next
HOSTNAME = WScript.Arguments.Item(0)

If Err <> 0 Then
	If Err = 9 Then
		WScript.echo "ERROR: You Forgot to pass HOSTNAME as an argument..."
	Else
		WScript.echo "Error: Not Continueing... Error " & Err.Number & ": " & Err.Description
	End If
	Err.Clear 
Else
	If DEBUG = 1 Then
		wscript.echo " hostname: " & HOSTNAME
	End If

	BBHOME = "C:\Program Files\BBWin"
	TEST = "netfiles"
	
	BBEXTDIR = BBHOME & "\tmp"
	BBTMPDIR = BBHOME & "\tmp"
	DATAFILE = BBEXTDIR & "\" & TEST
	TMPOUTFILE = BBTMPDIR & "\" & TEST & ".tmp"
	LOGDIR = BBHOME & "\logs"
	DATEDAY = "" & get_dateDay
	DATESEC = "" & get_datetimeSortable
	LOGFILE = LOGDIR & "\netfiles_" & HOSTNAME & "_" & DATESEC & ".log"
	LOGFILECAB = LOGDIR & "\netfiles_" & HOSTNAME & "_" & DATESEC & ".cab"
	compressCMD = "cmd /K compress -S -Z """ & LOGFILE & """ """ & LOGFILECAB & """>NUL"

	color = "green"
	today = date() & " " & time()

	Set fso = CreateObject("Scripting.FileSystemObject")
	Set oShell = CreateObject("WScript.Shell")
	Set tmpFile = fso.CreateTextFile(TMPOUTFILE)

	netfilesInfo = getOpenFiles(HOSTNAME)
	tmpFile.WriteLine  color & " " & today & " (" & DATESEC & ") " & TEST
	tmpFile.WriteLine "Open Resources and Sessions on " & HOSTNAME & ":"
	tmpFile.WriteBlankLines(2)
	tmpFile.WriteLine netfilesInfo
	tmpFile.Close

	Set objDataFile = fso.GetFile(TMPOUTFILE)
	objDataFile.copy DATAFILE
	objDataFile.copy LOGFILE
	fso.deleteFile TMPOUTFILE

	runResult = oShell.Run(compressCMD, 0, false)
	Wscript.Sleep 200

	If (runResult <> 0) Then
		Wscript.echo "Error running cmd: " & compressCMD & ": " & runResult
	End If
	Set oShell = Nothing

	If fso.FileExists(LOGFILECAB) Then
		fso.deleteFile LOGFILE
	Else
		Wscript.echo "Failed to create compressed log.  Keeping uncompressed file " &_
		LOGFILE & " instead... (CMD:" & compressCMD & ")"
	End If
End If


Function get_dateDay
	get_dateDay = Year(Now()) & Month(Now()) & Day(Now())
End Function

Function get_datetimeSortable
	Dim varYear, varMon, varDay, varHour, varMin, varSec
	varYear = Year(Now())
	varMon = Month(Now()) 
	varDay = Day(Now()) 
	varHour = Hour(Now()) 
	varMin = Minute(Now()) 
	varSec = Second(Now())

	If varMon < 10  Then
		varMon = "0" & varMon
	End If
	If varDay < 10  Then
		varDay = "0" & varDay
	End If
	If  varHour < 10  Then
		varHour = "0" & varHour
	End If
	If varMin < 10  Then
		varMin = "0" & varMin
	End If
	If varSec < 10  Then
		varSec = "0" & varSec
	End If

	get_datetimeSortable = varYear & varMon & varDay & varHour & varMin & varSec
	
	If Err <> 0 AND DEBUG = 1 Then
		WScript.echo "Function get_datetimeSortable: " & Err.Number & ": " & Err.Description
		Err.Clear 
	End If
	If Err <> 0 AND DEBUG <> 1 Then
		Err.Clear
	End If

End Function

Function getOpenFiles (Host)
	Dim Files(), Sessions(), sesCount, resCount, tCount, objFS
	set objFS = GetObject("WinNT://" & Host & "/lanmanserver,fileservice")
	resCount = 0
	sesCount = 0
	tCount = 0
	getOpenFiles = ""
	
	On Error Resume Next
	
	' SECTION: Resources
	For Each objRes in objFS.Resources
		tCount = tCount + 1
	Next

	If (tCount > 0) Then
		redim Files(tCount+300)

		resCount = 0
		For Each objRes in objFS.Resources
			If (isNull(objRes.Path) = False) Then
				resCount = resCount + 1
				Files(resCount) = objRes.User & " : " & objRes.Path  & " : " &  objRes.LockCount 
			Else
				If Err.Number <> 0 Then
					WScript.echo "Function: getOpenFiles, Section: Resources, For Each loop: " &_
					 Err.Number & ": " & Err.Description
					Err.Clear 
				End If
			End If
		Next
		For tName = 1 to resCount
			For nName = (tName + 1) to resCount 
				If strComp(Files(tName),Files(nName),0)=1 then 
					buffer = Files(nName) 
					Files(nName) = Files(tName) 
					Files(tName) = buffer 
				End If
			Next
		Next
		getOpenFiles = getOpenFiles & "(Open Files count: " &  resCount & " (total count: " & tCount  &_
		 "  - null path entries are excluded from list)" & vbCrLf
		For n = 1 to resCount
			getOpenFiles = getOpenFiles & Files(n) & vbCrLf
		Next
		getOpenFiles = getOpenFiles & "(Open Files count: " &  resCount & " (total count: " & tCount  &_
		 "  - null path entries are excluded from list)" & vbCrLf

	Else
		getOpenFiles = getOpenFiles & "No open Resouces" & vbCrLf
	End IF

	If Err <> 0 AND DEBUG = 1 Then
		WScript.echo "Function: getOpenFiles, Section: Resources -- " & Err.Number & ": " & Err.Description
		Err.Clear 
	End If
	If Err <> 0 AND DEBUG <> 1 Then
		Err.Clear
	End If
	
	getOpenFiles = getOpenFiles & vbCrLf & vbCrLf
	tCount = 0

	' SECTION: Sessions
	For Each objRes in objFS.Sessions
		tCount = tCount + 1
	Next

	If (tCount > 0) Then
		redim Sessions(tCount+300)

		sesCount = 0
		For Each objRes in objFS.Sessions
			If (isNull(objRes.User) = False) Then
				sesCount = sesCount + 1
				Sessions(sesCount) = objRes.User & " : " & objRes.Computer
			Else
				If Err.Number <> 0 Then
					WScript.echo "Function: getOpenFiles, Section: Sessions, For Each loop: " &_
					 Err.Number & ": " & Err.Description
					Err.Clear 
				End If
			End If
		Next
		For tName = 1 to sesCount
			For nName = (tName + 1) to resCount 
				If strComp(Files(tName),Files(nName),0)=1 then 
					buffer = Files(nName) 
					Files(nName) = Files(tName) 
					Files(tName) = buffer 
				End If
			Next
		Next
		getOpenFiles = getOpenFiles & "(Open Sessions count: " &  sesCount & " (total count: " & tCount  &  ")" & vbCrLf
		For n = 1 to sesCount
			getOpenFiles = getOpenFiles & Sessions(n) & vbCrLf
		Next
		getOpenFiles = getOpenFiles & "(Open Sessions count: " &  sesCount & " (total count: " & tCount  & ")" & vbCrLf

	Else
		getOpenFiles = getOpenFiles & "No open Sessions" & vbCrLf
	End IF

	If Err <> 0 AND DEBUG = 1 Then
		WScript.echo "Function: getOpenFiles, Section: Sessions -- " & Err.Number & ": " & Err.Description
		Err.Clear 
	End If
	If Err <> 0 AND DEBUG <> 1 Then
		Err.Clear
	End If
	
End Function