''''''''''''''''''''''' ' ' VBS template for BBWin ' ' ' Gautier BEGIN ' CSC Luxembourg ' 11/02/2010 ' ' ' The XYMON server name, the debug level, the test/column name and the LifeTime are read from the BBWin.cfg file ' ' An execution log is created in the BBWin\logs\.log file. The VBS object associated is ObjectFileLog. ' ' ' You have to fill the value of Msg_KO and Msg_OK according your own custom test. ' ' ' You can use the ERROR variable to send your error message (0=> MSG_OK, 1=> MSG_KO) ' ' ''''''''''''''''''''''' ''''''''''''''''''''''' ' ' VBS std Variables ' ''''''''''''''''''''''' Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim Obj_FSO, WshShell, WshNetwork, xmlDoc set Obj_FSO = CreateObject("Scripting.FileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") Set WshNetwork = WScript.CreateObject("WScript.Network") Set xmlDoc = CreateObject("Microsoft.XMLDOM") xmlDoc.async = "false" ' permit to load the document in memory before computing ''''''''''''''''''''''' ' ' BBWin Variables ' ''''''''''''''''''''''' Dim Cmd, BB, PathBBWin, LOG, LifeTime, DebugLevel Dim Msg_OK, Msg_KO, Msg, Color, Color_KO, Color_OK, Column, ERROR, Srv, Return, ExecCmd, ObjectFileLog, CFG, CFGVal, X ' To be Modified Msg_OK = "" Msg_KO = "" Color_OK = "green" Color_KO = "yellow" ' Default values Column = "mig_log" DebugLevel = 0 LifeTime = 10 'minutes PathBBWin = WshShell.RegRead("HKLM\SOFTWARE\BBWin\binpath") PathBBWin = Left(PathBBWin,Len(PathBBWin)-3) BB = PathBBWin & "bin\BBWinCmd.exe" CFG = PathBBWin & "etc\BBWin.cfg" Return = 0 ERROR = 0 ' Reading the config file xmlDoc.load(CFG) set CFGVal = xmlDoc.documentElement If Not CFGVal Is Nothing Then For Each x In CFGVal.selectNodes("/configuration/bbwin/setting") If X.getAttribute("name") = "bbdisplay" Then Srv = X.getAttribute("value") End If If X.getAttribute("name") = "loglevel" Then DebugLevel = X.getAttribute("value") End If If X.getAttribute("name") = "timer" Then LifeTime = X.getAttribute("value") Select Case Right(LifeTime ,1) Case "m" LifeTime = Left(LifeTime,Len(LifeTime)-1) Case "h" LifeTime = Left(LifeTime,Len(LifeTime)-1) * 60 Case "d" LifeTime = Left(LifeTime,Len(LifeTime)-1) * 60 * 24 Case Else LifeTime = 1 End Select End If Next For Each x In CFGVal.selectNodes("/configuration/externals/load") If InStrRev(X.getAttribute("value"),WScript.ScriptName) > 0 Then Column = X.getAttribute("name") End If Next End If LOG = PathBBWin & "logs\" & Column & ".log" if ( DebugLevel > 0 ) Then set ObjectFileLog = Obj_FSO.OpenTextFile(LOG,ForAppending ,True) End if ''''''''''''''''''''''' ' ' Custom test ' ''''''''''''''''''''''' ' Test LOG Dim MIG_LOG, Nbr_Line, Mig_Log_Tmp, Cpt, File_Cpt, Line, Error_Log Dim Cpt_OK, Mig_Log_OK, File_Cpt_OK MIG_LOG = "E:\Logs\MIGFlow.log" 'MIG_LOG = "C:\temp\MIGFlow.log" Mig_Log_Tmp = PathBBWin & "tmp\mig_log.flg" Mig_Log_OK = PathBBWin & "tmp\mig_log.cpt" Nbr_Line = 0 Cpt = Clng(0) Error_Log = 0 Cpt_OK = 0 'MsgBox "Mig_Log_Tmp: " & Mig_Log_Tmp ' Does the file exist ? If Obj_FSO.FileExists(MIG_LOG) Then Set File = Obj_FSO.GetFile(MIG_LOG) ' The last line read is If File.Size <> 0 Then If Obj_FSO.FileExists(Mig_Log_Tmp) Then Set File_Cpt = Obj_FSO.OpenTextFile(Mig_Log_Tmp,ForReading) Nbr_Line = File_Cpt.ReadAll File_Cpt.Close ' The reading of the file gives a STRING variable, conversion into long number Nbr_Line = Clng(Nbr_Line) End If Else Set File_Cpt = Obj_FSO.OpenTextFile(Mig_Log_Tmp,ForWriting,True) File_Cpt.WriteLine(Cpt) File_Cpt.Close End If 'MsgBox "Nbr_Line: -" & Nbr_Line & "-" 'MsgBox "Nbr_Line " & VarType(Nbr_Line) & " Cpt " & VarType(Cpt) ' We read the file Set File = Obj_FSO.OpenTextFile(MIG_LOG,ForReading) Do While Not File.AtEndOfStream Line = File.ReadLine Cpt = Cpt + 1 'MsgBox "Coucou 1 - Cpt: " & Cpt & "Nbr_Line -" & Nbr_Line & "-" If ( Cpt > Nbr_Line ) Then 'MsgBox "Coucou 2" If Mid(Line,25,5) = "ERROR" Then 'MsgBox "Coucou 3" Error_Log = 1 Msg_KO = Msg_KO & vbCRLF & "&" & Color_KO & " " & Line End If End If Loop File.Close If Cpt < Nbr_Line Then Cpt = Clng(0) End If Set File_Cpt = Obj_FSO.OpenTextFile(Mig_Log_Tmp,ForWriting,True) File_Cpt.WriteLine(Cpt) File_Cpt.Close ' To not do lose a cycle is new file ' doesn't work 'If Cpt < Nbr_Line Then ' ' Return = WshShell.Run("cscript C:\progra~1\BBWin\Mig.vbs", 0, True) ' MsgBox "Return: " & Return ' WScript.Quit(0) 'End If If Error_Log = 0 Then Msg_OK = Msg_OK & vbCRLF & "&" & Color_OK & " No error in the log" Else ERROR = 1 End If 'MsgBox "Coucou 5" Else ERROR = 1 Msg_KO = Msg_KO & vbCRLF & "&" & Color_KO & " Log file absent: " & MIG_LOG Set File_Cpt = Obj_FSO.OpenTextFile(Mig_Log_Tmp,ForWriting,True) File_Cpt.WriteLine(Cpt) File_Cpt.Close End If ''''''''''''''''''''''' ' ' Sending the alarm ' ''''''''''''''''''''''' 'MsgBox "Msg_KO " & Msg_KO 'MsgBox "Msg_OK " & Msg_OK If ( ERROR = 0 ) Then Color = Color_OK Msg = Msg_OK If Obj_FSO.FileExists(Mig_Log_OK) Then Set File_Cpt_OK = Obj_FSO.OpenTextFile(Mig_Log_OK,ForReading) Cpt_OK = File_Cpt_OK.ReadAll File_Cpt_OK.Close ' The reading of a file gives a STRING var, turned in to long number Cpt_OK = Cint(Cpt_OK) Cpt_OK = Cpt_OK - 1 End If Else Color = Color_KO Msg = Msg_KO End if if Cpt_OK = 0 Then Msg = Now() & " " & Msg LifeTime = LifeTime * 2 ' Chr(34) => " Cmd = Chr(34) & BB & Chr(34) & " " & Srv & " status " & WshNetwork.ComputerName & " " & Column & " " & Color & " " & Chr(34) & Msg & Chr(34) & " " & LifeTime On Error Resume Next Set ExecCmd = WshShell.Exec(Cmd): ' Old version of VBS If Err.Number <> 0 Then Return = WshShell.Run("C:\progra~1\BBWin\bin\BBWinCmd.exe " & Cmd, 0, True) Else Return = ExecCmd.ExitCode End If On Error Goto 0 If ( DebugLevel > 0 ) Then If ( Return <> 0 ) Then ObjectFileLog.WriteLine(Now() & " " & Cmd & " Output error code: " & Return ) Else if ( DebugLevel = 4 ) Then ObjectFileLog.WriteLine(Now() & " " & Cmd & " Output error code: " & Return ) End if End if End if Cpt_OK = 3 ' Send a message at least ones third times End If ' Update of the sending counter if OK Set File_Cpt_OK = Obj_FSO.OpenTextFile(Mig_Log_OK,ForWriting,True) File_Cpt_OK.WriteLine(Cpt_OK) File_Cpt_OK.Close ''''''''''''''''''''''' ' ' Destruction of Objects ' ''''''''''''''''''''''' ' Objects of the test Set IE = Nothing ' Std BBWin Objects if ( DebugLevel > 0 ) Then ObjectFileLog.Close set ObjectFileLog = Nothing End if set File = Nothing set File_Cpt = Nothing set File_Cpt_OK = Nothing set ExecCmd = Nothing set WshNetwork = Nothing set WshShell = Nothing set Obj_FSO = Nothing set xmlDoc = Nothing set X = Nothing