Commit ef4449eb authored by Eduardo L. Buratti's avatar Eduardo L. Buratti

collect: Change timestamp sent to server from maximum to mean time

bandwidth-usage.tree: Output the mean time of the five minutes
intervals instead of the maximum due to recent changes in the database.
Signed-off-by: Eduardo L. Buratti's avatarEduardo L. Buratti <elb09@c3sl.ufpr.br>
parent ee3eac8d
......@@ -20,26 +20,28 @@
# USA.
#------------------------------------------------------------------------------
# Function: increase
# Function used to increment the counter used in xml_data_generator.
function increase()
# Function: increment_time
# Increment the time counter by 5 minutes
function increment_time()
{
if test $MINUTE -eq 55; then
MINUTE=0
((HOUR=${HOUR}+1))
else
((MINUTE=${MINUTE}+5))
TRAFFIC_MINUTE=$((TRAFFIC_MINUTE + 5))
if test $TRAFFIC_MINUTE -ge 60; then
TRAFFIC_HOUR=$((TRAFFIC_HOUR + 1))
TRAFFIC_MINUTE=2
fi
}
# Function: print
# Function: xml_print
# Function that prints the contents of XML when there are collected
# data. Print the statistics according to the collection.
function print()
function xml_print()
{
printf "<netuse id=\"$COUNT\">"
printf "<date value=\"$FILE_DATE\" type=\"string\"/>"
printf "<time value=\"$HOUR_COLLECT:$MINUTE_COLLECT\" type=\"string\"/>"
local TRAFFIC_TIME=$(printf "%02d:%02d:30" $TRAFFIC_HOUR $TRAFFIC_MINUTE)
printf "<netuse id=\"$((COUNT+1))\">"
printf "<date value=\"$TRAFFIC_DATE\" type=\"string\"/>"
printf "<time value=\"$TRAFFIC_TIME\" type=\"string\"/>"
printf "<rx>"
printf "<packets value=\"$(awk '{ print $2 }' <<< $line)\" type=\"int\"/>"
printf "<bytes value=\"$(awk '{ print $3 }' <<< $line)\" type=\"int\"/>"
......@@ -49,17 +51,18 @@ function print()
printf "<bytes value=\"$(awk '{ print $5 }' <<< $line)\" type=\"int\"/>"
printf "</tx>"
printf "</netuse>"
((COUNT = ${COUNT} + 1))
}
# Function: print_null
# Function: xml_print_zero
# Function that prints the contents of XML when there was no collected
# data. Print the statistics with zeroed content.
function print_null()
function xml_print_zero()
{
printf "<netuse id=\"$COUNT\">"
printf "<date value=\"$FILE_DATE\" type=\"string\"/>"
printf "<time value=\"$HOUR:$MINUTE\" type=\"string\"/>"
local TRAFFIC_TIME=$(printf "%02d:%02d:30" $TRAFFIC_HOUR $TRAFFIC_MINUTE)
printf "<netuse id=\"$((COUNT+1))\">"
printf "<date value=\"$TRAFFIC_DATE\" type=\"string\"/>"
printf "<time value=\"$TRAFFIC_TIME\" type=\"string\"/>"
printf "<rx>"
printf "<packets value=\"0\" type=\"int\"/>"
printf "<bytes value=\"0\" type=\"int\"/>"
......@@ -69,64 +72,6 @@ function print_null()
printf "<bytes value=\"0\" type=\"int\"/>"
printf "</tx>"
printf "</netuse>"
((COUNT=${COUNT}+1))
}
# Function: generator
# Function responsible for generating an XML containing the collected
# data. Assign zero to the time tags which there wasn't collection.
function generator()
{
TMP="$PREFIX/../../../tmp/usage-tmp"
COUNT=1
HOUR=0
MINUTE=5
END_HOUR=24
cat $PREVIOUS_TRAFFIC | tail -n +2 > $TMP
FILE_DATE=$(head -1 $PREVIOUS_TRAFFIC)
while read line; do
TIMESTAMP=$(cut -d" " -f1 <<< $line)
HOUR_COLLECT=$(cut -d":" -f1 <<< "$TIMESTAMP")
MINUTE_COLLECT=$(cut -d":" -f2 <<< "$TIMESTAMP")
# Exchange the "0" of midnight by "24"
if test $HOUR_COLLECT -eq 0 -a $MINUTE_COLLECT -eq 0; then
HOUR_COLLECT=24
fi
# If the time of the collected data is the same counter, just prints
# and increments the counter
if test $HOUR_COLLECT -eq $HOUR -a $MINUTE_COLLECT -eq $MINUTE; then
print
increase
# else print null until the counter reaches the value of collected data
else
until test $HOUR_COLLECT -eq $HOUR \
-a $MINUTE_COLLECT -eq $MINUTE -o $HOUR -ge 24 ; do
print_null
increase
done
if test $HOUR_COLLECT -eq $HOUR -a $MINUTE_COLLECT -eq $MINUTE; then
print
fi
increase
fi
done < $TMP
rm -rf $TMP
# If the last line of the file isn't 00:00 (24:00), then zero out until
# 24:00
if test $HOUR -lt 24; then
until test $HOUR -eq 24 -a $MINUTE -eq 5; do
print_null
increase
done
fi
}
# Function: xml_data_generator
......@@ -135,15 +80,52 @@ function generator()
# data to the XML file.
function xml_data_generator()
{
# If previous-traffic.txt exists, check if the date of last modification
# is the current date or not. If equals, means the XML data can be
# generated
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 -eq $CURRENT_DATE; then
generator
local CURRENT_TIMESTAMP=$(date -d"$(date +%F)" +%s)
local TRAFFIC_TIMESTAMP=$(date -d"$(head -n1 $TRAFFIC)" +%s)
# Check if date in TRAFFIC file aint older than 15 days
if test $TRAFFIC_TIMESTAMP -ge $(($CURRENT_TIMESTAMP - 1296000)); then
# Since we want the mean time between the five minutes intervals,
# the minute counter starts at 2 and gets 5 minutes increments.
# Also the "seconds counter" is fixed at 30. So we always get
# something like HH:2:30 or HH:7:30 (mean times).
#
# Example: if interval = 10:15~10:20 => mean = 10:17:30
TRAFFIC_HOUR=0
TRAFFIC_MINUTE=2
COUNT=-1
while read line; do
# Check if its the first line (containing the date)
if test $COUNT -eq -1; then
TRAFFIC_DATE=$(head -1 $TRAFFIC)
COUNT=$((${COUNT} + 1))
else
INTERVALID=$(cut -d" " -f1 <<< $line)
while test $COUNT -lt $INTERVALID -a $COUNT -lt 288; do
xml_print_zero
COUNT=$((${COUNT} + 1))
increment_time
done
if test $COUNT -eq $INTERVALID -a $COUNT -lt 288; then
xml_print
COUNT=$((${COUNT} + 1))
increment_time
fi
fi
done < $TRAFFIC
while test $COUNT -lt 288; do
xml_print_zero
COUNT=$((${COUNT} + 1))
increment_time
done
fi
printf "\n"
}
......@@ -160,10 +142,10 @@ if test "$(bash $PROJECT)" != "0" -o "${FIRST_EXECUTION}"; then
exit 0
fi
PREVIOUS_TRAFFIC="${PREFIX}/../../../net/previous-traffic.txt"
TRAFFIC="${PREFIX}/../../../net/previous-traffic.txt"
test -f "${PREVIOUS_TRAFFIC}" || exit 0
test -s $PREVIOUS_TRAFFIC || exit 0
test -f "${TRAFFIC}" || exit 0
test -s $TRAFFIC || exit 0
# Call the function xml_data_generator to generate the data to XML
xml_data_generator
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