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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW_TO: Real-Time Log file 1

Status
Not open for further replies.
Nov 17, 2006
2
US
I had a problem lots of people have. Procomm Plus's capture file is only written when closed. This bugged me and wouldnt work so I wrote a script (based on the works of others, Knob showed me the way) that writes a real-time log file and a couple of other interesting things:

1) Names the Log file based on Date
2) Creates a New log file every Day
3) emails daily log files
4) Sends emails when certain strings are recieved.

If anyone cares about how I send emails let me know, but my comments should cover it. Whithour further ado here is the script:
; This Script writes real-time daily log files
; from a serial port connection called "PBX".
; In addition it creates a seperate
; log file especially for 911 calls.
; When a 911 call comes in an email is created
; and sent using the MS SMTP service.
; At the end of a day (or more precisely when the first record of a new day comes in) it emails the log from the day before.
;
; To send emails an outside SMTP service is used. If using MS SMTP Service, simply
; configure a smart-host in the SMTP service config, create a template text containing the following three lines PLUS A RETURN:
; To: XXX@somewhere.com
; From: XXX@mydomain.com
; Subject: Put Some TExt Here

; Save the text file and update sMailTemplate911 or sMailTemplateLog with its location.
; Lastly configure sMailPath to be the SMTP pickup directory which by default is
; c:\inetpub\mailroot\pickup

; All the random number nonsense in the log and 911 mail procedures is to make sure each mail message and temporary file gets a unique name.
; This is incase the SMTP service isnt working or two records are created between polls, they will not have the same filename

; This script uses fopen, fput, fclose for EVERY LINE IN THE LOG. This means that there are lots of file accesses.
; For my application (a log entry every 30 seconds or so and logging 100k-200K a day) this is not a problem and I have stress-tested it. However test VERY carefully
; if used in situations where log files get into the megabytes a day due to . YOU HAVE BEEN WARNED!!!

; I run this as an NT service and it works fine. For information on how to do it look at ; If you know how the registry works and how services work you should be able to do it.


; Where possible all variable are set at the top of proc main, however sFileName and sLogPath also need to be changed


