Hi,
I would like to share some "nasty" bash scripts to simplify generation of collectd configuration for collecting snmp statistics used in Grafana.
They are a bit hardcoded, and should maybe be done in Python. *TODO when I have the skills*.
Feel free to spit on it if you have that approach, for the rest, use it, modify it to suit your needs. Add improvements or similar things if you would like to share back to me and others. No need to re-invent the wheel...
#
# This script will fetch interfaces with st0.x and prepare config to be used with colleced.
# The usage is ./script.sh myfirewall.fqdn.net
# A lot of settings is hardcoded in here. if you make changes, make sure to backup the data and make a copy of your own to play with.
# The output filename will be myfirewall.fqdn.net.conf. Dont trust that the output is 100 percent working. but close to at least.
if [[ $# -eq 0 ]] ; then
echo 'Usage: ./script.sh switch-fqdn'
exit 1
fi
OUTFILE=$1.conf
TMPFILE=snmpdata.txt
rm $OUTFILE
/usr/bin/snmpwalk -c prod-VPN01-vr@SNMPCOMMAB08 -v 2c $1 1.3.6.1.2.1.2.2.1.2 | grep -v "\.0"\" | grep -v vlan |grep -v reth > $TMPFILE
#echo "snmpwalk executed"
LINES=`cat $TMPFILE | wc -l`
for line in `seq 1 $LINES`;
do
INTNAME=`cat $TMPFILE | head -n $line | tail -n 1 | cut -d " " -f 4`
INTINDEX=`cat $TMPFILE | head -n $line | tail -n 1 | cut -d "." -f 11 | cut -d " " -f 1`
#echo $INTNAME
#echo $INTINDEX
if [ "$INTINDEX" -ge 504 ]; then
#echo "<Plugin snmp>" >> $OUTFILE
echo " <Data $INTNAME>" >> $OUTFILE
echo " Type \"if_octets\"" >> $OUTFILE
echo " Table false" >> $OUTFILE
echo " Instance $INTNAME" >> $OUTFILE
echo " Values \"IF-MIB::ifHCInOctets.$INTINDEX\" \"IF-MIB::ifHCOutOctets.$INTINDEX\"" >> $OUTFILE
echo " </Data>" >> $OUTFILE
#echo "</Plugin>" >> $OUTFILE
#
fi
done
#rm $TMPFILE
....other script.
###
# Will generate 10Gbits interface monitoring and use description within this.
#
###
if [[ $# -eq 0 ]] ; then
echo 'Usage: ./script.sh switch-fqdn'
exit 1
fi
OUTFILE=$1.10G.conf
TMPFILE=snmpdata.txt
DESCRIPTIONFILE=snmpdescription.txt
/usr/bin/snmpwalk -c COMMUNITYY_some77 -v 2c $1 1.3.6.1.2.1.31.1.1.1.18 | grep -v "\.0"\" | grep STRING > $DESCRIPTIONFILE
/usr/bin/snmpwalk -c COMMUNITYY_some77 -v 2c $1 1.3.6.1.2.1.2.2.1.2 | grep -v "\.0"\" | grep xe- > $TMPFILE
#echo "snmpwalk executed"
INSTANCELIST=""
#Count the lines to loop them
LINES=`cat $TMPFILE | wc -l`
for line in `seq 1 $LINES`;
do
INTNAME=`cat $TMPFILE | head -n $line | tail -n 1 |cut -d "." -f 11 | cut -d " " -f 4`
INTINDEX=`cat $TMPFILE | head -n $line | tail -n 1 | cut -d "." -f 11 | cut -d " " -f 1`
#echo $INTNAME
#echo $INTINDEX
if [ "$INTINDEX" -ge 504 ]; then
INTDESC=`grep $INTINDEX $DESCRIPTIONFILE | cut -d ":" -f 2`
#Continue if a description exists
if [ -n "$INTDESC" ]; then
#Removes *
STRIPINTDESC=`sed 's/\*\+//g' <<< $INTDESC`
#Removes "
STRIPINTDESC=`sed 's/\"\+//g' <<< $STRIPINTDESC`
#Replace whitepace with underscore
STRIPINTDESC="${STRIPINTDESC// /_}"
#To lower case
STRIPINTDESC="${STRIPINTDESC,,}"
#Removes first char if it is _
STRIPINTDESC=`sed 's/_*//' <<< $STRIPINTDESC`
#Removes "
INTNAME=`sed 's/\"\+//g' <<< $INTNAME`
#Create delim
DELIM="_"
#Join Strings
DATAJOINED=$STRIPINTDESC$DELIM$INTNAME
INTJOINED=$INTNAME$DELIM$STRIPINTDESC
case $STRIPINTDESC in
*fw*)
;;
*vc0*)
;;
*cwdm*)
;;
*lan*)
;;
*)
echo "# $INTDESC" >> $OUTFILE
echo " <Data \"$DATAJOINED\">" >> $OUTFILE
echo " Type \"if_octets\"" >> $OUTFILE
echo " Table false" >> $OUTFILE
echo " Instance \"$DATAJOINED\"" >> $OUTFILE
echo " Values \"IF-MIB::ifHCInOctets.$INTINDEX\" \"IF-MIB::ifHCOutOctets.$INTINDEX\"" >> $OUTFILE
echo " </Data>" >> $OUTFILE
#echo "</Plugin>" >> $OUTFILE
INSTANCELIST=\"$DATAJOINED\"" "$INSTANCELIST
;;
esac
fi
fi
done
echo $INSTANCELIST >> $OUTFILE
#rm $TMPFILE