public void TestSendTrap() { TrapV1Message message = new TrapV1Message(VersionCode.V1, IPAddress.Parse("127.0.0.1"), new OctetString("public"), new ObjectIdentifier(new uint[] { 1, 3, 6 }), GenericCode.AuthenticationFailure, 0, 0, new List <Variable>()); byte[] bytes = message.ToBytes(); ISnmpMessage parsed = MessageFactory.ParseMessages(bytes, new UserRegistry())[0]; Assert.AreEqual(SnmpType.TrapV1Pdu, parsed.TypeCode()); TrapV1Message m = (TrapV1Message)parsed; Assert.AreEqual(GenericCode.AuthenticationFailure, m.Generic); Assert.AreEqual(0, m.Specific); Assert.AreEqual("public", m.Community.ToString()); Assert.AreEqual(IPAddress.Parse("127.0.0.1"), m.AgentAddress); Assert.AreEqual(new uint[] { 1, 3, 6 }, m.Enterprise.ToNumerical()); Assert.AreEqual(0, m.TimeStamp); Assert.AreEqual(0, m.Variables().Count); }
public void TestToTrapMessageChinese() { Variable v = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }), new OctetString("ол╣Щ", Encoding.Unicode)); List <Variable> vList = new List <Variable>(); vList.Add(v); TrapV1Pdu pdu = new TrapV1Pdu(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }), new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), new Integer32(12), new TimeTicks(16352), vList); byte[] bytes = SnmpMessageExtension.PackMessage(VersionCode.V1, new OctetString("public"), pdu).ToBytes(); TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(bytes, new UserRegistry())[0]; Assert.AreEqual("127.0.0.1", message.AgentAddress.ToString()); Assert.AreEqual(GenericCode.EnterpriseSpecific, message.Generic); Assert.AreEqual(12, message.Specific); Assert.AreEqual(16352, message.TimeStamp); Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical()); Assert.AreEqual(1, message.Variables.Count); Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables[0].Id.ToNumerical()); Assert.AreEqual("ол╣Щ", ((OctetString)message.Variables[0].Data).ToString(Encoding.Unicode)); }
public void TestToTrapMessage() { Variable v = new Variable(new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }), new OctetString("TrapTest")); List <Variable> vList = new List <Variable> { v }; TrapV1Pdu pdu = new TrapV1Pdu(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, new IP("127.0.0.1"), new Integer32((int)GenericCode.EnterpriseSpecific), new Integer32(12), new TimeTicks(16352), vList); byte[] bytes = TrapV1Message.PackMessage(VersionCode.V1, new OctetString("public"), pdu).ToBytes(); TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(bytes, new UserRegistry())[0]; Assert.Equal("127.0.0.1", message.AgentAddress.ToString()); Assert.Equal(GenericCode.EnterpriseSpecific, message.Generic); Assert.Equal(12, message.Specific); Assert.Equal(16352U, message.TimeStamp); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical()); Assert.Equal(1, message.Variables().Count); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); Assert.Equal("TrapTest", message.Variables()[0].Data.ToString()); Assert.Equal("SNMPv1 TRAP PDU: agent address: 127.0.0.1; time stamp: 00:02:43.5200000; enterprise: 1.3.6.1.4.1.2162.1000.2; generic: EnterpriseSpecific; specific: 12; varbind count: 1", pdu.ToString()); }
private void CreateTrapV1MessageReceivedEventArgs(IPEndPoint sender, TrapV1Message request, IListenerBinding binding, IList <IMembershipProvider> matchedMembership, IList <EngineUser> matchedUsers) { Sender = sender ?? throw new ArgumentNullException(nameof(sender)); TrapV1Message = request ?? throw new ArgumentNullException(nameof(request)); Binding = binding ?? throw new ArgumentNullException(nameof(binding)); MatchedMemberships = matchedMembership; MatchedUsers = matchedUsers; }
public void TestParseOneVarbind() { byte[] buffer = Properties.Resources.onevarbind; TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(buffer, new UserRegistry())[0]; Assert.AreEqual(1, message.Variables().Count); Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical()); Assert.AreEqual("TrapTest", message.Variables()[0].Data.ToString()); Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); }
public void TestParseOneVarbind() { byte[] buffer = File.ReadAllBytes(Path.Combine("Resources", "onevarbind.dat")); TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(buffer, new UserRegistry())[0]; Assert.Equal(1, message.Variables().Count); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, message.Enterprise.ToNumerical()); Assert.Equal("TrapTest", message.Variables()[0].Data.ToString()); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); }
public void TestParseTwoVarbinds() { byte[] buffer = Properties.Resources.twovarbinds; TrapV1Message message = (TrapV1Message)MessageFactory.ParseMessages(buffer, new UserRegistry())[0]; Assert.Equal(2, message.Variables().Count); Assert.Equal("TrapTest", message.Variables()[0].Data.ToString()); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, message.Variables()[0].Id.ToNumerical()); Assert.Equal("TrapTest", message.Variables()[1].Data.ToString()); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 22, 0 }, message.Variables()[1].Id.ToNumerical()); }
public void TestParseNoVarbind() { byte[] buffer = Properties.Resources.novarbind; ISnmpMessage m = MessageFactory.ParseMessages(buffer, new UserRegistry())[0]; TrapV1Message message = (TrapV1Message)m; Assert.AreEqual(GenericCode.EnterpriseSpecific, message.Generic); Assert.AreEqual(12, message.Specific); Assert.AreEqual("public", message.Community.ToString()); Assert.AreEqual(IPAddress.Parse("127.0.0.1"), message.AgentAddress); Assert.AreEqual(new uint[] { 1, 3 }, message.Enterprise.ToNumerical()); Assert.AreEqual(16352, message.TimeStamp); Assert.AreEqual(0, message.Variables().Count); }
/// <summary> /// Initializes a new instance of the <see cref="TrapV1MessageReceivedEventArgs"/> class. /// </summary> /// <param name="sender">The sender.</param> /// <param name="request">The request.</param> /// <param name="binding">The binding.</param> public TrapV1MessageReceivedEventArgs(IPEndPoint sender, TrapV1Message request, IListenerBinding binding) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } if (request == null) { throw new ArgumentNullException(nameof(request)); } if (binding == null) { throw new ArgumentNullException(nameof(binding)); } Sender = sender; TrapV1Message = request; Binding = binding; }
public void TestSendTrap() { TrapV1Message message = new TrapV1Message(VersionCode.V1, IPAddress.Parse("127.0.0.1"), new OctetString("public"), new ObjectIdentifier(new uint[] {1,3,6}), GenericCode.AuthenticationFailure, 0, 0, new List<Variable>()); byte[] bytes = message.ToBytes(); ISnmpMessage parsed = MessageFactory.ParseMessages(bytes, new UserRegistry())[0]; Assert.AreEqual(SnmpType.TrapV1Pdu, parsed.TypeCode()); TrapV1Message m = (TrapV1Message)parsed; Assert.AreEqual(GenericCode.AuthenticationFailure, m.Generic); Assert.AreEqual(0, m.Specific); Assert.AreEqual("public", m.Community.ToString()); Assert.AreEqual(IPAddress.Parse("127.0.0.1"), m.AgentAddress); Assert.AreEqual(new uint[] {1,3,6}, m.Enterprise.ToNumerical()); Assert.AreEqual(0, m.TimeStamp); Assert.AreEqual(0, m.Variables().Count); }
public void Test() { var mock = new Mock <ISnmpContext>(); var mock2 = new Mock <IListenerBinding>(); IList <Variable> v = new List <Variable>(); var message = new TrapV1Message(VersionCode.V1, IPAddress.Any, new OctetString("community"), new ObjectIdentifier("1.3.6"), GenericCode.ColdStart, 0, 0, v); mock.Setup(foo => foo.Binding).Returns(mock2.Object); mock.Setup(foo => foo.Request).Returns(message); mock.Setup(foo => foo.Sender).Returns(new IPEndPoint(IPAddress.Any, 0)); var handler = new TrapV1MessageHandler(); Assert.Throws <ArgumentNullException>(() => handler.Handle(null, null)); Assert.Throws <ArgumentNullException>(() => handler.Handle(mock.Object, null)); handler.MessageReceived += delegate(object args, TrapV1MessageReceivedEventArgs e) { Assert.Equal(mock2.Object, e.Binding); Assert.Equal(message, e.TrapV1Message); Assert.True(new IPEndPoint(IPAddress.Any, 0).Equals(e.Sender)); }; handler.Handle(mock.Object, new ObjectStore()); }
//----------------------------------------------- public static CTrapInstance FromMessage( ISnmpMessage message, CAgentSnmpPourSupervision agentPourSupervision) { TrapV1Message trapV1 = message as TrapV1Message; TrapV2Message trapV2 = message as TrapV2Message; InformRequestMessage inform = message as InformRequestMessage; if (trapV1 != null) { CInterrogateurSnmp agent = new CInterrogateurSnmp(); agent.ConnexionIp = trapV1.AgentAddress.ToString(); agent.Connexion.Version = trapV1.Version; agent.ConnexionCommunity = trapV1.Community.ToString(); agent.ConnexionPort = 161; if (agentPourSupervision != null && agentPourSupervision.TypeAgent != null) { agentPourSupervision.TypeAgent.PrepareDynamicSnmpAgent(agent); } CTrapInstance instance = new CTrapInstance(agent); instance.m_strEntreprise = trapV1.Enterprise.ToString(); instance.m_version = trapV1.Version; instance.m_strAgentIP = trapV1.AgentAddress.ToString(); instance.m_strCommunity = trapV1.Community.ToString(); instance.GenericCode = trapV1.Generic; instance.SpecificValue = trapV1.Specific.ToString(); instance.AgentSnmp = agentPourSupervision; foreach (Variable variable in trapV1.Variables()) { CTrapFieldValueBrute valeur = new CTrapFieldValueBrute(variable.Id.ToString(), variable.Data.ToString()); instance.AddValue(valeur); } return(instance); } else if (trapV2 != null) { CInterrogateurSnmp agent = new CInterrogateurSnmp(); if (trapV2.SenderIP == null) { return(null); } agent.ConnexionIp = trapV2.SenderIP.ToString(); agent.Connexion.Version = trapV2.Version; string strCommunauté = ""; if (trapV2.Parameters != null) { if (trapV2.Parameters.UserName != null) { strCommunauté = trapV2.Parameters.UserName.ToString(); } } agent.ConnexionCommunity = strCommunauté; agent.ConnexionPort = 161; if (agentPourSupervision != null && agentPourSupervision.TypeAgent != null) { agentPourSupervision.TypeAgent.PrepareDynamicSnmpAgent(agent); } CTrapInstance instance = new CTrapInstance(agent); instance.m_strEntreprise = trapV2.Enterprise.ToString(); instance.m_strAgentIP = trapV2.SenderIP.ToString(); instance.m_version = trapV2.Version; instance.m_strCommunity = strCommunauté; if (trapV2.Enterprise.ToString() == ".1.3.6.1.6.3.1.1.5.1") { instance.GenericCode = GenericCode.ColdStart; } else if (trapV2.Enterprise.ToString() == ".1.3.6.1.6.3.1.1.5.2") { instance.GenericCode = GenericCode.WarmStart; } else if (trapV2.Enterprise.ToString() == ".1.3.6.1.6.3.1.1.5.3") { instance.GenericCode = GenericCode.LinkDown; } else if (trapV2.Enterprise.ToString() == ".1.3.6.1.6.3.1.1.5.4") { instance.GenericCode = GenericCode.LinkUp; } else if (trapV2.Enterprise.ToString() == ".1.3.6.1.6.3.1.1.5.5") { instance.GenericCode = GenericCode.AuthenticationFailure; } else if (trapV2.Enterprise.ToString() == ".1.3.6.1.6.3.1.1.5.6") { instance.GenericCode = GenericCode.EgpNeighborLoss; } else { instance.GenericCode = GenericCode.EnterpriseSpecific; int nPos = instance.m_strEntreprise.LastIndexOf("."); if (nPos >= 0) { instance.m_strSpecific = instance.m_strEntreprise.Substring(nPos + 1); instance.m_strEntreprise = instance.m_strEntreprise.Substring(0, nPos); } } instance.AgentSnmp = agentPourSupervision; foreach (Variable variable in trapV2.Variables()) { CTrapFieldValueBrute valeur = new CTrapFieldValueBrute(variable.Id.ToString(), variable.Data.ToString()); instance.AddValue(valeur); } return(instance); } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="TrapV1MessageReceivedEventArgs"/> class. /// </summary> /// <param name="sender">The sender.</param> /// <param name="request">The request.</param> /// <param name="binding">The binding.</param> /// <param name="matchedMembership">the members</param> /// <param name="matchedUsers">the users</param> public TrapV1MessageReceivedEventArgs(IPEndPoint sender, TrapV1Message request, IListenerBinding binding, IList <IMembershipProvider> matchedMembership, IList <EngineUser> matchedUsers) { CreateTrapV1MessageReceivedEventArgs(sender, request, binding, matchedMembership, matchedUsers); }
//----------------------------------------------- private void TraiteMessagesATraiter() { if (m_nWaitingCounter > 5) { return; } m_nWaitingCounter++; lock (typeof(CLockerTraiteMessage)) { m_nWaitingCounter--; List <ISnmpMessage> lstMessages = new List <ISnmpMessage>(); lock (m_listeMessagesATraiter) { lstMessages.AddRange(m_listeMessagesATraiter); m_listeMessagesATraiter.Clear(); } foreach (ISnmpMessage message in lstMessages) { m_nNbMessages++; DateTime dtChrono = DateTime.Now; string strIpAgent = ""; TrapV1Message trapV1 = message as TrapV1Message; TrapV2Message trapV2 = message as TrapV2Message; if (trapV1 != null) { m_gestionnaireAlarmes.Trace.Write( "Trap received : " + trapV1.GetTraceInfo(), ALTRACE.TRACE, ALTRACE.DEBUG); strIpAgent = trapV1.AgentAddress.ToString(); StringBuilder bl = new StringBuilder(); foreach (Variable valeur in trapV1.Variables()) { bl.Append(valeur.Id.ToString()); bl.Append("="); bl.Append(valeur.Data.ToString()); bl.Append("\r\n"); } m_gestionnaireAlarmes.Trace.Write( "Trap fields : \r\n" + bl.ToString(), ALTRACE.TRACE, ALTRACE.DEBUG); } else if (trapV2 != null) { m_gestionnaireAlarmes.Trace.Write( "Trap received : " + trapV2.GetTraceInfo(), ALTRACE.TRACE, ALTRACE.DEBUG); if (trapV2.SenderIP != null) { strIpAgent = trapV2.SenderIP.ToString(); } StringBuilder bl = new StringBuilder(); foreach (Variable valeur in trapV2.Variables()) { bl.Append(valeur.Id.ToString()); bl.Append("="); bl.Append(valeur.Data.ToString()); bl.Append("\r\n"); } m_gestionnaireAlarmes.Trace.Write( "Trap fields : \r\n" + bl.ToString(), ALTRACE.TRACE, ALTRACE.DEBUG); } try { CTrapInstance trapTmp = CTrapInstance.FromMessage(message, null); if (trapTmp == null) { m_gestionnaireAlarmes.Trace.Write( "Can not interpret message " + message.ToString(), ALTRACE.TRACE, ALTRACE.DEBUG); continue; } IEnumerable <CAgentSnmpPourSupervision> lstAgents = GetAgentsForTrap(trapTmp, strIpAgent); bool bHasAgent = false; foreach (CAgentSnmpPourSupervision agent in lstAgents) { m_gestionnaireAlarmes.Trace.Write( "Trap managed by agent " + agent.Id, ALTRACE.DEBUG); CTypeAgentPourSupervision typeAgent = agent.TypeAgent; if (typeAgent != null) { CTrapInstance trap = CTrapInstance.FromMessage( message, agent); if (trap != null) { IEnumerable <CTrapHandler> lstTrapsHandlers = typeAgent.GetTrapsHandlersAAppliquer(trap); foreach (CTrapHandler handler in lstTrapsHandlers) { m_gestionnaireAlarmes.Trace.Write("Apply trap handler " + handler.Libelle, ALTRACE.DEBUG); handler.CreateAlarmesOnTrap(trap, m_gestionnaireAlarmes.Trace); } if (lstTrapsHandlers.Count() == 0) { m_gestionnaireAlarmes.Trace.Write( "No trap handler for this trap", ALTRACE.DEBUG); } foreach (CAlarmeACreer creation in trap.AlarmesACreer) { m_gestionnaireAlarmes.CreateAlarme(creation, EModeRemonteeAlarmes.InscrireARemonterMaisNePasLeFaire); } if (trap.AlarmesACreer.Count() == 0) { m_gestionnaireAlarmes.Trace.Write("No alarm creator for this trap", ALTRACE.DEBUG); } } } } if (!bHasAgent) { m_gestionnaireAlarmes.Trace.Write( "No agent for this trap", ALTRACE.DEBUG); } } catch (Exception ex) { m_gestionnaireAlarmes.Trace.Write( "Exception : " + ex.Message, ALTRACE.DEBUG); lock (m_listeMessagesATraiter) { m_listeMessagesATraiter.InsertRange(0, lstMessages); } break; } TimeSpan sp = DateTime.Now - dtChrono; if (m_nNbMessages % 100 == 0) { Console.WriteLine("Traite alrm (" + m_nNbMessages + "): " + sp.TotalMilliseconds); } } } m_gestionnaireAlarmes.SendAlarmes(); }