; Warning IANAP (I Am Not A Programmer). This should work but is
; probably not the best way, and certainly not the only way, of doing this. In particular there is NO ERROR HANDLING!! Again you have
; been warned.
; This script is a combination of a script/methods by Bill Hillman (found at ; a script found at and a lot of work by myself.


; Disclaimer: I hope that this script is valuable to you.
; Your use of this script, however, is at your sole risk.
; This script is provided "as -is", without any warranty, whether express or implied,
; of its accuracy, completeness, fitness for a particular purpose, title or non-infringement,
; Neither myself or anyone else shall be liable for any damages you may sustain by using this script,
; whether direct, indirect, special, incidental or consequential, even if it has
; been advised of the possibility of such damages.
; Neither myself, my wife, our unborn children, my dog, my wifes 2 cats, or any other person, group, or company
; either living, dead, or ficticious, can be held responsible for what you do with this script.

; For more info on Aspect scripting I highly reccomend
;;;




string sFileName ;first part of file name
string sDate ;this is the date part of file name
string sDataHold
string sLogPath ;file_path
string sBegin911 ; String that begins a 911 call record
string sEnd911 ; String that ends a 911 call record
integer iRecord_Over_911
string sMail_Path ; Path to SMTP service pickup dir
string sMailName ; Name of 911 mail message
string sRandomText ; used to create unique filenames for mail
string sTempPath
string sTempPathlog
string sMailTemplate911
string sMailTemplateLog
string s911LogFile
string sCurrentData
string sLastLogName ; This stores the last log filename
string sTempFileName
String sTempLogPath
string sTempMail
string sTempPath911

proc main
dial data "PBX"

; Global Variables EDIT PER YOUR SITE

sBegin911 = "911 CALL RECORD" ; This is the first line of a 911 call record
sEnd911 = "RECORD END" ; This is the end of a 911 Call record
sFileName = "PBX_log_ " ;put the first part of name here leave a space at the end.
sLogPath = "D:\VOIP_Logs\" ; Put the root log directory here
sMail_Path = "C:\Inetpub\mailroot\Pickup\" ; This is the path to the SMTP pickup directory
sTempPath = "d:\voip_logs" ; This is the path were temporary files should be created
s911LogFile = "d:\VOIP_logs\911_log.txt" ; This is the log file for 911 calls
sMailTemplate911 = "d:\voip_logs\911mailtemplate.txt" ; Template file for 911 mail (should include at least TO:, from, and Subject)
sMailTemplateLog = "d:\voip_logs\log_mailtemplate.txt" ; Template file for log mail (should include at least TO:, from, and Subject)
sTempPath = "d:\voip_logs\" ; This is the path were temporary files should be created
sMailName = "log_email.txt" ; Email text file name. This really doesnt matter as the text file should be deleted


;Pause 60 ; pauses 60 seconds to give server time to boot
clear
;set aspect rgetchar 13
sLastLogName = "" ;set to "" when script starts


while 1
sDataHold = ""
sDate = "" ;to ensure string sDate is empty
sTempFileName = sFileName
sTempLogPath = sLogPath
call file_date ;calls file_date proc
strcat sTempFileName sDate
strcat sTempLogPath sTempFileName ;this combines full name and path to map out the full log file path.
rget sDataHold 256
fopen 0 sTempLogPath append
fputs 0 sDataHold
fclose 0
strfind sDataHold sBegin911 ;Check to see if last line was the beginning of a 911 call record
if Success
call Email_911
else
endif

if strnicmp sLastLogName sTempLogPath 255 ; Check to see if the log file name has changed (ie date has changed)

elseif strnicmp sLastLogName "" 255 ; If lastLog has no value, logging just started
sLastLogName = sTempLogPath ; set sLastLogName
else ; Date must have changed, email the log
call email_log ; call procedure to email log file
sLastLogName = sTempLogPath ; set sLastLogName
endif

endwhile
endproc
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




proc file_date
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Procedure to get the date part
;; of a filename
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


integer iDay, iMonth, iYear, iHour, iMin, iSec
ltimeints $LTIME iYear iMonth iDay iHour iMin iSec
strfmt sDate "%d%02d%02d" iYear iMonth iDay
strcat sDate ".TXT"
endproc




proc Email_911

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This function Writes 911 calls to the 911 log and creates
;; an email in the MS SMTP service pickup directory
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

integer iNumber ; Random number.
integer iSeed ; Seed for better randomization.
rand iSeed ; Get a random integer.
rand iNumber iSeed
numtostr iNumber sRandomText 10
sMailName = ".txt"
strcat sRandomText sMailName
iRecord_Over_911 = 0
sTempPath911 = sTempPath
strcat sTempPath sRandomText ; Create filename for temporary file
strcat sMail_Path sRandomText ; Create filename for mail file
copyfile sMailTemplate911 sTempPath
iRecord_Over_911 = 0 ; set record over 911 to 0
fopen 0 s911LogFile append
fputs 0 sdataHold
fclose 0


while iRecord_Over_911 != 1
strfind sDataHold "RECORD END" ; Are we at the end of the record yet????

if Success ; If 911 record ends, close the log and email, exit back to main proc
iRecord_Over_911 = 1 ; Specify end of record
copyfile sTempPath sMail_Path
delfile sTempPath
else ; if record hasnt closed yet, keep writing
rget sDataHold 256
sCurrentData = sDataHold
fopen 0 s911LogFile append
fputs 0 sCurrentData
fclose 0
fopen 1 sTempPath append
fputs 1 sCurrentData
fclose 1
fopen 2 sTempLogPath append
fputs 2 sCurrentdata
fclose 2
iRecord_Over_911 = 0 ; 911 record not done yet
endif
endwhile
endproc



proc email_log
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This procedure emails log files. Note it DOES NOT attach log files in an email
;; it puts the log file text as the body of the message
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;




integer Number ; Random number.
integer Seed ; Seed for better randomization.
rand Seed ; Get a random integer.
rand Number Seed
sTempPathLog = sTempPath
sRandomText = ""
numtostr Number sRandomText 10
sTempMail = sMail_Path
strcat sTempPathlog sRandomText ;Get the full path
strcat sTempPathlog sMailName ;for the temp file
strcat sTempMail sRandomText ; Get the full
strcat sTempMail sMailName ; path for the mail file
copyfile sMailTemplateLog sTempPathlog ;
filetoclip TEXT sLastLogName
cliptofile TEXT sTempPathLog APPEND
copyfile sTempPathLog sTempmail
delfile sTempPathLog
endproc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top