public SnmpDevice ScanDevice(string host, string community = SnmpScanner.DEFAULT_COMMUNITY) { try { return(SnmpScanner.ScanDevice(host, community)); } catch (Exception ex) { PackageHost.WriteError($"Unable to scan '{host}' : {ex.Message}"); return(null); } }
/// <summary> /// Scans the specified host. /// </summary> /// <typeparam name="T">Type of result</typeparam> /// <param name="snmpClient">The SNMP client.</param> /// <param name="oidRoot">The root OID.</param> /// <param name="instance">The result instance.</param> /// <param name="version">The SNMP version.</param> /// <returns> /// The T instance. /// </returns> /// <exception cref="System.Exception">SNMP agent host name/ip address is invalid.</exception> public static T Scan <T>(SimpleSnmp snmpClient, string oidRoot = null, T instance = null, SnmpVersion version = SnmpVersion.Ver1) where T : class, new() { if (!snmpClient.Valid) { throw new Exception("SNMP agent host name/ip address is invalid."); } if (oidRoot == null) { oidRoot = ROOT_MIB_OID; } if (instance == null) { instance = Activator.CreateInstance(typeof(T)) as T; } var scanner = new SnmpScanner() { Client = snmpClient, Version = version }; scanner.FeedObject(instance, new Oid(oidRoot)); return(instance); }
/// <summary> /// Called when the package is started. /// </summary> public override void OnStart() { var config = PackageHost.GetSettingAsConfigurationSection <SnmpConfiguration>("snmpConfiguration"); if (config != null) { foreach (Device device in config.Devices) { PackageHost.WriteInfo($"Starting monitoring task for {device.Host}/{device.Community} (every {config.QueryInterval.TotalSeconds} sec)"); Task.Factory.StartNew(() => { string snmpDeviceId = $"{device.Host}/{device.Community}"; int stateObjectTimeout = (int)config.QueryInterval.Add(STATEOBJECT_TIMEOUT).TotalSeconds; var snmpDeviceMetadatas = new Dictionary <string, object>() { ["Host"] = device.Host, ["Community"] = device.Community }; DateTime lastQuery = DateTime.MinValue; while (PackageHost.IsRunning) { if (DateTime.Now.Subtract(lastQuery) >= config.QueryInterval) { try { SnmpDevice snmpResult = SnmpScanner.ScanDevice(device.Host, device.Community); if (config.MultipleStateObjectsPerDevice) { // Push Description PackageHost.PushStateObject($"{snmpDeviceId}/Description", snmpResult.Description, lifetime: stateObjectTimeout, metadatas: snmpDeviceMetadatas); // Push Addresses foreach (var address in snmpResult.Addresses) { PackageHost.PushStateObject($"{snmpDeviceId}/Addresses/{address.Key}", address.Value, lifetime: stateObjectTimeout, metadatas: new Dictionary <string, object>(snmpDeviceMetadatas) { ["Key"] = address.Key }); } // Push Network Interfaces foreach (var netInterface in snmpResult.Interfaces) { PackageHost.PushStateObject($"{snmpDeviceId}/Interfaces/{netInterface.Key}", netInterface.Value, lifetime: stateObjectTimeout, metadatas: new Dictionary <string, object>(snmpDeviceMetadatas) { ["Key"] = netInterface.Key }); } // Push Host if (snmpResult.Host != null) { PackageHost.PushStateObject($"{snmpDeviceId}/Host", snmpResult.Host, lifetime: stateObjectTimeout, metadatas: snmpDeviceMetadatas); } // Push ProcessorsLoad if (snmpResult.ProcessorsLoad != null) { foreach (var proc in snmpResult.ProcessorsLoad) { PackageHost.PushStateObject($"{snmpDeviceId}/Processors/{proc.Key}", proc.Value, lifetime: stateObjectTimeout, metadatas: new Dictionary <string, object>(snmpDeviceMetadatas) { ["Key"] = proc.Key }); } } // Push Storages if (snmpResult.Storages != null) { foreach (var storage in snmpResult.Storages) { PackageHost.PushStateObject($"{snmpDeviceId}/Storages/{storage.Key}", storage.Value, lifetime: stateObjectTimeout, metadatas: new Dictionary <string, object>(snmpDeviceMetadatas) { ["Key"] = storage.Key }); } } } else { // Push the full SNMP device PackageHost.PushStateObject(snmpDeviceId, snmpResult, lifetime: stateObjectTimeout, metadatas: snmpDeviceMetadatas); } } catch (MissingMemberException) { // Device offline -> Send message "DeviceOffline" to the "SNMP" group PackageHost.CreateMessageProxy(MessageScope.ScopeType.Group, "SNMP").DeviceOffline(snmpDeviceId); } catch (Exception ex) { PackageHost.WriteDebug($"Error while scanning {snmpDeviceId} : {ex.Message}"); } lastQuery = DateTime.Now; } Thread.Sleep(1000); } }, TaskCreationOptions.LongRunning); } } PackageHost.WriteInfo("Package started!"); }
public bool CheckAgent(string host, string community = SnmpScanner.DEFAULT_COMMUNITY) { return(SnmpScanner.CheckAgent(host, community)); }