Example #1
0
        /// <summary>
        /// Checks if at least one if the machines in the server configuration reacts to ping.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns>The result as CheckSuspendResult.</returns>
        public CheckSuspendResult CheckPing(WsapmSettings settings)
        {
            if (!settings.EnableMonitorNetworkMachines || settings.NetworkMachinesToMonitor == null || settings.NetworkMachinesToMonitor.Count == 0)
            {
                return(new CheckSuspendResult(false, String.Empty));
            }

            for (int i = 0; i < settings.NetworkMachinesToMonitor.Count; i++)
            {
                // Check IP address based.
                if ((settings.NetworkMachinesToMonitor[i].IPAddress) != null)
                {
                    IPAddress ip     = settings.NetworkMachinesToMonitor[i].IPAddress;
                    var       result = Ping(ip);

                    if (result.SuspendStandby)
                    {
                        return(result);
                    }
                }

                // Check machine name based.
                if (!String.IsNullOrEmpty(settings.NetworkMachinesToMonitor[i].Name))
                {
                    var result = Ping(settings.NetworkMachinesToMonitor[i].Name);

                    if (result.SuspendStandby)
                    {
                        return(result);
                    }
                }
            }

            return(new CheckSuspendResult(false, String.Empty));
        }
Example #2
0
        /// <summary>
        /// Checks if the current memory usage is over the menory load value in the settings.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns>The result as CheckSuspendResult.</returns>
        public CheckSuspendResult CheckMemoryLoad(WsapmSettings settings)
        {
            if (settings == null)
            {
                return(new CheckSuspendResult(false, String.Empty));
            }

            try
            {
                if (!settings.EnableCheckMemoryLoad || settings.MemoryLoad == 0.0f)
                {
                    return(new CheckSuspendResult(false, String.Empty));
                }

                var averageLoad = this.memoryLoad.GetAverageMemoryLoad(NumberCheckProbes, TimeSpan.FromMilliseconds(WsapmConstants.PerformanceCounterBreakIntervallMs));
                var result      = averageLoad > settings.MemoryLoad;

                if (result)
                {
                    return(new CheckSuspendResult(true, String.Format(Resources.Wsapm_Core.MemoryLoadCheck_MemoryLoadCheckReason, settings.MemoryLoad, averageLoad.ToString("0"))));
                }
                else
                {
                    return(new CheckSuspendResult(false, String.Empty));
                }
            }
            catch (Exception ex)
            {
                // Might happen if the performance counters get broken.
                WsapmLog.Log.WriteError(ex);
            }

            return(new CheckSuspendResult(false, String.Empty));
        }
Example #3
0
        private CheckSuspendResult CheckNetworkFileAccess(WsapmSettings settings)
        {
            if (!settings.EnableCheckNetworkResourceAccess)
            {
                return(new CheckSuspendResult(false, string.Empty));
            }

            string[] openedResources = null;

            if ((settings.CheckNetworkResourcesType & NetworkShareAccessType.Files) == NetworkShareAccessType.Files)
            {
                // Only check files.
                openedResources = NetworkShareManager.GetFilesOpenedInNetworkShare();
            }
            else if ((settings.CheckNetworkResourcesType & NetworkShareAccessType.Files) == NetworkShareAccessType.Directories)
            {
                // Only check directories.
                openedResources = NetworkShareManager.GetDirectoriesOpenedInNetworkShare();
            }
            else
            {
                // Check files and directories.
                openedResources = NetworkShareManager.GetResourcesOpenedInNetworkShare();
            }

            if (openedResources != null && openedResources.Length > 0)
            {
                return(new CheckSuspendResult(true, String.Format(Resources.Wsapm_Core.NetworkResourcesCheck_NetworkResourcesCheckReason, openedResources[0]))); // Only take first file for reason.
            }
            else
            {
                return(new CheckSuspendResult(false, string.Empty));
            }
        }
