Introduction
CZGL.SystemInfo is a resource information retrieval library that supports Windows and Linux, used to obtain system environment, machine resource information, and system resource usage.
Search for CZGL.SystemInfo
in NuGet to install it.
I have added comments to each property and method in the library, and you will be able to see them when calling.
Platform Agnostic
Under the CZGL.SystemInfo namespace, there is a static class called EnvironmentInfo used for retrieving various pieces of information.
In the CZGL.SystemInfo.Info namespace, there are three types for retrieving and recording different types of information.
MachineRunInfo
is used to obtain resource usage information regarding machine operation;
SystemPlatformInfo
is used to obtain system platform information;
SystemRunEvnInfo
is for retrieving system property information;
EnvironmentInfo.GetEnvironmentVariables()
is used to get all the environment variables of the system.
Obtaining Specific Property Information
You can use it like this:
// New instance acquisition
MachineRunInfo m = new MachineRunInfo();
Console.WriteLine("Current process used memory: " + m.ThisUsedMem);
In the above three types, there is a static instance that can also be used like this:
Console.WriteLine("Current process used memory: " + MachineRunInfo.Instance.ThisUsedMem);
The three types MachineRunInfo
, SystemPlatformInfo
, and SystemRunEvnInfo
can have their information output directly using properties.
Obtaining All Property Information
If you want to output all at once to the console or make a statistic, you can use methods in EnvironmentInfo to quickly generate the information.
If the current system is in Chinese, comments in Chinese will be output.
// Note: some resource units are in kb
// Retrieve system platform information
KeyValuePair<string, object>[] a = env.GetSystemPlatformInfoValue();
// Retrieve system operational property information
KeyValuePair<string, object>[] b = env.GetSystemRunInfoValue();
// Retrieve machine resource information
KeyValuePair<string, object>[] c = env.GetMachineInfoValue();
// Retrieve all system environment variables
KeyValuePair<string, object>[] d = env.GetEnvironmentVariables();
Example of printing output:
Console.WriteLine("\nSystem platform information:\n");
foreach (var item in a)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Console.WriteLine("\n\nSystem operational property information:\n");
foreach (var item in b)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Console.WriteLine("\n\nMachine resource information:\n");
foreach (var item in c)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Console.WriteLine("\n\nAll system environment variables:\n");
foreach (var item in d)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Output (partially displayed):
System platform information:
Runtime framework : .NET Core 3.1.0
Operating system : Microsoft Windows 10.0.17763
Operating system version : Microsoft Windows NT 6.2.9200.0
Platform architecture : X64
System operational property information:
Machine name : aaaa-PC
Current associated username : aaa
User network domain name : aaa-PC
System uptime (milliseconds) : 3227500
Web application core framework version : 3.1.0
Running in interactive mode : True
Partition disks : D:\, E:\, F:\, G:\, H:\, X:\
System directory : X:\windows\system32
Machine resource information:
Current process used physical memory : 20020
Current process CPU time consumed : 328.125
Memory used by all system processes : System.Collections.Generic.KeyValuePair`2[System.String,System.Int64][]
System used memory : 5988340
All system environment variables:
VisualStudioVersion : 16.0
CommonProgramFiles(x86) : x:\Program Files (x86)\Common Files
You can also use (string, KeyValuePair<string, object>[]) GetMachineInfo()
and so on, where the string returns the description of this type of information.
Linux
Search for CZGL.SystemInfo.Linux
in NuGet for installation.
In this library, Linux resource information includes process metrics, memory metrics, CPU metrics, virtual memory metrics, and metrics of various processes.
You can obtain information by instantiating DynamicInfo
.
There are 5 objects used to map the corresponding information.
Tasks: Used to count the total number of processes and the number of processes in different states.
CpuState: CPU usage and various load information.
Mem: Physical memory and cache usage.
Swap: Virtual memory usage.
PidInfo: Resource information for a single process.
All of them have a property IsSuccess
to determine if Linux information can be obtained successfully.
Instantiate to get the object:
DynamicInfo info = new DynamicInfo();
Direct Usage
You can obtain the corresponding object using methods.
var item = info.GetTasks();
Console.WriteLine("Total number of processes in the system: " + item.Total);
Console.WriteLine("Number of running processes: " + item.Running);
Bulk Retrieval
Below is an example of bulk retrieval, where each property and property value generates a key-value pair, allowing for bulk information retrieval.
If the current system is in Chinese, Chinese remarks will be output.
// Retrieve process statistics
KeyValuePair<string, object>[] a = info.GetRefTasks();
// Retrieve CPU resource statistics
KeyValuePair<string, object>[] b = info.GetRefCpuState();
// Retrieve memory statistics
KeyValuePair<string, object>[] c = info.GetRefMem();
// Retrieve virtual memory statistics
KeyValuePair<string, object>[] d = info.GetRefSwap();
Dictionary<int, PidInfo> dic = info.GetPidInfo();
Console.WriteLine("\nProcess statistics:\n");
foreach (var item in a)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Console.WriteLine("\n\nCPU resource statistics:\n");
foreach (var item in b)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Console.WriteLine("\n\nMemory statistics:\n");
foreach (var item in c)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Console.WriteLine("\n\nRetrieve virtual memory statistics:\n");
foreach (var item in d)
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
Console.WriteLine("\n\nResources used by each process:\n");
Console.WriteLine(" ProcessId ProcessName User Priority Nice VirtualMemory PhysicalMemory SharedMemory ProcessState CPUUsage(%) MemoryUsage(%d) ");
foreach (var item in dic)
{
Console.WriteLine($"{item.Key} {item.Value.Command} {item.Value.User} {item.Value.PR} " +
$"{item.Value.Nice} {item.Value.VIRT} {item.Value.RES} {item.Value.SHR} " +
$"{item.Value.State} {item.Value.CPU} {item.Value.Mem}");
}
Output:
Process statistics:
Total : 93
Running : 1
Sleeping : 59
Stopped : 0
Zombie : 0
CPU resource statistics:
UserSpace : 1
Sysctl : 0.6
NI : 0
Idolt : 98.3
WaitIO : 0.1
HardwareIRQ : 0
SoftwareInterrupts : 0
Memory statistics:
Total : 1009048
Used : 334040
Free : 85408
Buffers : 589600
CanUsed : 675008
Retrieve virtual memory statistics:
Total : 0
Used : 0
Free : 0
AvailMem : 505744
Windows implementation is not written for now. Off to get a haircut.
文章评论