//----------------------------------------------- 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); }
//----------------------------------------------- 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(); }