public SNMPRawEntryDTO(ISNMPDeviceDataDTO regardingobj, string oid, string data, EnumSNMPOIDType datatype) { RegardingObject = regardingobj; OID = oid; ValueData = data; DataType = datatype; }
private void GetBasicInfo(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo) { IOIDSettingDTO SelectedSetting; IList <Action <IList <string>, string, object> > MappingHandlers; //Get setting of interest SelectedSetting = OIDSettings["DeviceBasicInfo"]; //Define handle collection in order MappingHandlers = new List <Action <IList <string>, string, object> >(); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).Description = y; }); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).OIDobjectID = y; }); MappingHandlers.Add(null); MappingHandlers.Add(null); MappingHandlers.Add((x, y, z) => { EnumDeviceType dvt; IDeviceTopologyInfoDTO Data = (IDeviceTopologyInfoDTO)z; Data.DeviceName = y; Data.DeviceType = Enum.TryParse <EnumDeviceType>(Regex.Match(y, @"^.*?(?=[0-9])").ToString(), out dvt) ? dvt : EnumDeviceType.Unknown; }); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).Location = y; }); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).OSIImplementedLayers = (EnumOSILayers)Enum.Parse(typeof(EnumOSILayers), y); }); //Add network known data TopologyInfo.DeviceIPAndMask = $"{Device.TargetIP}/{Device.NetworkMask}"; if (RegardingObject.ARPTable.Any(x => x.Value == $"{Device.TargetIP}")) { TopologyInfo.DeviceMAC = RegardingObject.ARPTable.Where(x => x.Value == $"{Device.TargetIP}").First().Key; } //Collect data mapping with handlers StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers); }
private void SNMPWalkByIP(ISNMPDeviceSettingDTO SNMPSettingEntry) { IList <IPAddress> IPinventory; OctetString Community; //Compute full list of devices that have responded to ARP request IPinventory = ModelHelper.GenerateHostList(SNMPSettingEntry.InitialIP, SNMPSettingEntry.FinalIP, SNMPSettingEntry.NetworkMask, ARPTable.Values.ToList()); Community = new OctetString(SNMPSettingEntry.CommunityString); foreach (IPAddress target in IPinventory) { ISNMPDeviceDataDTO device = BuildSNMPDevice(target, SNMPSettingEntry.NetworkMask); try { SNMPWalkByOIDSetting(Community, SNMPSettingEntry, device); } catch (SnmpException e) { NotifyError(e); //Device entry not containing full info for processing DeviceData.Remove(target.ToString()); continue; } } }
private void ShowData(ISNMPDeviceDataDTO data) { if (data != null) { ViewHelper.RedirectConsoleToFile(true); Console.WriteLine($"SNMP device {data.TargetIP}.\n"); ViewHelper.RedirectConsoleToFile(false); } }
public IList <ISNMPDeviceDataDTO> GetSNMPDeviceData(string key = null) { if (string.IsNullOrWhiteSpace(key)) { return(Model.DeviceData.Values.ToList()); } else { ISNMPDeviceDataDTO res = null; Model.DeviceData.TryGetValue(key, out res); return(res == null ? new List <ISNMPDeviceDataDTO>() : new List <ISNMPDeviceDataDTO>(new[] { res })); } }
public static void OIDEntryProcessor(ISNMPDeviceDataDTO Device, object StrategyDTOobject, IOIDSettingDTO SelectedSetting, IList <Action <IList <string>, string, object> > MappingHandler) { string[] RootEntries = SelectedSetting.IndexedOIDSettings.Keys.ToArray(); //Loop of each subset for (int i = 0; i < RootEntries.Length; i++) { //1) select OID data subset IList <ISNMPRawEntryDTO> SelectedDeviceOID = OIDDataSelector(Device, RootEntries[i], i + 1 == RootEntries.Length ? RootEntries[i] : RootEntries[i + 1]); if (SelectedDeviceOID != null) { //2) apply specific handle on entryparser OIDEntryParser(SelectedDeviceOID, new CustomPair <string, IList <EnumSNMPOIDIndexType> >(RootEntries[i], SelectedSetting.IndexedOIDSettings[RootEntries[i]]), StrategyDTOobject, MappingHandler[i]); } } }
private void GetPortMACAddress(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo) { IOIDSettingDTO SelectedSetting; IList <Action <IList <string>, string, object> > MappingHandlers; //Get setting of interest SelectedSetting = OIDSettings["PhysPortMACAddress"]; //Define handle collection in order MappingHandlers = new List <Action <IList <string>, string, object> >(); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortMACAddress.Add(x[0], y); }); //Define container if necesary TopologyInfo.PortMACAddress = new Dictionary <string, string>(); //Collect data mapping with handlers StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers); }
private void GetLearnedMACAddresses(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo) { IOIDSettingDTO SelectedSetting; IList <Action <IList <string>, string, object> > MappingHandlers; //Get setting of interest SelectedSetting = OIDSettings["LearnedMACByPhysPortID"]; //Define handle collection in order MappingHandlers = new List <Action <IList <string>, string, object> >(); MappingHandlers.Add(LearnedAddressMapper); //Initialize container if necesary TopologyInfo.PortLearnedAddresses = new Dictionary <string, IDictionary <string, string> >(); //Collect data mapping with handlers StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers); }
private void GetAggregateDestinations(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo) { IOIDSettingDTO SelectedSetting; IList <Action <IList <string>, string, object> > MappingHandlers; //Get setting of interest SelectedSetting = OIDSettings["TrunkDestinationsCDP"]; //Define handle collection in order MappingHandlers = new List <Action <IList <string>, string, object> >(); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortAggregateDestinations.Add(x[0], new CustomPair <string, string>(y, null)); }); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortAggregateDestinations[x[0]].Second = y; }); //Define container if necesary TopologyInfo.PortAggregateDestinations = new Dictionary <string, CustomPair <string, string> >(); //Collect data mapping with handlers StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers); }
private void SNMPRunAgent(OctetString Community, IOIDSettingDTO OIDSetting, ISNMPDeviceDataDTO SNMPDeviceData) { bool nextEntry = true; AgentParameters AgParam; Pdu pdu; SnmpV2Packet Result; Oid indexOid = new Oid(OIDSetting.InitialOID); //Walk control Oid finalOid = new Oid(OIDSetting.FinalOID); AgParam = new AgentParameters(Community); AgParam.Version = SnmpVersion.Ver2; // Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3) pdu = new Pdu(PduType.GetBulk); pdu.NonRepeaters = 0; //NonRepeaters tells how many OID asociated values (leafs of this object) get. 0 is all pdu.MaxRepetitions = 5; // MaxRepetitions tells the agent how many Oid/Value pairs to return in the response packet. pdu.RequestId = 1; using (UdpTarget UDPtarget = new UdpTarget(SNMPDeviceData.TargetIP, DefaultPort, DefaultTimeout, DefaultRetries)) { while (nextEntry) { pdu.VbList.Add(indexOid); //Add starting OID for request try { Result = (SnmpV2Packet)UDPtarget.Request(pdu, AgParam); nextEntry = SNMPDecodeData(Result, indexOid, finalOid, OIDSetting.InclusiveInterval, SNMPDeviceData); } catch //(SnmpException e) { throw; } finally { //Prepare PDU object for iteration. Otherwise, wipe contents of pdu pdu.RequestId++; pdu.VbList.Clear(); } } } }
public static IList <ISNMPRawEntryDTO> OIDDataSelector(ISNMPDeviceDataDTO Device, string currentRoot, string nextRoot) { if (Device.SNMPRawDataEntries == null) { return(null); } if (currentRoot != nextRoot) { return(Device.SNMPRawDataEntries.Where(x => CompareOID(x.Key, currentRoot) >= 0 && CompareOID(x.Key, nextRoot) < 0) .OrderBy(x => x.Key, Comparer <string> .Create(CompareOID)) .Select(x => x.Value) .ToList()); } else { return(Device.SNMPRawDataEntries.Where(x => CompareOID(x.Key, currentRoot) >= 0 && CompareOID(x.Key, nextRoot) <= 0) .OrderBy(x => x.Key, Comparer <string> .Create(CompareOID)) .Select(x => x.Value) .ToList()); } }
private bool SNMPDecodeData(SnmpV2Packet Result, Oid indexOid, Oid finalOid, bool InclusiveInterval, ISNMPDeviceDataDTO SNMPDeviceData) { bool nextEntry = true; if (Result != null && Result.Pdu.ErrorStatus == 0) { // Walk through returned variable bindings foreach (Vb ResBinding in Result.Pdu.VbList) { // Check that retrieved Oid is higher than the limit or is the last block of leafs if (ResBinding.Oid < finalOid || finalOid.IsRootOf(ResBinding.Oid) && InclusiveInterval) { //Check OID Value Type. If unknown we break loop and storage EnumSNMPOIDType OIDType = (EnumSNMPOIDType)Enum.Parse(typeof(EnumSNMPOIDType), SnmpConstants.GetTypeName(ResBinding.Value.Type)); if (OIDType != EnumSNMPOIDType.Unknown) { ISNMPRawEntryDTO SNMPRawData = SNMPDeviceData.BuildSNMPRawEntry(ResBinding.Oid.ToString(), ResBinding.Value.ToString(), OIDType); } else { nextEntry = false; break; } //Check if we have already drilled down all contents if (ResBinding.Value.Type != SnmpConstants.SMI_ENDOFMIBVIEW) { indexOid.Set(ResBinding.Oid); //If we use = operator, we are changing references! In that case, ref keyword is mandatory nextEntry = true; } } else { nextEntry = false; break; } } } else { //Console.WriteLine("Error in SNMP reply. Error {0} index {1}", Result.Pdu.ErrorStatus, Result.Pdu.ErrorIndex); } return(nextEntry); }
private void SNMPWalkByOIDSetting(OctetString Community, ISNMPDeviceSettingDTO SNMPSettingEntry, ISNMPDeviceDataDTO SNMPDeviceData) { //Get all OID requested for all processing algorithms IEnumerable <IOIDSettingDTO> OIDSettingCollection = Processes.Values.SelectMany(x => x.OIDSettings.Values); foreach (IOIDSettingDTO OIDSetting in OIDSettingCollection) { try { SNMPRunAgent(Community, OIDSetting, SNMPDeviceData); } catch //(SnmpException e) { throw; } } }
private void GetVLANInfo(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo) { //Key: Port ID, Value: Tuple of VLAN ID and VLAN name //IDictionary<string, CustomPair<string, string>> VLANByInterfaceID { get; set; } IOIDSettingDTO SelectedSetting; IList <Action <IList <string>, string, object> > MappingHandlers; IDictionary <string, string> VLANMappingResult; SelectedSetting = OIDSettings["VLANDescription"]; MappingHandlers = new List <Action <IList <string>, string, object> >(); #region VLAN description MappingHandlers.Add((x, y, z) => { ((IDictionary <string, string>)z).Add(x[0], y); }); TopologyInfo.VLANInventory = new Dictionary <string, string>(); StrategyHelper.OIDEntryProcessor(Device, TopologyInfo.VLANInventory, SelectedSetting, MappingHandlers); #endregion #region VLAN Mapping //By default, all ports are access ports. Initialize dictionary MappingHandlers.Clear(); MappingHandlers.Add((x, y, z) => { ((IDictionary <string, string>)z).Add(x[0], y); }); VLANMappingResult = new Dictionary <string, string>(); SelectedSetting = OIDSettings["VLANMapping"]; StrategyHelper.OIDEntryProcessor(Device, VLANMappingResult, SelectedSetting, MappingHandlers); TopologyInfo.PortVLANMapping = new Dictionary <string, List <string> >(); if (VLANMappingResult.Count != 0) { foreach (KeyValuePair <string, string> VLANMaskInfo in VLANMappingResult) { string[] positions = StrategyHelper.GetFlagArrayPositions(VLANMaskInfo.Value); for (int i = 0; i < positions.Length; i++) { if (TopologyInfo.PortDescriptions.ContainsKey(positions[i])) { if (TopologyInfo.PortVLANMapping.ContainsKey(positions[i])) { TopologyInfo.PortVLANMapping[positions[i]].Add(VLANMaskInfo.Key); } else { TopologyInfo.PortVLANMapping.Add(positions[i], new List <string>() { VLANMaskInfo.Key }); } } } } } #endregion }
private void GetPortIDInfo(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo) { IOIDSettingDTO SelectedSetting; IList <Action <IList <string>, string, object> > MappingHandlers; IDictionary <string, IList <string> > LACPResults; IDictionary <string, IList <string> > PortHierarchyResults; #region Port description SelectedSetting = OIDSettings["PhysPortDescription"]; MappingHandlers = new List <Action <IList <string>, string, object> >(); MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortDescriptions.Add(x[0], y); }); TopologyInfo.PortDescriptions = new Dictionary <string, string>(); StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers); #endregion #region Port typology //By default, all ports are access ports. Initialize dictionary TopologyInfo.PortSettings = new Dictionary <string, CustomPair <EnumPhysPortType, string> >(); foreach (string portID in TopologyInfo.PortMACAddress.Keys) { TopologyInfo.PortSettings.Add(portID, new CustomPair <EnumPhysPortType, string>(EnumPhysPortType.Access, null)); } //Detect ports without MAC --> type loopback IEnumerable <string> LoopbackPorts = TopologyInfo.PortMACAddress.Where(x => string.IsNullOrEmpty(x.Value)).Select(x => x.Key); foreach (string loopbackitem in LoopbackPorts) { TopologyInfo.PortSettings[loopbackitem].First = EnumPhysPortType.Loopback; } // Adrress by port > LearnedMACThreshold --> InferedTrunks IEnumerable <string> InferedTrunks = TopologyInfo.PortLearnedAddresses.Where(x => x.Value.Count > LearnedMACThreshold).Select(x => x.Key); foreach (string inferedtrunkitem in InferedTrunks) { TopologyInfo.PortSettings[inferedtrunkitem].First = EnumPhysPortType.InferedTrunk; } // VirtualPort - Trunk MappingHandlers.Clear(); MappingHandlers.Add(PortHierarchyMapper); PortHierarchyResults = new Dictionary <string, IList <string> >(); SelectedSetting = OIDSettings["PortHierarchy"]; StrategyHelper.OIDEntryProcessor(Device, PortHierarchyResults, SelectedSetting, MappingHandlers); if (PortHierarchyResults.Count != 0) { //Virtual ports if (PortHierarchyResults.ContainsKey("0")) { foreach (string vlanport in PortHierarchyResults["0"]) { TopologyInfo.PortSettings[vlanport].First = EnumPhysPortType.VirtualPort; } } //Trunk ports (any protocol) IEnumerable <KeyValuePair <string, IList <string> > > trunkports = PortHierarchyResults.Where(x => x.Value.Count > 1 && x.Key != "0").Where(x => !PortHierarchyResults["0"].Contains(x.Key)); foreach (KeyValuePair <string, IList <string> > trunkentry in trunkports) { TopologyInfo.PortSettings[trunkentry.Key].First = EnumPhysPortType.Trunk; foreach (string aggregateentry in trunkentry.Value) { TopologyInfo.PortSettings[aggregateentry].First = EnumPhysPortType.Aggregate; TopologyInfo.PortSettings[aggregateentry].Second = trunkentry.Key; } } } //LACP - Aggregate MappingHandlers.Clear(); MappingHandlers.Add(LACPAssignmentMapper); LACPResults = new Dictionary <string, IList <string> >(); SelectedSetting = OIDSettings["LACPSetting"]; StrategyHelper.OIDEntryProcessor(Device, LACPResults, SelectedSetting, MappingHandlers); if (LACPResults.Count != 0) { foreach (KeyValuePair <string, IList <string> > lacpentry in LACPResults) { TopologyInfo.PortSettings[lacpentry.Key].First = EnumPhysPortType.LACP; foreach (string groupedport in lacpentry.Value) { if (TopologyInfo.PortSettings.ContainsKey(groupedport)) { TopologyInfo.PortSettings[groupedport].First = EnumPhysPortType.Aggregate; TopologyInfo.PortSettings[groupedport].Second = lacpentry.Key; } } } } #endregion }