Exemplo n.º 1
0
 protected override void InternalProcessRecord()
 {
     TaskLogger.LogEnter();
     ADSession.SetAdminTopologyMode();
     if (ExchangePropertyContainer.IsContainerInitialized(base.SessionState))
     {
         ExchangePropertyContainer.SetServerSettings(base.SessionState, null);
     }
     base.SessionState.Variables[ExchangePropertyContainer.ADServerSettingsVarName] = null;
     TaskLogger.LogExit();
 }
        // Token: 0x06000005 RID: 5 RVA: 0x000021B4 File Offset: 0x000003B4
        internal static void Main(string[] args)
        {
            MonitoringService.LogDiagnosticInfo("Registering for Watson.", new object[0]);
            ExWatson.Register();
            bool flag  = !Environment.UserInteractive;
            bool flag2 = false;
            bool flag3 = false;

            MonitoringService.LogDiagnosticInfo("Parsing command line args.", new object[0]);
            foreach (string text in args)
            {
                if (text.StartsWith("-?", StringComparison.OrdinalIgnoreCase))
                {
                    MonitoringService.LogDiagnosticInfo("Printing usage and exiting.", new object[0]);
                    MonitoringService.PrintUsage();
                    Environment.Exit(0);
                }
                else if (text.StartsWith("-console", StringComparison.OrdinalIgnoreCase))
                {
                    MonitoringService.LogDiagnosticInfo("Running from console.", new object[0]);
                    flag2 = true;
                }
                else if (text.StartsWith("-wait", StringComparison.OrdinalIgnoreCase))
                {
                    flag3 = true;
                }
                else if (text.StartsWith("-ForceConsole", StringComparison.OrdinalIgnoreCase))
                {
                    MonitoringService.LogDiagnosticInfo("Force running from console.", new object[0]);
                    flag  = false;
                    flag2 = true;
                }
            }
            if (!flag)
            {
                if (!flag2)
                {
                    MonitoringService.LogDiagnosticInfo("Printing usage and exiting.", new object[0]);
                    MonitoringService.PrintUsage();
                    Environment.Exit(0);
                }
                Console.WriteLine("Starting {0}, running in console mode.", Assembly.GetExecutingAssembly().GetName().Name);
                if (flag3)
                {
                    MonitoringService.LogDiagnosticInfo("Waiting for user input before continuing.", new object[0]);
                    Console.WriteLine("Press ENTER to continue.");
                    Console.ReadLine();
                }
            }
            if (!ServiceTopologyProvider.IsAdTopologyServiceInstalled())
            {
                MonitoringService.LogDiagnosticInfo("Can't use AD Topology service; setting admin mode instead.", new object[0]);
                ADSession.SetAdminTopologyMode();
            }
            MonitoringService.LogDiagnosticInfo("Initializing perf counters.", new object[0]);
            Globals.InitializeMultiPerfCounterInstance("MSExchangeHM");
            MonitoringService monitoringService = new MonitoringService(flag);

            if (!monitoringService.Initialize())
            {
                ExTraceGlobals.ServiceTracer.TraceError(0L, "Failed to initialize the service. Exiting.");
                MonitoringService.LogDiagnosticInfo("Initialization of the service failed. Stopping service and exiting.", new object[0]);
                ProcessManagerService.StopService();
            }
            if (!flag)
            {
                monitoringService.OnStartInternal(args);
                bool flag4 = false;
                while (!flag4)
                {
                    Console.WriteLine("Enter 'q' to shutdown.");
                    string text2 = Console.ReadLine();
                    if (string.IsNullOrEmpty(text2))
                    {
                        break;
                    }
                    switch (text2[0])
                    {
                    case 'q':
                        flag4 = true;
                        break;

                    case 'r':
                        monitoringService.OnCustomCommandInternal(200);
                        break;

                    case 'u':
                        monitoringService.OnCustomCommandInternal(201);
                        break;
                    }
                }
                Console.WriteLine("Shutting down ...");
                monitoringService.OnStopInternal();
                Console.WriteLine("Done.");
                return;
            }
            ServiceBase.Run(monitoringService);
        }
        // Token: 0x06000010 RID: 16 RVA: 0x000024E8 File Offset: 0x000006E8
        private void Run(string[] args)
        {
            bool   flag  = false;
            bool   flag2 = false;
            bool   flag3 = false;
            bool   flag4 = false;
            string text  = null;
            string name  = null;
            string name2 = null;
            string s     = null;

            foreach (string text2 in args)
            {
                if (text2.StartsWith("-?", StringComparison.OrdinalIgnoreCase))
                {
                    MonitoringWorker.PrintUsageAndExit();
                }
                else if (text2.StartsWith("-console", StringComparison.OrdinalIgnoreCase))
                {
                    flag3 = true;
                }
                else if (text2.StartsWith("-stopkey:", StringComparison.OrdinalIgnoreCase))
                {
                    text = text2.Remove(0, "-stopkey:".Length);
                }
                else if (text2.StartsWith("-hangkey:", StringComparison.OrdinalIgnoreCase))
                {
                    name = text2.Remove(0, "-hangkey:".Length);
                }
                else if (text2.StartsWith("-resetkey:", StringComparison.OrdinalIgnoreCase))
                {
                    text2.Remove(0, "-resetkey:".Length);
                }
                else if (text2.StartsWith("-readykey:", StringComparison.OrdinalIgnoreCase))
                {
                    name2 = text2.Remove(0, "-readykey:".Length);
                }
                else if (text2.StartsWith("-pipe:", StringComparison.OrdinalIgnoreCase))
                {
                    s = text2.Remove(0, "-pipe:".Length);
                }
                else if (text2.StartsWith("-paused", StringComparison.OrdinalIgnoreCase))
                {
                    flag = true;
                }
                else if (text2.StartsWith("-passive", StringComparison.OrdinalIgnoreCase))
                {
                    flag2 = true;
                }
                else if (text2.StartsWith("-wait", StringComparison.OrdinalIgnoreCase))
                {
                    flag4 = true;
                }
            }
            this.serviceControlled = !string.IsNullOrEmpty(text);
            if (!this.serviceControlled)
            {
                if (!flag3)
                {
                    MonitoringWorker.PrintUsageAndExit();
                }
                Console.WriteLine("Starting {0}, running in console mode.", Assembly.GetExecutingAssembly().GetName().Name);
                if (flag4)
                {
                    Console.WriteLine("Press ENTER to continue.");
                    Console.ReadLine();
                }
            }
            this.stopHandle  = MonitoringWorker.OpenSemaphore(text, "stop");
            this.hangHandle  = MonitoringWorker.OpenSemaphore(name, "hang");
            this.readyHandle = MonitoringWorker.OpenSemaphore(name2, "ready");
            if (this.hangHandle != null)
            {
                new Thread(new ThreadStart(this.WaitForHangSignal))
                {
                    IsBackground = true
                }.Start();
            }
            if (CriticalDependencyAlertEscalator.RunningInMicrosoftDatacenter())
            {
                bool   flag5 = true;
                string name3 = "SOFTWARE\\Microsoft\\ExchangeServer\\v15\\ActiveMonitoring\\CriticalDependencyVerification\\";
                using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(name3))
                {
                    if (registryKey != null)
                    {
                        object value = registryKey.GetValue("Enabled");
                        if (value != null && value is int && (int)value == 0)
                        {
                            flag5 = false;
                        }
                    }
                }
                if (flag5)
                {
                    WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Attempting to verify critical dependencies before continuing with initialization.", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 543);
                    CriticalDependencyVerification criticalDependencyVerification = new CriticalDependencyVerification(ExTraceGlobals.CommonComponentsTracer, TracingContext.Default);
                    if (!criticalDependencyVerification.VerifyDependencies())
                    {
                        WTFLogger.Instance.LogError(WTFLog.Core, TracingContext.Default, "Failed to verify one or more critical dependencies. We will try to continue running, but may hang, crash, or exit unexpectedly.", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 551);
                    }
                    else
                    {
                        WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Successfully verified all critical dependencies. Continuing with initialization.", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 555);
                    }
                }
            }
            if (!ServiceTopologyProvider.IsAdTopologyServiceInstalled())
            {
                ADSession.SetAdminTopologyMode();
            }
            Globals.InitializeMultiPerfCounterInstance("ExHMWorker");
            LocalEndpointManager.UseMaintenanceWorkItem = true;
            WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Initializing global override lists", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 573);
            try
            {
                DirectoryAccessor.Instance.LoadGlobalOverrides();
            }
            catch (Exception arg)
            {
                ExTraceGlobals.WorkerTracer.TraceError <Exception>(0L, "Initializing global override lists failed with exception {0}", arg);
            }
            WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Initializing local override lists", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 586);
            LocalOverrideManager.LoadLocalOverrides();
            Settings.RemoveAllOverrides();
            if (MaintenanceDefinition.GlobalOverrides != null)
            {
                foreach (WorkDefinitionOverride o in MaintenanceDefinition.GlobalOverrides)
                {
                    this.ApplyConfigurationOverrideIfNecessary(o);
                }
            }
            if (MaintenanceDefinition.LocalOverrides != null)
            {
                foreach (WorkDefinitionOverride o2 in MaintenanceDefinition.LocalOverrides)
                {
                    this.ApplyConfigurationOverrideIfNecessary(o2);
                }
            }
            Assembly assembly = typeof(LocalEndpointManager).Assembly;

            WorkItemFactory.DefaultAssemblies["Microsoft.Exchange.Monitoring.ActiveMonitoring.Local.Components.dll"] = assembly;
            WorkItemFactory.DefaultAssemblies[assembly.Location] = assembly;
            WorkItemFactory factory = new WorkItemFactory();
            bool            flag6   = true;
            bool            flag7   = true;

            try
            {
                flag6 = ServerComponentStateManager.IsOnline(ServerComponentEnum.Monitoring);
            }
            catch (Exception ex)
            {
                MonitoringWorker.eventLogger.LogEvent(MSExchangeHMEventLogConstants.Tuple_FailedToGetIsOnlineState, null, new object[]
                {
                    MonitoringWorker.processId,
                    ServerComponentEnum.Monitoring.ToString(),
                    flag6.ToString(),
                    ex.ToString()
                });
            }
            try
            {
                flag7 = ServerComponentStateManager.IsOnline(ServerComponentEnum.RecoveryActionsEnabled);
            }
            catch (Exception ex2)
            {
                MonitoringWorker.eventLogger.LogEvent(MSExchangeHMEventLogConstants.Tuple_FailedToGetIsOnlineState, null, new object[]
                {
                    MonitoringWorker.processId,
                    ServerComponentEnum.RecoveryActionsEnabled.ToString(),
                    flag7.ToString(),
                    ex2.ToString()
                });
            }
            if (flag6 && flag7)
            {
                this.activeMonitoringWorker = new Worker(new WorkBroker[]
                {
                    new ProbeWorkBroker <LocalDataAccess>(factory),
                    new MonitorWorkBroker <LocalDataAccess>(factory),
                    new ResponderWorkBroker <LocalDataAccess>(factory),
                    new MaintenanceWorkBroker <LocalDataAccess>(factory)
                }, new Action(this.OnExitCallback), true);
                MonitoringWorker.eventLogger.LogEvent(MSExchangeHMEventLogConstants.Tuple_HealthManagerWorkerStarted, null, new object[]
                {
                    MonitoringWorker.processId,
                    Strings.StartedWithAllWorkBrokers(flag6, flag7)
                });
            }
            else if (flag6 && !flag7)
            {
                this.activeMonitoringWorker = new Worker(new WorkBroker[]
                {
                    new ProbeWorkBroker <LocalDataAccess>(factory),
                    new MonitorWorkBroker <LocalDataAccess>(factory),
                    new MaintenanceWorkBroker <LocalDataAccess>(factory)
                }, new Action(this.OnExitCallback), true);
                MonitoringWorker.eventLogger.LogEvent(MSExchangeHMEventLogConstants.Tuple_HealthManagerWorkerStarted, null, new object[]
                {
                    MonitoringWorker.processId,
                    Strings.StartedWithAllWorkBrokersExceptResponder(flag6, flag7)
                });
            }
            else
            {
                this.activeMonitoringWorker = new Worker(new WorkBroker[]
                {
                    new MaintenanceWorkBroker <LocalDataAccess>(factory)
                }, new Action(this.OnExitCallback), true);
                MonitoringWorker.eventLogger.LogEvent(MSExchangeHMEventLogConstants.Tuple_HealthManagerWorkerStarted, null, new object[]
                {
                    MonitoringWorker.processId,
                    Strings.StartedWithMaintenanceWorkBrokerOnly(flag6, flag7)
                });
            }
            try
            {
                if (this.serviceControlled)
                {
                    SafeFileHandle handle = new SafeFileHandle(new IntPtr(long.Parse(s)), true);
                    this.listenPipeStream = new PipeStream(handle, FileAccess.Read, true);
                    this.controlObject    = new ControlObject(this.listenPipeStream, this);
                    if (this.controlObject.Initialize())
                    {
                        if (this.readyHandle != null)
                        {
                            WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Signal the process is ready", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 726);
                            this.readyHandle.Release();
                            this.readyHandle.Close();
                            this.readyHandle = null;
                        }
                        if (!flag && !flag2)
                        {
                            WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Start processing work items", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 736);
                            this.Activate();
                        }
                        WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Waiting for shutdown signal to exit.", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 742);
                        this.stopHandle.WaitOne();
                    }
                }
                else
                {
                    if (!flag)
                    {
                        Console.WriteLine("Start processing work items");
                        this.Activate();
                    }
                    Console.WriteLine("Press ENTER to exit ");
                    bool flag8 = false;
                    while (!flag8)
                    {
                        string text3 = Console.ReadLine();
                        if (string.IsNullOrEmpty(text3))
                        {
                            Console.WriteLine("Exiting.");
                            flag8 = true;
                        }
                        else if (text3.Equals("p"))
                        {
                            Console.WriteLine("Pause.");
                            this.Pause();
                        }
                        else if (text3.Equals("c"))
                        {
                            Console.WriteLine("Continue.");
                            this.Continue();
                        }
                        else if (text3.Equals("s"))
                        {
                            Console.WriteLine("Stop.");
                            this.Stop();
                        }
                        else
                        {
                            Console.WriteLine("Unknown command.");
                        }
                    }
                }
            }
            catch (Exception ex3)
            {
                MonitoringWorker.eventLogger.LogEvent(MSExchangeHMEventLogConstants.Tuple_WorkerRestartOnUnknown, null, new object[]
                {
                    MonitoringWorker.processId,
                    ex3.ToString()
                });
                if (!this.serviceControlled && ((ex3 is Win32Exception && ((Win32Exception)ex3).NativeErrorCode == 5) || ex3 is UnauthorizedAccessException || ex3 is SecurityAccessDeniedException))
                {
                    Console.WriteLine("You must run the worker process with elevated privileges");
                }
            }
            WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Received a signal to shut down.  Closing control pipe.", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 814);
            this.DoCleanUp();
            this.SavePersistentState();
            WTFLogger.Instance.LogDebug(WTFLog.Core, TracingContext.Default, "Monitoring worker process stopped.", "Run", "f:\\15.00.1497\\sources\\dev\\monitoring\\src\\ActiveMonitoring\\Local\\WorkerProcess\\Program.cs", 820);
            WTFLogger.Instance.Flush();
            MonitoringWorker.eventLogger.LogEvent(MSExchangeHMEventLogConstants.Tuple_HealthManagerWorkerStopped, null, new object[]
            {
                MonitoringWorker.processId
            });
        }