An application team approached us and wanted us to monitor a log file. No problem! However, when we got a look at the log file we discovered that log file was set up to be a human-readable table -- complete with headers, columns and rows. The application team wanted us to check on a value in a specific column each time the file was updated (every 5 minutes). Being relatively new to PowerShell I reached out to one of the other teams under our director and they came up with the idea of casting the log file into an $array and using the $split() function to break it apart into individual elements. After a little testing and tweaking this is what we came up with. (And, yes, I will post this to the Content Exchange once I get it sanitized.)
I am sure there are lots of other more efficient, complex or down-right interesting ways to do this but this one worked for us.
Script Arguments
$args[0] A unique identifier to the log file -- in this case there were multiple servers writing logs to a single log file directory -- with the format "Performance_HOSTNAME_LOGFILEDATE.log"
$args[1] Numeric threshold for the value in the specific column
Execution Mode
Local Host (Although this is a PowerShell script the only connectivity it makes to the remote host is via a UNC path to check the file. No remote server WinRM setup required)
$startDate = get-date -Format "yyyy-MM-dd"
$hostname=$args[0] #Node Name is the first in the Script Arguments
$perfFileLoc="\\${Node.SysName}\d$\INSERT UNC PATH without FILENAME"
while(1){
$perfLogName="$perfFileLoc\Performance_$hostname"+"_$startDate.log"
if (Test-Path $perfLogName){ #Tests for the existence of the file at the specified path and fails with a DOWN status Exit 1 if it does not exist
$sel= Get-Content $perfLogName | Select-Object -last 1 #Grabs the last line of the log file and assigns it to the variable
$array = $sel.Split() #Splits the $Sel variable in an array based on the space as the delimiter ... you can use "'t" if you want to use a tab-delimited file
$split = $array[8] #Selects the 8th value in the array which happened to correspond with our DocSessions variable we needed to check
if ($Split -ge $args[1]) #Is DocSessions greater than or equal to $args[1]
{
Write-Host "Message: DocSessions are greater than or equal to "$args[1]
Write-Host "Statistic: " ($Split)
Exit 3; #Exit with CRITICAL status
}
Write-Host "Message: DocSessions are OK"
Write-Host "Statistic: " ($Split)
Exit 0; #Exit with UP status
}
else
{
Write-Host "Message: $perfLogName file does not exist"
Write-Host "Statistic: 0"
Exit 1; #Exit with DOWN status
}#end of if and else
}#end while loop