Attivare il mapping dei nomi SNMP delle interfacce in NTOP

From RVM Wiki
Revision as of 14:51, 12 January 2024 by Gabriele.vivinetto (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Questo script mappa i nomi delle interfacce snmp estraendo gli IP dei monitored devices da redis

export NTOPSNMPCOMMUNITY="public"
  • Creare lo script:
cat > /usr/local/sbin/make_snmp_mappings.sh <<'EOFile'
#!/bin/bash

COMMUNITY=$1
NEW_DEV_DETECTED="1"

if [ -z "$COMMUNITY" ]
then
 echo "Usage: $0 snmp_community_name"
 exit 127
fi

IPS=$(redis-cli -h 127.0.0.1 --scan --pattern '*' | grep snmp| grep system | cut -f 2-5 --delimiter='.')
if [ -f /etc/nprobe/snmp_mappings.txt ]
then
 /bin/mv -f /etc/nprobe/snmp_mappings.txt /etc/nprobe/snmp_mappings.txt.old
fi

for IP in $IPS
do
        echo "Adding $IP"
       /usr/local/sbin/build_snmp_mappings.sh $IP 2c ${COMMUNITY} >> /etc/nprobe/snmp_mappings.txt
done

if [ -f /etc/nprobe/snmp_mappings.txt.old ]
then
 diff -q /etc/nprobe/snmp_mappings.txt /etc/nprobe/snmp_mappings.txt.old
 NEW_DEV_DETECTED="$?"
fi

if [ $NEW_DEV_DETECTED -eq "1" ]
then
        echo "New device detected. Restart Nprobe instances"
        #echo "New device detected. Restart Ntop and Nprobe"
        #systemctl restart ntopng.service
        for UNIT in $(systemctl list-units nprobe\* --all | grep nprobe | grep '\.service' | tr -s ' '  | cut -f 2 --delimiter=' ')
        do
                echo " Restart $UNIT"
                systemctl restart $UNIT
        done
else
        echo "No new devices detected"
fi
EOFile
  • Renderlo eseguibile
chmod +x /usr/local/sbin/make_snmp_mappings.sh


  • Creare il link allo script che crea il file, o copiare e customizzare l'originale per usare la descrizione dell'interfaccia:
ln -s /usr/bin/build_snmp_mappings.sh  /usr/local/sbin/build_snmp_mappings.sh
  • Provare lo script
/usr/local/sbin/make_snmp_mappings.sh $NTOPSNMPCOMMUNITY
  • Creare il cronjob
cat > /etc/cron.d/ntopngSnmpMappings <<EOFile 
# Check for new SNMP deviced and map them every 5 minutes
*/5 * * * * root /usr/local/sbin/make_snmp_mappings.sh $NTOPSNMPCOMMUNITY > /dev/null
EOFile

Modifica dello script per ottenere le descrizioni delle interfacce dall'alias SNMP

  • Creare il fiel di patch
cat > build_snmp_mappings.sh.diff <<EOFile
--- /usr/bin/build_snmp_mappings.sh     2024-01-11 10:24:37.000000000 +0100
+++ /usr/local/sbin/build_snmp_mappings.sh      2023-12-21 17:56:36.042388664 +0100
@@ -41,9 +41,14 @@
     fi

     IFNAME_OID="1.3.6.1.2.1.2.2.1.2"
+    IF_POS="12"
     ALIAS_OID="1.3.6.1.2.1.31.1.1.1.18"
+    ALIAS_POS="13"
+    # .1.3.6.1.2.1.31.1.1.1.18.1 = STRING: "Vodafone Standby"
+    OID=$ALIAS_OID
+    POS=$ALIAS_POS

-    $WALK -On -v $VERSION -c $COMMUNITY $AGENT $IFNAME_OID | cut -d '.' -f 12 | cut -d ' ' -f 1,4 |
+    $WALK -On -v $VERSION -c $COMMUNITY $AGENT $OID | cut -d '.' -f $POS | cut -d ' ' -f 1,4 |
        while IFS= read -r line; do
            echo "$AGENT $line"
        done
EOFile
  • Applicare il patch:
/bin/rm -i /usr/local/sbin/build_snmp_mappings.sh
cp /usr/bin/build_snmp_mappings.sh /usr/local/sbin/build_snmp_mappings.sh
cd /usr/local/sbin/ && patch build_snmp_mappings.sh build_snmp_mappings.sh.diff
  • Verificare
diff /usr/bin/build_snmp_mappings.sh /usr/local/sbin/build_snmp_mappings.sh