pridano vyčítání uživatelskeho popisu portu
parent
bf01254a79
commit
648bb5da70
|
|
@ -3,7 +3,9 @@ package jnet.server.snmp;
|
|||
import java.io.IOException;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import jnet.lib.object.Interface;
|
||||
import org.snmp4j.CommunityTarget;
|
||||
import org.snmp4j.PDU;
|
||||
|
|
@ -23,38 +25,58 @@ public class SNMPInterfaceScanner {
|
|||
private String address;
|
||||
private String community;
|
||||
private int version;
|
||||
private String customAliasOid;
|
||||
|
||||
// OID pro rozhraní v IF-MIB
|
||||
private static final String IF_INDEX_OID = "1.3.6.1.2.1.2.2.1.1"; // ifIndex
|
||||
private static final String IF_DESCR_OID = "1.3.6.1.2.1.2.2.1.2"; // ifDescr
|
||||
private static final String IF_SPEED_OID = "1.3.6.1.2.1.2.2.1.5"; // ifSpeed
|
||||
private static final String IF_TYPE_OID = "1.3.6.1.2.1.2.2.1.3"; // ifType
|
||||
private static final String IF_MAC_OID = "1.3.6.1.2.1.2.2.1.6"; // ifPhysAddress (opraveno)
|
||||
private static final String IF_STATUS_OID = "1.3.6.1.2.1.2.2.1.8"; // ifOperStatus
|
||||
private static final String IF_STATUS_ADM_OID = "1.3.6.1.2.1.2.2.1.7"; // ifAdminStatus
|
||||
private static final String IF_LAST_CHANGE_OID = "1.3.6.1.2.1.2.2.1.9"; // ifLastChange (TimeTicks)
|
||||
private static final String IF_INDEX_OID = "1.3.6.1.2.1.2.2.1.1";
|
||||
private static final String IF_DESCR_OID = "1.3.6.1.2.1.2.2.1.2";
|
||||
private static final String IF_SPEED_OID = "1.3.6.1.2.1.2.2.1.5";
|
||||
private static final String IF_TYPE_OID = "1.3.6.1.2.1.2.2.1.3";
|
||||
private static final String IF_MAC_OID = "1.3.6.1.2.1.2.2.1.6";
|
||||
private static final String IF_STATUS_OID = "1.3.6.1.2.1.2.2.1.8";
|
||||
private static final String IF_STATUS_ADM_OID = "1.3.6.1.2.1.2.2.1.7";
|
||||
private static final String IF_LAST_CHANGE_OID = "1.3.6.1.2.1.2.2.1.9";
|
||||
private static final String IF_ALIAS_OID = "1.3.6.1.2.1.31.1.1.1.18";
|
||||
|
||||
// Mapa prefixů pro zkrácení názvů rozhraní
|
||||
private static final Map<String, String> PREFIX_MAP = new HashMap<>();
|
||||
static {
|
||||
PREFIX_MAP.put("GIGABITETHERNET", "GE");
|
||||
PREFIX_MAP.put("TEN-GIGABITETHERNET", "TE");
|
||||
PREFIX_MAP.put("TENGIGABITETHERNET", "TE");
|
||||
PREFIX_MAP.put("FASTETHERNET", "FE");
|
||||
PREFIX_MAP.put("ETHERNET", "E");
|
||||
PREFIX_MAP.put("PORT-CHANNEL", "PO");
|
||||
PREFIX_MAP.put("PORTCHANNEL", "PO");
|
||||
PREFIX_MAP.put("VLAN", "VL");
|
||||
PREFIX_MAP.put("LOOPBACK", "LO");
|
||||
PREFIX_MAP.put("NULL", "NU");
|
||||
PREFIX_MAP.put("TUNNEL", "TU");
|
||||
// Přidejte další dle potřeby
|
||||
}
|
||||
|
||||
public SNMPInterfaceScanner(String address, String port, String community, int version) throws IOException {
|
||||
this(address, port, community, version, null);
|
||||
}
|
||||
|
||||
public SNMPInterfaceScanner(String address, String port, String community, int version, String customAliasOid) throws IOException {
|
||||
this.address = address + "/" + port;
|
||||
this.community = community;
|
||||
this.version = version;
|
||||
this.customAliasOid = customAliasOid;
|
||||
|
||||
TransportMapping<UdpAddress> transport = new DefaultUdpTransportMapping();
|
||||
snmp = new Snmp(transport);
|
||||
transport.listen();
|
||||
}
|
||||
|
||||
/**
|
||||
* Získání detailních informací o všech interfaces
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public List<Interface> scanInterfaces() throws IOException {
|
||||
List<Interface> list = new ArrayList<>();
|
||||
CommunityTarget target = createTarget();
|
||||
OID oid = new OID(IF_INDEX_OID); // Start na ifIndex
|
||||
OID oid = new OID(IF_INDEX_OID);
|
||||
boolean finished = false;
|
||||
|
||||
String aliasOidToUse = (customAliasOid != null) ? customAliasOid : IF_ALIAS_OID;
|
||||
|
||||
try {
|
||||
while (!finished) {
|
||||
PDU pdu = new PDU();
|
||||
|
|
@ -68,12 +90,9 @@ public class SNMPInterfaceScanner {
|
|||
}
|
||||
|
||||
VariableBinding vb = response.getResponse().get(0);
|
||||
if (vb == null) {
|
||||
break;
|
||||
}
|
||||
if (vb == null) break;
|
||||
OID nextOid = vb.getOid();
|
||||
|
||||
// Pokud jsme mimo tabulku, ukončit cyklus
|
||||
if (!nextOid.startsWith(new OID(IF_INDEX_OID))) {
|
||||
finished = true;
|
||||
continue;
|
||||
|
|
@ -81,7 +100,7 @@ public class SNMPInterfaceScanner {
|
|||
|
||||
int ifIndex = vb.getVariable().toInt();
|
||||
|
||||
// Načtení jednotlivých atributů - ošetřeno proti null
|
||||
// Načtení atributů
|
||||
String ifDescr = getSnmpValue(IF_DESCR_OID + "." + ifIndex, target);
|
||||
String ifType = getSnmpValue(IF_TYPE_OID + "." + ifIndex, target);
|
||||
String ifStatus = getSnmpValue(IF_STATUS_OID + "." + ifIndex, target);
|
||||
|
|
@ -89,8 +108,23 @@ public class SNMPInterfaceScanner {
|
|||
String ifMacRaw = getSnmpValue(IF_MAC_OID + "." + ifIndex, target);
|
||||
String ifStatusAdm = getSnmpValue(IF_STATUS_ADM_OID + "." + ifIndex, target);
|
||||
String ifLastChange = getSnmpValue(IF_LAST_CHANGE_OID + "." + ifIndex, target);
|
||||
String ifAlias = getSnmpValue(aliasOidToUse + "." + ifIndex, target);
|
||||
|
||||
// Parsování rychlosti s ošetřením
|
||||
// Původní popis
|
||||
String fullDescr = (ifDescr != null) ? ifDescr : "Neznámé";
|
||||
|
||||
// Zkrácená verze
|
||||
String shortDescr = shortenInterfaceDescription(ifDescr);
|
||||
if (shortDescr == null || shortDescr.isEmpty()) {
|
||||
shortDescr = fullDescr;
|
||||
}
|
||||
|
||||
// Alias – ponecháme prázdný, pokud chybí
|
||||
if (ifAlias == null) {
|
||||
ifAlias = "";
|
||||
}
|
||||
|
||||
// Zpracování rychlosti
|
||||
long ifSpeed = 0;
|
||||
if (ifSpeedRaw != null && !ifSpeedRaw.equals("Neznámé")) {
|
||||
try {
|
||||
|
|
@ -100,62 +134,47 @@ public class SNMPInterfaceScanner {
|
|||
}
|
||||
}
|
||||
|
||||
// Formátování MAC adresy
|
||||
String ifMac = formatMac(ifMacRaw);
|
||||
|
||||
// Formátování TimeTicks (ifLastChange)
|
||||
String lastChangeFormatted = formatTimeTicks(ifLastChange);
|
||||
|
||||
// Vytvoření objektu Interface
|
||||
list.add(new Interface(
|
||||
ifIndex, // index
|
||||
ifDescr != null ? ifDescr : "Neznámé", // description
|
||||
getInterfaceStatus(ifStatus), // operStatus
|
||||
formatIfSpeed(ifSpeed), // speed
|
||||
getInterfaceType(ifType), // type
|
||||
ifMac, // mac
|
||||
convertIfAdminStatus(ifStatusAdm), // adminStatus
|
||||
lastChangeFormatted // lastChange (formátovaný)
|
||||
ifIndex,
|
||||
fullDescr,
|
||||
shortDescr,
|
||||
getInterfaceStatus(ifStatus),
|
||||
formatIfSpeed(ifSpeed),
|
||||
getInterfaceType(ifType),
|
||||
ifMac,
|
||||
convertIfAdminStatus(ifStatusAdm),
|
||||
lastChangeFormatted,
|
||||
ifAlias // může být prázdný
|
||||
));
|
||||
|
||||
// Posuneme OID na další položku
|
||||
oid = nextOid;
|
||||
}
|
||||
} finally {
|
||||
// Zajištění uvolnění zdrojů
|
||||
if (snmp != null) {
|
||||
try {
|
||||
snmp.close();
|
||||
} catch (IOException e) {
|
||||
// logování chyby
|
||||
// log
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pomocná metoda pro vytvoření SNMP cíle (target) s ohledem na verzi
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private CommunityTarget createTarget() {
|
||||
CommunityTarget target = new CommunityTarget();
|
||||
target.setCommunity(new OctetString(community));
|
||||
target.setAddress(new UdpAddress(address));
|
||||
// Nastavení SNMP verze podle parametru
|
||||
if (version == 1) {
|
||||
target.setVersion(SnmpConstants.version1);
|
||||
} else {
|
||||
target.setVersion(SnmpConstants.version2c); // výchozí v2c
|
||||
}
|
||||
target.setVersion((version == 1) ? SnmpConstants.version1 : SnmpConstants.version2c);
|
||||
target.setRetries(2);
|
||||
target.setTimeout(1500);
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Získání hodnoty pro dané OID pomocí GET. Vrací null v případě chyby.
|
||||
*/
|
||||
private String getSnmpValue(String oid, CommunityTarget target) throws IOException {
|
||||
PDU pdu = new PDU();
|
||||
pdu.add(new VariableBinding(new OID(oid)));
|
||||
|
|
@ -168,12 +187,26 @@ public class SNMPInterfaceScanner {
|
|||
return vb.getVariable().toString();
|
||||
}
|
||||
}
|
||||
return null; // nebo "Neznámé", ale raději null pro ošetření
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Převede číselné označení typu rozhraní na název
|
||||
* Zkrátí název rozhraní podle definovaných prefixů.
|
||||
* Např. "GigabitEthernet1/0/15" -> "GE1/0/15"
|
||||
*/
|
||||
private static String shortenInterfaceDescription(String desc) {
|
||||
if (desc == null || desc.isEmpty()) return desc;
|
||||
String upper = desc.toUpperCase();
|
||||
for (Map.Entry<String, String> entry : PREFIX_MAP.entrySet()) {
|
||||
if (upper.startsWith(entry.getKey())) {
|
||||
String rest = desc.substring(entry.getKey().length());
|
||||
return entry.getValue() + rest;
|
||||
}
|
||||
}
|
||||
return desc;
|
||||
}
|
||||
|
||||
// ---------- Pomocné metody (beze změny) ----------
|
||||
private String getInterfaceType(String type) {
|
||||
if (type == null) return "Neznámé";
|
||||
switch (type) {
|
||||
|
|
@ -208,9 +241,6 @@ public class SNMPInterfaceScanner {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Převede číselné označení operativního stavu rozhraní na název
|
||||
*/
|
||||
private String getInterfaceStatus(String status) {
|
||||
if (status == null) return "Neznámé";
|
||||
switch (status) {
|
||||
|
|
@ -225,9 +255,6 @@ public class SNMPInterfaceScanner {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Převede administrátorský stav na text
|
||||
*/
|
||||
private static String convertIfAdminStatus(String status) {
|
||||
if (status == null) return "Unknown";
|
||||
switch (status) {
|
||||
|
|
@ -238,14 +265,9 @@ public class SNMPInterfaceScanner {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formátuje rychlost (v bitech za sekundu) do čitelné podoby.
|
||||
*/
|
||||
private static String formatIfSpeed(long ifSpeed) {
|
||||
String[] UNITS = {"bps", "kbps", "Mbps", "Gbps", "Tbps"};
|
||||
if (ifSpeed <= 0) {
|
||||
return "-";
|
||||
}
|
||||
if (ifSpeed <= 0) return "-";
|
||||
int unitIndex = 0;
|
||||
double speed = ifSpeed;
|
||||
while (speed >= 1000 && unitIndex < UNITS.length - 1) {
|
||||
|
|
@ -257,18 +279,12 @@ public class SNMPInterfaceScanner {
|
|||
return df.format(roundedSpeed) + " " + UNITS[unitIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Formátuje MAC adresu z řetězce (hex bez oddělovačů) do tvaru xx:xx:xx:xx:xx:xx
|
||||
*/
|
||||
private static String formatMac(String macRaw) {
|
||||
if (macRaw == null || macRaw.isEmpty() || macRaw.equals("Neznámé")) {
|
||||
return "Neznámé";
|
||||
}
|
||||
// Očekáváme hex řetězec bez oddělovačů, např. "001122334455"
|
||||
String cleaned = macRaw.replaceAll("[^0-9a-fA-F]", "");
|
||||
if (cleaned.length() != 12) {
|
||||
return macRaw; // vrátíme původní, pokud neodpovídá délce
|
||||
}
|
||||
if (cleaned.length() != 12) return macRaw;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < cleaned.length(); i += 2) {
|
||||
if (i > 0) sb.append(":");
|
||||
|
|
@ -277,21 +293,15 @@ public class SNMPInterfaceScanner {
|
|||
return sb.toString().toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Převede TimeTicks (setiny sekundy) na čitelný řetězec "X dní, Y hodin, Z minut, W sekund"
|
||||
* Pokud je vstup null nebo nečíselný, vrátí "Neznámé".
|
||||
*/
|
||||
private static String formatTimeTicks(String timeTicks) {
|
||||
if (timeTicks == null || timeTicks.isEmpty()) {
|
||||
return "Neznámé";
|
||||
}
|
||||
if (timeTicks == null || timeTicks.isEmpty()) return "Neznámé";
|
||||
long ticks;
|
||||
try {
|
||||
ticks = Long.parseLong(timeTicks);
|
||||
} catch (NumberFormatException e) {
|
||||
return "Neznámé";
|
||||
}
|
||||
long totalSeconds = ticks / 100; // setiny → sekundy
|
||||
long totalSeconds = ticks / 100;
|
||||
long days = totalSeconds / 86400;
|
||||
long hours = (totalSeconds % 86400) / 3600;
|
||||
long minutes = (totalSeconds % 3600) / 60;
|
||||
|
|
|
|||
Loading…
Reference in New Issue