public ContainerPerfInfo GetContainerPerfInfo(string id = null, string name = null) { if (id != null) { List <ContainerPerfInfo> containerPerfInfos = GetContainerPerfInfoListByPS(); ContainerPerfInfo foundContainerInfo = containerPerfInfos.Find(c => c.id == id); return(foundContainerInfo); } else if (name != null) { List <ContainerPerfInfo> containerPerfInfos = GetContainerPerfInfoListByPS(); ContainerPerfInfo foundContainerInfo = containerPerfInfos.Find(c => c.id == id); return(foundContainerInfo); } else { return(null); } }
public List <ContainerPerfInfo> GetContainerPerfInfoListByPS() { List <ContainerPerfInfo> containerPerfInfos = new List <ContainerPerfInfo>(); /* run cmd example: * PS C:\windows\system32> docker stats --no-stream * CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS * 9d450203744e pensive_jennings 0.00% 504KiB / 2.924GiB 0.02% 586B / 0B 0B / 0B 1 */ Collection <PSObject> psResults = CommonUtility.RunPSCommand("docker stats --no-stream"); if (psResults.Count == 0) { return(null); } bool isFirst = true; foreach (PSObject psResult in psResults) { //if (isFirst) //{ // isFirst = false; // continue; //} foreach (PSPropertyInfo prop in psResult.Properties) { var count = prop.Name; var name = prop.Value; //In other words generate output as you desire. Console.WriteLine("name is:" + count.ToString()); Console.WriteLine("value is:" + name.ToString()); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(psResult.ToString()); string perfStr = stringBuilder.ToString(); Console.WriteLine(perfStr); string [] perfItems = perfStr.Split('\t'); foreach (string perfItem in perfItems) { Console.WriteLine("\n" + perfItem); } ContainerPerfInfo currenteContainerPerfInfo = new ContainerPerfInfo(); Console.WriteLine(Convert.ToString(psResult)); currenteContainerPerfInfo.id = Convert.ToString(psResult.Properties["CONTAINER ID"].Value); currenteContainerPerfInfo.name = Convert.ToString(psResult.Properties["NAME"].Value); currenteContainerPerfInfo.cpuPercentage = Convert.ToDouble(psResult.Properties["CPU %"].Value.ToString().Replace("%", "")); currenteContainerPerfInfo.memPercentage = Convert.ToDouble(psResult.Properties["MEM %"].Value.ToString().Replace("%", "")); string memInfo = Convert.ToString(psResult.Properties["MEM USAGE / LIMIT"].Value); string memUsed = memInfo.Split(new char[3] { ' ', '/', ' ' })[0]; string memLimit = memInfo.Split(new char[3] { ' ', '/', ' ' })[1]; /*currenteContainerPerfInfo.id = Convert.ToString(psResult.Members["CONTAINER ID"].Value); * currenteContainerPerfInfo.id = Convert.ToString(psResult.Members["CONTAINER ID"].Value); * currenteContainerPerfInfo.name = Convert.ToString(psResult.Members["NAME"].Value); * currenteContainerPerfInfo.cpuPercentage = Convert.ToDouble(psResult.Members["CPU %"].Value.ToString().Replace("%", "")); * currenteContainerPerfInfo.memPercentage = Convert.ToDouble(psResult.Members["MEM %"].Value.ToString().Replace("%", "")); * string memInfo = Convert.ToString(psResult.Members["MEM USAGE / LIMIT"].Value); * string memUsed = memInfo.Split(new char[3] { ' ', '/', ' ' })[0]; * string memLimit = memInfo.Split(new char[3] { ' ', '/', ' ' })[1];*/ // extract memUsed, KB if (memUsed.Contains("KiB")) { memUsed = memUsed.Replace("KiB", ""); currenteContainerPerfInfo.memUsage = Convert.ToDouble(memUsed); } else if (memUsed.Contains("MiB")) { memUsed = memUsed.Replace("MiB", ""); currenteContainerPerfInfo.memUsage = Convert.ToDouble(memUsed) * 1024; } else if (memUsed.Contains("GiB")) { memUsed = memUsed.Replace("GiB", ""); currenteContainerPerfInfo.memUsage = Convert.ToDouble(memUsed) * 1024 * 1024; } else { currenteContainerPerfInfo.memUsage = -1; } // extract memLimit, KB if (memLimit.Contains("KiB")) { memLimit = memLimit.Replace("KiB", ""); currenteContainerPerfInfo.memLimit = Convert.ToDouble(memLimit); } else if (memLimit.Contains("MiB")) { memLimit = memLimit.Replace("MiB", ""); currenteContainerPerfInfo.memLimit = Convert.ToDouble(memLimit) * 1024; } else if (memLimit.Contains("GiB")) { memLimit = memLimit.Replace("GiB", ""); currenteContainerPerfInfo.memLimit = Convert.ToDouble(memLimit) * 1024 * 1024; } else { currenteContainerPerfInfo.memLimit = -1; } currenteContainerPerfInfo.PIDS = Convert.ToInt32(psResult.Members["PIDS"].Value); } return(containerPerfInfos); }