Example #4
0
        /// <summary>
        /// Saves the settings to the settings file.
        /// </summary>
        /// <param name="settings">The settings to save.</param>
        public void SaveSettings(WsapmSettings settings)
        {
            if (settings == null)
            {
                return;
            }

            if (!Directory.Exists(this.settingsFolder))
            {
                Directory.CreateDirectory(this.settingsFolder);
            }

            SaveSettings(settings, this.settingsFile);
        }
Example #5
0
        /// <summary>
        /// Checks if the standby should be suspended.
        /// </summary>
        /// <param name="settings">The WsapmSettings to use.</param>
        /// <returns>The result as CheckSuspendResult.</returns>
        public CheckSuspendResult CheckStandby(WsapmSettings settings)
        {
            var temporaryUptime = this.temporaryUptimeManager.TemporaryUptimeDefinedAndActive;

            if (!temporaryUptime.HasValue)
            {
                // No temporary uptime defined.
                return(new CheckSuspendResult(false, String.Empty));
            }
            else
            {
                // Temporary uptime defined and active.
                return(new CheckSuspendResult(true, string.Format(Wsapm.Core.Resources.Wsapm_Core.TemporaryUptimeCheck_UptimeDefinedReason, temporaryUptime.Value.ToShortTimeString())));
            }
        }
Example #6
0
        /// <summary>
        /// Loads the settings and updates the current settings.
        /// </summary>
        private void LoadSettings()
        {
            try
            {
                this.currentSettings        = this.settingsManager.LoadSettings();
                WsapmLog.Log.MaxLogFileSize = this.currentSettings.MaxLogFileSize;
                WsapmLog.Log.LogMode        = this.currentSettings.LogMode;

                ValidateSettings(this.currentSettings);
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
Example #7
0
        public static void ExecuteActionsAfterPolicyCheckAtLeastPolicySatisfied(WsapmSettings settings)
        {
            if (settings == null || !settings.EnableActionsAfterPolicyCheck || settings.ActionsAfterPolicyCheck == null || settings.ActionsAfterPolicyCheck.Count == 0)
            {
                return;
            }

            foreach (var action in settings.ActionsAfterPolicyCheck)
            {
                if (action.ActionModeAfterPolicyCheck == ActionModeAfterPolicyCheck.AtLeastOnePolicySatisfied)
                {
                    ExecuteAction(action);
                }
            }
        }
Example #8
0
        /// <summary>
        /// Validates the given settings.
        /// </summary>
        /// <param name="settings"></param>
        private void ValidateSettings(WsapmSettings settings)
        {
            // Validate settings and print warning when needed.
            if (!WsapmTools.ValidateCheckInterval(settings))
            {
                var winIdleTimeoutAcMinutes = WsapmTools.GetCurrentWindowsIdleTimeoutAcMinutes();
                WsapmLog.Log.WriteWarning(String.Format(Resources.Wsapm_Core.WsapmManager_ValidationErrorCheckInterval, winIdleTimeoutAcMinutes, settings.MonitoringTimerInterval), LogMode.Verbose);
            }

            // Validate if wake timers are allowed.
            if (!WsapmTools.ValidateWakeTimers(settings))
            {
                WsapmLog.Log.WriteWarning(Resources.Wsapm_Core.WsapmManager_ValidationErrorWakeTimers, LogMode.Verbose);
            }
        }
Example #9
0
        /// <summary>
        /// Determines if the state of WakeScheduler and Windows power options are valid.
        /// </summary>
        /// <param name="settings">The WsampSettings to validate.</param>
        /// <returns>True if the state of the setting's WakeScheduler is valid with the current Windows power options, otherwise false.</returns>
        public static bool ValidateWakeTimers(WsapmSettings settings)
        {
            bool atLeastOneWakeSchedulerActive = false;

            if (settings.WakeSchedulers != null)
            {
                foreach (var wakeScheduler in settings.WakeSchedulers)
                {
                    if (wakeScheduler.EnableWakeScheduler)
                    {
                        atLeastOneWakeSchedulerActive = true;
                        break;
                    }
                }
            }

            return(!(settings.WakeSchedulers != null && atLeastOneWakeSchedulerActive && !WsapmTools.GetWakeTimersAllowed()));
        }
Example #10
0
        /// <summary>
        /// Checks if at least one of the processes in the server configuration is running.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns>The result as CheckSuspendResult.</returns>
        private CheckSuspendResult CheckProcesses(WsapmSettings settings)
        {
            if (!settings.EnableMonitorProcesses || settings.ProcessesToMonitor == null || settings.ProcessesToMonitor.Count == 0)
            {
                return(new CheckSuspendResult(false, String.Empty));
            }

            for (int i = 0; i < settings.ProcessesToMonitor.Count; i++)
            {
                var result = CheckProcess(settings.ProcessesToMonitor[i]);

                if (result.SuspendStandby)
                {
                    return(result);
                }
            }

            return(new CheckSuspendResult(false, String.Empty));
        }
Example #11
0
        /// <summary>
        /// Checks for a scheduled uptime is running.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns>The result as CheckSuspendResult.</returns>
        private CheckSuspendResult CheckScheduledUptime(WsapmSettings settings)
        {
            if (!settings.EnableUptimes || settings.UptimeSchedulers == null || settings.UptimeSchedulers.Count == 0)
            {
                return(new CheckSuspendResult(false, String.Empty));
            }

            for (int i = 0; i < settings.UptimeSchedulers.Count; i++)
            {
                var result = CheckUptime(settings.UptimeSchedulers[i]);

                if (result.SuspendStandby)
                {
                    return(result);
                }
            }

            return(new CheckSuspendResult(false, String.Empty));
        }
Example #12
0
        /// <summary>
        /// Checks if a given file is a valid WSAPM settings file.
        /// </summary>
        /// <param name="fileName">The file to check.</param>
        /// <returns>True, if the given file is a valid WSAPM settings file, otherwise false.</returns>
        private bool IsValidSettingsFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName))
            {
                return(false);
            }

            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    WsapmSettings settings = this.serializer.Deserialize(fs) as WsapmSettings;
                    return(settings != null);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #13
