Exemple #1
0
        private static SnmpEngine CreateSnmpEngine(RavenServer server, ObjectStore objectStore)
        {
            (HashSet <SnmpVersion> versions, string handlerVersion) = GetVersions(server);
            var membershipProvider = CreateMembershipProvider(server, versions);

            var handlers = new[]
            {
                new HandlerMapping(handlerVersion, "GET", new GetMessageHandler()),
                new HandlerMapping(handlerVersion, "GETNEXT", new GetNextMessageHandler()),
                new HandlerMapping(handlerVersion, "GETBULK", new GetBulkMessageHandler())
            };

            var messageHandlerFactory = new MessageHandlerFactory(handlers);

            var factory = new SnmpApplicationFactory(new SnmpLogger(Logger), objectStore, membershipProvider, messageHandlerFactory);

            var listener = new Listener();

            if (versions.Contains(SnmpVersion.V3))
            {
                var authenticationPassword = server.Configuration.Monitoring.Snmp.AuthenticationPassword ?? server.Configuration.Monitoring.Snmp.Community;

                IAuthenticationProvider authenticationProvider;
                switch (server.Configuration.Monitoring.Snmp.AuthenticationProtocol)
                {
                case SnmpAuthenticationProtocol.SHA1:
                    authenticationProvider = new SHA1AuthenticationProvider(new OctetString(authenticationPassword));
                    break;

                case SnmpAuthenticationProtocol.MD5:
                    authenticationProvider = new MD5AuthenticationProvider(new OctetString(authenticationPassword));
                    break;

                default:
                    throw new InvalidOperationException($"Unknown authentication protocol '{server.Configuration.Monitoring.Snmp.AuthenticationProtocol}'.");
                }

                var privacyPassword = server.Configuration.Monitoring.Snmp.PrivacyPassword;

                IPrivacyProvider privacyProvider;
                switch (server.Configuration.Monitoring.Snmp.PrivacyProtocol)
                {
                case SnmpPrivacyProtocol.None:
                    privacyProvider = new DefaultPrivacyProvider(authenticationProvider);
                    break;

                case SnmpPrivacyProtocol.DES:
                    privacyProvider = new BouncyCastleDESPrivacyProvider(new OctetString(privacyPassword), authenticationProvider);
                    break;

                case SnmpPrivacyProtocol.AES:
                    privacyProvider = new BouncyCastleAESPrivacyProvider(new OctetString(privacyPassword), authenticationProvider);
                    break;

                default:
                    throw new InvalidOperationException($"Unknown privacy protocol '{server.Configuration.Monitoring.Snmp.AuthenticationProtocol}'.");
                }

                listener.Users.Add(new OctetString(server.Configuration.Monitoring.Snmp.AuthenticationUser), privacyProvider);
            }

            var engineGroup   = new EngineGroup();
            var engineIdField = engineGroup.GetType().GetField("_engineId", BindingFlags.Instance | BindingFlags.NonPublic);

            engineIdField.SetValue(engineGroup, new OctetString(Guid.NewGuid().ToString("N")));

            var engine = new SnmpEngine(factory, listener, engineGroup);

            engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, server.Configuration.Monitoring.Snmp.Port));
            engine.Listener.ExceptionRaised += (sender, e) =>
            {
                if (Logger.IsOperationsEnabled)
                {
                    Logger.Operations("SNMP error: " + e.Exception.Message, e.Exception);
                }
            };

            return(engine);
        }
Exemple #2
0
        private static SnmpEngine CreateSnmpEngine(RavenServer server, ObjectStore objectStore)
        {
            (HashSet <SnmpVersion> versions, string handlerVersion) = GetVersions(server);
            var membershipProvider = CreateMembershipProvider(server, versions);

            var handlers = new[]
            {
                new HandlerMapping(handlerVersion, "GET", new GetMessageHandler()),
                new HandlerMapping(handlerVersion, "GETNEXT", new GetNextMessageHandler()),
                new HandlerMapping(handlerVersion, "GETBULK", new GetBulkMessageHandler())
            };

            var messageHandlerFactory = new MessageHandlerFactory(handlers);

            var factory = new SnmpApplicationFactory(new SnmpLogger(Logger), objectStore, membershipProvider, messageHandlerFactory);

            var listener = new Listener();

            if (versions.Contains(SnmpVersion.V3))
            {
                var authenticationPassword = server.Configuration.Monitoring.Snmp.AuthenticationPassword ?? server.Configuration.Monitoring.Snmp.Community;

                IAuthenticationProvider authenticationProvider;
                switch (server.Configuration.Monitoring.Snmp.AuthenticationProtocol)
                {
                case SnmpAuthenticationProtocol.SHA1:
                    authenticationProvider = new SHA1AuthenticationProvider(new OctetString(authenticationPassword));
                    break;

                case SnmpAuthenticationProtocol.MD5:
                    authenticationProvider = new MD5AuthenticationProvider(new OctetString(authenticationPassword));
                    break;

                default:
                    throw new InvalidOperationException($"Unknown authentication protocol '{server.Configuration.Monitoring.Snmp.AuthenticationProtocol}'.");
                }

                var privacyPassword = server.Configuration.Monitoring.Snmp.PrivacyPassword;

                IPrivacyProvider privacyProvider;
                switch (server.Configuration.Monitoring.Snmp.PrivacyProtocol)
                {
                case SnmpPrivacyProtocol.None:
                    privacyProvider = new DefaultPrivacyProvider(authenticationProvider);
                    break;

                case SnmpPrivacyProtocol.DES:
                    privacyProvider = new BouncyCastleDESPrivacyProvider(new OctetString(privacyPassword), authenticationProvider);
                    break;

                case SnmpPrivacyProtocol.AES:
                    privacyProvider = new BouncyCastleAESPrivacyProvider(new OctetString(privacyPassword), authenticationProvider);
                    break;

                default:
                    throw new InvalidOperationException($"Unknown privacy protocol '{server.Configuration.Monitoring.Snmp.AuthenticationProtocol}'.");
                }

                listener.Users.Add(new OctetString(server.Configuration.Monitoring.Snmp.AuthenticationUser), privacyProvider);
            }

            int engineBoots;

            using (server.ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var tx = context.OpenWriteTransaction())
                {
                    var tree = tx.InnerTransaction.CreateTree(nameof(SnmpWatcher));
                    engineBoots = (int)tree.Increment("EngineBoots", 1);

                    tx.Commit();
                }

            var engineGroup = new EngineGroup(engineBoots, GetIsInTime(server.Configuration.Monitoring))
            {
                EngineId = new OctetString(server.ServerStore.GetServerId().ToString("N"))
            };

            var engine = new SnmpEngine(factory, listener, engineGroup);

            engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, server.Configuration.Monitoring.Snmp.Port));
            engine.Listener.ExceptionRaised += (sender, e) =>
            {
                if (Logger.IsOperationsEnabled)
                {
                    Logger.Operations("SNMP error: " + e.Exception.Message, e.Exception);
                }
            };

            return(engine);
        }