no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
| — | monitors:usb [2012/05/04 07:29] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== usb.vbs ====== | ||
| + | |||
| + | ^ Author | [[ goldfndr@gmail.com | Richard Finegold ]] | | ||
| + | ^ Compatibility | Xymon or Big Brother | | ||
| + | ^ Requirements | VBScript and BBWin on client | | ||
| + | ^ Download | None | | ||
| + | ^ Last Update | 2012-05-03 | | ||
| + | |||
| + | ===== Description ===== | ||
| + | This monitors what USB devices are plugged in, with a configuration file to alert if a USB device becomes unreachable. It does not check where (i.e. which hub/hub chain) a device is plugged in. Checked devices appear as normal text, unchecked devices are italicized small, hubs (if displayed at all) are italicized small gray struck-through. | ||
| + | |||
| + | ===== Installation ===== | ||
| + | === Client side === | ||
| + | * Copy usb.vbs (below) to your BBWin' | ||
| + | * Optionally change the DISPLAYHUB value to False if you don't want hubs displayed (normally gray strike-thru). | ||
| + | * Edit your BBWin' | ||
| + | * Add a line in the '' | ||
| + | <load value=" | ||
| + | </ | ||
| + | * If the externals agent was disabled in the '' | ||
| + | <load name=" | ||
| + | </ | ||
| + | * If you want checking/ | ||
| + | * The first character on each line is ''#'' | ||
| + | * After a digit, add a space and the text of a Description or Information of a device, with an optional ''#'' | ||
| + | 1 HASP | ||
| + | 2 USB UltraPro | ||
| + | 1 USB SuperPro | ||
| + | </ | ||
| + | |||
| + | === Server side === | ||
| + | (no server side changes needed) | ||
| + | |||
| + | ===== Source ===== | ||
| + | ==== usb.vbs ==== | ||
| + | |||
| + | <hidden onHidden=" | ||
| + | <code vb> | ||
| + | Const dbg=True | ||
| + | |||
| + | 'The structure of this file is a number (quantity), a space, then the device Description or Information (match output). | ||
| + | 'Use an empty file if you don't need to test anything (i.e. always green). | ||
| + | 'Add a "#" | ||
| + | Dim cfgfile : cfgfile = " | ||
| + | Const DISPLAYHUB = True | ||
| + | |||
| + | Function CMEC(u) | ||
| + | ' | ||
| + | Dim a : a = Array(" | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , "Boot configuration for the device conflicts with other devices." | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | , " | ||
| + | Dim n : n = u.ConfigManagerErrorCode ' | ||
| + | CMEC = n & " | ||
| + | if n <= UBound(a) then CMEC = n & " | ||
| + | End Function | ||
| + | |||
| + | Dim color : color = " | ||
| + | |||
| + | Const wbemFlagReturnImmediately = &h10 | ||
| + | Const wbemFlagForwardOnly = &h20 | ||
| + | Dim FSO : Set FSO = CreateObject(" | ||
| + | |||
| + | Phase "Read configuration file" | ||
| + | If not FSO.FileExists(cfgfile) then | ||
| + | If FSO.GetFile(cfgfile).Size > 0 Then | ||
| + | With FSO.OpenTextFile(cfgfile, | ||
| + | cfgfile = Split(.ReadAll, | ||
| + | .Close | ||
| + | End With | ||
| + | Else | ||
| + | cfgfile = Array("" | ||
| + | End If | ||
| + | |||
| + | Phase "Parse configuration file" | ||
| + | Dim needed : Set needed = CreateObject(" | ||
| + | Dim usb : for each usb in cfgfile | ||
| + | if Instr(usb, "#" | ||
| + | if len(usb) > 3 then | ||
| + | if not IsNumeric(Left(usb, | ||
| + | WScript.Echo """" | ||
| + | WScript.Echo "First character must be a digit!" | ||
| + | WScript.Quit 13 ' | ||
| + | end if | ||
| + | needed(Trim(Mid(usb, | ||
| + | end if | ||
| + | next | ||
| + | |||
| + | Phase " | ||
| + | Dim WMI : Set WMI = GetObject(" | ||
| + | |||
| + | ' | ||
| + | ' | ||
| + | 'Both derive from Win32_PNPEntity, | ||
| + | |||
| + | Dim WMIQuery : WMIQuery = " | ||
| + | Dim col : Set col = WMI.ExecQuery(WMIQuery, | ||
| + | |||
| + | Phase "Check " & col.Count & " devices" | ||
| + | Dim Summary, Table : Summary = "" | ||
| + | For Each usb In col | ||
| + | If Right(usb.Name, | ||
| + | Phase "Skip a WMI device: " & usb.Name | ||
| + | If DISPLAYHUB Then Table = Table & Replace(DeviceLine(usb) _ | ||
| + | , "< | ||
| + | Else | ||
| + | Phase "Check a WMI device: " & usb.Name | ||
| + | Table = Table & DeviceLine(usb) | ||
| + | End If | ||
| + | Next | ||
| + | |||
| + | Function DeviceLine(usb) | ||
| + | ' | ||
| + | 'So try to look up the LocationInformation in the registry. Mention it. | ||
| + | ' | ||
| + | Dim WSH : Set WSH = CreateObject(" | ||
| + | On Error Resume Next ' | ||
| + | Dim s, nm : s = "" | ||
| + | Const ENM = " | ||
| + | s = Trim(WSH.RegRead(ENM & usb.DeviceID & " | ||
| + | if "" | ||
| + | On Error Goto 0 | ||
| + | |||
| + | ' | ||
| + | If Instr(s, ";" | ||
| + | nm = Trim(usb.Name) : If Instr(nm, ";" | ||
| + | |||
| + | 'Make small and italic if configuration doesn' | ||
| + | dim dl : dl = "< | ||
| + | dim c : c = 0 | ||
| + | if needed.Exists(nm) then | ||
| + | c = needed(nm) ' | ||
| + | dl = "< | ||
| + | end if | ||
| + | if needed.Exists(s) then | ||
| + | c = needed(s) ' | ||
| + | dl = "< | ||
| + | end if | ||
| + | dl = dl & "< | ||
| + | 'We could also add Service. Useful? | ||
| + | if usb.Status = " | ||
| + | dl = dl & " | ||
| + | elseif usb.Status = " | ||
| + | dl = dl & " | ||
| + | if c > 0 then | ||
| + | if color = " | ||
| + | Summary = Summary & vbCRLF & "& | ||
| + | end if | ||
| + | else | ||
| + | dl = dl & " | ||
| + | if c > 0 then | ||
| + | color = " | ||
| + | Summary = Summary & vbCRLF & "& | ||
| + | end if | ||
| + | end if | ||
| + | dl = dl & " " & usb.Status & vbCRLF & "< | ||
| + | if c > 0 then | ||
| + | c = c - 1 | ||
| + | if 0 = c then | ||
| + | if needed.Exists(nm) then needed.Remove(nm) else needed.Remove(s) | ||
| + | else | ||
| + | if needed.Exists(nm) then needed(nm) = c else needed(s) = c | ||
| + | end if | ||
| + | end if | ||
| + | DeviceLine = dl | ||
| + | End Function | ||
| + | |||
| + | Phase "Was anything found?" | ||
| + | 'The first column is DeviceDesc/ | ||
| + | If len(Table) > 0 then Table = "< | ||
| + | & "< | ||
| + | & Table & "</ | ||
| + | |||
| + | Phase "Was anything missing?" | ||
| + | if needed.Count > 0 then | ||
| + | Quickie = "At least " & needed.Count & " USB devices needed but missing" | ||
| + | Summary = Table & vbCRLF & Summary | ||
| + | Summary = Summary & vbCRLF & "& | ||
| + | for each usb in needed.keys | ||
| + | Summary = Summary & vbCRLF & " &clear Qty=" & needed(usb) & " : " & usb | ||
| + | next | ||
| + | color = " | ||
| + | else | ||
| + | Summary = Table & vbCRLF & Summary | ||
| + | Quickie = "All required USB devices present" | ||
| + | end if | ||
| + | |||
| + | Phase "Write status" | ||
| + | WriteStatus " | ||
| + | WScript.Quit 0 | ||
| + | |||
| + | '###################################################################### | ||
| + | 'Write out the status; depends on FSO and WSH | ||
| + | Sub WriteStatus(column, | ||
| + | 'Why so long? Assume that client might collect file while writing, so make temp file and rename it. | ||
| + | '###################################################################### | ||
| + | Dim FSO : Set FSO = CreateObject(" | ||
| + | Dim WSH : Set WSH = CreateObject(" | ||
| + | Const BBWinreg = " | ||
| + | Const Questreg = " | ||
| + | On error resume next | ||
| + | ' | ||
| + | Dim colfile : colfile = WSH.RegRead(BBWinreg) | ||
| + | If "" | ||
| + | If "" | ||
| + | colfile = colfile & " | ||
| + | Dim tempfile : tempfile = colfile & " | ||
| + | If FSO.FileExists(tempfile) Then WScript.Quit 183' | ||
| + | On Error Resume Next | ||
| + | With FSO.CreateTextFile(tempfile, | ||
| + | if err then WScript.Quit err ' | ||
| + | .WriteLine color & " " _ | ||
| + | & WeekdayName(Weekday(Now), | ||
| + | & Mid(Now, | ||
| + | & WSH.Environment(" | ||
| + | & "] " & quickie | ||
| + | .Write line | ||
| + | .Close | ||
| + | End With | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | if FSO.FileExists(colfile) then FSO.DeleteFile(colfile) | ||
| + | FSO.MoveFile tempfile, colfile | ||
| + | End Sub | ||
| + | |||
| + | '###################################################################### | ||
| + | 'If the script gets stuck, we can use Process Explorer to examine its environment | ||
| + | '###################################################################### | ||
| + | Sub Phase(n) | ||
| + | if dbg then wscript.echo n | ||
| + | CreateObject(" | ||
| + | End Sub | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Known Bugs and Issues ===== | ||
| + | (none) | ||
| + | |||
| + | ===== To Do ===== | ||
| + | * Perhaps also display each device' | ||
| + | * Find better column headers than " | ||
| + | ===== Credits ===== | ||
| + | * Nir Sofer' | ||
| + | |||
| + | ===== Changelog ===== | ||
| + | |||
| + | * **2012-05-03** | ||
| + | * Initial release | ||