Commit 3e83a5e7 authored by Eduardo L. Buratti's avatar Eduardo L. Buratti

collect: Fix bug with multiple interfaces in net/bandwidth.sh

Bug description: when there are multiple interfaces connected to WAN,
the net/bandwidth.sh script collects data only from the first one.

Also, due to the changes made, the program net/discover-network is no
longer needed and therefore removed.
Signed-off-by: Eduardo L. Buratti's avatarEduardo L. Buratti <elb09@c3sl.ufpr.br>
parent c7bf8b3d
......@@ -67,7 +67,6 @@ if test "$(bash $PROJECT)" != "0" || ! sudo -v &> /dev/null; then
exit 0
fi
# Change to the script directory
cd $PREFIX
# Collected data from the current day
......@@ -76,106 +75,65 @@ TRAFFIC="./traffic.txt"
# Collected data on the last day before the machine is disconnected
PREVIOUS_TRAFFIC="./previous-traffic.txt"
# Interfaces that have "Destination" the network 0.0.0.0
# Stored interfaces from the last run
INTERFACES="./interfaces"
# Receive the address(es) and mask(s) filtered from "interfaces"
ADDR_MASK="./addr-mask"
# Used in the control of iptables rules. To verify if interfaces have not been
# added or removed
PREVIOUS_NETWORKS="./previous-networks"
NETWORKS="./networks"
# Find binaries
IP=$(which ip)
test $? -ne 0 && IP="/sbin/ip"
# Temporary
TMP1="./tmp1"
TMP2="./tmp2"
TMP3="./tmp3"
IPTABLES=$(which iptables)
test $? -ne 0 && IPTABLES="/sbin/iptables"
test -x $IP || exit 1
test -x $IPTABLES || exit 1
# Comments of iptables rules
INPUT="INPUT_COUNTS"
OUTPUT="OUTPUT_COUNTS"
# Call the function date_comparator to check if traffic.txt already been
# copied to previous-traffic.txt in case of a new day has started
date_comparator
# Capture interfaces that have "Destination " the network 0.0.0.0
/sbin/route -n | awk '/^0.0.0.0/ {print $8}' > $INTERFACES
rm -rf $ADDR_MASK $NETWORKS
# Save on "ADDR_MASK" the address(es) and mask(s) filtered from "interfaces"
while read i; do
/sbin/ifconfig $i | grep 'inet ' > $TMP1
cat $TMP1 | cut -d ":" -f2 > $TMP2
sed s/' '// < $TMP2 > $TMP3
cat $TMP3 | cut -d " " -f1 >> $ADDR_MASK
cat $TMP1 | cut -d ":" -f4 >> $ADDR_MASK
done < $INTERFACES
TIMESTAMP=$(date +%H:%M)
rm -rf $TMP1 $TMP2 $TMP3
INPUT_COUNT=$(sudo $IPTABLES -nvxL INPUT | grep INPUT_COUNTS | \
awk 'BEGIN{pkts=0; bytes=0} \
{pkts+=$1; bytes+=$2} \
END{print pkts,bytes}')
# Replace the "." by " " to facilitate on time to read the input
sed -i 's/\./ /g' $ADDR_MASK
OUTPUT_COUNT=$(sudo $IPTABLES -nvxL OUTPUT | grep OUTPUT_COUNTS | \
awk 'BEGIN{pkts=0; bytes=0} \
{pkts+=$1; bytes+=$2} \
END{print pkts,bytes}')
# Run the discover-network program to discover the local networks
./discover-network
echo "$TIMESTAMP $INPUT_COUNT $OUTPUT_COUNT" >> $TRAFFIC
# Check if files are present for verification of local networks
if ! test -f "$PREVIOUS_NETWORKS"; then
cp $NETWORKS $PREVIOUS_NETWORKS
fi
rm -rf $ADDR_MASK $INTERFACES
# Variable to check if rules are included
TEST=$(sudo /sbin/iptables -L INPUT -n -v -x | grep $INPUT | head -n 1)
if test -z "$TEST"; then
# If $TEST is empty, then the rule(s) does not exist. Create rules for each
# internal network
while read net && read int; do
sudo /sbin/iptables -I INPUT -i $int ! -s $net -m comment \
--comment $INPUT
sudo /sbin/iptables -I OUTPUT -o $int ! -d $net -m comment \
--comment $OUTPUT
done < $NETWORKS
else
# If new networks were added, first removes your old networks and then
# added the new ones
TEST_DIFF=$(diff $NETWORKS $PREVIOUS_NETWORKS)
if test -n "$TEST_DIFF"; then
while read net && read int; do
sudo /sbin/iptables -D INPUT -i $int ! -s $net -m comment \
--comment $INPUT
sudo /sbin/iptables -D OUTPUT -o $int ! -d $net -m comment \
--comment $OUTPUT
done < $PREVIOUS_NETWORKS
while read net && read int; do
sudo /sbin/iptables -I INPUT -i $int ! -s $net -m comment \
--comment $INPUT
sudo /sbin/iptables -I OUTPUT -o $int ! -d $net -m comment \
--comment $OUTPUT
done < $NETWORKS
cp $NETWORKS $PREVIOUS_NETWORKS
fi
fi
# Variable that stores the current time
TIMESTAMP=$(date +%H:%M)
# Remove current rules
while IFS=$' ' read -a net; do
sudo $IPTABLES -D INPUT -i ${net[0]} ! -s ${net[1]} -m comment \
--comment INPUT_COUNTS
sudo $IPTABLES -D OUTPUT -o ${net[0]} ! -d ${net[1]} -m comment \
--comment OUTPUT_COUNTS
done < $INTERFACES
# Filter the result of "iptables -L INPUT/OUTPUT -n -v -x"
TRAFFICIN=$(sudo /sbin/iptables -L INPUT -n -v -x | grep $INPUT | head -n 1)
TRAFFICOUT=$(sudo /sbin/iptables -L OUTPUT -n -v -x| grep $OUTPUT | head -n 1)
# Truncate networks file
echo -n > $INTERFACES
# Filter "TRAFFICIN/TRAFFICOUT" preparing to save in the file
IN=$(echo "$TRAFFICIN" | awk '{print $1,$2}')
OUT=$(echo "$TRAFFICOUT" | awk '{print $1,$2}')
# Grab all the interfaces in wich default traffic gets routed
TMP=$($IP route list | sed -n 's/^default.*dev \([a-zA-Z0-9]*\).*$/\1/p')
# Save in the "traffic.txt" the collected data in a period of 5 minutes
echo "$TIMESTAMP $IN $OUT" >> $TRAFFIC
# Find IP address and subnet mask for each interface
for IF in $TMP; do
addr_mask=$($IP addr show $IF | sed -n 's/^.*inet \([0-9.]*\/[0-9]*\).*$/\1/p')
echo "$IF $addr_mask" >> $INTERFACES
done
# Reset the counters of iptables rules to the next collection
sudo /sbin/iptables -Z
# Insert new rules
while IFS=$' ' read -a net; do
sudo $IPTABLES -I INPUT -i ${net[0]} ! -s ${net[1]} -m comment \
--comment INPUT_COUNTS
sudo $IPTABLES -I OUTPUT -o ${net[0]} ! -d ${net[1]} -m comment \
--comment OUTPUT_COUNTS
done < $INTERFACES
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