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

logfile monitor

Status
Not open for further replies.

subok

MIS
Feb 21, 2005
37
BE
Hi,

I wonder if somebody can assist me. i'm looking for a script or batch file that i can run every hour or more. comparing the file timestamp from the previous check. if it's the same then a notification (e.g. email) is sent.

Thanks.

subok
 
I think the intention was to determine when log files updates were not occurring, in which case there would be nothing to include in the email. I guess that would be alarming in itself though. ;-)

Something like this perhaps?

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$prev_mtime[/blue]=[fuchsia]0[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$sleeptime[/blue]=[fuchsia]3600[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$email[/blue]=[red]'[/red][purple]someone@example.com[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$filename[/blue]=[red]'[/red][purple]/some/log.log[/purple][red]'[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red][red])[/red] [red]{[/red]
        [black][b]my[/b][/black] [red]([/red][blue]$dev[/blue],[blue]$ino[/blue],[blue]$mode[/blue],[blue]$nlink[/blue],[blue]$uid[/blue],[blue]$gid[/blue],[blue]$rdev[/blue],[blue]$size[/blue],[blue]$atime[/blue],[blue]$mtime[/blue],[blue]$ctime[/blue],[blue]$blksize[/blue],[blue]$blocks[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/stat.html][black][b]stat[/b][/black][/url][red]([/red][blue]$filename[/blue][red])[/red][red];[/red]
        [olive][b]if[/b][/olive] [red]([/red][blue]$mtime[/blue] == [blue]$prev_mtime[/blue][red])[/red] [red]{[/red]
                [url=http://perldoc.perl.org/functions/system.html][black][b]system[/b][/black][/url][red]([/red][red]'[/red][purple]echo | mailx -s "log file has stalled, last modified [/purple][red]'[/red] . [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red]([/red][blue]$mtime[/blue][red])[/red] . [red]'[/red][purple]" [/purple][red]'[/red] . [blue]$email[/blue][red])[/red][red];[/red]
        [red]}[/red]
        [blue]$prev_mtime[/blue] = [blue]$mtime[/blue] [red];[/red]
        [url=http://perldoc.perl.org/functions/sleep.html][black][b]sleep[/b][/black][/url][red]([/red][blue]$sleeptime[/blue][red])[/red][red];[/red]
[red]}[/red]

Annihilannic.
 
@Annihilannic, you're right i'm interested if updates are not occuring. Thanks for the feedback.

I thought of a new approach.. how about a notification in case a failed write/update to the logfile occurs?

thanks.
 
That depends on the behaviour of the application that updates the log file... if it doesn't send an error message somewhere (i.e. to standard error or some other log file) then there is nothing you can act on. Do you have control over the application as well, i.e. to modify its behaviour in that situation?

Annihilannic.
 
Yes, i have control on the application (written in PErl) I can adapt it but.. the problem is i'm a newbie.

thought of another approach.... in case there is an issue in the logfile.... e.g. attempted write access or delete.... would it be possible to send a notification or alarm to an email or a pop-up to a workstation?
 
Yes, you can send an email using a method similar to the one I described above.

Annihilannic.
 
If it's not a unix ,but a Windows system - then you would need a different mail sending method (like using "blat" utility). ex:

system("blat.exe \"$SYSTEMDRIVE\\tmp\\alert.txt\" -s \"$computer - report from $DATE\" -t \"$mailAddress\" -attach \"$all_csv\ >\"$TEMP\\blat\"");


Long live king Moshiach !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top