/// <summary> /// Event triggered when a device is selected from combDevices /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void combDevices_SelectedIndexChanged(object sender, EventArgs e) { if (combDevices.SelectedItem == null) { return; } // Kill any graph threads. AbortBackgroundWorker(); // Remove all tabs. ResetUI(); // Create new instance of objects used. DeviceConfiguration selectedDevice = storedDevices.Devices.Find(d => d.Name == combDevices.SelectedItem.ToString()); try { snmp = new SnmpInterop(selectedDevice); snmp.dnsLookups = ConfigurationSettings.DNSLookups; } catch (Exception ex) { MessageBox.Show( "Unable to connect to the selected device:" + System.Environment.NewLine + System.Environment.NewLine + ex.Message, "Unable to connect to device", MessageBoxButtons.OK, MessageBoxIcon.Error ); return; } // By now we know we can connect to the device, since the SnmpInterop() constructor tests this. tabPages.TabPages.Remove(tabWelcome); // General information tab. threadGeneral = new Thread(new ThreadStart(ThreadSNMP)); threadGeneral.Name = "SNMP Tables"; threadGeneral.Start(); // Graphing bgWorkerChartComplete = new AutoResetEvent(false); bgWorkerChart = new BackgroundWorker(); bgWorkerChart.WorkerSupportsCancellation = true; bgWorkerChart.DoWork += new DoWorkEventHandler(PlotInterfaceUsageWorker); }
/// <summary> /// /// </summary> /// <param name="device"></param> public SnmpInterop(DeviceConfiguration device) { // // Basic setup, version and community. switch (device.Version) { case DeviceConfiguration.SNMPVersion.V1: snmpVersion = VersionCode.V1; snmpCommunity = new OctetString(device.CommunityRO); break; case DeviceConfiguration.SNMPVersion.V2C: snmpVersion = VersionCode.V2; snmpCommunity = new OctetString(device.CommunityRO); break; case DeviceConfiguration.SNMPVersion.V3: snmpVersion = VersionCode.V3; snmpUser = new OctetString(device.Username); // SNMP Auth types. if (device.AuthType == DeviceConfiguration.V3AuthTypes.MD5) { snmpAuth = new Lextm.SharpSnmpLib.Security.MD5AuthenticationProvider(new OctetString(device.AuthPass)); } else if (device.AuthType == DeviceConfiguration.V3AuthTypes.SHA1) { snmpAuth = new Lextm.SharpSnmpLib.Security.SHA1AuthenticationProvider(new OctetString(device.AuthPass)); } else { snmpAuth = Lextm.SharpSnmpLib.Security.DefaultAuthenticationProvider.Instance; } // SNMP Privacy settings if (device.PrivType == DeviceConfiguration.V3PrivTypes.DES) { snmpPriv = new Lextm.SharpSnmpLib.Security.DESPrivacyProvider(new OctetString(device.PrivPass), snmpAuth); } else if (device.PrivType == DeviceConfiguration.V3PrivTypes.AES) { snmpPriv = new Lextm.SharpSnmpLib.Security.AESPrivacyProvider(new OctetString(device.PrivPass), snmpAuth); } else { snmpPriv = new Lextm.SharpSnmpLib.Security.DefaultPrivacyProvider(snmpAuth); } break; } // // Determine where we should connect. IPAddress snmpAddr; if (!IPAddress.TryParse(device.Address, out snmpAddr)) { IPAddress[] ipAddr; try { ipAddr = Dns.GetHostAddresses(device.Address); } catch { throw new Exception("Unable to resolve hostname of the device to its IP address."); } foreach (IPAddress ip in Dns.GetHostAddresses(device.Address).Where(address => address.AddressFamily == AddressFamily.InterNetwork)) { snmpAddr = ip; } if (snmpAddr == null) { throw new Exception("Unable to resolve hostname of the device to a suitable InterNetwork IP address."); } } snmpEndpoint = new IPEndPoint(snmpAddr, device.Port); // Test to see if we can actually communicate. validConnection(); }
/// <summary> /// Adds device to the DeviceConfigurationList on this form. /// </summary> public void addDeviceToStorage() { DeviceConfiguration device = new DeviceConfiguration(); device.Name = txtName.Text; device.Address = txtAddress.Text; device.Port = Convert.ToInt32(txtPort.Text); if (combVersion.Text == "V1") { device.Version = DeviceConfiguration.SNMPVersion.V1; } else if (combVersion.Text == "V2C") { device.Version = DeviceConfiguration.SNMPVersion.V2C; } else { device.Version = DeviceConfiguration.SNMPVersion.V3; } device.CommunityRO = txtCommunityRO.Text; if (device.Version == DeviceConfiguration.SNMPVersion.V3) { device.Username = txtUsername.Text; device.AuthPass = txtAuth.Text; if (combAuth.Text == "NoAuth") { device.AuthType = DeviceConfiguration.V3AuthTypes.NoAuth; } else if (combAuth.Text == "MD-5") { device.AuthType = DeviceConfiguration.V3AuthTypes.MD5; } else { device.AuthType = DeviceConfiguration.V3AuthTypes.SHA1; } // Can only use privacy if we authenticate. if (device.AuthType != DeviceConfiguration.V3AuthTypes.NoAuth) { device.PrivPass = txtPriv.Text; if (combPriv.Text == "NoPriv") { device.PrivType = DeviceConfiguration.V3PrivTypes.NoPriv; } else if (combPriv.Text == "AES-128") { device.PrivType = DeviceConfiguration.V3PrivTypes.AES; } else { device.PrivType = DeviceConfiguration.V3PrivTypes.DES; } } } // Save object. if (storedDevices.Devices.Find(d => d.Name == device.Name) != null) { DialogResult dlgOverwritePrompt = MessageBox.Show("A device with this name already exists, do you with to update it?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dlgOverwritePrompt == DialogResult.No) { return; } else { storedDevices.Devices.Remove(storedDevices.Devices.Find(d => d.Name == device.Name)); } } storedDevices.Devices.Add(device); ClearFormContents(); grpGeneral.Enabled = false; grpCommunities.Enabled = false; grpSecPriv.Enabled = false; txtPort.Text = "161"; txtCommunityRO.Text = "public"; RefreshDeviceList(); }
/// <summary> /// Device in device list selected. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void listDevices_SelectedIndexChanged(object sender, EventArgs e) { // Display the properties from the selected item. if (listDevices.SelectedItem == null) { btnSave.Enabled = false; return; } // // Populate the form with the device settings. DeviceConfiguration device = storedDevices.Devices.Find(d => d.Name == listDevices.SelectedItem.ToString()); txtName.Text = device.Name; txtAddress.Text = device.Address; txtPort.Text = device.Port.ToString(); switch (device.Version) { case DeviceConfiguration.SNMPVersion.V1: combVersion.SelectedIndex = 0; txtCommunityRO.Text = device.CommunityRO; break; case DeviceConfiguration.SNMPVersion.V2C: combVersion.SelectedIndex = 1; txtCommunityRO.Text = device.CommunityRO; break; case DeviceConfiguration.SNMPVersion.V3: combVersion.SelectedIndex = 2; break; } txtUsername.Text = device.Username; txtAuth.Text = device.AuthPass; switch (device.AuthType) { case DeviceConfiguration.V3AuthTypes.NoAuth: combAuth.SelectedIndex = 0; break; case DeviceConfiguration.V3AuthTypes.MD5: combAuth.SelectedIndex = 1; break; case DeviceConfiguration.V3AuthTypes.SHA1: combAuth.SelectedIndex = 2; break; } txtPriv.Text = device.PrivPass; switch (device.PrivType) { case DeviceConfiguration.V3PrivTypes.NoPriv: combPriv.SelectedIndex = 0; break; case DeviceConfiguration.V3PrivTypes.DES: combPriv.SelectedIndex = 1; break; case DeviceConfiguration.V3PrivTypes.AES: combPriv.SelectedIndex = 2; break; } btnDelete.Enabled = true; ValidateChildren(); // // Enable form editing for the selected device. btnSave.Enabled = true; grpGeneral.Enabled = true; if (combVersion.SelectedIndex == 0 || combVersion.SelectedIndex == 1) { grpCommunities.Enabled = true; } else { grpSecPriv.Enabled = true; } }