/// <summary> /// 获取磁盘信息 /// </summary> /// <returns></returns> public static List <object> LogicalDisk() { var listld = new List <object>(); var cmd = "wmic logicaldisk where DriveType=3 get FreeSpace,Name,Size,VolumeName"; try { var cr = CmdTo.Execute(cmd).CrOutput.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); foreach (var item in cr) { var mr = Regex.Match(item, @"(\d+)\s+(\w:)\s+(\d+)\s+(.*)"); if (mr.Success) { listld.Add(new { Name = mr.Groups[2].ToString(), VolumeName = mr.Groups[4].ToString().Trim(), Size = long.Parse(mr.Groups[3].ToString()), FreeSpace = long.Parse(mr.Groups[1].ToString()), }); } } } catch (Exception ex) { Console.WriteLine(cmd); Console.WriteLine(ex); } return(listld); }
/// <summary> /// 获取磁盘信息 /// </summary> /// <returns></returns> public static List <object> LogicalDisk() { var listld = new List <object>(); try { var dfresult = CmdTo.Execute("df").CrOutput; var listdev = dfresult.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Where(x => x.StartsWith("/dev/")); foreach (var devitem in listdev) { var dis = devitem.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); var size = long.Parse(dis[1]) * 1024; //大于 1GB if (size > 1073741824) { listld.Add(new { Name = dis[0], Size = size, FreeSpace = long.Parse(dis[3]) * 1024 }); } } } catch (Exception ex) { Console.WriteLine("df"); Console.WriteLine(ex); } return(listld); }
/// <summary> /// 获取操作系统 /// </summary> /// <returns></returns> public static string OperatingSystem() { var cmd = "wmic os get Caption /value"; try { var cr = CmdTo.Execute(cmd).CrOutput.Split('=')[1].Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0].Trim(); return(cr); } catch (Exception ex) { Console.WriteLine(cmd); Console.WriteLine(ex); } return(null); }
/// <summary> /// 获取CPU使用率 % /// </summary> /// <returns></returns> public static decimal CPUUsage() { try { var br = CmdTo.Execute("vmstat 1 2").CrOutput; var cpuitems = br.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).LastOrDefault().Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToList(); var usi = cpuitems.Count - 5; var us = cpuitems[usi]; return(decimal.Parse(us)); } catch (Exception ex) { Console.WriteLine("vmstat 1 2"); Console.WriteLine(ex); } return(0); }
/// <summary> /// 获取CPU使用率 % /// </summary> /// <returns></returns> public static decimal CPUUsage() { var cmd = "PowerShell \"Get-Counter '\\Processor(_Total)\\% Processor Time'\""; try { var list = CmdTo.Execute(cmd).CrOutput.Trim().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); var cu = Math.Ceiling(Convert.ToDecimal(list.LastOrDefault().ToString().Trim())); return(cu); } catch (Exception ex) { Console.WriteLine(cmd); Console.WriteLine(ex); } return(0); }
/// <summary> /// 获取可用内存 B /// </summary> public static long FreePhysicalMemory() { var cmd = "wmic os get FreePhysicalMemory /value"; try { var cr = CmdTo.Execute(cmd).CrOutput.Split('=').LastOrDefault().Trim().Split('.').First(); long FreePhysicalMemory = 1024 * long.Parse(cr); return(FreePhysicalMemory); } catch (Exception ex) { Console.WriteLine(cmd); Console.WriteLine(ex); } return(0); }
/// <summary> /// 运行时长 /// </summary> /// <returns></returns> public static long RunTime() { var cmd = "wmic os get LastBootUpTime /value"; try { var cr = CmdTo.Execute(cmd).CrOutput.Split('=').LastOrDefault().Trim().Split('.').First(); cr = cr.Insert(12, ":").Insert(10, ":").Insert(8, " ").Insert(6, "-").Insert(4, "-"); _ = DateTime.TryParse(cr, out DateTime startTime); var rt = Convert.ToInt64((DateTime.Now - startTime).TotalMilliseconds); return(rt); } catch (Exception ex) { Console.WriteLine(cmd); Console.WriteLine(ex); } return(0); }
/// <summary> /// 获取 /proc/cpuinfo /// </summary> /// <param name="pkey"></param> /// <returns></returns> public static string CpuInfo(string pkey) { try { var meminfo = FileTo.ReadText("/proc/cpuinfo") + Environment.NewLine + CmdTo.Execute("lscpu").CrOutput; var pitem = meminfo.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(x => x.StartsWith(pkey, StringComparison.OrdinalIgnoreCase)); var pvalue = pitem.Split(':')[1].Trim(); return(pvalue); } catch (Exception ex) { Console.WriteLine("/proc/cpuinfo"); Console.WriteLine("lscpu"); Console.WriteLine(ex); } return(null); }