Hi chaps,

I need a script to search a log file (whose name is DD-MM-YYYY.log)
for an error (for example "AN ERROR!").

However, even though the script rolls over each day, the files can get
very large, and errors occur often.

This script will be run at regular intervals (every 15 mins I reckon
at the mo) and needs to return something like a 1 if an error string
was found and a 0 if not. This value in turn will be used by the
alerting package to fire off alerts and poke things to get it working
again.

Now, i've got all that working more or less (getting the date bits to
auto-fill the filename is being a pain) expect for one important bit.
In my script, the log file is opened and then searched line by line,
and if an error is found, 1 is returned. The problem is that it
restarts from line 1 each time it's fired, so basically we are always
getting alerts, even for alerts we have already been told about.

What I need is some way to store the last line that was checked by the
script, and to start from the following line on the next check. I
guess this would in turn have to save the line number to a text file
and read it back when the check starts next.

Any ideas how I can do the latter bit? Finding the line number and
then starting from that point again ?

Olly

Re: Reading file from certain point by Pegasus

Pegasus
Fri Nov 30 00:43:58 PST 2007


"Oliver Marshall" <oliver.marshall@gmail.com> wrote in message
news:69278d7e-1f6c-4825-98ba-640d33be8715@s36g2000prg.googlegroups.com...
> Hi chaps,
>
> I need a script to search a log file (whose name is DD-MM-YYYY.log)
> for an error (for example "AN ERROR!").
>
> However, even though the script rolls over each day, the files can get
> very large, and errors occur often.
>
> This script will be run at regular intervals (every 15 mins I reckon
> at the mo) and needs to return something like a 1 if an error string
> was found and a 0 if not. This value in turn will be used by the
> alerting package to fire off alerts and poke things to get it working
> again.
>
> Now, i've got all that working more or less (getting the date bits to
> auto-fill the filename is being a pain) expect for one important bit.
> In my script, the log file is opened and then searched line by line,
> and if an error is found, 1 is returned. The problem is that it
> restarts from line 1 each time it's fired, so basically we are always
> getting alerts, even for alerts we have already been told about.
>
> What I need is some way to store the last line that was checked by the
> script, and to start from the following line on the next check. I
> guess this would in turn have to save the line number to a text file
> and read it back when the check starts next.
>
> Any ideas how I can do the latter bit? Finding the line number and
> then starting from that point again ?
>
> Olly

Here is the pseudo-code to do this:
- Get the line number "n" from the text file.
- Open the log file.
- Read "n" lines but ignore their contents.
- Process all subsequent lines.



Re: Reading file from certain point by Richard

Richard
Fri Nov 30 06:24:31 PST 2007


"Pegasus (MVP)" <I.can@fly.com> wrote in message
news:OnT8m0yMIHA.280@TK2MSFTNGP03.phx.gbl...
>
> "Oliver Marshall" <oliver.marshall@gmail.com> wrote in message
> news:69278d7e-1f6c-4825-98ba-640d33be8715@s36g2000prg.googlegroups.com...
>> Hi chaps,
>>
>> I need a script to search a log file (whose name is DD-MM-YYYY.log)
>> for an error (for example "AN ERROR!").
>>
>> However, even though the script rolls over each day, the files can get
>> very large, and errors occur often.
>>
>> This script will be run at regular intervals (every 15 mins I reckon
>> at the mo) and needs to return something like a 1 if an error string
>> was found and a 0 if not. This value in turn will be used by the
>> alerting package to fire off alerts and poke things to get it working
>> again.
>>
>> Now, i've got all that working more or less (getting the date bits to
>> auto-fill the filename is being a pain) expect for one important bit.
>> In my script, the log file is opened and then searched line by line,
>> and if an error is found, 1 is returned. The problem is that it
>> restarts from line 1 each time it's fired, so basically we are always
>> getting alerts, even for alerts we have already been told about.
>>
>> What I need is some way to store the last line that was checked by the
>> script, and to start from the following line on the next check. I
>> guess this would in turn have to save the line number to a text file
>> and read it back when the check starts next.
>>
>> Any ideas how I can do the latter bit? Finding the line number and
>> then starting from that point again ?
>>
>> Olly
>
> Here is the pseudo-code to do this:
> - Get the line number "n" from the text file.
> - Open the log file.
> - Read "n" lines but ignore their contents.
> - Process all subsequent lines.
>

