feature: Add support for dynamic interfaces in ifAlias script (#6005)

* add support for dynamic interfaces in ifAlias script

* netlink and ip support alias (`ip link set IFACE alias ALIAS`). In an
  environment with dynamic interfaces, it is useful to be able retrieve
  aliases from ip.

* we cannot assume next iface oid is n + 1 since oids do not necessarily
  follow incrementally when some interfaces has been destroyed and
  recreated.

* use straight POSIX shell

* I agree to the conditions of the Contributor Agreement contained in
doc/General/Contributing.md.
This commit is contained in:
ollb
2017-02-27 00:22:03 +01:00
committed by Neil Lathwood
parent 3f5bf56b55
commit 698584e14d
2 changed files with 102 additions and 31 deletions

View File

@@ -183,6 +183,7 @@ LibreNMS contributors:
- Stefan Behte <craig at haquarter.de> (craig) - Stefan Behte <craig at haquarter.de> (craig)
- Zane C. Bowers-Hadley <vvelox@vvelox.net> (vvelox) - Zane C. Bowers-Hadley <vvelox@vvelox.net> (vvelox)
- Valentin Polonuyer <valik.vicious@gmail.com> (ValikVicious) - Valentin Polonuyer <valik.vicious@gmail.com> (ValikVicious)
- Olivier Le Brouster <olb@nebkha.net> (ollb)
Observium was written by: Observium was written by:
- Adam Armstrong - Adam Armstrong

View File

@@ -1,58 +1,128 @@
#!/bin/bash #!/bin/sh
# (c) 2013, 2014, f0o@devilcode.org # (c) 2013-2017, f0o@devilcode.org, olb@nebkha.net
# This program is free software: you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or # the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
DISTRO_BIN="/usr/bin/distro"
BASE='.1.3.6.1.2.1.31.1.1.1.18' BASE='.1.3.6.1.2.1.31.1.1.1.18'
ID=$(cut -d . -f 13 <<< $2) GET_TYPE="$1"
cache=$(ip l) GET_OID="$2"
if [ -z "$ID" ]; then # cache ip link output
ID=1 IP_LINK="$(ip link)"
else
ID=$(($ID+1))
fi
IFACE=$(grep "^${ID}: " <<<"$cache" | sed 's/[:@]\s/ /g'| cut -d " " -f 2 | cut -d @ -f 1) # Get interface id from GET_OID script parameter depending on the get type -g
# or -n.
#
# snmpd specify two behaviors: GETNEXT and GET.
#
# script -g <GET_OID>
#
# : Should return OID value
#
# script -n <GET_OID>
#
# : Should return next OID value
#
# Note that interface id are not necessarly following incrementally.
# We need tho find the next interface id (which is not necessarily n+1).
#
interface_id()
{
local N=
local L=
local ID="${GET_OID#${BASE}.}"
echo ${BASE}.${ID} case "$GET_TYPE" in
if [ "X${IFACE}" = "X" ]; then -g)
echo noSuchName echo "$ID"
else return 0
echo "string" ;;
if [ -x /usr/bin/distro ]; then -n)
case $(distro | cut -d " " -f 1) in if [ "$ID" = "$BASE" ]
then
# find the first iface_id
echo "$IP_LINK" | grep -oE "^[0-9]+:" | head -n 1 | cut -d':' -f 1
return 0
else
# find the next iface_id
for N in $(echo "$IP_LINK" | grep -oE "^[0-9]+:" | cut -d':' -f 1)
do
if [ "$L" = "$ID" -o ! "$ID" ]
then
echo -n "$N"
return 0
fi
L="$N"
done
fi
;;
esac
return 1
}
interface_name()
{
local IFACE_ID="$1"
echo "$IP_LINK" | grep -oE "^${IFACE_ID}: [^:@ ]*" | cut -d " " -f 2
}
alias_from_interfaces_config_file()
{
local IFACE="$1"
local CONFIG_FILE=
if [ -x "$DISTRO_BIN" ]
then
case "$("$DISTRO_BIN" | cut -d " " -f 1)" in
Debian) Debian)
cnf="/etc/network/interfaces" CONFIG_FILE="/etc/network/interfaces"
;; ;;
Gentoo) Gentoo)
cnf="/etc/conf.d/net" CONFIG_FILE="/etc/conf.d/net"
;; ;;
CentOS|RedHat|SuSE|Mandriva|Mandrake) CentOS|RedHat|SuSE|Mandriva|Mandrake)
cnf="/etc/sysconfig/network-scripts/ifcfg-$IFACE" CONFIG_FILE="/etc/sysconfig/network-scripts/ifcfg-$IFACE"
;; ;;
Archlinux) Archlinux)
cnf="/etc/conf.d/net-conf-$IFACE" CONFIG_FILE="/etc/conf.d/net-conf-$IFACE"
;;
*)
cnf=""
;; ;;
esac esac
fi fi
if [ -n "$cnf" ]; then if [ "$CONFIG_FILE" ]
echo $(grep -i "^# $IFACE:" $cnf | sed "s/^# $IFACE: //i") then
else echo "$(grep -i "^# $IFACE:" $CONFIG_FILE | sed "s/^# $IFACE: //i")"
echo
fi fi
fi }
alias_from_ip_link()
{
local IFACE="$1"
ip link show "$IFACE" | grep -e "^[[:space:]]*alias[[:space:]]" | sed -e 's/^[[:space:]]*alias //'
}
IFACE_ID="$(interface_id)"
[ "$IFACE_ID" ] || exit
IFACE="$(interface_name "$IFACE_ID")"
VALUE=
# we first try to get alias from interface config file
[ "$VALUE" ] || VALUE="$(alias_from_interfaces_config_file "$IFACE")"
# then from ip link show $IFACE output
[ "$VALUE" ] || VALUE="$(alias_from_ip_link "$IFACE")"
echo "${BASE}.${IFACE_ID}"
echo "string"
echo "$VALUE"
exit 0 exit 0