0
        /// <summary>
        /// Loads the default settings.
        /// </summary>
        public WsapmSettings GetDefaultSettings()
        {
            var newSettings = new WsapmSettings();

            newSettings.MonitoringTimerInterval    = WsapmTools.GetOptimalCheckIntervalInMinutes();
            newSettings.MaxLogFileSize             = WsapmConvert.ConvertKBToByte(100);
            newSettings.NetworkInterfacesToMonitor = new System.Collections.Generic.List <NetworkInterfaceToMonitor>();
            newSettings.HddsToMonitor = new System.Collections.Generic.List <HddToMonitor>();
            newSettings.EnableCheckNetworkResourceAccess = false;
            newSettings.CheckNetworkResourcesType        = NetworkShareAccessType.Files;
            newSettings.CpuLoad                    = 0.0f;
            newSettings.MemoryLoad                 = 0.0f;
            newSettings.UptimeSchedulers           = new System.Collections.Generic.List <UptimeScheduler>();
            newSettings.WakeSchedulers             = new System.Collections.Generic.List <WakeScheduler>();
            newSettings.LogMode                    = LogMode.Normal;
            newSettings.EnableRemoteShutdown       = false;
            newSettings.RemoteShutdownPort         = 9;
            newSettings.RemoteShutdownPasswordHash = string.Empty;
            return(newSettings);
        }
Example #14
0
        /// <summary>
        /// Loads settings from settings file.
        /// </summary>
        /// <returns>The loaded settings.</returns>
        public WsapmSettings LoadSettings()
        {
            try
            {
                if (!Directory.Exists(WsapmTools.GetCommonApplicationDataFolder()) || !File.Exists(this.settingsFile))
                {
                    var newSetings = GetDefaultSettings();
                    return(newSetings);
                }

                using (FileStream fs = new FileStream(this.settingsFile, FileMode.Open, FileAccess.Read))
                {
                    WsapmSettings settings = this.serializer.Deserialize(fs) as WsapmSettings;
                    return(settings);
                }
            }
            catch (IOException ex)
            {
                throw new WsapmException(Resources.Wsapm_Core.SettingsManager_LoadSettingsError, ex);
            }
        }
