/// <summary>
        /// Formats as block-formatted string of an <see cref="IWirelessPeerInfos" />.
        /// </summary>
        /// <param name="wirelessPeerInfos">The data to format.</param>
        /// <returns>The formatted string.</returns>
        public string Format(IWirelessPeerInfos wirelessPeerInfos)
        {
            if (wirelessPeerInfos == null)
            {
                return("<null>");
            }

            StringBuilder returnBuilder = new StringBuilder(256);

            returnBuilder.Append("Wireless Peer Infos: ");

            if (wirelessPeerInfos.Details == null)
            {
                returnBuilder.Append(NotAvailableString);
            }
            else
            {
                foreach (var item in wirelessPeerInfos.Details)
                {
                    returnBuilder.AppendLine().AppendLine(SnmpAbstraction.IndentLines(this.Format(item)));
                }
            }

            return(returnBuilder.ToString());
        }
示例#2
0
        /// <summary>
        /// Sends an SNMP request for the given OIDs.
        /// </summary>
        /// <param name="oids">The OIDs to request via SNMP.</param>
        /// <returns>The response SnmpPacket with the data.</returns>
        private SnmpPacket SendRequest(IEnumerable <Oid> oids)
        {
            var pduType = PduType.Get;

            Interlocked.CompareExchange(ref nextRequestId, 0, int.MaxValue); // wrap the request ID
            var requestId = Interlocked.Increment(ref nextRequestId);
            Pdu pdu       = new Pdu(pduType)
            {
                RequestId = requestId // 0 --> generate random ID on encode, anyhting else --> use that value for request ID
            };

            foreach (Oid item in oids)
            {
                pdu.VbList.Add(item);
            }

            log.Debug($"Using request ID '{requestId}' for PDU with {pdu.VbCount} elements to {this.Address}");

            using (var target = new UdpTarget((IPAddress)this.Address, this.Options.Port, Convert.ToInt32(this.Options.Timeout.TotalMilliseconds), this.Options.Retries))
            {
                SnmpPacket result = target.Request(pdu, this.QueryParameters);

                SnmpAbstraction.RecordSnmpRequest(this.Address, pdu, result);

                return(result);
            }
        }
        /// <summary>
        /// Formats as block-formatted string of an <see cref="IInterfaceDetails" />.
        /// </summary>
        /// <param name="interfaceDetails">The data to format.</param>
        /// <returns>The formatted string.</returns>
        public string Format(IInterfaceDetails interfaceDetails)
        {
            if (interfaceDetails == null)
            {
                return("<null>");
            }

            StringBuilder returnBuilder = new StringBuilder(256);

            returnBuilder.Append("Interface Details: ");

            if (interfaceDetails.Details == null)
            {
                returnBuilder.Append(NotAvailableString);
            }
            else
            {
                foreach (var item in interfaceDetails.Details)
                {
                    returnBuilder.AppendLine().AppendLine(SnmpAbstraction.IndentLines(this.Format(item)));
                }
            }

            return(returnBuilder.ToString());
        }
        /// <summary>
        /// Formats as block-formatted string of an <see cref="IBgpPeers" />.
        /// </summary>
        /// <param name="bgpPeers">The data to format.</param>
        /// <returns>The formatted string.</returns>
        public string Format(IBgpPeers bgpPeers)
        {
            if (bgpPeers == null)
            {
                return("<null>");
            }

            StringBuilder returnBuilder = new StringBuilder((bgpPeers.Details?.Count ?? 1) * 128);

            returnBuilder.Append("BGP Peers:");

            if (bgpPeers.Details.Count == 0)
            {
                returnBuilder.Append(" No BGP peers found.");
            }
            else
            {
                foreach (var item in bgpPeers.Details)
                {
                    returnBuilder.AppendLine().AppendLine(SnmpAbstraction.IndentLines(this.Format(item)));
                }
            }

            return(returnBuilder.ToString());
        }
        /// <summary>
        /// Formats as block-formatted string of an <see cref="ILinkDetails" />.
        /// </summary>
        /// <param name="linkDetails">The data to format.</param>
        /// <returns>The formatted string.</returns>
        public string Format(ILinkDetails linkDetails)
        {
            if (linkDetails == null)
            {
                return("<null>");
            }

            StringBuilder returnBuilder = new StringBuilder((linkDetails.Details?.Count ?? 1) * 128);

            returnBuilder.Append("Link Details:");

            if (linkDetails.Details.Count == 0)
            {
                returnBuilder.Append(" No link found.");
            }
            else
            {
                foreach (var item in linkDetails.Details)
                {
                    returnBuilder.AppendLine().AppendLine(SnmpAbstraction.IndentLines(this.Format(item)));
                }
            }

            return(returnBuilder.ToString());
        }
