MatsonWatson
MatsonWatson

Reputation: 65

Why there is an epoch time from empty value?

I'm trying to improve a monitor that reads log file and sends a notification if an application has stopped running and logging. To avoid getting notifications from moments when log file rotates and app hasn't logged anything I added a check to see if the oldest log line is older than two minutes.

Here's a snippet of the important part.

<bash lines and variable setting>
LATEST=$(gawk 'match($0, /\[#\|(.*[0-9]*?)T(.*[0-9]*?)\+.*<AppToMonitor>/, m) {print m[1], m[2];} ' $LOG_TO_MONITOR | tail -1 ) 
OLDEST=$(gawk 'match($0, /\[#\|(.*[0-9]*?)T(.*[0-9]*?)\+.*INFO/, m) {print m[1], m[2];} ' $LOG_TO_MONITOR | head -1)

if [ -z "$LATEST" ]
then
      # no line in log
                OLDEST_EPOCH=`(date --date="$OLDEST" +%s)`
                CURR_MINUS_TWO=$(date +"%Y-%m-%d %T" -d "2 mins ago")
                CURR_MINUS_TWO_EPOCH=`(date --date="$CURR_MINUS_TWO" +%s)`

                # If oldest log line is over two minutes old
                if [[ "$OLDEST_EPOCH" -lt "$CURR_MINUS_TWO_EPOCH" ]]
                then
                 log "No lines found."
                 <send notification>
                else
                 log "Server.log rotated."
                fi
<else and stuff>

I still got some notifications when log rotated and the culprit reason was that the epoch time was taken from totally empty log file. I tested this by creating empty .log-file with touch test.log, then setting EMPTY=$(gawk 'match($0, /\[#\|(.*[0-9]*?)T(.*[0-9]*?)\+.*INFO/, m) {print m[1], m[2];} ' /home/USER/test.log | head -1) Now, if I echo $EMPTY, I get a blank line. But, if I convert this empty line to epoch time EPOCHEMPTY=`(date --date="$EMPTY" +%s)` I get an epoch time 1584914400 from echo. This refers to yesterday evening. Apparently, this same epoch comes every time an empty date is converted to epoch time, like replacing "$EMPTY" with "", at least while writing this.

So the question is, what is this epoch time from empty line? When the if-statement makes the comparison with this value it triggers the notification even though it should not. Is there a way to avoid taking empty string to comparison but some else time value from the log file?

Upvotes: 1

Views: 485

Answers (1)

Aaron
Aaron

Reputation: 24812

date's manual defines that an empty string passed to -d will be considered as the start of the day.

You could however rely on the -f/--file option and process substitution :

date -f <(echo -n "$your_date")

The -f option lets you pass a file as parameters, each line of which will be treated as an input for -d. An empty file will just return an empty output.

The process substitution is used to create on the fly a ephemeral file (an anonymous pipe to be precise, but that's still a file) that will only contain the content of your variable, that is either an empty file if the variable is undefined or the empty string, and a single line otherwise.

Upvotes: 1

Related Questions