nanomonsetotse
 New Member Posts:4

 |
| 07 Feb 2010 04:03 AM |
|
Hello all
I am new to powershell scripting, so this may seem like a basic question, but I have an application running that writes to a log file every minute. The only way to tell if the app stops responding is if the log file does not get updated (it's an old, bad app).
So I'd like to monitor that log file and alert if it doesn't have an updated timestamp the next time it checks. We have a monitoring tool that I can set to run the job every x number of minutes, I just need the code to check the file, and alert if the next time it checks the timestamp has not changed.
Thanks in advance for any ideas and suggestions.
Gina |
|
|
|
|
GWHowarth88
 Basic Member Posts:125

 |
| 08 Feb 2010 10:33 AM |
|
What is the format of the log file? If, for example, the timestamp is the first line of the log file, it would just be a matter of reading the first line and storing it for future comparison with the next 'heartbeat'. $TimeStamp = (Get-Content -Path "PathToMyLog.log")[0] # Gets first line |
|
|
|
|
nanomonsetotse
 New Member Posts:4

 |
| 08 Feb 2010 03:54 PM |
|
That's a good idea.... but the log file writes the timestamp at the end... looks like this: 2010-02-08 09:50:50 AM -DEBUG- (NotificationDispatcher.ping 170) - PING sent Is there a way to read the last line of the file? |
|
|
|
|
GWHowarth88
 Basic Member Posts:125

 |
| 08 Feb 2010 04:09 PM |
|
There sure is:
$Line = (Get-Content -Path "PathToMyLog.log")[-1] # Gets last line $TimeStamp = $Line.Split(' ')[0] # Gets time stamp
(Edit: updated my answer) |
|
|
|
|
nanomonsetotse
 New Member Posts:4

 |
| 08 Feb 2010 10:33 PM |
|
Thanks! Could you also help me with how I can store this information for future use, like the next time the script runs? Sorry, my scripting inexperience is really showing through. |
|
|
|
|
sureshbabutadisetty
 New Member Posts:29

 |
| 09 Feb 2010 06:55 AM |
|
Simple this way: Log file full path is required as argument param ( [string] $logPath = $(throw "Log path is required as argument"), [int] $sleep = 300 # in seconds ) [DateTime] $lastWriteTime = 0 [DateTime] $latestWriteTime = 0 while ($true) { $latestWriteTime = (ls $logPath).LastWriteTime if ( ($latestWriteTime -$lastWriteTime) -eq 0 ) { ## Error Action } $lastWriteTime = $latestWriteTime sleep $sleep }
|
|
|
|
|
GWHowarth88
 Basic Member Posts:125

 |
| 09 Feb 2010 10:18 AM |
|
The easiest (and best) way would be to just use a text file. First, create a file called "LastWriteLog.log" in the same directory of the script, put a date string in it (e.g. 2010-02-09 11:15:43), then run the script: param ( [string] $logPath = $(throw "Log path is required as argument"), [int] $sleep = 300 # in seconds ) function Main { [DateTime] $lastWriteTime = _Get-LastWriteTime [DateTime] $latestWriteTime = _Get-LatestWriteTime if ($lastWriteTime -eq $latestWriteTime) { [DateTime] $now = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Write-Host "WARNING: Log file has not been updated!" -BackgroundColor Black -ForegroundColor Yellow Write-Host "Last write time: $lastWriteTime" -BackgroundColor Black -ForegroundColor Yellow Write-Host "Time now: $now" -BackgroundColor Black -ForegroundColor Yellow } else { $lastWriteTime = $latestWriteTime $lastWriteTime | Out-File -FilePath $lastWriteLogPath -Force # Overwrite previous last write time } Start-Sleep -Seconds $sleep Main # Calls itself for next check } function _Get-LastWriteTime { [string] $dateString = Get-Content -Path $lastWriteLogPath [DateTime] $lastWriteTime = [System.DateTime]::Parse($dateString) # Convert $dateString to standard System.DateTime format return $lastWriteTime } function _Get-LatestWriteTime { $logFile = Get-Item -Path $logPath [DateTime] $latestWriteTime = $logFile.LastWriteTime return $latestWriteTime } $ScriptLocation = Split-Path -Path $Script:MyInvocation.MyCommand.Path -Parent # Gets the location of this script $lastWriteLogPath = "$ScriptLocation\LastWriteLog.log" # Assumes that the log that holds the last write time is in the same directory of the script Main # Calls Main for first-time use If you run into any problems, let me know. |
|
|
|
|
nanomonsetotse
 New Member Posts:4

 |
| 23 Feb 2010 03:16 AM |
|
Perfect, thank you! Sorry for the huge time to respond, I've been away from work on vacation and finally am able to catch up and get back to this. I appreciate the timeliness of the response and the help. I'm going to work on this right now and I will let you know if I run into troubles. Thanks! |
|
|
|
|