public static double GetPercentFreeMemory()
        {
            Int64 phav = PerformanceInfo.GetPhysicalAvailableMemoryInMiB();
            Int64 tot  = PerformanceInfo.GetTotalMemoryInMiB();

            return((double)(((decimal)phav / (decimal)tot) * 100));
        }
        public string GetResponseForMode(Mode myMode)
        {
            if (readAgentStatusFromConfig)
            {
                if (DateTime.Compare(lastExecutionTime.AddSeconds(readAgentStatusFromConfigInterval), DateTime.Now) <= 0 || DateTime.Compare(lastExecutionTime, DateTime.MinValue) == 0)
                {
                    ReadConfiguration();
                    lastExecutionTime = DateTime.Now;
                    if (agentStatus.ToLower().Equals("normal"))
                    {
                        myMode = Mode.Normal;
                    }
                    if (agentStatus.ToLower().Equals("down"))
                    {
                        myMode = Mode.Halt;
                    }
                    if (agentStatus.ToLower().Equals("drain"))
                    {
                        myMode = Mode.Drain;
                    }
                }
            }

            string Response = "";

            switch (myMode)
            {
            case Mode.Normal:
                double cpuLoad     = GetCPULoad();
                double cpuFree     = 100 - cpuLoad;
                double ramOccupied = PerformanceInfo.GetPercentOccupiedMemory();
                double ramFree     = 100 - ramOccupied;

                int divider = 0;

                double utilization = 0.0;

                // If any resource is important and utilized 100% then everything else is not important
                if ((cpuLoad > cpuThresholdValue && cpuThresholdValue > 0) || (ramOccupied > ramThresholdValue && ramThresholdValue > 0))
                {
                    Response = "0%\n"; break;
                }

                utilization = utilization + cpuLoad * cpuImportance;
                if (cpuImportance > 0)
                {
                    divider++;
                }

                utilization = utilization + ramOccupied * ramImportance;
                if (ramImportance > 0)
                {
                    divider++;
                }

                foreach (TCPService myTCPService in myTCPServices)
                {
                    double sessionOccupied = GetSessionsUtilized(myTCPService.serviceIPAddress, myTCPService.servicePort, myTCPService.serviceMaxConnections);

                    utilization = utilization + sessionOccupied * myTCPService.serviceImportance;
                    if (myTCPService.serviceImportance > 0)
                    {
                        divider++;
                    }

                    if (sessionOccupied > 99 && myTCPService.serviceImportance == 1)
                    {
                        Response = "0%\n"; break;
                    }
                }

                utilization = utilization / divider;

                if (utilization < 0)
                {
                    utilization = 0;
                }
                if (utilization > 100)
                {
                    utilization = 100;
                }

                if (ReturnIdle)
                {
                    Response = (100 - (int)utilization).ToString() + "%\n";
                }
                else
                {
                    Response = ((int)utilization).ToString() + "%\n";
                }
                return(Response);

            case Mode.Halt:
                Response = "down\n";
                break;

            case Mode.Drain:
                Response = "drain\n";
                break;

            default:
                Response = string.Empty;
                break;
            }

            return(Response);
        }