Example #15
0
        /// <summary>
        /// Cleanup of the class.
        /// </summary>
        private void CleanUp()
        {
            try
            {
                StopResumeAutomaticTimer();

                EnableStandby();

                this.standbyManager = null;

                StopShutdownListening();
                this.shutdownManager = null;

                StopMonitoringTimer();
                this.monitoringTimer = null;

                if (this.wakeManager != null)
                {
                    this.wakeManager.WakeTimerCompleted -= wakeManager_WakeTimerCompleted;
                    this.wakeManager.CancelAllWakeTimers();
                }

                this.wakeManager   = null;
                this.pluginManager = null;

                WsapmLog.Log.WriteLine(Resources.Wsapm_Core.WsapmManager_StopMonitoringTimer, LogMode.Normal);

                if (this.settingsManager != null)
                {
                    this.settingsManager.SettingsChanged -= settingsManager_SettingsChanged;
                }

                this.currentSettings = null;
                this.settingsManager = null;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
Example #16
0
        /// <summary>
        /// Saves the settings to the settings file specified.
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="settingsFile"></param>
        internal void SaveSettings(WsapmSettings settings, string settingsFile)
        {
            if (settings == null || string.IsNullOrEmpty(settingsFile))
            {
                return;
            }

            try
            {
                if (File.Exists(settingsFile))
                {
                    File.Delete(settingsFile);
                }

                using (FileStream fs = new FileStream(settingsFile, FileMode.Create, FileAccess.Write))
                {
                    this.serializer.Serialize(fs, (object)settings);
                }
            }
            catch (Exception ex)
            {
                throw new WsapmException(Resources.Wsapm_Core.SettingsManager_SaveSettingsError, ex);
            }
        }
Example #17
0
        /// <summary>
        /// Checks if the HDD load is over the HDD load value in the settings.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns>True, if the current HDD load is greater than the HDD load value in the settings, otherwise false.</returns>
        public CheckSuspendResult CheckHddLoad(WsapmSettings settings)
        {
            if (settings == null || !settings.EnableHddsToMonitor)
            {
                return(new CheckSuspendResult(false, String.Empty));
            }

            var availableHdds = WsapmTools.GetAvailableLogicalVolumeNames();

            foreach (var hddToMonitor in settings.HddsToMonitor)
            {
                if (hddToMonitor.Drive != WsapmConstants.AllHddsName && !availableHdds.Contains(hddToMonitor.Drive))
                {
                    // HDD not available -> don't check, just write entry in log.
                    WsapmLog.Log.WriteLine(string.Format(Wsapm.Core.Resources.Wsapm_Core.HddLoadCheck_HddNotAvailable, hddToMonitor.Drive), LogMode.Normal);
                    continue;
                }

                bool   checkAllHdds = false;
                string hddName      = hddToMonitor.Drive;

                if (hddToMonitor.Drive == WsapmConstants.AllHddsName)
                {
                    checkAllHdds = true;
                    hddName      = WsapmTools.GetCommonDiaplayNameAllDrives();
                }

                // Check load.
                try
                {
                    if (hddToMonitor.EnableCheckHddLoad && hddToMonitor.HddLoad != 0.0f)
                    {
                        var averageHddLoad = 0.0f;

                        if (checkAllHdds)
                        {
                            averageHddLoad = this.hddLoad.GetAverageHddLoadAllVolumesInBytesPerSecond(); // Byte/s
                        }
                        else
                        {
                            averageHddLoad = this.hddLoad.GetAverageHddLoadInBytesPerSecond(hddToMonitor.Drive); // Byte/s
                        }
                        var result = averageHddLoad > WsapmConvert.ConvertKBToByte(hddToMonitor.HddLoad);        // Settings are saved as KBit/s.

                        if (result)
                        {
                            return(new CheckSuspendResult(true, String.Format(Resources.Wsapm_Core.HddLoadCheck_HddLoadReason, hddName, hddToMonitor.HddLoad, WsapmConvert.ConvertByteToKB(averageHddLoad).ToString("0"))));
                        }
                        else
                        {
                            return(new CheckSuspendResult(false, String.Empty));
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Might happen if the performance counters get broken.
                    WsapmLog.Log.WriteError(ex);
                }
            }

            return(new CheckSuspendResult(false, String.Empty));
        }
Example #18
0
 /// <summary>
 /// Checks if the standby should be suspended.
 /// </summary>
 /// <param name="settings">The WsapmSettings to use.</param>
 /// <returns>The result as CheckSuspendResult.</returns>
 public CheckSuspendResult CheckStandby(WsapmSettings settings)
 {
     return(this.CheckHddLoad(settings));
 }
Example #19
0
 /// <summary>
 /// Checks if the standby should be suspended.
 /// </summary>
 /// <param name="settings">The WsapmSettings to use.</param>
 /// <returns>The result as CheckSuspendResult.</returns>
 public CheckSuspendResult CheckStandby(WsapmSettings settings)
 {
     return(this.CheckProcesses(settings));
 }
Example #20
0
        /// <summary>
        /// Determines if the state of the check interval is valid with the current Windows power options.
        /// </summary>
        /// <param name="settings">The WsampSettings to validate.</param>
        /// <returns>True if the state of the check interval is valid with the current Windows power options, otherwise false.</returns>
        public static bool ValidateCheckInterval(WsapmSettings settings)
        {
            var winIdleTimeoutAcMinutes = WsapmTools.GetCurrentWindowsIdleTimeoutAcMinutes();

            return(!(settings.MonitoringTimerInterval >= winIdleTimeoutAcMinutes));
        }
Example #21
0
 /// <summary>
 /// Checks if the standby should be suspended.
 /// </summary>
 /// <param name="settings">The WsapmSettings to use.</param>
 /// <returns>The result as CheckSuspendResult.</returns>
 public CheckSuspendResult CheckStandby(WsapmSettings settings)
 {
     return(CheckScheduledUptime(settings));
 }
Example #22
0
 /// <summary>
 /// Checks if the standby should be suspended.
 /// </summary>
 /// <param name="settings">The WsapmSettings to use.</param>
 /// <returns>The result as CheckSuspendResult.</returns>
 public CheckSuspendResult CheckStandby(WsapmSettings settings)
 {
     return(this.CheckNetworkFileAccess(settings));
 }
Example #23
0
        /// <summary>
        /// Checks if the network load is over the network load value in the settings.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns>True, if the current network load is greater than the network load value in the settings, otherwise false.</returns>
        public CheckSuspendResult CheckNetworkLoad(WsapmSettings settings)
        {
            if (settings == null || !settings.EnableNetworkInterfacesToMonitor)
            {
                return(new CheckSuspendResult(false, String.Empty));
            }

            var availableNics = WsapmTools.GetAvailableNetworkInterfaces();

            foreach (var networkInterfaceToMonitor in settings.NetworkInterfacesToMonitor)
            {
                if (networkInterfaceToMonitor.NetworkInterface != WsapmConstants.AllNetworkInterfacesName && !availableNics.Contains(networkInterfaceToMonitor.NetworkInterface))
                {
                    // NIC not available -> don't check, just write entry in log.
                    WsapmLog.Log.WriteLine(string.Format(Wsapm.Core.Resources.Wsapm_Core.NetworkLoadCheck_NicNotAvailable, networkInterfaceToMonitor.NetworkInterface), LogMode.Normal);
                    continue;
                }

                bool   checkAllNics = false;
                string nicName      = networkInterfaceToMonitor.NetworkInterface;

                if (networkInterfaceToMonitor.NetworkInterface == WsapmConstants.AllNetworkInterfacesName)
                {
                    checkAllNics = true;
                    nicName      = WsapmTools.GetCommonDiaplayNameAllNetworkInterfaces();
                }

                // Check total network load.
                try
                {
                    if (networkInterfaceToMonitor.EnableCheckNetworkLoadTotal && networkInterfaceToMonitor.NetworkLoadTotal != 0.0f)
                    {
                        var averageNetworkLoad = 0.0f;

                        if (checkAllNics)
                        {
                            averageNetworkLoad = this.networkLoad.GetAverageNetworkLoadTotalAllNicsInBytesPerSecond(); // Byte/s
                        }
                        else
                        {
                            averageNetworkLoad = this.networkLoad.GeAverageNetworkLoadTotalInBytesPerSecond(networkInterfaceToMonitor.NetworkInterface); // Byte/s
                        }
                        var result = averageNetworkLoad > WsapmConvert.ConvertKBitToByte(networkInterfaceToMonitor.NetworkLoadTotal);                    // Settings are saved as KBit/s.

                        if (result)
                        {
                            return(new CheckSuspendResult(true, String.Format(Resources.Wsapm_Core.NetworkLoadCheck_CombinedNetworkLoadReason, nicName, networkInterfaceToMonitor.NetworkLoadTotal, WsapmConvert.ConvertByteToKBit(averageNetworkLoad).ToString("0"))));
                        }
                        else
                        {
                            return(new CheckSuspendResult(false, String.Empty));
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Might happen if the performance counters get broken.
                    WsapmLog.Log.WriteError(ex);
                }

                // Check download network load.
                try
                {
                    if (networkInterfaceToMonitor.EnableCheckNetworkLoadDownload && networkInterfaceToMonitor.NetworkLoadDownload != 0.0f)
                    {
                        var averageNetworkReceived = 0.0f;

                        if (checkAllNics)
                        {
                            averageNetworkReceived = this.networkLoad.GetAverageNetworkLoadDownloaAllNicsInBytesPerSecond(); //Byte/s
                        }
                        else
                        {
                            averageNetworkReceived = this.networkLoad.GetAverageNetworkLoadDownloadInBytesPerSecond(networkInterfaceToMonitor.NetworkInterface); //Byte/s
                        }
                        var result = averageNetworkReceived > WsapmConvert.ConvertKBitToByte(networkInterfaceToMonitor.NetworkLoadDownload);                     // Settings are saved as KBit/s.

                        if (result)
                        {
                            return(new CheckSuspendResult(true, String.Format(Resources.Wsapm_Core.NetworkLoadCheck_DownloadNetworkLoadReason, nicName, networkInterfaceToMonitor.NetworkLoadDownload, WsapmConvert.ConvertByteToKBit(averageNetworkReceived).ToString("0"))));
                        }
                        else
                        {
                            return(new CheckSuspendResult(false, String.Empty));
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Might happen if the performance counters get broken.
                    WsapmLog.Log.WriteError(ex);
                }

                // Check upload network load.
                try
                {
                    if (networkInterfaceToMonitor.EnableCheckNetworkLoadUpload && networkInterfaceToMonitor.NetworkLoadUpload != 0.0f)
                    {
                        var averageNetworkSent = 0.0f;

                        if (checkAllNics)
                        {
                            averageNetworkSent = this.networkLoad.GetAverageNetworkLoadUploadAllNicsInBytesPerSecond(); // Byte/s
                        }
                        else
                        {
                            averageNetworkSent = this.networkLoad.GetAverageNetworkLoadUploadInBytesPerSecond(networkInterfaceToMonitor.NetworkInterface); // Byte/s
                        }
                        var result = averageNetworkSent > WsapmConvert.ConvertKBitToByte(networkInterfaceToMonitor.NetworkLoadUpload);                     // Settings are saved as KBit/s.

                        if (result)
                        {
                            return(new CheckSuspendResult(true, String.Format(Resources.Wsapm_Core.NetworkLoadCheck_UploadNetworkLoadReason, nicName, networkInterfaceToMonitor.NetworkLoadUpload, WsapmConvert.ConvertByteToKBit(averageNetworkSent).ToString("0"))));
                        }
                        else
                        {
                            return(new CheckSuspendResult(false, String.Empty));
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Might happen if the performance counters get broken.
                    WsapmLog.Log.WriteError(ex);
                }
            }

            return(new CheckSuspendResult(false, String.Empty));
        }