| |
drew2fast
Posts: 1
Score: 0
Joined: 5/25/2007
Status: offline
|
I've been managing Oracle database mostly on Linux for years. Now I'm in an all windows shop and need a script to check my alert logs. Batch programming is junk, can't be trusted, errorlevel is worthless, so I wrote this little program. This is the begining of alert_log_checker.vbs ' ' Check the alert log for errors, then send email and rename file ' takes the alert log location as the 1st parameter Set oArgs = WScript.Arguments ' Set this to the log you want to check for or pass it in. currently its passed in v_alert_log = oArgs(0) ' Set this to the text pattern you want to grep for v_pattern = "ORA-" v_mail_to = "dba@someplace.com" Set objNet = CreateObject("WScript.NetWork") v_computername = objNet.ComputerName v_mail_from = v_computername & "@someplace.com" ' Check if the file exists if FileExists(v_alert_log) = FALSE then 'wscript.echo "No log file" & v_alert_log Wscript.quit(0) end if ' Grep the log v_grepResults = GrepFile(v_alert_log,v_pattern) if v_grepResults = "" then 'wscript.echo "Log is empty" wscript.quit(0) else ' send email concerning alert Set WshShell = WScript.CreateObject("WScript.Shell") Return = WshShell.Run("sendmail.vbs -t "& chr(34) & v_mail_to & chr(34) &" -f "& chr(34) & v_computername & "@crocentral.com "& chr(34) &" -s "& chr(34) & "Oracle Alerts in Log on "& v_computername & chr(34) & " -a "& v_alert_log & " -b "& chr(34) & v_grepresults & chr(34), 1, true) Set WshShell = Nothing ' rename file prepending date time YYYYMMDD_HHMM renameLogFile(v_alert_log) wscript.quit(0) end if '*********************** FileExists Function FileExists(filespec) Dim fe, msg Set fe = CreateObject("Scripting.FileSystemObject") If (fe.FileExists(filespec)) Then msg = TRUE Else msg = FALSE End If FileExists = msg End Function '*********************** GrepFile Function GrepFile(p_txtFile, p_pattern) Dim v_txtTemp, objFS, objFL, v_lineTemp Set objFS = CreateObject("Scripting.FileSystemObject") Set objFL = objFS.OpenTextFile(p_txtFile) Do While Not objFL.AtEndOfStream v_lineTemp=objFL.ReadLine if instr(1,v_lineTemp,p_pattern,1) > 0 then v_txtTemp = v_txtTemp & v_lineTemp & vbCrLf end if Loop objFL.Close Set objFS = Nothing GrepFile = v_txtTemp End Function '*********************** renameLogFile sub renameLogFile(p_file) dim v_file, v_path, v_filename, v_newname, v_yyyymmdd v_dtNow = Now() v_YYYYMMDD24hhMM = Right("0" & Year(v_dtNow), 4) & Right("0" & Month(v_dtNow), 2) & Right("0" & Day(v_dtNow), 2) & "_" & replace(FormatDateTime(v_dtnow,4),":","") Set fso = CreateObject("Scripting.FileSystemObject") set v_file=fso.GetFile(p_file) v_path=v_file.ParentFolder v_filename = v_file.name v_newname = v_path & "\" & v_YYYYMMDD24hhMM & "_" & v_filename 'wscript.echo v_newname v_file.Move v_newname end sub *** this is the end of alert_log_checker.vbs alert_log_checker.vbs calls sendmail.vbs is is below, which I didn't write (just hacked) and am including for the whole package. *** begin sendmail.vbs ' ' Sends email from the remote SMTP service using CDO objects ' ' Usage: ' sendmail -t <to> -f <from> -s "<subject>" -b "<message>" ' sendmail [-help|-?] ' '-------------------------------------------------- 'Option Explicit 'On Error Resume Next Dim objSendMail, oArgs, ArgNum Dim strTo, strFrom, strSubject, strBody, strAttachFile, strBodyFile strBodyFile=FALSE Set oArgs = WScript.Arguments ArgNum = 0 While ArgNum < oArgs.Count Select Case LCase(oArgs(ArgNum)) Case "-to","-t": ArgNum = ArgNum + 1 strTo = oArgs(ArgNum) Case "-from","-f": ArgNum = ArgNum + 1 strFrom = oArgs(ArgNum) Case "-subject","-s": ArgNum = ArgNum + 1 strSubject = oArgs(ArgNum) Case "-body","-b": ArgNum = ArgNum + 1 strBody = oArgs(ArgNum) Case "-bf": ArgNum = ArgNum + 1 strBodyFile=TRUE strBody = oArgs(ArgNum) Case "-attachfile","-a": ArgNum = ArgNum + 1 strAttachFile = oArgs(ArgNum) Case "-help","-?": Call DisplayUsage Case Else: Call DisplayUsage End Select ArgNum = ArgNum + 1 Wend 'WScript.Echo strto & strFrom & strsubject & strbody If oArgs.Count=0 Or strTo="" Or strFrom="" Or strSubject="" Then Call DisplayUsage Else Set objMessage = CreateObject("CDO.Message") objMessage.Subject = strsubject objMessage.From = strFrom objMessage.To = strto if strBodyFile = TRUE then objMessage.TextBody = ReadFile(strbody) else objMessage.TextBody = strbody end if if strAttachFile <> "" then 'wscript.echo "attaching file" objMessage.AddAttachment(strAttachFile) end if end if '==This section provides the configuration information for the remote SMTP server. objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Name or IP of Remote SMTP Server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "jupiter" 'Server port (typically 25) objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 objMessage.Configuration.Fields.Update '==End remote SMTP server configuration section== objMessage.Send ' Display the usage for this script Sub DisplayUsage WScript.Echo "Usage:" WScript.Echo " sendmail -t <to address> -f <from address> -s " & _ Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & _ "<message body>" & Chr(34) WScript.Echo " -a <attachment file name>" WScript.Echo " sendmail [-help|-?]" WScript.Echo "" WSCript.Quit End Sub Function ReadFile(txtFile) Dim txtTemp, objFS, objFL Set objFS = CreateObject("Scripting.FileSystemObject") Set objFL = objFS.OpenTextFile(txtFile) Do While Not objFL.AtEndOfStream txtTemp = txtTemp & objFL.ReadLine txtTemp = txtTemp & vbCrLf Loop objFL.Close Set objFS = Nothing ReadFile = txtTemp End Function *** end sendmail.vbs
|
|