Sign in to follow this  
Followers 0
susserj

mail_tail_checker

1 post in this topic

#1 ·  Posted (edited)

Hi

I recently created this program to check a server's log file. If one of the key words are found in the tail of the log file, an email is sent out. I will be using the Windows Scheduler to launch the program at periodic intervals. The configuration parameters are set in the mail_tail_checker.ini file. In that way, the executable can be distributed easily and users of the application don't have to fiddle with the coding.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.8.1
Author: Joel Susser
Created: 12/11/2012
Modified: 12/17/2012
Status: In development

Script Function:
Our server indexes are periodically stopping.
This program is designed to check the log file on a Server
If the log file says certain words such as "stopped", then it will send out an email.

(I will be configuring this program to run with the Windows Scheduler at a set intervals)


Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here


#include
#include
#include
#include
#include

;initialize variables
;********************
$logfile="" ;location of the index log FileChangeDir
$tail_lines=""; number of lines to check in the bottom of log file
$s_SmtpServer="" ; smtp server
$log_tail="" ; I will be searching this string for keywords.
$s_FromName="" ; from email address
$s_FromAddress="" ;from email address (indicated in email)
$s_ToAddress="" ; to email address
$s_Subject=""; subject
$host = ""

;initialize arrays
;******************
Local $aRecords ;array to hold all the lines in the log file
Local $search_Array; array to hold search termss

;get variables from configuration file
;*************************************
readin_ini("mail_tail_checker.ini"); read in variable from configuration file

;Read the log file into an array. If it doesn't work display an error message
;*********************************************************************************
If Not _FileReadToArray($logfile, $aRecords) Then
;If Not _FileReadToArray("IILMCGWIMC.xlg", $aRecords) Then
MsgBox(4096, "Error", " Error reading logfile into Array:" & @error)
Exit
Endif

For $x = ($aRecords[0]-$tail_lines) To $aRecords[0]
$log_tail = $log_tail & $aRecords[$x]
;MsgBox(0, 'Record:' & $x, $aRecords[$x])
Next

;search for string. If you find it sent out the email.
For $x = 1 to $search_Array[0]
;MsgBox(0, "Records", $log_tail)
if StringInStr ( $log_tail, $search_Array[$x]) Then
sendmail()
$x = $search_Array[0]; stop looping if a a search term is found.
EndIf
Next
;**************************************************************************


; reads in configuration file and pulls out variables
;**************************************************************************
func readin_ini($ini)
; Script Start - Add your code below here
$s_SmtpServer = IniRead($ini, "Email Parameters", "s_SmtpServer", "")
;MsgBox(4096, "Result", $s_SmtpServer)
$s_FromName = IniRead($ini, "Email Parameters", "s_FromName", "")
;MsgBox(4096, "Result", $s_FromName)
$s_FromAddress = IniRead($ini, "Email Parameters", "s_FromAddress", "")
;MsgBox(4096, "Result", $s_FromAddress)
$s_ToAddress = IniRead($ini, "Email Parameters", "s_ToAddress", "")
$s_Subject =IniRead ($ini, "Email Parameters", "s_Subject", "")
;MsgBox(4096, "Result", $s_ToAddress)
$host = IniRead($ini, "Server Parameters", "host", "")
;MsgBox(4096, "Result", $host)
$tail_lines = IniRead($ini, "Server Parameters", "tail_lines", "")
;MsgBox(4096, "Result", $tail_lines)
$search_strings = IniRead($ini, "Server Parameters", "search_strings", "")
$search_strings = StringStripWS($search_strings,8)
;MsgBox(4096, "Result", $search_strings)
$search_Array=StringSplit( $search_strings, "," )
;_ArrayDisplay($search_Array, "info")
$logfile = IniRead($ini, "Server Parameters", "logfile", "")
EndFunc
;**************************************************************************

;Send out the email
;**************************************************************************
func sendmail()
Local $username = EnvGet("USERNAME")
Local $as_Body[4]
$as_Body[0] = "DATE: "&_Now()
$as_Body[1] = "HOST: "& $host
$as_Body[2] = "IP_ADDRESS: " & _GetIP()
$as_Body[3] = $log_tail
Local $Response = _INetSmtpMail($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, @ComputerName, -1)
Local $err = @error
If $Response = 1 Then
;MsgBox(0, "Success!", "Mail sent")
Else
;MsgBox(0, "Error!", "Mail failed with error code " & $err)
EndIf
;SEND EMAIL
endFunc
;**************************************************************************

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; mail_tail.ini configuration file
; last modified: 12-17-2012
; create by Joel Susser
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[Email Parameters]
s_SmtpServer = smtp-server.widget.com
s_FromName = smtp_userid
s_FromAddress = first.last@widget.com
s_ToAddress = fist.lasr@widget.com
s_Subject = HTVS36 Log File

[Server Parameters]
host = HTVS36
tail_lines = 3
search_strings = stopped,crashed,completed
logfile= IILMCGWIMC.xlg
Edited by susserj

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
Sign in to follow this  
Followers 0