示例#6
0
        /// <inheritdoc />
        public override string ToString()
        {
            StringBuilder returnBuilder = new StringBuilder((this.InterfaceDetailsBacking?.Count ?? 1) * 128);

            returnBuilder.Append("Interface Details:");

            if (!this.interfaceDetailsQueried)
            {
                returnBuilder.Append(" Not yet queried");
            }
            else
            {
                if (this.InterfaceDetailsBacking == null)
                {
                    returnBuilder.Append(" Not yet retrieved");
                }
                else
                {
                    foreach (var item in this.InterfaceDetailsBacking)
                    {
                        returnBuilder.AppendLine().AppendLine(SnmpAbstraction.IndentLines(item.ToString()));
                    }
                }
            }

            return(returnBuilder.ToString());
        }
示例#7
0
        /// <inheritdoc />
        public override string ToString()
        {
            StringBuilder returnBuilder = new StringBuilder(1024);

            returnBuilder.AppendLine("Overall statistics:");
            returnBuilder.AppendLine("===================");
            returnBuilder.AppendLine(SnmpAbstraction.IndentLines(this.overallResultsBacking.ToString()));

            returnBuilder.AppendLine().AppendLine("Per request-type statistics:");
            returnBuilder.AppendLine("============================");
            foreach (var item in this.resultsPerRequestTypeInterfaced)
            {
                returnBuilder.AppendLine().Append(item.Key).AppendLine(":");
                returnBuilder.AppendLine(SnmpAbstraction.IndentLines(item.Value.ToString()));
            }

            returnBuilder.AppendLine().AppendLine("Per device statistics:");
            returnBuilder.AppendLine("======================");
            foreach (var item in this.resultsPerDeviceInterfaced)
            {
                returnBuilder.AppendLine().Append(item.Key).AppendLine(":");
                returnBuilder.AppendLine(SnmpAbstraction.IndentLines(item.Value.ToString()));
            }

            return(returnBuilder.ToString());
        }
示例#8
0
        /// <inheritdoc />
        public override string ToString()
        {
            StringBuilder returnBuilder = new StringBuilder(128);

            returnBuilder.Append("Device ").Append(this.DeviceAddress).Append(" (").Append(this.DeviceModel).AppendLine("):");
            returnBuilder.AppendLine(SnmpAbstraction.IndentLines(formatter.Format(this)));
            returnBuilder.Append(SnmpAbstraction.IndentLines("--> Query took ")).Append(this.QueryDuration.TotalMilliseconds).Append(" ms");

            return(returnBuilder.ToString());
        }
示例#9
0
        /// <inheritdoc />
        public override string ToString()
        {
            StringBuilder returnBuilder = new StringBuilder((this.Details?.Count ?? 1) * 128);

            returnBuilder.Append("Link Details:");

            if (this.Details.Count == 0)
            {
                returnBuilder.Append(" No link found.");
            }
            else
            {
                foreach (var item in this.Details)
                {
                    returnBuilder.AppendLine().AppendLine(SnmpAbstraction.IndentLines(item.ToString()));
                }
            }

            return(returnBuilder.ToString());
        }
示例#10
0
        /// <inheritdoc />
        public override string ToString()
        {
            StringBuilder returnBuilder = new StringBuilder((this.Details?.Count ?? 1) * 128);

            returnBuilder.Append("Peer Infos:");

            if (this.Details == null)
            {
                returnBuilder.Append(" Not yet retrieved");
            }
            else
            {
                foreach (var item in this.Details)
                {
                    returnBuilder.AppendLine().AppendLine(SnmpAbstraction.IndentLines(item.ToString()));
                }
            }

            return(returnBuilder.ToString());
        }
