Created
January 24, 2021 21:45
-
-
Save HeLiBloks/cb83370fb738fb645addf70c4414ecc5 to your computer and use it in GitHub Desktop.
nagios plugin ethtool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/sh | |
# Check if an interface is UP or DOWN and return data suitable for pnp4nagios | |
# Copyright © 2021 henrik lindgren <henrikprojekt at gmail.com> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# Check if an interface is UP or DOWN and return interface data suitable for pnp4nagios | |
# ./check_ethtool.sh -i eth[0-3] | |
# | |
# tested on Centos7, Fedora33 | |
function _usage () | |
{ | |
echo "Usage : $0 [options] [--] | |
Check if an interface is UP or DOWN and return interface data suitable for pnp4nagios | |
./check_ethtool.sh -i eth[0-3] | |
./check_ethtool.sh -o lo | |
Options: | |
-h|help Display this message | |
-w warning, TODO: | |
-c critical, TODO: | |
-M max, TODO: | |
-m min, TODO: | |
-i comma delimited list of interface names can be regex to check eth0,vif[0-1] defaults to all | |
-o comma delimited list of interface names can be regex to omit lo,fbi0,mp0 | |
" | |
} # ---------- end of function usage ---------- | |
#----------------------------------------------------------------------- | |
# Handle command line arguments | |
#----------------------------------------------------------------------- | |
while getopts ":hc:w:m:M:i:o:" opt | |
do | |
case $opt in | |
h|help) _usage; exit 0 ;; | |
w|warning) warning="$OPTARG";; | |
c|critical) critical="$OPTARG";; | |
m|min) min="$OPTARG";; | |
M|max) max="$OPTARG";; | |
i|ifaces) ifaces="${OPTARG}";; | |
o|ofaces) ofaces="${OPTARG}";; | |
* ) echo -e "\n Option does not exist : $OPTARG\n" | |
_usage; exit 4 ;; | |
esac # --- end of case --- | |
done | |
shift $((OPTIND-1)) | |
if command -v ip &> /dev/null ; then | |
allfaces="$(ip a | awk -vFS=': ' '/^[0-9]/{printf "%s ",$2}')" | |
elif command -v ifconfig &> /dev/null ; then | |
allfaces="$(ifconfig | awk -vFS=':' '/^\w/{printf "%s ",$1}')" | |
else | |
echo "UNKNOWN - no interface specified on cmdline and no ifconfig or ip command found" | |
exit 3 | |
fi | |
if [ ! x${ifaces} = "x" ] ; then | |
rfaces="$(echo ${ifaces[@]} | sed 's/,/\\|/g; s/ /\\|/g')" | |
ifaces="$(echo $allfaces | grep -o "$rfaces")" | |
#echo "debug include regex: $rfaces" | |
#echo "debug include ifaces: $ifaces" | |
else | |
ifaces="$allfaces" | |
fi | |
# handle excluded interfaces | |
if [ ! x${ofaces} = "x" ] ; then | |
rfaces="$(echo ${ofaces[@]} | sed 's/,/\\|/g; s/ /\\|/g')" | |
ifaces="$(echo $ifaces | sed "s/$rfaces//g")" | |
#echo "debug regex: $rfaces" | |
#echo "debug ifaces: $ifaces" | |
fi | |
lastrun="" | |
ifstatus="" | |
OK="OK" | |
for iface in ${ifaces[@]}; do | |
# ethtool formats output like this: | |
# NIC statistics: | |
# tx_packets: 238710 | |
# rx_errors: 0 | |
# awk will format output like: | |
# eth0_rx_octets=571706c | |
lastrun="$(echo -e $lastrun; ethtool -S $iface 2>/dev/null |\ | |
awk -v"iface=$iface" ' | |
/^\s+\w+:/{ | |
gsub(/:\s/,"=",$0); | |
printf "%s_%s%sc ",iface,$1,$2}')" | |
if ethtool $iface | tail -1 | grep yes &> /dev/null ; then | |
ifstatus="$ifstatus $iface=UP" | |
else | |
ifstatus="$ifstatus $iface=DOWN" | |
OK='CRITICAL' | |
fi | |
done | |
echo "$OK - Link$ifstatus | $lastrun" | |
if [ "x$OK" = 'xOK' ] ; then | |
exit 0; | |
elif [ "x$OK" = 'xWARNING' ] ; then | |
exit 1; | |
elif [ "x$OK" = 'xCRITICAL' ]; then | |
exit 2; | |
elif [ "x$OK" = 'xUNKNOWN' -o "x$OK" == 'x' ] ; then | |
exit 3; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment