コード例 #1
0
 public static bool IsWindowsServer()
 {
     return(HTTPActivationTest.IsOS(HTTPActivationTest.OS_ANYSERVER));
 }
コード例 #2
0
ファイル: QuickMonService.cs プロジェクト: utobe/QuickMon
        protected override void OnStart(string[] args)
        {
            #region Provide way to attach debugger by Waiting for specified time
#if DEBUG
            //The following code is simply to ease attaching the debugger to the service to debug the startup routine
            DateTime startTime = DateTime.Now;
            while ((!Debugger.IsAttached) && ((TimeSpan)DateTime.Now.Subtract(startTime)).TotalSeconds < 20) // Waiting until debugger is attached
            {
                RequestAdditionalTime(1000);                                                                 // Prevents the service from timeout
                Thread.Sleep(1000);                                                                          // Gives you time to attach the debugger
            }
            // increase as needed to prevent timeouts
            RequestAdditionalTime(5000);     // for Debugging the OnStart method,
#endif
            #endregion

            InitializeGlobalPerformanceCounters();
            concurrencyLevel = Properties.Settings.Default.ParralelThreads;
            int monitorPacksLoaded = 0;

            //There are two ways to specify Monitor packs
            //Old way is by using the MonitorPackPaths array
            if (Properties.Settings.Default.MonitorPackPaths != null && Properties.Settings.Default.MonitorPackPaths.Count > 0)
            {
                foreach (string monitorPackPath in Properties.Settings.Default.MonitorPackPaths)
                {
                    if (System.IO.File.Exists(monitorPackPath))
                    {
                        AddAndStartMonitorPack(monitorPackPath);
                        monitorPacksLoaded++;
                    }
                }
            }
            //New way is to list them in an external file
            if (Properties.Settings.Default.MonitorPackFile != null && Properties.Settings.Default.MonitorPackFile.Length > 0)
            {
                string monitorPackFile = Properties.Settings.Default.MonitorPackFile;
                if (!monitorPackFile.Contains("\\"))
                {
                    monitorPackFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), monitorPackFile);
                }
                if (System.IO.File.Exists(monitorPackFile))
                {
                    foreach (string monitorPackPath in System.IO.File.ReadAllLines(monitorPackFile))
                    {
                        if (System.IO.File.Exists(monitorPackPath))
                        {
                            AddAndStartMonitorPack(monitorPackPath);
                            monitorPacksLoaded++;
                        }
                    }
                }
            }
            if (monitorPacksLoaded == 0)
            {
                EventLog.WriteEntry(Globals.ServiceEventSourceName, "No (valid) monitor pack specified in service config! This service will only operate as a Remote Collector Host (if enabled). To hide this warning please add some MonitorPacks in QuickMonService.exe.config",
                                    EventLogEntryType.Warning, 0);
            }
            else
            {
                EventLog.WriteEntry(Globals.ServiceEventSourceName, string.Format("Started QuickMon Service with {0} monitor pack(s).",
                                                                                  packs.Count), EventLogEntryType.Information, 0);
            }

            EventLog.WriteEntry(Globals.ServiceEventSourceName,
                                string.Format("Started QuickMon monitoring and alerting service.\r\nService version: {0}\r\nShared components version: {1}\r\nConcurrency level: {2}",
                                              System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
                                              System.Reflection.Assembly.GetAssembly(typeof(MonitorPack)).GetName().Version.ToString(),
                                              concurrencyLevel
                                              ),
                                EventLogEntryType.Information, 0);

            if (Properties.Settings.Default.EnableRemoteHost)
            {
                if (wcfServiceHost != null)
                {
                    wcfServiceHost.Close();
                }
                try
                {
                    if (!HTTPActivationTest.IsHTTPActivationEnabled())
                    {
                        EventLog.WriteEntry(Globals.ServiceEventSourceName, "WARNING: Server Feature WCF HTTP Activation is not installed! Remote host functionality may not work.", EventLogEntryType.Warning, 0);
                    }
                }
                catch (Exception httpActEx)
                {
                    EventLog.WriteEntry(Globals.ServiceEventSourceName, "Server Feature WCF HTTP Activation Test failed: " + httpActEx.Message, EventLogEntryType.Error, 0);
                }
                if (Properties.Settings.Default.WcfRenameLocalHostNameToRealHost)
                {
                    if (Properties.Settings.Default.WcfServiceURL.Contains("localhost"))
                    {
                        baseAddress = new Uri(Properties.Settings.Default.WcfServiceURL.Replace("localhost", System.Net.Dns.GetHostName()));
                    }
                }
                EventLog.WriteEntry(Globals.ServiceEventSourceName, "Starting Remote Agent Host service for : " + baseAddress.OriginalString, EventLogEntryType.Information, 0);
                wcfServiceHost = new ServiceHost(typeof(CollectorEntryRelay), baseAddress);
                if (Properties.Settings.Default.WcfEnableMetadata)
                {
                    // Enable metadata publishing.
                    System.ServiceModel.Description.ServiceMetadataBehavior smb = new System.ServiceModel.Description.ServiceMetadataBehavior();
                    smb.HttpGetEnabled = true;
                    smb.MetadataExporter.PolicyVersion = System.ServiceModel.Description.PolicyVersion.Policy15;
                    wcfServiceHost.Description.Behaviors.Add(smb);
                    wcfServiceHost.AddServiceEndpoint(typeof(ICollectorEntryRelay), new BasicHttpBinding(), baseAddress);
                }
                wcfServiceHost.Open();
            }
        }