Commit 483e19b2 authored by Eduardo L. Buratti's avatar Eduardo L. Buratti

collect: Fix date_comparator function in bandwidth.sh

The function date_comparator now checks for the date stored in the traffic file
rather than previous-traffic creation date, leading to a cleaner code.

Also adding a test to check if the current time is 00:00, in which case
the data collected belongs to the day before and should not trigger the
creation of a new traffic and previous-traffic.
Signed-off-by: Eduardo L. Buratti's avatarEduardo L. Buratti <elb09@c3sl.ufpr.br>
parent 3e83a5e7
......@@ -24,32 +24,18 @@
# This function compares the dates of the collected data files.
function date_comparator()
{
# If traffic.txt file doesn't exist, this may the first time the script is
# running. Thus, traffic.txt and previous-traffic.txt files are created.
# The first line of traffic.txt contains the date of the first execution of
# script
if ! test -f "$TRAFFIC"; then
echo $(date +%F) > $TRAFFIC
touch $PREVIOUS_TRAFFIC
else
# If previous-traffic.txt was deleted, copy the contents of traffic.txt to
# previous-traffic.txt. This is necessary to avoid FILE_DATE returning a
# failure
if ! test -f "$PREVIOUS_TRAFFIC"; then
exit 0
fi
CURRENT_DATE=$(date -d"$(date +%F)" +%s)
TRAFFIC_DATE=$(date -d"$(head -n1 $TRAFFIC)" +%s)
if test $CURRENT_DATE -ne $TRAFFIC_DATE; then
if ! test "$TIMESTAMP" = "00:00"; then
cp $TRAFFIC $PREVIOUS_TRAFFIC
echo $(date +%F) > $TRAFFIC
# If previous-traffic.txt exists, check if the last modification date is the
# current date or not. If it's the second case, it means it's a new day and
# traffic.txt will be copied to previous-traffic.txt. traffic.txt will
# contain the new collected data
else
FILE_DATE=$(date -d"$(ls -l --time-style="+%F" \
$PREVIOUS_TRAFFIC | awk '{ print $6 }')" +%s)
CURRENT_DATE=$(date -d"$(date +%F)" +%s)
if test $FILE_DATE -ne $CURRENT_DATE; then
cp $TRAFFIC $PREVIOUS_TRAFFIC
echo $(date +%F) > $TRAFFIC
fi
fi
fi
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment