//-------------------------------------------------------------------- public void SendTrapUnsafe(string strReceiverIP, int nReceiverPort) { IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(strReceiverIP), nReceiverPort); List <Variable> lstVariables = new List <Variable>(); foreach (KeyValuePair <ObjectIdentifier, object> kv in m_dicValeursVariables) { ISnmpData data = null; if (kv.Value is ISnmpData) { data = (ISnmpData)kv.Value; } else { data = AbstractTypeAssignment.GetSnmpFromDotNet(kv.Value); } if (data != null) { lstVariables.Add(new Variable(kv.Key, data)); } } Messaging.Messenger.SendTrapV1(endPoint, IpAgent, new OctetString(m_strCommunaute), EntrepriseOID, GenericCode, SpecificCode, 0, lstVariables); }
//----------------------------------- private static void FillListeTables( CEasyQuerySource laBase, IDefinition definition, List <ITableDefinition> lstToFill, CEasyQuerySourceFolder folderRacine) { CTableDefinitionSnmpOfScalar tableScalars = null; if (definition.Entity != null) { tableScalars = new CTableDefinitionSnmpOfScalar(); tableScalars.OIDRacine = definition.GetNumericalForm(); tableScalars.TableName = folderRacine.Name + "_Scalars"; tableScalars.FolderId = folderRacine.Id; } foreach (IDefinition children in definition.Children) { if (children.Type == DefinitionType.Table) { CTableDefinitionSNMP table = FromDefinition(laBase, children, folderRacine); if (table != null) { lstToFill.Add(table); laBase.AddTableUniquementPourObjetConnexion(table); } } else if (children.Type == DefinitionType.OidValueAssignment) { CEasyQuerySourceFolder folder = folderRacine.GetSubFolderWithCreate(children.Name); FillListeTables(laBase, children, lstToFill, folder); } else if (children.Type == DefinitionType.Scalar) { if (children.Type == DefinitionType.Scalar && tableScalars != null) { CColumnDefinitionSNMP colSnmp = new CColumnDefinitionSNMP(); colSnmp.ColumnName = children.Name; colSnmp.OID = children.GetNumericalForm(); Type tp = typeof(string); ObjectType objType = children.Entity as ObjectType; if (objType != null) { TextualConvention conv = objType.Syntax as TextualConvention; if (conv != null) { tp = ((AbstractTypeAssignment)conv.Syntax).GetTypeDotNet(); } else { AbstractTypeAssignment abs = objType.Syntax as AbstractTypeAssignment; if (abs != null) { tp = abs.GetTypeDotNet(); } } } switch (objType.Access) { case MaxAccess.accessibleForNotify: case MaxAccess.readOnly: colSnmp.IsReadOnly = true; break; case MaxAccess.readWrite: case MaxAccess.readCreate: colSnmp.IsReadOnly = false; break; } colSnmp.DataType = tp; tableScalars.AddColumn(colSnmp); } } } if (tableScalars != null && tableScalars.Columns.Count() > 0) { laBase.AddTableUniquementPourObjetConnexion(tableScalars); tableScalars.Base = laBase; } }
public bool SetSnmpValueForField(int nIdChampCustom, object value) { ITableDefinition table = null; IColumnDefinition col = null; CResultAErreurType <IObjetDeEasyQuery> resObj = TypeEntiteSnmp.FindSourceObjetInQuery(); if (!resObj || resObj.DataType == null) { return(false); } IObjetDeEasyQuery objetSource = resObj.DataType; CEasyQuery query = objetSource.Query; IEnumerable <CChampEntiteFromQueryToChampCustom> lstChamps = TypeEntiteSnmp.ChampsFromQuery; CChampEntiteFromQueryToChampCustom champ = null; foreach (CChampEntiteFromQueryToChampCustom champTest in lstChamps) { if (champTest.IdChampCustom != null && champTest.IdChampCustom == nIdChampCustom) { champ = champTest; break; } } if (champ == null) { return(false); } if (query.FindSource(champ.Champ.ColonneSource, objetSource, out table, out col)) { CEntiteSnmpPourSupervision ettPourSup = null; CColumnDefinitionSNMP colSnmp = col as CColumnDefinitionSNMP; if (colSnmp != null && !colSnmp.IsReadOnly) { ISnmpData snmpVal = AbstractTypeAssignment.GetSnmpFromDotNet(colSnmp.SnmpType, value); if (snmpVal == null && value != null) { snmpVal = new OctetString(value.ToString()); } if (snmpVal != null) { string strIndex = Index; if (champ.Champ.FormuleIndex != null) { if (ettPourSup == null) { ettPourSup = GetEntitePourSupervision(TypeEntiteSnmp.GetTypeEntitePourSupervision(null, false)); } CContexteEvaluationExpression ctx = new CContexteEvaluationExpression(ettPourSup); CResultAErreur restTmp = champ.Champ.FormuleIndex.Eval(ctx); if (restTmp && restTmp.Data != null) { strIndex = restTmp.Data.ToString(); } } if (!strIndex.StartsWith(".")) { strIndex = "." + strIndex; } try { CInterrogateurSnmp agent = new CInterrogateurSnmp(); agent.Connexion.Version = AgentSnmp.SnmpVersion; agent.ConnexionIp = AgentSnmp.SnmpIp; agent.ConnexionPort = AgentSnmp.SnmpPort; agent.ConnexionCommunity = AgentSnmp.SnmpCommunaute; CResultAErreur resTmp = agent.Set(colSnmp.OIDString + strIndex, snmpVal); if (resTmp) { return(true); } } catch { } } } } return(false); }
//----------------------------------------------------------------------- //Si idsChampsAMettreAJour est vide, il faut tout mettre à jour public CResultAErreur SendToSnmp(bool bOnlyModifiés, params int[] idsChampsAMettreAJour) { CResultAErreur result = CResultAErreur.True; if (TypeEntiteSnmp == null) { return(result); } if (TypeEntiteSnmp.ReadOnly) { result.EmpileErreur(I.T("This is a readonly entity|20127")); return(result); } IEnumerable <CChampEntiteFromQueryToChampCustom> lstChamps = TypeEntiteSnmp.ChampsFromQuery; Dictionary <string, object> lstValeurs = new Dictionary <string, object>(); CResultAErreurType <IObjetDeEasyQuery> resObj = TypeEntiteSnmp.FindSourceObjetInQuery( ); if (!resObj || resObj.DataType == null) { result.EmpileErreur(resObj.Erreur); return(result); } IObjetDeEasyQuery objetSource = resObj.DataType; CEasyQuery query = objetSource.Query; Dictionary <string, string> dicOIDStringToChamp = new Dictionary <string, string>(); CEntiteSnmpPourSupervision ettPourSup = null; foreach (CChampEntiteFromQueryToChampCustom champ in lstChamps) { if (champ.IdChampCustom != null && (idsChampsAMettreAJour.Length == 0 || idsChampsAMettreAJour.Contains(champ.IdChampCustom.Value))) { CRelationElementAChamp_ChampCustom rel = CUtilElementAChamps.GetRelationToChamp(this, champ.IdChampCustom.Value); if (rel != null && (bOnlyModifiés && (rel.Row.RowState == DataRowState.Added || rel.Row.RowState == DataRowState.Modified) || !bOnlyModifiés)) { object valeur = rel.Valeur; ITableDefinition table = null; IColumnDefinition col = null; if (query.FindSource(champ.Champ.ColonneSource, objetSource, out table, out col)) { CColumnDefinitionSNMP colSnmp = col as CColumnDefinitionSNMP; if (colSnmp != null && !champ.Champ.IsReadOnly) { ISnmpData snmpVal = AbstractTypeAssignment.GetSnmpFromDotNet(colSnmp.SnmpType, valeur); if (snmpVal == null && valeur != null) { snmpVal = new OctetString(valeur.ToString()); } if (snmpVal != null) { string strIndex = Index; if (champ.Champ.FormuleIndex != null) { if (ettPourSup == null) { ettPourSup = GetEntitePourSupervision(TypeEntiteSnmp.GetTypeEntitePourSupervision(null, false)); } CContexteEvaluationExpression ctx = new CContexteEvaluationExpression(ettPourSup); CResultAErreur restTmp = champ.Champ.FormuleIndex.Eval(ctx); if (restTmp && restTmp.Data != null) { strIndex = restTmp.Data.ToString(); } } if (!strIndex.StartsWith(".")) { strIndex = "." + strIndex; } lstValeurs[colSnmp.OIDString + strIndex] = snmpVal; dicOIDStringToChamp[colSnmp.OIDString + strIndex] = champ.ChampCustom.Nom; } } } } } } if (lstValeurs.Count > 0) { CInterrogateurSnmp agent = new CInterrogateurSnmp(); agent.Connexion.Version = AgentSnmp.SnmpVersion; agent.ConnexionIp = AgentSnmp.SnmpIp; agent.ConnexionPort = AgentSnmp.SnmpPort; agent.ConnexionCommunity = AgentSnmp.SnmpCommunaute; foreach (KeyValuePair <string, object> kv in lstValeurs) { try { CResultAErreur resTmp = agent.Set(kv.Key, kv.Value); if (!resTmp) { string strChamp = null; if (dicOIDStringToChamp.TryGetValue(kv.Key, out strChamp)) { result.EmpileErreur(new CErreurValidation(I.T("Entity @1 (@2), Field @3 : |20105", Libelle, Index, strChamp) + resTmp.Erreur.ToString(), resTmp.IsAvertissement)); } else { result.EmpileErreur(resTmp.Erreur); } if (!result.IsAvertissement) { return(result); } } } catch (Exception e) { result.EmpileErreur(new CErreurException(e)); } } } return(result); }