Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone help me with this little script? 1

Status
Not open for further replies.

DrSeussFreak

Programmer
Feb 16, 2007
149
US
Look for the << >> and read what is inside to see what I need/want it to do.

Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\oracle\dba_util\scripts\log\listener.log", _
    ForReading)

strSearchThis = objTextFile.ReadAll
if instr(strSearchThis, "TNS-12541") > 0 Then
	SendEmail "to", "from", "MARS - Listener Has Stopped", strSearchThis
	   << Execute a dos Command here called 'lsnrctl start', and then run 
	   'lsnrctl status > D:\oracle\dba_util\scripts\log\listener.log again, and do then>>
	   Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\oracle\dba_util\scripts\log\listener.log", _
    ForReading)

strSearchThis = objTextFile.ReadAll
if instr(strSearchThis, "TNS-12541") > 0 Then
	SendEmail "to", "from", "MARS - Listener Could Not Be Restarted", "Listener could not automatically be restarted, TAKE ACTION"
End If
Else
End If

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP Server"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg
    objEmail.Textbody = objEmail.Textbody & VbCrLf

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
essentially, it looks in a log file for an Oracle error. If it finds it, it sends an e-mail and automatically tries to restart the service. Then it re-creates the log file and looks again, if it started ok, nothing, if the error exists again, it sends a different e-mail saying, take action.
 
This is the error for my revised code (below)

D:\dba_util\scripts>listener.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

D:\dba_util\scripts\listener.vbs(13, 22) Microsoft VBScript compilation error: Expected identifier

Code:
Set lCheck = WScript.CreateObject("WScript.Shell")
Set lStart = WScript.CreateObject("WScript.Shell")

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\oracle\dba_util\scripts\log\listener.log", _
    ForReading)

strSearchThis = objTextFile.ReadAll
if instr(strSearchThis, "TNS-12541") > 0 Then
	SendEmail "To", "From", "MARS - Listener Has Stopped", strSearchThis
	 lStart.run WScript."d:\oracle\dba_util\scripts\listener_start.bat"
	 lCheck.run	WScript."d:\oracle\dba_util\scripts\listener_check.bat"
strSearchThis = objTextFile.ReadAll
if instr(strSearchThis, "TNS-12541") > 0 Then
	SendEmail "To", "From", "MARS - Listener Could Not Be Restarted", _
					 "Listener could not automatically be restarted, TAKE ACTION"	 
Else
	SendEmail "To", "From", "MARS - Listener Was Restarted", _
					 "The Listener was Successfully restarted!"		 
End If
Else
End If
Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP Server Here"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
never mind got it, thanks

Code:
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\dba_util\scripts\log\listener.log", _
    ForReading)

strSearchThis = objTextFile.ReadAll
if instr(strSearchThis, "TNS-") > 0 Then
	SendEmail "to", "from", "MARS - Listener Has Stopped - Trying to Restart", strSearchThis

Dim lStart
Set lStart=createobject("wscript.shell")
lStart.run "D:\dba_util\scripts\listener_start.bat"
set lStart=Nothing

Dim lCheck
Set lCheck=createobject("wscript.shell")
lCheck.run "D:\dba_util\scripts\listener_check.bat"
set lCheck=nothing

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\dba_util\scripts\log\listener.log", _
    ForReading)

strSearchThis = objTextFile.ReadAll
if instr(strSearchThis, "TNS-") > 0 Then
	SendEmail "to", "from", "MARS - Listener Has Stopped - Can not Restart", strSearchThis
Else
SendEmail "to", "from", "MARS - Listener Restarted", strSearchThis
End If	
Else
	'SendEmail "to", "from", "MARS - Listener Is O....K", "Chill"
End If
Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP Server"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
Should be more like this...but I think your If...Else logic is a bit off.

Code:
Const ForReading = 1

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(_
		"D:\oracle\dba_util\scripts\log\listener.log", ForReading)
strSearchThis = objTextFile.ReadAll
objTextFile.Close

If InStr(strSearchThis, "TNS-12541") > 0 Then
	SendEmail "To", "From", "MARS - Listener Has Stopped", strSearchThis
	objShell.run "d:\oracle\dba_util\scripts\listener_start.bat", 0, True
	objShell.run "d:\oracle\dba_util\scripts\listener_check.bat", 0, True
	If InStr(strSearchThis, "TNS-12541") > 0 Then
		SendEmail "To", "From", "MARS - Listener Could Not Be Restarted", _
		"Listener could not automatically be restarted, TAKE ACTION"    
	Else
		SendEmail "To", "From", "MARS - Listener Was Restarted", _
		"The Listener was Successfully restarted!"        
	End If
End If

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP Server Here"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Ok, the only problem is that it NEVER sends an e-mail saying that it has restarted the listener even though it has. I have added a lot of pauses to give enough time for the services to start.