This also requires that you read the file line by line, perhaps with the
ReadLine method of the FileSystemObject instead of using ReadAll. Also, you
need a counter to count the line numbers as you read lines.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



Re: Reading file from certain point by McKirahan

McKirahan
Fri Nov 30 08:33:20 PST 2007

"Oliver Marshall" <oliver.marshall@gmail.com> wrote in message
news:69278d7e-1f6c-4825-98ba-640d33be8715@s36g2000prg.googlegroups.com...
> Hi chaps,
>
> I need a script to search a log file (whose name is DD-MM-YYYY.log)
> for an error (for example "AN ERROR!").
>
> However, even though the script rolls over each day, the files can get
> very large, and errors occur often.
>
> This script will be run at regular intervals (every 15 mins I reckon
> at the mo) and needs to return something like a 1 if an error string
> was found and a 0 if not. This value in turn will be used by the
> alerting package to fire off alerts and poke things to get it working
> again.
>
> Now, i've got all that working more or less (getting the date bits to
> auto-fill the filename is being a pain) expect for one important bit.
> In my script, the log file is opened and then searched line by line,
> and if an error is found, 1 is returned. The problem is that it
> restarts from line 1 each time it's fired, so basically we are always
> getting alerts, even for alerts we have already been told about.
>
> What I need is some way to store the last line that was checked by the
> script, and to start from the following line on the next check. I
> guess this would in turn have to save the line number to a text file
> and read it back when the check starts next.
>
> Any ideas how I can do the latter bit? Finding the line number and
> then starting from that point again ?


Will this help? Watch for word-wrap.

It determines the date of today's log file automatically.
It does a ReadAll of today's log file and examines each
line from the beginning of the file unless the there is a
starting point (line) saved (and read from) a text file in
which case it only exmaines the file from after that line.

It returns 1 if the string (specified by the value of "cINS")
is found; it also writes that string to the text "cTXT" file.

You didn't say how you wanted to handle multiple instances
in the file of the string be searched for....

I haven't fully tested it as I don't fully understand your reqs.

Option Explicit
'*
'* Read_Log()
'*
MsgBox "Read_Log() = " & Read_Log(),vbInformation,"Read_Log"

