Beispiel #1
0
 protected bool Equals(Oid other)
 {
     return string.Equals(_oid, other._oid) 
         && string.Equals(_name, other._name) 
         && string.Equals(_fullName, other._fullName) 
         && Equals(_childOids, other._childOids) 
         && string.Equals(_description, other._description);
 }
Beispiel #2
0
        public IEnumerable<SnmpResult> Walk(SnmpVersion version, string octetString, Oid oid, WalkingMode walkMode, IPAddress ipAddress = null, string hostname = null, string userLoginV3 = null, string passwordV3 = null)
        {
            if (ipAddress != null)
            {
                return WalkOperation(version, ipAddress, octetString, oid, walkMode);
            }
            else if (!string.IsNullOrEmpty(hostname))
            {
                return WalkOperation(version, GetIpAddressFromHostName(hostname), octetString, oid, walkMode);
            }

            return null;
        }
Beispiel #3
0
        public IEnumerable<SnmpResult> GetBulk(SnmpVersion version, int maxBulkRepetiotions, string octetString, Oid oid, IPAddress ipAddress = null, string hostname = null, string userLoginV3 = null, string passwordV3 = null)
        {
            if (ipAddress != null)
            {
                return GetBulkOperation(version, ipAddress, maxBulkRepetiotions, octetString, oid);
            }
            else if (!string.IsNullOrEmpty(hostname))
            {
                return GetBulkOperation(version, GetIpAddressFromHostName(hostname), maxBulkRepetiotions, octetString, oid);
            }

            return null;
        }
Beispiel #4
0
        private static void Initialize()
        {
            var confPath = Path.Combine(CurrentDir, ConfDir);
            var codesPath = Path.Combine(CurrentDir, ConfDir, CodesDir);

            if (!Directory.Exists(confPath) && !Directory.Exists(codesPath)) return;

            var dirInfo = new DirectoryInfo(confPath);
            _commoInfos = dirInfo.GetFiles("*.xml").Where(file => file.Name.Contains(OidFileIdentifier)).ToList();

            var codesDirInfo = new DirectoryInfo(codesPath);
            _codesInfo = codesDirInfo.GetFiles("*.xml").Where(file => file.Name.Contains(CodesFileIdentifier)).ToList();

            if (!_commoInfos.Any() || !_commoInfos.All(file => file.Name.Contains(ConfMain))) return;

            foreach (var file in _commoInfos)
            {
                var xml = XDocument.Load(file.OpenRead());

                if (xml.Root == null) continue;
                var rootNode = xml.Root;

                if (!ValidateOidFile(rootNode)) continue;

                var subNode = (XElement)rootNode.FirstNode;

                if (string.IsNullOrEmpty(subNode.FirstAttribute.Name.LocalName) || subNode.FirstAttribute.Name.LocalName != OidAttr) continue;

                var rootOid = new Oid(subNode.FirstAttribute.Value, subNode.Name.LocalName, subNode.Name.LocalName);

                var oids = subNode.Elements();

                var childOids = oids.Select(oid => new Oid(oid.FirstAttribute.Value, oid.Name.LocalName, string.Concat(rootOid.Name, ".", oid.Name.LocalName))).ToList();

                rootOid.ChildOids = InitializeCodes(childOids);

                ConfOids.Add(rootOid);
            }

            var brandNameInfos = dirInfo.GetFiles("*.xml").Where(file => file.Name.Contains(DeviceModelFileName)).ToList();
            var bnInfo = DeserializeCodes(brandNameInfos.First());

            foreach (var code in bnInfo.Code)
            {
                BrandHashtable.Add(code.Decimal, code.Name);
            }
        }
Beispiel #5
0
        private IEnumerable<SnmpResult> WalkOperation(SnmpVersion version, IPAddress ipAddress, string octetString, Oid oid, WalkingMode walkMode)
        {
            _log.Info("SnmpEngine.WalkOperation(): Started");

            List<SnmpResult> result;

            try
            {
                if (_timeOut == 0)
                {
                    _timeOut = SnmpHelper.DefaultTimeOut;
                }

                if (string.IsNullOrEmpty(octetString))
                {
                    octetString = SnmpHelper.DefaultOctetString;
                }

                result = oid.HasChildOids ? Walk(version, ipAddress, octetString, oid, walkMode).ToList() : WalkSingle(version, ipAddress, octetString, oid, walkMode).ToList();

            }
            catch (Exception e)
            {
                if (e is TimeoutException)
                {
                    _log.Error("SnmpEngine.WalkOperation():Timeout Exception caught:", e);
                    throw new SnmpTimeOutException(e.Message, _timeOut);
                }
                else if (e is ArgumentOutOfRangeException)
                {
                    _log.Error("SnmpEngine.WalkOperation():Argument Out Of Range Exception caught:", e);
                    throw new SnmpEngineConvertorException((ArgumentOutOfRangeException)e);
                }
                else
                {
                    _log.Error("SnmpEngine.WalkOperation():Exception caught:", e);
                    throw new SnmpEngineException(e.Message);
                }
            }
            finally
            {
                _log.Info("SnmpEngine.WalkOperation(): Finished");
            }

            return result;
        }
