E
E
Egor Semenov2018-09-14 13:44:08
linux
Egor Semenov, 2018-09-14 13:44:08

How to use awk to calculate the date difference without losing the whole "table"?

The command displays which tasks are currently running and when these tasks started.

# bjobs -u all -o "jobid user stat SUBMIT_TIME"
JOBID USER STAT SUBMIT_TIME
16188 chemicalinorg_serov RUN Aug 29 12:04
16286 aerohydromech RUN Sep 10 11:35
16287 ifmbmdcam RUN Sep 10 16:03
16283 KozlovaAS RUN Sep  8 13:32
16307 chemicalorg RUN Sep 13 14:37
16306 KozlovaAS RUN Sep 13 14:31

It is required to fasten monitoring on zabbix so that messages are sent when the task takes too long, more than 10 days. That is, you need to analyze columns 4 and 5, compare with the current date, and send an alert in case of a gap of more than 10 days. I didn’t come up with anything smarter than how to multiply months by 30, add days and get the number of days from the new year
#DATENOW=$(date +%m:%d | awk -F ":" '{print ($1*30+$2)}')
#echo $DATENOW
284

By the same logic, I work with the output:
# bjobs -u all -o "jobid user stat SUBMIT_TIME" | tail -n +2 | awk ' (system("date +%m:%d --date=\""$4 $5"\""))' | awk -F ":" '{print ($1*30+$2)}'
269
280
280
278
283
283
284

now compare with our DATENOW variable
# bjobs -u all -o "jobid user stat SUBMIT_TIME" | tail -n +2 | awk ' (system("date +%m:%d --date=\""$4 $5"\""))' | awk -F ":" '(('$DATENOW'-($1*30+$2)) > 10)' 
08:29

That's right, the very first line "16188 chemicalinorg_serov RUN Aug 29 12:04" contains a date older than the current one (September 14) with a delta of more than 10 days.
I do not understand how to combine the date conversion (from Aug 29 to 08:29) and the subsequent calculation of days (awk -F ":" '{print ($1*30+$2)}') in one awk, and even without losing the first two columns (jobid user).
Tell me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Ratkin, 2018-09-14
@Hanharr

The date can still be transformed in this way, perhaps it will suggest
date -d "12:04 Aug 29" +%s

S
Saboteur, 2018-09-22
@saboteur_kiev

They said correctly above - convert the date to a timestamp and perform any comparisons.
For example, you can do this:

while read
do
  SUBMIT_DATE=$(echo $REPLY | awk -F "RUN" '{print $2}')
  declare -i DAYS=($(date "+%s")-$(date -d "$SUBMIT_DATE" "+%s"))/3600/24
  if ; then
    echo "This job was executed more than 10 days ago: $REPLY"
  fi
done <yourcommand

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question