Home > VbScripts > script to check event viewer

script to check event viewer

October 6th, 2011 Leave a comment Go to comments

I have written similar script to check for a single server but this time
it scans a list of server.
It still not integrated with nagios because of the time it takes to execute
such script.
It does requires 7z.exe and dumpel.exe


'Get Event Viewer Filering by: event,source,id,days,server USING: dumpel.exe
'Be able to get Multiple Evens from Multiple Servers(servers array ?) events in array (?)
'NOTES: dumpel.exe is much faster then WMI or LogParser API
'Author: Felipe Ferreira
'Date: 24/10/07
'Version: 4.0 - Get Servers From txt file

'ìRequires:
'dumpel.exe WResourceKit
'Create log folder
'Create ServerList.txt with one server per line

'TODO:
'OK - zip logs and attach
'OK - Get List of Servers

Option Explicit


'@@GLOBAL VARS
Dim t1 : t1 = Timer  'time the script
dim strScriptFile : strScriptFile = WScript.ScriptFullname
dim strScriptPath : strScriptPath = Left(strScriptFile, Len(strScriptFile) - Len(WScript.Scriptname))
Dim EventDumpPath : EventDumpPath = strScriptPath & "log"
Dim ofso : Set ofso = CreateObject("Scripting.FileSystemObject")
Dim blnErrorFound : blnErrorFound = false
Dim EventDumplog, strLine, strBody, Server
Dim dumpFileCnt
Dim verbose,outputfile
Dim EventDaysOld
Dim eType, eSource, eId, EventType, EventSource
Dim arrTxtFile,inputFile

' COUNTERS
Dim RunTime : Runtime = 0
Dim intNumberOfErrrors : intNumberOfErrrors = 0
Dim cntServers : cntServers = 0
Dim cntServersOK : cntServersOK = 0
Dim cntServersErr : cntServersErr = 0

'@@@@@@@@@@@@DEFINE, or get dynamic from  TXT or Array
inputFile = strScriptPath & "ServersList.txt"
Dim strMailServer : strMailServer = "infomailgw"
verbose = 1                    ' If True WIll Print out to Screen, otherwise FALSE = silent mode
EventDaysOld = 2
eType="Security"
eSource="Security"
eID="681"

'write into Body Email the Errors being searched and its comments
strbody = "Checking for Event ID : "& eID & " Source: " & eSource & " - Type: " & eType & vbCrlf
strbody = "Checking: " & Server & " Event Source: " & eSource & " Event ID: " & eID & " Source: " &  eSource & " DaysOld = " & EventDaysOld &  vbCrlf &  vbCrlf
strbody = "SERVIDOR, NUMERO DE ERROS"
'--
Dim fso : Set fSo = CreateObject("Scripting.FileSystemObject")
Dim rdFile : Set rdFile = fSo.OpenTextFile(inputFile, 1)

call CleanUp( strScriptPath & "log")

Do while rdFile.AtEndofStream=false
   Server = rdFile.readline()
'Loop thru each server in the input list
	pt "Checking: " & Server & " Event Source: " & eSource & " Event ID: " & eID & " Source: " &  eSource & " DaysOld = " & EventDaysOld &  vbCrlf
	Call Execute(eType,eSource,eID,Server,EventDaysOld)
	'Check Error should check only the just created log file
	Call CheckifError(EventDumplog)
	 cntServers = cntServers + 1
'If errors/events found output
	if blnErrorFound = true then
	     	'@@@@@@@@@@@@OUTPUT	EMAIL
		 strBody = strBody & vbCrlf & Server & " ,       " & intNumberOfErrrors
    	 cntServersErr = cntServersErr + 1
		elseif blnErrorFound = False then
		 cntServersOK = cntServersOK + 1
    end if

Loop
RunTime = Left(Timer  - t1, 4)
strBody = strbody & vbCrlf &  "----------------------------------------------------------"
strBody = strbody & vbCrlf &  "Checked : " & cntServers & " servers. "
strBody = strbody & vbCrlf & "Servers with Errors : " & cntServersErr & " Servers OK: " & cntServersOK
strBody = strbody & vbCrlf & "Script Runtime: " & Runtime & " sec."

'ZIP the files
call fZip ("c:PROGRA~1NC_netscriptlog", "SecLogs.zip")
outputfile =  strScriptPath & "SecLogs.zip"
pt "Attaching = " & outputfile