Beispiel #6
0
        private IEnumerable<SnmpResult> GetOperation(SnmpVersion version, IPAddress ipAddress, string octetString, Oid oid)
        {
            _log.Info("SnmpEngine.GetOperation(): Started");
            var variables = new List<Variable>
            {
                new Variable(new ObjectIdentifier(oid.Value))
            };

            try
            {
                if (_timeOut == 0)
                {
                    _timeOut = SnmpHelper.DefaultTimeOut;
                }

                if (string.IsNullOrEmpty(octetString))
                {
                    octetString = SnmpHelper.DefaultOctetString;
                }

                Messenger.Get(_converter.ToVersionCodeConverter(version), new IPEndPoint(ipAddress, SnmpHelper.SnmpServerPort), new OctetString(octetString), variables, _timeOut);
            }
            catch (Exception e)
            {
                if (e is TimeoutException)
                {
                    _log.Error("SnmpEngine.GetOperation():Timeout Exception caught:", e);
                    throw new SnmpTimeOutException(e.Message, _timeOut);
                }
                else if (e is ArgumentOutOfRangeException)
                {
                    _log.Error("SnmpEngine.GetOperation():Argument Out Of Range Exception caught:", e);
                    throw new SnmpEngineConvertorException((ArgumentOutOfRangeException)e);
                }
                else
                {
                    _log.Error("SnmpEngine.GetOperation():Exception caught:", e);
                    throw new SnmpEngineException(e.Message);
                }
            }
            finally
            {
                _log.Info("SnmpEngine.GetOperation(): Finished");
            }

            return variables.Select(var => new SnmpResult(new Oid(var.Id.ToString()), var.Data, _converter.ToSnmpDataType(var.Data.TypeCode)));
        }
Beispiel #7
0
        private IEnumerable<SnmpResult> GetNextOperation(SnmpVersion version, IPAddress ipAddress, string octetString, Oid oid)
        {
            _log.Info("SnmpEngine.GetNextOperation() : Started oid: " + oid.Value);
            List<SnmpResult> results;
            var variable = new List<Variable>
            {
                new Variable(new ObjectIdentifier(oid.Value))
            };

            try
            {
                var getNextRequest = new GetNextRequestMessage(0, _converter.ToVersionCodeConverter(version), new OctetString(octetString), variable);
                var responce = getNextRequest.GetResponse(SnmpHelper.DefaultTimeOut, new IPEndPoint(ipAddress, SnmpHelper.SnmpServerPort));

                if (responce.Pdu().ErrorStatus.ToInt32() != 0)
                {
                    throw new SnmpEngineException("SnmpEngine.GetNextOperation() error status = 0; oid = " + oid.Value);
                }

                results = responce.Pdu().Variables.Select(var => new SnmpResult(new Oid(var.Id.ToString()), var.Data, _converter.ToSnmpDataType(var.Data.TypeCode))).ToList();
            }
            catch (Exception e)
            {
                if (e is TimeoutException)
                {
                    _log.Error("SnmpEngine.GetOperation():Timeout Exception caught:", e);
                    throw new SnmpTimeOutException(e.Message, _timeOut);
                }
                else if (e is ArgumentOutOfRangeException)
                {
                    _log.Error("SnmpEngine.GetOperation():Argument Out Of Range Exception caught:", e);
                    throw new SnmpEngineConvertorException((ArgumentOutOfRangeException)e);
                }
                else
                {
                    _log.Error("SnmpEngine.GetOperation():Exception caught:", e);
                    throw new SnmpEngineException(e.Message);
                }
            }
            finally
            {
                _log.Info("SnmpEngine.GetNextOperation(): Finished");
            }

            return results;
        }
Beispiel #8
0
        private List<SnmpResult> WalkWithAdditionalCodes(SnmpVersion version, IPAddress ipAddress, string octetString, Oid oid, WalkingMode walkMode)
        {
            _log.Info("SnmpEngine.WalkWithAdditionalCodes(): Started oid: " + oid.Value);
            var list = new List<Variable>();
            var results = new List<SnmpResult>();
            
            var codesTable = XmlLoader.InitAdditionalCodes(oid);
            var codes = (Codes)codesTable[oid.Name];

            if (codes != null)
            {


                foreach (var code in codes.Code)
                {
                    try
                    {

                        Messenger.Walk(_converter.ToVersionCodeConverter(version),
                        new IPEndPoint(ipAddress, SnmpHelper.SnmpServerPort), new OctetString(octetString),
                        new ObjectIdentifier(string.Concat(oid.Value, ".", code.Decimal)), list, _timeOut,
                        _converter.ToWalkModeConverter(walkMode));
                    }
                    catch (Exception e)
                    {
                        _log.Error("SnmpEngine.WalkWithAdditionalCodes(): Exception caught:", e);
                        _log.Error("SnmpEngine.WalkWithAdditionalCodes(): Exception oid: " + string.Concat(oid.Value, ".", code.Decimal));
                    }

                    if (list.Any())
                    {
                        _log.Info("SnmpEngine.WalkWithAdditionalCodes(): sucess oid: " + string.Concat(oid.Value, ".", code.Decimal));
                        _log.Info("SnmpEngine.WalkWithAdditionalCodes(): request result oids: " + list.Count);
                        results.AddRange(list.Select(var => new SnmpResult(new Oid(string.Concat(oid.Value, ".", code.Decimal),
                            code.Name, string.Concat(oid.FullName, ".", code.Name)),
                            var.Data, _converter.ToSnmpDataType(var.Data.TypeCode))));
                        list.Clear();
                    }
                }
            }

            codesTable.Clear();

            _log.Info("SnmpEngine.WalkWithAdditionalCodes(): Finished");
            return results;
        }
