I am trying to monitor the content of a logfile, using a File Content Sensor.

My problem is that our server creates logfiles with the date encoded as part of the filename, e.g. MyServerLog_dd_mm_yyyy.txt.

Is there anyway of monitoring these logfiles by changing the filename dynamically as the sensor executes ?


Article Comments

Hi,

Thank you for your question. Unfortunately, that is not possible, the File Content Sensor (https://www.paessler.com/manuals/prtg/file_content_sensor) is only able to check one predefined file. But maybe you can use tools like robocopy to copy the recent log version to a "log_temp.txt"? Our sensor could then look into this file.


Feb, 2015 - Permalink

For anyone else looking into this, you can do something similar to what Severin suggested using a batch file and then call that batch file every so often with task manager. The below will scan through all logs in the directory that start with "logfile_" (example logfile_10-24-2017.txt, logfile_11-01-2017.txt) copy the newest one and save it in the same directory as temp_log.txt

Batch file:

rem get latest file:
for /f "tokens=*" %%i in ('dir "c:\logs\logfile_*.txt" /s /od /b') do set a=%%i
rem copy it:
copy "%a%" "c:\logs\temp_log.txt"

May, 2017 - Permalink

That's a sample how you can do it with powershell. It's a logfile with an dynamic name, it changes every day with the actual date of the day. In the file it will search the last 20 rows for the word "error".

$date = get-date -format "yyyyMMdd"
$file = "servername\d$\staticname_" + $date + ".log"

$string = Get-Content $file | select -Last 20
$last = Get-Content $file | select -Last 1

if ($string -like "*error*")
    {
    write-host "<prtg>"
    write-host "<result>"
    write-host "<channel>Log Alarm</channel>"
    write-host "<value>2</value>"
    write-host "</result>"
    write-host "<text>Error in logfile</text>"
    write-host "</prtg>"
    }
else
    {
    write-host "<prtg>"
    write-host "<result>"
    write-host "<channel>Log Alarm</channel>"
    write-host "<value>0</value>"
    write-host "</result>"
    write-host "<text>Logfile OK - $last</text>"
    write-host "</prtg>"
    }

Oct, 2018 - Permalink

Hi there,

Thank you for the addition, will surely come in handy for some customers.

Best regards.


Oct, 2018 - Permalink

Can PRTG team modify File-Contrent sensor with third response's suggestion? It is pretty useful.


Sep, 2020 - Permalink

Hello,

Thank you for your interest in this sensor.

I shared your request with our developers but such feature is not planned (yet). The PowerShell scripts provided above remain the best options, at least for now.

Kind regards.


Sep, 2020 - Permalink