Bash script to compare date and time

I recently had the need to be able to compare the current time and date with an entry stored in a MySQL database. The script takes the standard date format from MySQL and converts the time to seconds. This conversion makes the comparison MUCH simpler.

The Script

I did my best to explain the script in the comments but if you have questions please post a comment and I’ll do my best to explain.

Below is the source code, you can also download a tar ball with the script, check_lastrun.tar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
# This script will return 0 if dates do not match
# 0 = fail
# Else the time difference in seconds is returned
# Then using something like Zabbix the trigger can be set based on the returned difference or if it's 0

lastrun=$(HOME=/var/lib/zabbix mysql -B -s -r -e "SELECT glpi_crontasks.lastrun FROM glpi_crontasks WHERE glpi_crontasks.name='mailgate'" itschelpGLPI)
lastrunDate=$(echo $lastrun | awk -F" " '{ print $1 }')
lastrunTime=$(echo $lastrun | awk -F" " '{ print $2 }')

# Get current date in fullyear-month-day ie 2011-04-25
currentDate=$(date +%Y-%m-%d)
# Get current time in 24-hour, minutes, seconds ie 20:15:30
currentTime=$(date +%H:%M:%S)
# Initalize datesMatch, assume they match
datesMatch="true"

function timeToSeconds {
        timeSeconds=0
        # Got the idea to convert colon to space here using bash string mangling
        # http://stackoverflow.com/questions/1406966/linux-shell-script-split-string-put-them-in-an-array-then-loop-through-them
        timeStripped=${1//:/$' '}

        # Using the multiplier variable to allow the for loop
        # To iterate in order to multiple hours then mninutes then seconds
        # So 3600 to 60 to 1
        multiplier=3600
        for item in $timeStripped
        do
                # This is a great reference for bash arithmetic
                # http://www.softpanorama.org/Scripting/Shellorama/arithmetic_expressions.shtml

                # Sanatize leading zero using globbing
                # if $item is 09 you get this error, value too great for base (error token is "09")
                item=${item#0}

                timeSeconds=$((timeSeconds + ((item*multiplier))))
                #iterate the seconds multiplier to next "level" ie, minute, hour...
                (( multiplier /= 60))
        done
}
# compare dates and return false if they do not match
# a false indicates the last run has not run as scheduled
function compareDates {
        if [ $currentDate != $lastrunDate ]
        then
                # Return 0 if they do not match
                datesMatch="false"
        fi
}

# If dates do not match then assume lastrun is overdue
compareDates
if [ $datesMatch == "false" ]
then
        echo 0
        exit
fi

# passing the current and lastrun times to timeToSeconds() functions
# takes the returned value and stores it for each
timeToSeconds "$lastrunTime"
lastrunSeconds=$timeSeconds
timeToSeconds "$currentTime"
currentTimeSeconds=$timeSeconds

# Checks to validate
#       echo "Current time seconds " . $currentTimeSeconds
#       echo "Last run time seconds " . $lastrunSeconds

# This calculates the time difference between now and last run
# So we can output it to the program calling this function
timeDiff=$((currentTimeSeconds - lastrunSeconds))

# This checks if the time diff is zero
# This is to keep a valid lastrun time of 0 from making zabbix think
# zero was returned due to failure
if [ $timeDiff == "0" ]
then
        echo 1
        exit
fi

echo $timeDiff

exit

Real World Application

I applied this script to the immediate need of having Zabbix do a check to make sure the mail inbox for GLPI is indeed checked. Zabbix is an amazingly powerful network monitoring tool. GLPI is the current support ticket system and inventory system for the College of Liberal Arts. In GLPI there is an automatic action to check the helpdesk’s email account for new email on a regular basis. For our setup that check is done every 5 minutes. Recently I noticed that the check somehow got stuck in queue for about 2 hours which is a long time considering the volume of emails this account gets. So I developed this script so I could check the GLPI mail check’s last runtime in its MySQL database to verify it has indeed run.

Comments (0)

› No comments yet.

Leave a Reply


*

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.