3

I have written a script that notifies me when a value is not within a given range. All values "out of range" are logged in a set of per day files.

Every line is timestamped in a proprietary reverse way: yyyymmddHHMMSS

Now, I would like to refine the script, and receive notifications just when at least 60 minutes are passed since the last notification for the given out of range value.

I already solved the issue to print the logs in reverse ordered way with:

for i in $(ls -t /var/log/logfolder/*); do zcat $i|tac|grep \!\!\!|grep --color KEYFORVALUE; done

that results in:

...
20170817041001 - WARNING: KEYFORVALUE=252.36 is not between 225 and 245 (!!!)
20170817040001 - WARNING: KEYFORVALUE=254.35 is not between 225 and 245 (!!!)
20170817035001 - WARNING: KEYFORVALUE=254.55 is not between 225 and 245 (!!!)
20170817034001 - WARNING: KEYFORVALUE=254.58 is not between 225 and 245 (!!!)
20170817033001 - WARNING: KEYFORVALUE=255.32 is not between 225 and 245 (!!!)
20170817032001 - WARNING: KEYFORVALUE=254.99 is not between 225 and 245 (!!!)
20170817031001 - WARNING: KEYFORVALUE=255.95 is not between 225 and 245 (!!!)
20170817030001 - WARNING: KEYFORVALUE=255.43 is not between 225 and 245 (!!!)
20170817025001 - WARNING: KEYFORVALUE=255.26 is not between 225 and 245 (!!!)
20170817024001 - WARNING: KEYFORVALUE=255.42 is not between 225 and 245 (!!!)
20170817012001 - WARNING: KEYFORVALUE=252.04 is not between 225 and 245 (!!!)
...

Anyway, I'm stuck at calculating the number of seconds between two of those timestamps, for instance:

20170817040001
20160312000101

What should I do in order to calculate the time elapsed between two timestamps?

  • Are you looking for time delta expressed in a particular format, I.E. "HH:MM:SS"? Also, why not just log UNIX timestamps? – 111--- Aug 18 '17 at 16:21
  • 1
    @Marco see the link in my comment for several ways to calculate the unix timestamp. – Jesse_b Aug 18 '17 at 16:29
  • 1
    @Jesse_b found it, TY! – Marco Aug 18 '17 at 16:32
  • 1
    Oh I also just noticed you are parsing ls in your script. You shouldn't do this. You can do for i in "/var/log/logfolder/*"; instead. mywiki.wooledge.org/ParsingLs – Jesse_b Aug 18 '17 at 16:43
  • 1
    @Jesse_b that's not really any different to using ls -t; you're still parsing the output of a command that generates a list of filenames. (Oh, -name "*" is almost a no-op; you wouldn't need it here. And how would sort -n be able to sort file names numerically?) – roaima Aug 18 '17 at 21:44
7

This will give you the date in seconds (since the UNIX epoch)

date --date '2017-08-17 04:00:01' +%s    # "1502938801"

And this will give you the date as a readable string from a number of seconds

date --date '@1502938801'    # "17 Aug 2017 04:00:01"

So all that's needed is to convert your date/timestamp into a format that GNU date can understand, use maths to determine the difference, and output the result

datetime1=20170817040001
datetime2=20160312000101

seconds1=$(date --date "$(echo "$datetime1" | sed -nr 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3 \4:\5:\6/p')" +%s)
seconds2=$(date --date "$(echo "$datetime2" | sed -nr 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3 \4:\5:\6/p')" +%s)

delta=$((seconds1 - seconds2))
echo "$delta seconds"    # "45197940 seconds"

We've not provided timezone information here so it assumes local timezone. Your values for the seconds from the datetime will probably be different to mine. (If your values are UTC then you can use date --utc.)

  • When I started writing this post, I was almost sure it would have ended with sed doing the dirty job. TY @roaima – Marco Aug 18 '17 at 16:44
  • @Marco you could use bash string parsing but it seems to work out as being more complex. echo "${datetime1:0:4}-${datetime1:4:2}-${datetime1:6:2}..." – roaima Dec 4 '19 at 20:00
4

This is easy with datediff command provided in dateutils package.

ddiff -i '%Y%m%d%H%M%S' 20170817040001 20160312000101
  • this on ubuntu 16.04 LTS user@host:/~# apt-get install dateutils [...] Setting up dateutils (0.2.5-1) ... user@host:/~# datediff datediff: command not found user@host:/~# – Marco Aug 18 '17 at 19:28
  • sorry, the command-line command is ddiff – αғsнιη Aug 18 '17 at 19:35
  • 3
    Or dateutils.ddiff – MatsK Aug 18 '17 at 20:40
  • This solution is very intresting too, but I keep the sed one in my script; as I find it's most compatible with any GNU Linux and saves me one more dependency. – Marco Aug 19 '17 at 6:32
  • Also, please note that the correct name for this command in Linux Ubuntu 16.04 is dateutils.ddiff – Marco Aug 19 '17 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.