Function Read_Log()
'****
'* Read a daily Log file and find the next line that contains the value
'* of "cINS". If found then write the line to file "cTXT" and return 1.
'****
Read_Log = 0
'*
'* Declare Constants
'*
Const cTXT = "Read_Log.txt"
Const cLOG = "DD-MM-YYYY.log"
Const cINS = "AN ERROR!"
'*
'* Declare Variables
'*
Dim strCTF
strCTF = ""
Dim strDIR
strDIR = WScript.ScriptFullName
strDIR = Left(strDIR,InStrRev(strDIR,"\"))
Dim strDMY
strDMY = Right(Day(Now),2) & "-" _
& Right(Month(Now),2) & "-" _
& Year(Now)
Dim booLOG
booLOG = False
Dim strLOG
strLOG = strDIR & Replace(cLOG,"MM-DD-YYYY",strDMY)
Dim arrOTF
Dim intOTF
Dim strOTF
Dim intPOS
Dim booTXT
booTXT = False
Dim strTXT
strTXT = strDIR & cTXT
'*
'* Declare Objects
'*
Dim objCTF
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOTF
'*
'* Files Exists?
'*
If Not objFSO.FileExists(strLOG) Then
MsgBox strLOG & " does not exist!",vbExclamation,cVBS
Exit Function
End If
'*
'* Read TXT file
'*
If objFSO.FileExists(strTXT) Then
booTXT = True
Set objOTF = objFSO.OpenTextFile(strTXT,1)
strOTF = objOTF.ReadAll
Set objOTF = Nothing
strCTF = Split(strOTF,vbCrLf)(0)
End If
'*
'* Read LOG file
'*
Set objOTF = objFSO.OpenTextFile(strLOG,1)
strOTF = objOTF.ReadAll
Set objOTF = Nothing
'*
'*
'*
If booTXT Then
intPOS = InStr(strOTF,strCTF)
If intPOS = 0 Then
strCTF = ""
Else
strOTF = Mid(strOTF,intPOS+Len(strCTF))
End If
End If
'*
'* Split LOG file
'*
arrOTF = Split(strOTF,vbCrLf)
For intOTF = 0 To UBound(arrOTF)
If Trim(arrOTF(intOTF)) <> "" Then
If Instr(arrOTF(intOTF),cINS) > 0 Then
strCTF = arrOTF(intOTF)
booLOG = True
Read_Log = 1
Exit For
End If
End If
Next
'*
'* Write TXT file
'*
If booLOG Then
objFSO.CreateTextFile(strTXT,True).Write(strCTF)
End If
'*
'* Destroy Objects
'*
Set objFSO = Nothing
End Function




Re: Reading file from certain point by Paul

Paul
Fri Nov 30 11:04:02 PST 2007


"Richard Mueller [MVP]" <rlmueller-nospam@ameritech.nospam.net> wrote in
message news:%23M5B9y1MIHA.5244@TK2MSFTNGP03.phx.gbl...
>
> "Pegasus (MVP)" <I.can@fly.com> wrote in message
> news:OnT8m0yMIHA.280@TK2MSFTNGP03.phx.gbl...
>>
>> "Oliver Marshall" <oliver.marshall@gmail.com> wrote in message
>> news:69278d7e-1f6c-4825-98ba-640d33be8715@s36g2000prg.googlegroups.com...
>>> Hi chaps,
>>>
>>> I need a script to search a log file (whose name is DD-MM-YYYY.log)
>>> for an error (for example "AN ERROR!").
>>>
>>> However, even though the script rolls over each day, the files can get
>>> very large, and errors occur often.
>>>
>>> This script will be run at regular intervals (every 15 mins I reckon
>>> at the mo) and needs to return something like a 1 if an error string
>>> was found and a 0 if not. This value in turn will be used by the
>>> alerting package to fire off alerts and poke things to get it working
>>> again.
>>>
>>> Now, i've got all that working more or less (getting the date bits to
>>> auto-fill the filename is being a pain) expect for one important bit.
>>> In my script, the log file is opened and then searched line by line,
>>> and if an error is found, 1 is returned. The problem is that it
>>> restarts from line 1 each time it's fired, so basically we are always
>>> getting alerts, even for alerts we have already been told about.
>>>
>>> What I need is some way to store the last line that was checked by the
>>> script, and to start from the following line on the next check. I
>>> guess this would in turn have to save the line number to a text file
>>> and read it back when the check starts next.
>>>
>>> Any ideas how I can do the latter bit? Finding the line number and
>>> then starting from that point again ?
>>>
>>> Olly
>>
>> Here is the pseudo-code to do this:
>> - Get the line number "n" from the text file.
>> - Open the log file.
>> - Read "n" lines but ignore their contents.
>> - Process all subsequent lines.
>>
>
> This also requires that you read the file line by line, perhaps with the
> ReadLine method of the FileSystemObject instead of using ReadAll. Also,
> you need a counter to count the line numbers as you read lines.

I agree with what the others have said.

I think getting to the point you were at in the previous run might go faster
if you used the skipline method rather than readline method to get to that
point. Even faster perhaps, you could save the character count of where you
left off, and use the textsrtream skip method to skip directly to that
character position. You can resume reading from there line by line. At the
end of each run you could save the file size as the character count you need
to skip on the next run (or maybe half that if it is a 16-bit Unicode file).

I suppose you could keep a record of the line or character positions of all
reported errors, to ensure you don't double report.

-Paul Randall