Beispiel #9
0
        private IEnumerable<SnmpResult> WalkSingle(SnmpVersion version, IPAddress ipAddress, string octetString, Oid oid, WalkingMode walkMode)
        {
            _log.Info("SnmpEngine.WalkSingle(): Started oid: " + oid.Value);
            var list = new List<Variable>();

            if (oid.HasAdditionalCodes)
            {
                return WalkWithAdditionalCodes(version, ipAddress, octetString, oid, walkMode);
            }

            try
            {
                Messenger.Walk(
                _converter.ToVersionCodeConverter(version),
                new IPEndPoint(ipAddress, SnmpHelper.SnmpServerPort),
                new OctetString(octetString),
                new ObjectIdentifier(oid.Value),
                list,
                _timeOut,
                _converter.ToWalkModeConverter(walkMode));
            }
            catch (Exception e)
            {
                _log.Error("SnmpEngine.WalkSingle(): Exception caught: ", e);
                _log.Error("SnmpEngine.WalkSingle(): Exception oid: " + oid.Value);
            }

            _log.Info("SnmpEngine.WalkSingle(): Finished");

            return list.Select(var => new SnmpResult(new Oid(var.Id.ToString()), var.Data, _converter.ToSnmpDataType(var.Data.TypeCode)));
        }
Beispiel #10
0
        private IEnumerable<SnmpResult> WalkBulk(SnmpVersion version, IPAddress ipAddress, int maxBulkRepetitions, string octetString, Oid oid, WalkingMode walkMode)
        {
            _log.Info("SnmpEngine.WalkBulk(): Started oid: " + oid.Value);
            var result = new List<SnmpResult>();

            foreach (var childOid in oid.ChildOids)
            {
                result.AddRange(!childOid.HasChildOids ? WalkBulkSingle(version, ipAddress, maxBulkRepetitions, octetString, childOid, walkMode)
                    : WalkBulk(version, ipAddress, maxBulkRepetitions, octetString, childOid, walkMode));
            }

            _log.Info("SnmpEngine.WalkBulk(): Finished");
            return result;
        }
Beispiel #11
0
 public SnmpResult(Oid oid, object data, SnmpDataType dataType)
 {
     _oid = oid;
     _data = data;
     _dataType = dataType;
 }
Beispiel #12
0
        private static Oid InitializeCode(Oid oid, FileInfo file)
        {
            var childoids = new List<Oid>();

            var xml = XDocument.Load(file.OpenRead());

            if (xml.Root == null) return oid;
            var rootNode = xml.Root;

            if (!ValidateCodeFile(rootNode)) return oid;

            var codesElements = rootNode.Elements();

            foreach (var element in codesElements)
            {
                var decimalElement = element.Element(DecimalAttr);
                string objId = null;
                string name = null;
                string fullName = null;
                string decsr = null;

                if (decimalElement != null)
                {
                    var decVal = decimalElement.Value;
                    objId = CreateOid(oid.Value, decVal);
                }

                var nameElement = element.Element(NameAttr);

                if (nameElement != null)
                {
                    name = nameElement.Value;
                    fullName = CreateOid(oid.FullName, name);
                }

                var descriptionElement = element.Element(DescAttr);

                if (descriptionElement != null)
                {
                    decsr = descriptionElement.Value;
                }

                childoids.Add(new Oid(objId, name, fullName) { Description = decsr });
            }

            if (childoids.Any())
            {
                oid.ChildOids = InitializeCodes(childoids);
            }

            return oid;
        }
Beispiel #13
0
        public Hashtable InitAdditionalCodes(Oid oid)
        {
            if (!_codesInfo.Any(file => file.Name.Contains(oid.Name + Additions))) return null;

            var codesTable = new Hashtable();
            var value = DeserializeCodes(_codesInfo.First(file => file.Name.Contains(oid.Name + Additions)));
            codesTable.Add(oid.Name, value);
            return codesTable;
        }
Beispiel #14
0
 private void ChangeSelection(object context)
 {
     if (context is Oid)
     {
         _oidCurrent = (Oid)context;
     }
 }