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?
Best Answer
This will give you the date in seconds (since the UNIX epoch)
And this will give you the date as a readable string from a number of seconds
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 resultWe'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
.)