'(sFrom,sTo,sCC,sSub,sBody,sAttch)
sendmail "monitor@infoglobo.com.br", "felipe.ferreira@infoglobo.com.br","marcela.santos@infoglobo.com.br", "Event Viewer Monitor", strBody , outputfile

pt strBody
'call CleanUp( strScriptPath & "log")
wscript.quit





'@@@@@@@@@@@@@@@@@@@@@@@FUNCTIONS AND SUBS@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Function Execute(eType,eSource,eID,eServer,EventDaysOld)
	on error resume next
	blnErrorFound = False
	EventDumplog = "log" & eServer & "_" & eID & ".txt"
	dim strCmd,strCmdOut
	dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")

	strCmd = strScriptPath & "dumpel.exe -f " & EventDumpLog & " -l " & eType & " -m " & eSource & " -e " & eID & " -s " & eServer & " -d " & EventDaysOld
	'pt strCmd
	Dim objExecObject : Set objExecObject = objShell.Exec(strCmd)

'MUST GET STDOUT, SOMETIMES APP DONT RUN!
if verbose = 1 then
	Do While objExecObject.Status <> 1
		wscript.sleep 10
		Wscript.StdOut.Write(".")
	Loop
    if err.number = 0 and objExecObject.Status = 1 then
	end if
end if
end function

sub CheckIfError(EventDumplog)
'Looks into Log folder if any .txt and not 0kb turn ErrorFound=true that will zip and send email
	Dim fs,File,intSize
	dumpFileCnt = 0
	'pt("Checking if errors: " & EventDumplog)
	Set fs = CreateObject("Scripting.FileSystemObject")
	If fs.FileExists( EventDumplog ) Then
     Set File = fs.GetFile(EventDumplog)
       intSize = File.Size
       pt "Tamanho do Arquivo: " & intSize
	    if intSize = 0 then
	     fs.DeleteFile(EventDumplog)
	    else
		  arrTxtFile = ReadTxtToArray(EventDumplog)
		  intNumberOfErrrors = Ubound(arrTxtFile)
		  pt  intNumberOfErrrors & " erros found"
	      blnErrorFound = True
	    end if 'File Dump Size Check
	end if
end sub

Sub SendMail(sFrom,sTo,sCC,sSub,sBody,sAttch)
	err.clear
	Dim objEmail
	Set objEmail = CreateObject("CDO.Message")
	objEmail.From = sFrom
	objEmail.To = sTo
	ObjEmail.CC = sCC
	objEmail.Subject = sSub
	objEmail.Textbody = sBody
	objEmail.AddAttachment sAttch
	objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
	objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strMailServer
	objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
	objEmail.Configuration.Fields.Update
	objEmail.Send
	if err.number <> 0 then
			wscript.echo "Error sending email : " & err.descprition
			wscript.quit
	end if
end sub


 Function ReadTxtToArray(strInputFilePath)
  Const ForReading = 1
  ' create FSO
  Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
  ' open text file for reading
  Dim objFile : Set objFile = objFSO.OpenTextFile(strInputFilePath, ForReading)
  ' read the content of the file and split into an array using vbCrLf as the seperator
  ' and return the array
  ReadTxtToArray = Split(objFile.ReadAll, VbCrLf)
  ' close the file
  objFile.Close
 End Function

function fzip(strPaths,strZipFileName)
	on error resume next
	dim strCmd
	dim objShell : Set objShell = WScript.CreateObject("WScript.Shell")
	strCmd = "7z.exe a -tzip " & strZipFileName & "  " & strPaths
	pt strCmd
	Dim objExecObject : Set objExecObject = objShell.Exec(strCmd)

	Do While objExecObject.Status <> 1
		wscript.sleep 1000
		Wscript.StdOut.Write(".")
	Loop
end function

 Sub CleanUp(strFolderPath)
     Dim objFile
	 Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
     If objFSO.FolderExists(strFolderPath) Then
     dim objFolder : set objFolder = objFSO.GetFolder(strFolderPath)
	 For Each objFile In objFolder.Files
	 pt "Deleting file : " & objFile.name
         objFile.Delete True
     Next
     End If
 End Sub


Function pt(msgTxt)
if verbose = 1 then
	wscript.echo msgtxt
end if
end function

  
  1. No comments yet.
  1. No trackbacks yet.

VAMOVE *

*