/// <summary> /// Construct from all the parameters. /// </summary> /// <param name="port">The port number to use for the queries.</param> /// <param name="protocolVersion">The SNMP protocol version to use for the queries.</param> /// <param name="community">The community string to use for the queries.</param> /// <param name="timeout">The query timeout.</param> /// <param name="retries">The SNMP peer maximum retires setting. Value of 0 will result in a single request with no retries.</param> /// <param name="ver2cMaximumValuesPerRequest">A value telling the SNMP Agent how many GETNEXT like variables to retrieve</param> /// <param name="ver2cMaximumRequests">A value telling the SNMP Agent how many VBs to include in a single request.<br/> /// Only valid on GETBULK requests.</param> /// <param name="enableCaching">Value indicating whether to use caching of non-volatile data.</param> /// <param name="loginUser">The user name for logins (if needed).</param> /// <param name="loginPassword">The password for logins (if needed).</param> /// <param name="allowedApis">The allowed APIs. By default, only SNMP is allowed.</param> /// <param name="querierClassHint">If not null or empty, a full qualified class name of the handler class to use (if possible).</param> public QuerierOptions( int port, SnmpVersion protocolVersion, OctetString community, TimeSpan timeout, int retries, int ver2cMaximumValuesPerRequest, int ver2cMaximumRequests, bool enableCaching, string loginUser, string loginPassword, QueryApis allowedApis, string querierClassHint = null) { this.Port = port; this.ProtocolVersion = protocolVersion; this.Community = community; this.Timeout = timeout; this.Retries = retries; this.Ver2cMaximumRequests = ver2cMaximumRequests; this.Ver2cMaximumValuesPerRequest = ver2cMaximumValuesPerRequest; this.EnableCaching = enableCaching; this.LoginUser = loginUser; this.LoginPassword = loginPassword; this.AllowedApis = allowedApis; this.QuerierClassHint = querierClassHint; }
/// <summary> /// Initializes the lower querier. /// </summary> private void InitializeLowerQuerier() { if (this.lowerQuerier != null) { return; } QueryApis allowedApis = this.options.AllowedApis; string deviceHandlerHint = string.Empty; lock (this.SyncRoot) { if (this.cacheEntry == null) { this.InitializeCacheEntry(); if (this.lowerQuerier != null) { // InitializeCacheEntry might call InitializeLowerQuerier return; } } allowedApis = ((this.options.AllowedApis & this.cacheEntry.ApiUsed) != 0) ? this.cacheEntry.ApiUsed : this.options.AllowedApis; deviceHandlerHint = this.cacheEntry.DeviceHandlerClass; } this.lowerQuerier = SnmpQuerierFactory.Instance.Create( this.lowerLayer, new QuerierOptions( this.options.Port, this.options.ProtocolVersion, this.options.Community, this.options.Timeout, this.options.Retries, this.options.Ver2cMaximumValuesPerRequest, this.options.Ver2cMaximumRequests, false, // <-- this is the reason why we do this copy: keep all settings but force "enableCaching == false" this.options.LoginUser, this.options.LoginPassword, allowedApis, deviceHandlerHint)); lock (this.SyncRoot) { if (this.cacheEntry != null) { this.cacheEntry.DeviceHandlerClass = this.lowerQuerier.HandlerType.FullName; this.cacheEntry.ApiUsed = this.lowerQuerier.Api; this.cacheDatabaseContext.SaveChanges(); } } }
/// <summary> /// Construct from system data. /// </summary> /// <param name="address">The IP address that this host info reply is for.</param> /// <param name="systemData">The system data.</param> /// <param name="defaultApi">The API that is, by default, used for talking to this device.</param> /// <param name="lastDataUpdate">The date and time when the data has last been updated.</param> public HostInfoReply(IpAddress address, IDeviceSystemData systemData, QueryApis defaultApi, DateTime?lastDataUpdate) { this.address = address ?? throw new ArgumentNullException(nameof(address), "The IP address to construct a HostInfoReply for is null"); this.systemData = systemData ?? throw new ArgumentNullException(nameof(systemData), "The system data to construct a HostInfoReply from is null"); this.systemData.ForceEvaluateAll(); this.DefaultApi = defaultApi.ToString(); this.LastDataUpdate = lastDataUpdate; this.UnixTimeStamp = (ulong?)((this.LastDataUpdate - DateTime.UnixEpoch)?.TotalSeconds); this.SupportedFeatures = this.systemData.SupportedFeatures .ToString() .Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(f => f.Trim()); }
/// <summary> /// Creates a new object that is a copy of this object with modified allowed APIs setting. /// </summary> /// <param name="allowedApis">The allowed APIs. By default, only SNMP is allowed.</param> /// <returns>A new object that is a copy of this object with modified allowed APIs setting.</returns> public QuerierOptions WithAllowedApis(QueryApis allowedApis) { return(new QuerierOptions(this.Port, this.ProtocolVersion, this.Community, this.Timeout, this.Retries, this.Ver2cMaximumValuesPerRequest, this.Ver2cMaximumRequests, this.EnableCaching, this.LoginUser, this.LoginPassword, allowedApis)); }
/// <summary> /// Performs procedure for *QueryInterfaceData tests. /// </summary> /// <param name="address">The address to test with.</param> /// <param name="snmpVersion">The SNMP protocol version to use.</param> /// <param name="useCache">Value indicating whether to use caching of non-volatile data.</param> /// <param name="allowedApis">The list of allowed APIs</param> private static void QueryAndPrintInterfaces(IpAddress address, SnmpVersion snmpVersion, bool useCache = false, QueryApis allowedApis = QueryApis.Snmp) { var querier = SnmpQuerierFactory.Instance.Create(address.ToString(), QuerierOptions.Default.WithProtocolVersion(snmpVersion).WithCaching(useCache).WithAllowedApis(allowedApis)); Assert.NotNull(querier, "Create(...) returned null"); var networkInterfaceDetails = querier.NetworkInterfaceDetails; Assert.NotNull(networkInterfaceDetails, "querier.NetworkInterfaceDetails returned null"); networkInterfaceDetails.ForceEvaluateAll(); Assert.NotNull(networkInterfaceDetails.Details, "querier.NetworkInterfaceDetails.Details returned null"); Assert.Greater(networkInterfaceDetails.Details.Count, 0, "querier.NetworkInterfaceDetails.Details.Count == 0"); Console.WriteLine("Obtained interface details:"); Console.WriteLine(new BlockTextFormatter().Format(networkInterfaceDetails)); }
/// <summary> /// Performs procedure for *QuerySystemData tests. /// </summary> /// <param name="address">The address to test with.</param> /// <param name="snmpVersion">The SNMP protocol version to use.</param> /// <param name="useCache">Value indicating whether to use caching of non-volatile data.</param> /// <param name="allowedApis">The list of allowed APIs</param> private static void QueryAndPrintSystemData(IpAddress address, SnmpVersion snmpVersion, bool useCache = false, QueryApis allowedApis = QueryApis.Snmp) { var querier = SnmpQuerierFactory.Instance.Create(address.ToString(), QuerierOptions.Default.WithProtocolVersion(snmpVersion).WithCaching(useCache).WithAllowedApis(allowedApis)); Assert.NotNull(querier, "Create(...) returned null"); var systemData = querier.SystemData; Assert.NotNull(systemData, "querier.SystemData returned null"); systemData.ForceEvaluateAll(); Console.WriteLine("Obtained system data:"); Console.WriteLine(new BlockTextFormatter().Format(systemData)); }
/// <summary> /// Performs procedure for *QuerySystemData tests. /// </summary> /// <param name="address1">The address of side #1 to test with.</param> /// <param name="address2">The address of side #2 to test with.</param> /// <param name="snmpVersion">The SNMP protocol version to use.</param> /// <param name="useCache">Value indicating whether to use caching of non-volatile data.</param> /// <param name="allowedApis">The list of allowed APIs</param> private static void QueryAndPrintLinkDetails(IpAddress address1, IpAddress address2, SnmpVersion snmpVersion, bool useCache = false, QueryApis allowedApis = QueryApis.Snmp) { var querier = SnmpQuerierFactory.Instance.Create(address1.ToString(), QuerierOptions.Default.WithProtocolVersion(snmpVersion).WithCaching(useCache).WithAllowedApis(allowedApis)); Assert.NotNull(querier, "Create(...) returned null"); var linkDetails = querier.FetchLinkDetails(address2.ToString()); Assert.NotNull(linkDetails, "querier.FetchLinkDetails returned null"); Assert.NotNull(linkDetails.Details, "querier.FetchLinkDetails(...).Details returned null"); Assert.Greater(linkDetails.Details.Count, 0, "querier.FetchLinkDetails(...).Details.Count == 0"); Console.WriteLine($"=== Link details from {address1} to {address2} ==="); Console.WriteLine(new BlockTextFormatter().Format(linkDetails)); }
/// <summary> /// Performs procedure for BGP tests. /// </summary> /// <param name="address">The address to test with.</param> /// <param name="snmpVersion">The SNMP protocol version to use.</param> /// <param name="useCache">Value indicating whether to use caching of non-volatile data.</param> /// <param name="allowedApis">The list of allowed APIs</param> private static void QueryAndPrintBgpPeers(IpAddress address, SnmpVersion snmpVersion, bool useCache = false, QueryApis allowedApis = QueryApis.VendorSpecific) { var querier = SnmpQuerierFactory.Instance.Create(address.ToString(), QuerierOptions.Default.WithProtocolVersion(snmpVersion).WithCaching(useCache).WithAllowedApis(allowedApis)); var systemData = querier.SystemData; systemData.ForceEvaluateAll(); Assert.NotNull(querier, "Create(...) returned null"); var bgpPeers = querier.FetchBgpPeers(null); Assert.NotNull(bgpPeers, "querier.BgpPeers returned null"); bgpPeers.ForceEvaluateAll(); Console.WriteLine("Obtained BGP peers:"); Console.WriteLine(new BlockTextFormatter().Format(bgpPeers)); }
/// <summary> /// Performs procedure for traceroute tests. /// </summary> /// <param name="address">The address to test with.</param> /// <param name="target">The traceroute target host name or IP address.</param> /// <param name="snmpVersion">The SNMP protocol version to use.</param> /// <param name="useCache">Value indicating whether to use caching of non-volatile data.</param> /// <param name="allowedApis">The list of allowed APIs</param> private static void QueryAndPrintTraceroute(IpAddress address, string target, SnmpVersion snmpVersion, bool useCache = false, QueryApis allowedApis = QueryApis.VendorSpecific) { var querier = SnmpQuerierFactory.Instance.Create(address.ToString(), QuerierOptions.Default.WithProtocolVersion(snmpVersion).WithCaching(useCache).WithAllowedApis(allowedApis)); var systemData = querier.SystemData; systemData.ForceEvaluateAll(); Assert.NotNull(querier, "Create(...) returned null"); var traceroute = querier.Traceroute(target, 1, TimeSpan.FromSeconds(1), 500); Assert.NotNull(traceroute, "querier.BgpPeers returned null"); Console.WriteLine("Obtained route trace:"); Console.WriteLine(new BlockTextFormatter().Format(traceroute)); }