简体   繁体   中英

Bash - Calculating with given date

I want to do a check in Icinga, with a given date. The given date have his own formation, so I can change it only in Batch, what I tried.

So Bash have to give me a critical when the fDate is to 7 days under the actuall date and Bash have to give me a warning when fDate is to 21 days unter the actuall date.

Here's my Script:


#Actuall date in format from the Fortigate
LANG=en_us_8859_1 
aDate=`date +"%a %b %d %Y"`
aDateOW=`date +"%b/%d/%Y"`
#aDateOM=`date +"%d %Y"`
echo $aDate Actuall date with weekday/month
echo $aDateOW Actuall date without weekday with slash
#echo $aDateOM Actuall date without weekday/month
echo -------------------------------------------------------


#Date from Fortigate (webfilter-expliration) 
#testdate manuell declared
fDate="Sun Oct 27 2019"
fDateOW="${fDate:4:11}"
fDateOW="${fDateOW//[ ]//}"
echo $fDateOW Date from Fortigate with slash
date -d 'fDateOW 7 days'
echo $date test
#fDateOM="${fDate:8:8}"
echo $fDate Date from Fortigate with weekday/month
echo $fDateOW Date from Fortigate without weekday
#echo $fDateOM Date from Fortigate without weekday/month
echo -------------------------------------------------------


#Exit Codes Icinga
#ok=0
#warn=1
#crit=2
#unknown=3

if [[ "$aDateOW" < "$fDateOW" ]] 
then 
  echo ok
#  exit 0
elif
$fDateOW -v-7d
  [[ "$aDateOW" > "$fDateOW" ]]
then
  echo ok
elif 
  [[ "$aDateOW" == "$fDateOW" ]]
then
  echo warning
#  exit 1
elif 
  [[ "$aDateOW" > "$fDateOW" ]]
then  
  echo critical
#  exit 2
fi
echo $fDateOW

You have to format the date to a unix time stamp (or epoch). This is the amount of seconds since 1 Jan 1970. Below a small example.

#! /bin/bash

tomorrow=$(date -d "+1 days")
yesterday=$(date -d "-1 days")

t0=$(date -d "${tomorrow}" "+%s")
t1=$(date -d "${yesterday}" "+%s")

echo $t0, $t1

if [ $t0 -lt $t1 ]; then
        echo "Tomorrow is before yesterday"
else
        echo "Yesterday is before tomorrow"
fi
exit 0

Note that the epoch will overflow one second after 03:14:07 UTC 2038-01-19. Read this if you're interested:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM