Jump to content
ravindran0154

Monitor log folder containing text file and send email

Recommended Posts

ravindran0154

Dear All,

I am very new to AUTOIT. I am trying to put a monitor in place to monitor my log files. 

The folder containing log files creates over 1000 files in a second. i want a script to montior this folder and send me out an email if a text contains "error found" in it. 

It is ok if this monitors the folder each minute. I am clueless where to start. kindly help. 
 

Share this post


Link to post
Share on other sites
JLogan3o13

@ravindran0154 just to clarify - you are placing over 1000 files into the directory, and your goal is to search the text of each one of those 1000 for the text "error found"?


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
kylomas

ravindran0154,

A bare boned way (one of many) to do this...

#include <array.au3>
#include <date.au3>
#include <File.au3>

; show our items in tray, not default items
Opt("TrayMenuMode", 1)

; path to log files
Local $sFilePath = @ScriptDir

; string to search for
Local $sStringToMonitorFor = 'error found'

; how long to wait between searches in minutes
Local $iMonitorInterval = 1

; time of last scan used for time diff calcs
Local $sLastScan = _NowCalc()

; create tray controls
Local $ctlidExit = TrayCreateItem('Exit')

; variables for mail error routine and return
Local $oMyRet[2]
Local $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

; do initial scan
_Scanner()

While 1

    Switch TrayGetMsg()

        Case $ctlidExit
            Exit

    EndSwitch

    ; if more than $iMonitorInterval time has passed then do a scan
    If _DateDiff('n', $sLastScan, _NowCalc()) >= $iMonitorInterval Then _Scanner()

WEnd

Func _Scanner()

    ; variable for email report
    local $sOutString = ''

    ConsoleWrite('Start scan at ' & _Now() & @CRLF)

    ; create an array of log files.  if the folder is empty return to tray loop
    Local $aFiles = _FileListToArray($sFilePath, '*', 0, True)
    If Not IsArray($aFiles) Then Return

    ; process each file for error string and construct mail message if found
    For $i = 1 To $aFiles[0]
        If StringRegExp(FileRead($aFiles[$i]), $sStringToMonitorFor) Then $sOutString &= '     ' & $aFiles[$i] & @CRLF
        TrayTip('', 'Scanning file = ' & $aFiles[$i] & ' [' & $i & ']', 5)
    Next

    ; clear traytip
    TrayTip('', '', 0)

    ; update scan time for next scan
    $sLastScan = _NowCalc()

    ; if our output area is not blank then construct the email text and send it
    if $sOutString then
        ConsoleWrite('+ Errors found...Sending mail...')
        _Mailer('Error String Found in the Following File(s)' & @CRLF & @CRLF & $sOutString)
        ConsoleWrite(@error ? @CRLF & '! Error sending mail = ' & $oMyRet[1] & @CRLF : 'Send complete' & @CRLF)
    endif

    ConsoleWrite('End   scan at ' & _Now() & @CRLF & @CRLF)

EndFunc   ;==>_Scanner

; wrapper to set variables used in _INetSmtpMailCom.
Func _Mailer($str)

    Local $SmtpServer = 'smtp.mail.yahoo.com'       ; name of the yahoo smtp server
    Local $FromName = 'Me'                          ; what you want to appear in your inbox "From" column
    Local $FromAddress = 'kylomas010@yahoo.com'     ; name of your yahoo account
    Local $ToAddress = 'tjs010@samolyk.net'         ; who you are sending the mail to
    Local $Subject = "Log File Exception Warning"   ; title
    Local $Body = $str                              ; the body of the email
    Local $AttachFiles = ""                         ; the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
    Local $CcAddress = ""                           ; address for cc - leave blank if not needed
    Local $BccAddress = ""                          ; address for bcc - leave blank if not needed
    Local $Importance = "Normal"                    ; Send message priority: "High", "Normal", "Low"
    Local $Username = 'kylomas010@yahoo.com'        ; account username as known to the smtp server
    Local $Password = '********'                    ; password for the above account
    Local $IPPort = 465                             ; GMAIL / YAHOO port used for sending the mail
    Local $ssl = 1                                  ; GMAIL / YAHOO enables/disables secure socket layer sending - put to 1 if using httpS

    $rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
    If @error Then Return SetError(1)
EndFunc   ;==>_Mailer

; DO NOT ALTER ANYTHING BELOW HERE !!!!!

Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    Local $objEmail = ObjCreate("CDO.Message")
    $objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
    $objEmail.To = $s_ToAddress
    Local $i_Error = 0
    Local $i_Error_desciption = ""
    If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
    If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
    $objEmail.Subject = $s_Subject
    If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
        $objEmail.HTMLBody = $as_Body
    Else
        $objEmail.Textbody = $as_Body & @CRLF
    EndIf
    If $s_AttachFiles <> "" Then
        Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
        For $x = 1 To $S_Files2Attach[0]
            $S_Files2Attach[$x] = _PathFull($S_Files2Attach[$x])
;~          ConsoleWrite('@@ Debug : $S_Files2Attach[$x] = ' & $S_Files2Attach[$x] & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
            If FileExists($S_Files2Attach[$x]) Then
                ConsoleWrite('+> File attachment added: ' & $S_Files2Attach[$x] & @LF)
                $objEmail.AddAttachment($S_Files2Attach[$x])
            Else
                ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
                SetError(1)
                Return 0
            EndIf
        Next
    EndIf
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 Then $IPPort = 25
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    If $s_Username <> "" Then
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
    EndIf
    If $ssl Then
        $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    EndIf
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set Email Importance
    Switch $s_Importance
        Case "High"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
        Case "Normal"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
        Case "Low"
            $objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
    EndSwitch
    $objEmail.Fields.Update
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail = ""
EndFunc   ;==>_INetSmtpMailCom

Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ;ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1) ; something to check for when this function returns
    Return
EndFunc   ;==>MyErrFunc

This probably looks complicated.  However, work through it using the help file and it is pretty straight forward.  Some things that you can add:

  • logging to a file
  • mechanism (tray item) to change folder path and scan interval

For further help you will need to provide more details, such as;

  • how big are the files
  • how often are they made
  • how long do they live in the folder

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.