Code:
Const ForReading = 1

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\dba_util\scripts\log\listener.log", _ 
	ForReading)
strSearchThis = objTextFile.ReadAll
objTextFile.Close

If InStr(strSearchThis, "TNS-") > 0 Then
    SendEmail "to", "from", "MARS - Listener Has Stopped", strSearchThis
    
    WScript.Sleep 3000 ' pause for 3 second
    objShell.run "d:\dba_util\scripts\listener_start.bat", 0, True
    WScript.Sleep 20000 ' pause for 20 second
    
    Set objTextFile2 = objFSO.OpenTextFile("D:\dba_util\scripts\log\listener.log", _ 
		ForReading)
		strSearchThis2 = objTextFile2.ReadAll
		objTextFile2.Close
		
		WScript.Sleep 10000 ' pause for 10 seconds
    objShell.run "d:\dba_util\scripts\listener_check.bat", 0, True
    WScript.Sleep 20000 ' pause for 20 seconds
    
    If InStr(strSearchThis2, "TNS-") > 0 Then
        SendEmail "to", "from", "MARS - Listener Could Not Be Restarted", _
        "Listener could not automatically be restarted, TAKE ACTION"    
    Else
        SendEmail "to", "from", "MARS - Listener Was Restarted", _
        "The Listener was Successfully restarted!"        
    End If
End If

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
Do you get any errors?

What if you try:
objShell.run "%comspec% /c d:\dba_util\scripts\listener_start.bat", 0, True

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I tried that, the problem it seems is that it is not actually re-opening the new log file. I need it to somehow re-open the log file, and check again.

Code:
Const ForReading = 1

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\dba_util\scripts\log\listener.log", _ 
	ForReading)
strSearchThis = objTextFile.ReadAll
objTextFile.Close

If InStr(strSearchThis, "TNS-") > 0 Then
    SendEmail "To", "From", "MARS - Listener Has Stopped", strSearchThis
    
    WScript.Sleep 3000 ' pause for 3 second
    objShell.run "%comspec% /c d:\dba_util\scripts\listener_start.bat", 0, True
    WScript.Sleep 20000 ' pause for 20 second
    
    Set objTextFile2 = objFSO.OpenTextFile("D:\dba_util\scripts\log\listener.log", _ 
		ForReading)
		strSearchThis2 = objTextFile2.ReadAll
		objTextFile2.Close
		
		WScript.Sleep 10000 ' pause for 10 seconds
    objShell.run "%comspec% /c d:\dba_util\scripts\listener_check.bat", 0, True
    WScript.Sleep 20000 ' pause for 20 seconds
    
    If InStr(strSearchThis2, "TNS-") > 0 Then
        SendEmail "To", "From", "MARS - Listener Could Not Be Restarted", _
        "Listener could not automatically be restarted, TAKE ACTION"    
    Else
        SendEmail "To", "From", "MARS - Listener Was Restarted", _
        "The Listener was Successfully restarted!"        
    End If
End If

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
now I added a script to run the listener_check.bat at the beginning, and it errors out (seems to skip the step)

Code:
Set objShell = CreateObject("WScript.Shell")
objShell.run "d:\dba_util\scripts\listener_check.bat", 0, True
WScript.Sleep 3000 ' pause for 3 second

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("D:\dba_util\scripts\log\listener.log", _ 
	ForReading)
strSearchThis = objTextFile.ReadAll
objTextFile.Close

If InStr(strSearchThis, "TNS-") > 0 Then
    SendEmail "to", "from", "MARS - Listener Has Stopped", strSearchThis
    
    WScript.Sleep 3000 ' pause for 3 second
    objShell.run "%comspec% /c d:\dba_util\scripts\listener_start.bat", 0, True
    WScript.Sleep 20000 ' pause for 20 second
		
		WScript.Sleep 10000 ' pause for 10 seconds
    objShell.run "%comspec% /c d:\dba_util\scripts\listener_check2.bat", 0, True
    WScript.Sleep 20000 ' pause for 20 seconds
    
    Set objFSO2 = CreateObject("Scripting.FileSystemObject")   
    Set objTextFile2 = objFSO2.OpenTextFile("D:\dba_util\scripts\log\listener2.log", _ 
		ForReading)
		strSearchThis2 = objTextFile2.ReadAll
		objTextFile2.Close
    
    If InStr(strSearchThis2, "TNS-") > 0 Then
        SendEmail "to", "from", "MARS - Listener Could Not Be Restarted", _
        "Listener could not automatically be restarted, TAKE ACTION"    
    Else
        SendEmail "to", "from", "MARS - Listener Was Restarted", _
        "The Listener was Successfully restarted!"        
    End If
End If

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "SMTP"

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub

and the error is

D:\dba_util\scripts>listener.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

D:\dba_util\scripts\listener.vbs(4, 1) Microsoft VBScript runtime error: File not found
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top