示例#11
0
        /// <summary>
        /// Performs SNMP walk using Ver 1 GetNext operation.
        /// </summary>
        /// <param name="interfaceIdWalkRootOid">The retrievable value to start walking at. The actual OID is resolved from the device database.</param>
        /// <returns>A <see cref="VbCollection" /> with the received data.</returns>
        /// <remarks>Derived from example code at <see href="http://www.snmpsharpnet.com/?page_id=108" />.</remarks>
        private VbCollection DoWalkGetNext(Oid interfaceIdWalkRootOid)
        {
            // This Oid represents last Oid returned by
            //  the SNMP agent
            Oid lastOid = (Oid)interfaceIdWalkRootOid.Clone();

            // Pdu class used for all requests
            Pdu pdu = new Pdu(PduType.GetNext);

            VbCollection returnCollection = new VbCollection();

            // Loop through results
            while (lastOid != null)
            {
                // When Pdu class is first constructed, RequestId is set to a random value
                // that needs to be incremented on subsequent requests made using the
                // same instance of the Pdu class.
                Interlocked.CompareExchange(ref nextRequestId, 0, int.MaxValue); // wrap the request ID
                pdu.RequestId = Interlocked.Increment(ref nextRequestId);

                // Clear Oids from the Pdu class.
                pdu.VbList.Clear();

                // Initialize request PDU with the last retrieved Oid
                pdu.VbList.Add(lastOid);

                SnmpV1Packet result;
                using (var target = new UdpTarget((IPAddress)this.Address, this.Options.Port, Convert.ToInt32(this.Options.Timeout.TotalMilliseconds), this.Options.Retries))
                {
                    // Make SNMP request
                    result = (SnmpV1Packet)target.Request(pdu, this.QueryParameters);
                }

                SnmpAbstraction.RecordSnmpRequest(this.Address, pdu, result);

                // You should catch exceptions in the Request if using in real application.
                // [DJ3MU] : Yeah - cool idea - but I still wouldn't know what else to do with them.
                //           So for now, let fly them out to our caller.

                // If result is null then agent didn't reply or we couldn't parse the reply.
                if (result != null)
                {
                    // ErrorStatus other then 0 is an error returned by
                    // the Agent - see SnmpConstants for error definitions
                    if (result.Pdu.ErrorStatus != 0)
                    {
                        // agent reported an error with the request
                        log.Warn($"Error in SNMP reply of device '{this.Address}': Error status {result.Pdu.ErrorStatus} at index {result.Pdu.ErrorIndex}");
                        lastOid = null;
                        break;
                    }
                    else
                    {
                        // Walk through returned variable bindings
                        foreach (Vb v in result.Pdu.VbList)
                        {
                            // Check that retrieved Oid is "child" of the root OID
                            if (interfaceIdWalkRootOid.IsRootOf(v.Oid))
                            {
                                returnCollection.Add(v);

                                lastOid = v.Oid;
                            }
                            else
                            {
                                // we have reached the end of the requested
                                // MIB tree. Set lastOid to null and exit loop
                                lastOid = null;
                            }
                        }
                    }
                }
                else
                {
                    throw new HamnetSnmpException($"No response received from SNMP agent for device '{this.Address}'", this.Address?.ToString());
                }
            }

            return(returnCollection);
        }
        /// <summary>
        /// Formats as block-formatted string of an <see cref="ITracerouteResult" />.
        /// </summary>
        /// <param name="traceRouteResult">The data to format.</param>
        /// <returns>The formatted string.</returns>
        public string Format(ITracerouteResult traceRouteResult)
        {
            if (traceRouteResult == null)
            {
                return("<null>");
            }

            StringBuilder returnBuilder = new StringBuilder(traceRouteResult.HopCount * 128);

            returnBuilder.Append("Routing Hops:");

            if (traceRouteResult.HopCount == 0)
            {
                returnBuilder.Append(" No hops found.");
            }
            else
            {
                uint hopCounter = 0;
                foreach (var item in traceRouteResult.Hops)
                {
                    returnBuilder.Append("Hop #").Append(++hopCounter).Append(": ").AppendLine(SnmpAbstraction.IndentLines(this.Format(item)));
                }
            }

            return(returnBuilder.ToString());
        }