Commit version 24.12.13800
This commit is contained in:
188
scripts/ifAlias
Normal file → Executable file
188
scripts/ifAlias
Normal file → Executable file
@ -1,65 +1,129 @@
|
||||
#!/usr/bin/perl -w
|
||||
#!/bin/bash
|
||||
# -------------------------------------------------------------------------
|
||||
# Observium
|
||||
#
|
||||
# This file is part of Observium.
|
||||
#
|
||||
# @package observium
|
||||
# @subpackage scripts
|
||||
# @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2023 Observium Limited
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
## From Sascha Schaal
|
||||
# Add to snmpd.conf something like:
|
||||
# pass .1.3.6.1.2.1.31.1.1.1.18 /usr/local/bin/ifAlias
|
||||
BASE=".1.3.6.1.2.1.31.1.1.1.18" # IF-MIB::ifAlias
|
||||
REQ="$1" # -g get, -n getnext, -s set
|
||||
OID="$2" # Requested OID
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $REQ = $ARGV[0];
|
||||
my $ROID = $ARGV[1];
|
||||
my $Base = ".1.3.6.1.2.1.31.1.1.1.18";
|
||||
my @iface_list = `ip l | grep mtu`;
|
||||
my $index = 0;
|
||||
my %Table;
|
||||
my $p_oid;
|
||||
my $p_type = "STRING";
|
||||
my $p_desc;
|
||||
|
||||
if ($ROID eq $Base){
|
||||
$index = 0;
|
||||
}
|
||||
else{
|
||||
my @split = split("$Base.",$ROID);
|
||||
$index = $split[1];
|
||||
}
|
||||
foreach my $row (@iface_list){
|
||||
my @split = split(": ", $row);
|
||||
my $desc = `grep "^# $split[1]:" /etc/network/interfaces|sed s/^\\#\\ $split[1]:\\ //`;
|
||||
my ($key, $value) = ("$Base.$split[0]",$desc);
|
||||
$Table{$key} = $value;
|
||||
}
|
||||
if ($REQ eq "-g"){
|
||||
if ($index == 0){
|
||||
exit 0;
|
||||
}
|
||||
else{
|
||||
if( exists($Table{$ROID} ) ) {
|
||||
$p_oid = $ROID;
|
||||
$p_desc = $Table{$ROID};
|
||||
}
|
||||
else{
|
||||
print "noSuchName\n";
|
||||
exit 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
elsif ($REQ eq "-n"){
|
||||
for ($index, $index <= keys %Table,$index++){
|
||||
if( exists($Table{"$Base.$index"} ) ) {
|
||||
$p_oid = "$Base.$index";
|
||||
$p_desc = $Table{"$Base.$index"};
|
||||
}
|
||||
else{
|
||||
exit 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
exit 0;
|
||||
}
|
||||
if [ "$REQ" = "-s" ]; then
|
||||
#echo $* >> /tmp/passtest.log
|
||||
exit 0
|
||||
fi
|
||||
|
||||
print "$p_oid\n";
|
||||
print "$p_type\n";
|
||||
print "$p_desc";
|
||||
exit 0;
|
||||
#1 lo
|
||||
#2 eth0
|
||||
#3 eth1
|
||||
LINKS="$(ip link | awk -F ": " '/^[0-9]+:/ {print $1 " " $2}')"
|
||||
#echo "$LINKS"
|
||||
|
||||
_starts_with() {
|
||||
local _str="$1"
|
||||
local _sub="$2"
|
||||
echo "$_str" | grep "^$_sub" >/dev/null 2>&1
|
||||
}
|
||||
|
||||
ifindex() {
|
||||
local IF="${OID#"${BASE}".}"
|
||||
local line
|
||||
|
||||
if [ "$REQ" = "-g" ]; then
|
||||
# GET request
|
||||
case $IF in ''|*[!0-9]*) return 1;; esac; # not int number
|
||||
echo "$IF"
|
||||
return 0
|
||||
else
|
||||
# GETNEXT request
|
||||
if [ "$IF" = "$BASE" ] || [ "$IF" = "" ]; then
|
||||
# first interface index
|
||||
echo "${LINKS%% *}"
|
||||
return 0
|
||||
else
|
||||
local next=false
|
||||
echo "$LINKS" | while read line; do
|
||||
if $next; then echo "${line%% *}"; return 0; fi
|
||||
if _starts_with "$line" "$IF "; then next=true; fi
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
ifname() {
|
||||
local IF="$1"
|
||||
local line
|
||||
|
||||
echo "$LINKS" | while read line; do
|
||||
if _starts_with "$line" "$IF "; then echo "${line##* }"; fi
|
||||
done
|
||||
}
|
||||
|
||||
ifalias_sys() {
|
||||
#local IFNAME="$1"
|
||||
local sys_file="/sys/class/net/${1}/ifalias"
|
||||
|
||||
# 15: wg0: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
|
||||
# link/none
|
||||
# alias Wireguard VPN
|
||||
if [ -f "$sys_file" ]; then
|
||||
cat "$sys_file"
|
||||
fi
|
||||
}
|
||||
|
||||
ifalias_frr() {
|
||||
local IFNAME="$1"
|
||||
|
||||
# FRR
|
||||
if [ -x "/bin/vtysh" ]; then
|
||||
# prefer vtysh
|
||||
vtysh -c "show interface description" 2>&1 /dev/null | grep -si -m1 "^$IFNAME " | awk '{for (i=4; i<NF; i++) printf $i " "; print $NF}'
|
||||
elif [ -f "/etc/frr/frr.conf" ]; then
|
||||
awk "/^interface ${IFNAME}/,/^exit/" /etc/frr/frr.conf | awk "/^ description /" | sed "s/^\\ description\\ //"
|
||||
fi
|
||||
}
|
||||
|
||||
ifalias_conf() {
|
||||
local IFNAME="$1"
|
||||
|
||||
if [ -f "/etc/network/interfaces.d/$IFNAME" ]; then
|
||||
CFG="/etc/network/interfaces.d/$IFNAME"
|
||||
elif [ -f "/etc/network/interfaces" ]; then
|
||||
CFG="/etc/network/interfaces"
|
||||
elif [ -f "/etc/sysconfig/network-scripts/ifcfg-$IFNAME" ]; then
|
||||
CFG="/etc/sysconfig/network-scripts/ifcfg-$IFNAME"
|
||||
elif [ -f "/etc/conf.d/net-conf-$IFNAME" ]; then
|
||||
CFG="/etc/conf.d/net-conf-$IFNAME"
|
||||
elif [ -f "/etc/conf.d/net" ]; then
|
||||
CFG="/etc/conf.d/net"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
grep -si -m1 "^# $IFNAME:" $CFG | sed "s/^# $IFNAME: //i"
|
||||
}
|
||||
|
||||
IFINDEX="$(ifindex)"
|
||||
[ "$IFINDEX" ] || exit 1 # incorrect ifindex
|
||||
|
||||
IFNAME="$(ifname "$IFINDEX")"
|
||||
IFALIAS="$(ifalias_frr "$IFNAME")"
|
||||
# get ifalias from config files
|
||||
[ "$IFALIAS" ] || IFALIAS="$(ifalias_conf "$IFNAME")"
|
||||
# if not found in config, try common link alias
|
||||
# sudo ip link set <interface> alias "<ifalias>"
|
||||
[ "$IFALIAS" ] || IFALIAS="$(ifalias_sys "$IFNAME")"
|
||||
|
||||
echo "${BASE}.${IFINDEX}"
|
||||
echo "string"
|
||||
echo "$IFALIAS"
|
||||
|
||||
exit 0
|
Reference in New Issue
Block a user