Skip to content

Instantly share code, notes, and snippets.

@amboxer21
Created January 26, 2015 15:25
Show Gist options
  • Save amboxer21/403ec222372dc7b15bbd to your computer and use it in GitHub Desktop.
Save amboxer21/403ec222372dc7b15bbd to your computer and use it in GitHub Desktop.
Bash script to monitor wireless card overruns for incrementing values
#!/bin/bash
#
# Grabs each iface and its respective overruns variables, ties them all together
# and pushes them into the res variable. Resulting in a line for every iface.
res=$(readarray -t mon < <(cat ifconfig | \
awk '/w[a-z]*[0-9]/,/TX.*overruns:[0-9]/{print}'); echo "${mon[@]}" | \
egrep -o "(w[a-z]*[0-9]|overruns:[0-9]{1,99})" | \
sed -e 'N;N;s/\n/ /g');
readarray -t fin < <(echo "$res");
counter=0
for i in "${fin[@]}"; do
# Joins both overrun numbers from same iface together and pushes them into the don variable.
# Each iface has 2 overruns counters. One for TX and one for RX.
don=$(echo "$i" | egrep -o ":[0-9]{1,99}" | sed ':a;N;$!ba;s/\n//g;s/://g');
# Grabs just the iface name and stores it in the if variable.
iface=`echo "$i" | grep -o "w[a-z]*[0-9]\{1,99\}"`;
# Cretes necessary files if they're not present.
if [[ ! -f ".${iface}" || ! -f ".${iface}counter" ]]; then
echo -e "File does not exit. Creating now.\n";
touch .${iface};
touch .${iface}counter;
fi
# Checks to see if the overruns are greater than zero for each iface in the iteration.
if [[ $don == '00' ]]; then
echo -e "$iface remains clean\n";
else
echo -e "!!!!! $iface is not clean.\n";
echo $don >> ".${iface}"; # Pushes the current overrun numbers into a file.
# Checks if the last line in the respective iface file is greater than the second
# to last. If that's the case, then it will increment the counter inside that ifaces file.
if [[ `cat .${iface} | sed -n '$p'` > `cat .${iface} | sed -n 'x;$p'` ]]; then
echo -e "$iface has an incrementing overrun.\n";
echo $((`cat .${iface}counter`+1)) > .${iface}counter;
# If counter is equal to 5, then reset the respective server.
if [[ `cat .${iface}counter` -eq 5 ]]; then
echo -e "Restarting Server - ${iface}.\n";
echo > .${iface}; # Reset the file and counter after the server has been reset.
echo > .${iface}counter
fi
else
echo -e "Incrementing overruns on $iface has stopped.\n";
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment