netstat-monitor.sh script
Configuration
Script details
This script is available at $PATROL_HOME\..\TRO\Conf\Scripts\Samples directory. The script content is as follows:
#!/bin/sh -f
# this sample script can be used with the KM to set-up monitoring to network load per NIC
# command line tool 'netstat'.
# the output of the command is used to measure the byte count per NIC (in/out and state:OK,ERR,OVR,DRP)
# in this case the KM actually monitor the delta between 2 collection cycles.
# this is done by keeping a copy of the numbers between 1 collection to the next.
# - bc used to calculate difference between to values of the same parameter
# - 2 parameters: RcvOKRate, TxOKRate are set with Rate normalized value of other parameters.
#==================================================================================================
# netstat --interfaces yield the below report used by this script to monitor
# the network statistics of the local device
#=================================================================================
# Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
# ens192 1500 61280517 0 6 0 4657082 0 0 0 BMRU
# ens224 1500 359386765 0 12 0 915434 0 0 0 BMRU
# lo 65536 162702652 0 0 0 162702652 0 0 0 LRU
#
# Output format:
# Interface=eth10,MTU=1500,RcvOK=2330,RcvOKRate=66.57,RcvERR=0,RcvDrop=0,RcvOVR=0,TxOK=0,TxOKRate=0.54,TxERR=0,TxDrop=0,TxOVR=0
MET_FLAG=`netstat --interfaces | grep "^Iface" | tr -s ' ' | cut -f3 -d\ `
if [ "$MET_FLAG" = "Met" ]; then
CUT_FIELDS="-f1-2,4-12"
else
CUT_FIELDS="-f1-10"
fi
function process_netstat_line
{
RcvOK=`echo ${3}-${14}|bc`
RcvOKRate=`echo "scale=2;${RcvOK}/${POLLING_PERIOD}"|bc`
RcvERR=`echo ${4}-${15}|bc`
RcvDrop=`echo ${5}-${16}|bc`
RcvOVR=`echo ${6}-${17}|bc`
TxOK=`echo ${7}-${18}|bc`
TxOKRate=`echo "scale=2;${TxOK}/${POLLING_PERIOD}"|bc`
TxERR=`echo ${8}-${19}|bc`
TxDrop=`echo ${9}-${20}|bc`
TxOVR=`echo ${10}-${21}|bc`
echo Interface=$1,MTU=$2,RcvOK=$RcvOK,RcvOKRate=${RcvOKRate},RcvERR=$RcvERR,RcvDrop=$RcvDrop,RcvOVR=$RcvOVR,TxOK=$TxOK,TxOKRate=${TxOKRate},TxERR=$TxERR,TxDrop=$TxDrop,TxOVR=$TxOVR
}
if [ -f $0.timestamp.prev ]; then
NOW=`date +%s`
PREV_TIME_STAMP=`cat $0.timestamp.prev`
POLLING_PERIOD=`echo $NOW-$PREV_TIME_STAMP|bc`
else
POLLING_PERIOD=300
fi
for raw_line in $(netstat --interfaces | grep -v "\(^Kernel\|^Iface\)" | tr -s ' ' | cut -d\ ${CUT_FIELDS} | sed 's/ /,/g')
do
interfaceName=`echo $raw_line | cut -d\, -f1`
line=`echo $raw_line | sed 's/,/ /g'`
if [ -f "$0.$interfaceName.prev" ]; then
process_netstat_line $line `cat $0.$interfaceName.prev`
else
echo Interface=$interfaceName,MTU=0,RcvOK=0,RcvERR=0,RcvDrop=0,RcvOVR=0,TxOK=0,TxERR=0,TxDrop=0,TxOVR=0
fi
echo $line > $0.$interfaceName.prev
done
date +%s > $0.timestamp.prev
# this sample script can be used with the KM to set-up monitoring to network load per NIC
# command line tool 'netstat'.
# the output of the command is used to measure the byte count per NIC (in/out and state:OK,ERR,OVR,DRP)
# in this case the KM actually monitor the delta between 2 collection cycles.
# this is done by keeping a copy of the numbers between 1 collection to the next.
# - bc used to calculate difference between to values of the same parameter
# - 2 parameters: RcvOKRate, TxOKRate are set with Rate normalized value of other parameters.
#==================================================================================================
# netstat --interfaces yield the below report used by this script to monitor
# the network statistics of the local device
#=================================================================================
# Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
# ens192 1500 61280517 0 6 0 4657082 0 0 0 BMRU
# ens224 1500 359386765 0 12 0 915434 0 0 0 BMRU
# lo 65536 162702652 0 0 0 162702652 0 0 0 LRU
#
# Output format:
# Interface=eth10,MTU=1500,RcvOK=2330,RcvOKRate=66.57,RcvERR=0,RcvDrop=0,RcvOVR=0,TxOK=0,TxOKRate=0.54,TxERR=0,TxDrop=0,TxOVR=0
MET_FLAG=`netstat --interfaces | grep "^Iface" | tr -s ' ' | cut -f3 -d\ `
if [ "$MET_FLAG" = "Met" ]; then
CUT_FIELDS="-f1-2,4-12"
else
CUT_FIELDS="-f1-10"
fi
function process_netstat_line
{
RcvOK=`echo ${3}-${14}|bc`
RcvOKRate=`echo "scale=2;${RcvOK}/${POLLING_PERIOD}"|bc`
RcvERR=`echo ${4}-${15}|bc`
RcvDrop=`echo ${5}-${16}|bc`
RcvOVR=`echo ${6}-${17}|bc`
TxOK=`echo ${7}-${18}|bc`
TxOKRate=`echo "scale=2;${TxOK}/${POLLING_PERIOD}"|bc`
TxERR=`echo ${8}-${19}|bc`
TxDrop=`echo ${9}-${20}|bc`
TxOVR=`echo ${10}-${21}|bc`
echo Interface=$1,MTU=$2,RcvOK=$RcvOK,RcvOKRate=${RcvOKRate},RcvERR=$RcvERR,RcvDrop=$RcvDrop,RcvOVR=$RcvOVR,TxOK=$TxOK,TxOKRate=${TxOKRate},TxERR=$TxERR,TxDrop=$TxDrop,TxOVR=$TxOVR
}
if [ -f $0.timestamp.prev ]; then
NOW=`date +%s`
PREV_TIME_STAMP=`cat $0.timestamp.prev`
POLLING_PERIOD=`echo $NOW-$PREV_TIME_STAMP|bc`
else
POLLING_PERIOD=300
fi
for raw_line in $(netstat --interfaces | grep -v "\(^Kernel\|^Iface\)" | tr -s ' ' | cut -d\ ${CUT_FIELDS} | sed 's/ /,/g')
do
interfaceName=`echo $raw_line | cut -d\, -f1`
line=`echo $raw_line | sed 's/,/ /g'`
if [ -f "$0.$interfaceName.prev" ]; then
process_netstat_line $line `cat $0.$interfaceName.prev`
else
echo Interface=$interfaceName,MTU=0,RcvOK=0,RcvERR=0,RcvDrop=0,RcvOVR=0,TxOK=0,TxERR=0,TxDrop=0,TxOVR=0
fi
echo $line > $0.$interfaceName.prev
done
date +%s > $0.timestamp.prev
Example
The KM monitors the network activity on clm-aus-009454.bmc.com device. The data is collected by using the netstat --interfaces script.
Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*