-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
359 lines (327 loc) · 13.1 KB
/
Program.cs
File metadata and controls
359 lines (327 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections.Generic;
class Program
{
static List<float> cpuHistory = new();
static List<float> ramHistory = new();
static List<float> gpuHistory = new();
const int GRAPH_WIDTH = 25;
const int GRAPH_HEIGHT = 4;
static DateTime startTime = DateTime.Now;
// --- Windows counters
static PerformanceCounter? cpuCounter;
static PerformanceCounter? gpuUsageCounter;
static PerformanceCounter? netSentCounter;
static PerformanceCounter? netReceivedCounter;
static string? winNetInterface;
static string? winGpuInstance;
// Linux/Unix state for CPU/network
static ulong[]? lastCpuTimes;
static DateTime lastCpuSample = DateTime.MinValue;
static long lastNetSent = -1, lastNetRecv = -1;
static DateTime lastNetSample = DateTime.MinValue;
static void Main()
{
Console.Clear();
Console.OutputEncoding = System.Text.Encoding.UTF8;
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
winNetInterface = GetActiveNetworkInterfaceName();
if (winNetInterface != null)
{
Console.WriteLine($"Using network interface for counters: {winNetInterface}");
netSentCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", winNetInterface);
netReceivedCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", winNetInterface);
}
else
{
Console.WriteLine("No suitable network interface found for counters!");
}
winGpuInstance = GetGPU3DEngineInstance();
if (winGpuInstance != null)
{
gpuUsageCounter = new PerformanceCounter("GPU Engine", "Utilization Percentage", winGpuInstance);
}
#pragma warning restore CA1416
Thread.Sleep(1000);
}
// Prime CPU sample for non-Windows
if (!OperatingSystem.IsWindows())
GetCPUUsage();
while (true)
{
Console.SetCursorPosition(0, 0);
DisplaySystemStats();
Thread.Sleep(1000);
}
}
static void DisplaySystemStats()
{
float cpuUsage = GetCPUUsage();
float ramUsage = GetRAMUsage();
float diskUsage = GetDiskUsage();
float gpuUsage = GetGPUUsage();
gpuHistory.Add(gpuUsage);
if (gpuHistory.Count > GRAPH_WIDTH) gpuHistory.RemoveAt(0);
string uptime = (DateTime.Now - startTime).ToString(@"hh\:mm\:ss");
cpuHistory.Add(cpuUsage);
ramHistory.Add(ramUsage);
if (cpuHistory.Count > GRAPH_WIDTH) cpuHistory.RemoveAt(0);
if (ramHistory.Count > GRAPH_WIDTH) ramHistory.RemoveAt(0);
string cpuBar = GetBar(cpuUsage);
string ramBar = GetBar(ramUsage);
string diskBar = GetBar(diskUsage);
Console.WriteLine("\x1b[1;37m=== CLI SYSTEM MONITOR (Cross-platform) ===\x1b[0m\n");
Console.WriteLine($"\x1b[1;33mCPU Usage: {cpuUsage,5:F1}% {cpuBar}\x1b[0m");
Console.WriteLine($"\x1b[1;34mGPU Usage: {gpuUsage,5:F1}%\x1b[0m");
Console.WriteLine($"\x1b[1;36mRAM Usage: {ramUsage,5:F1}% {ramBar}\x1b[0m");
Console.WriteLine($"\x1b[1;35mDisk Usage: {diskUsage,5:F1}% {diskBar}\x1b[0m");
Console.WriteLine($"\x1b[1;32mNetwork Sent: {GetNetworkSent(),6:F1} KB/s\x1b[0m");
Console.WriteLine($"\x1b[1;32mNetwork Received: {GetNetworkReceived(),6:F1} KB/s\x1b[0m");
int right = 45;
Console.SetCursorPosition(right, 2);
Console.WriteLine("\x1b[1;37m╔════════════════════════════╗\x1b[0m");
Console.SetCursorPosition(right, 3);
Console.WriteLine($"\x1b[1;37m║ Host: {Environment.MachineName,-18}║\x1b[0m");
Console.SetCursorPosition(right, 4);
Console.WriteLine($"\x1b[1;37m║ OS: {RuntimeInformation.OSDescription,-16}║\x1b[0m");
Console.SetCursorPosition(right, 5);
Console.WriteLine($"\x1b[1;37m║ Uptime: {uptime,-16}║\x1b[0m");
Console.SetCursorPosition(right, 6);
Console.WriteLine("\x1b[1;37m╚════════════════════════════╝\x1b[0m");
DrawGraph(cpuHistory, "CPU Trend", 8, "\x1b[1;33m");
DrawGraph(ramHistory, "RAM Trend", 14, "\x1b[1;36m");
DrawGraph(gpuHistory, "GPU Trend", 20, "\x1b[1;34m");
}
static string GetBar(float percent)
{
int width = 10;
int filled = (int)(width * percent / 100);
return "[" + new string('█', filled) + new string('-', width - filled) + "]";
}
static float GetCPUUsage()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
if (cpuCounter == null) return 0f;
cpuCounter.NextValue();
Thread.Sleep(200);
return cpuCounter.NextValue();
#pragma warning restore CA1416
}
// non-Windows
try
{
var stat = File.ReadLines("/proc/stat").FirstOrDefault();
if (stat == null) return 0f;
var vals = stat.Split(' ', StringSplitOptions.RemoveEmptyEntries).Skip(1).Select(ulong.Parse).ToArray();
if (lastCpuTimes == null)
{
lastCpuTimes = vals;
lastCpuSample = DateTime.Now;
Thread.Sleep(200);
return GetCPUUsage();
}
else
{
ulong idle1 = lastCpuTimes[3] + (lastCpuTimes.Length > 4 ? lastCpuTimes[4] : 0);
ulong idle2 = vals[3] + (vals.Length > 4 ? vals[4] : 0);
double total1 = lastCpuTimes.Select(x => (double)x).Sum();
double total2 = vals.Select(x => (double)x).Sum();
float idleDelta = (float)(idle2 - idle1);
float totalDelta = (float)(total2 - total1);
lastCpuTimes = vals;
lastCpuSample = DateTime.Now;
if (totalDelta <= 0) return 0f;
return 100f * (1.0f - idleDelta / totalDelta);
}
}
catch { return 0f; }
}
static float GetRAMUsage()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
try
{
var wmi = new System.Management.ManagementObjectSearcher("SELECT TotalVisibleMemorySize, FreePhysicalMemory FROM Win32_OperatingSystem");
foreach (System.Management.ManagementObject obj in wmi.Get())
{
ulong total = (ulong)obj["TotalVisibleMemorySize"] * 1024;
ulong free = (ulong)obj["FreePhysicalMemory"] * 1024;
return 100.0f * (total - free) / total;
}
}
catch { }
#pragma warning restore CA1416
return 0f;
}
else
{
try
{
var lines = File.ReadAllLines("/proc/meminfo");
ulong total = ulong.Parse(lines.First(l => l.StartsWith("MemTotal")).Split(' ', StringSplitOptions.RemoveEmptyEntries)[1]);
ulong available = ulong.Parse(lines.First(l => l.StartsWith("MemAvailable")).Split(' ', StringSplitOptions.RemoveEmptyEntries)[1]);
return 100.0f * (total - available) / total;
}
catch { return 0f; }
}
}
static float GetDiskUsage()
{
try
{
DriveInfo? drive = null;
if (OperatingSystem.IsWindows())
drive = DriveInfo.GetDrives().FirstOrDefault(d => d.IsReady && d.Name == @"C:\");
else
drive = DriveInfo.GetDrives().FirstOrDefault(d => d.IsReady && d.Name == "/");
if (drive != null)
{
double used = drive.TotalSize - drive.TotalFreeSpace;
return (float)(100.0 * used / drive.TotalSize);
}
}
catch { }
return 0f;
}
static float GetGPUUsage()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
try
{
if (gpuUsageCounter != null)
{
gpuUsageCounter.NextValue();
Thread.Sleep(100);
return gpuUsageCounter.NextValue();
}
}
catch { }
#pragma warning restore CA1416
return 0f;
}
return 0f;
}
static float GetNetworkSent()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
try { return netSentCounter?.NextValue() / 1024 ?? 0f; } catch { return 0f; }
#pragma warning restore CA1416
}
else
{
return GetNetDelta("tx_bytes") / 1024f;
}
}
static float GetNetworkReceived()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
try { return netReceivedCounter?.NextValue() / 1024 ?? 0f; } catch { return 0f; }
#pragma warning restore CA1416
}
else
{
return GetNetDelta("rx_bytes") / 1024f;
}
}
static float GetNetDelta(string field)
{
string iface = NetworkInterface.GetAllNetworkInterfaces()
.FirstOrDefault(i => i.OperationalStatus == OperationalStatus.Up && i.NetworkInterfaceType != NetworkInterfaceType.Loopback)
?.Name ?? "eth0";
string path = $"/sys/class/net/{iface}/statistics/{field}";
long curr = 0;
try { curr = long.Parse(File.ReadAllText(path)); } catch { }
float rate = 0f;
DateTime now = DateTime.Now;
if (lastNetSample != DateTime.MinValue)
{
double dt = (now - lastNetSample).TotalSeconds;
if (field == "tx_bytes" && lastNetSent >= 0)
rate = (curr - lastNetSent) / (float)Math.Max(dt, 1.0);
else if (field == "rx_bytes" && lastNetRecv >= 0)
rate = (curr - lastNetRecv) / (float)Math.Max(dt, 1.0);
}
if (field == "tx_bytes") lastNetSent = curr;
else if (field == "rx_bytes") lastNetRecv = curr;
lastNetSample = now;
return rate;
}
static void DrawGraph(List<float> history, string title, int topRow, string color)
{
int right = 45;
float maxVal = history.Count > 0 && history.Max() > 0 ? history.Max() : 1f;
Console.SetCursorPosition(right, topRow);
Console.WriteLine($"{color}╔═══════ {title} ════════╗\x1b[0m");
for (int row = 1; row <= GRAPH_HEIGHT; row++)
{
Console.SetCursorPosition(right, topRow + row);
Console.Write($"{color}║ \x1b[0m");
for (int i = 0; i < history.Count; i++)
{
float val = history[i];
float heightPerRow = maxVal / GRAPH_HEIGHT;
Console.Write(val >= heightPerRow * (GRAPH_HEIGHT - row + 1) ? $"{color}█\x1b[0m" : "░");
}
Console.Write(new string(' ', GRAPH_WIDTH - history.Count));
Console.Write($"{color} ║\x1b[0m");
}
Console.SetCursorPosition(right, topRow + GRAPH_HEIGHT + 1);
Console.WriteLine($"{color}╚══════════════════════════╝\x1b[0m");
}
// Windows only helpers
static string? GetActiveNetworkInterfaceName()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
var category = new PerformanceCounterCategory("Network Interface");
var instances = category.GetInstanceNames();
Console.WriteLine("Available network interfaces for PerformanceCounter:");
foreach (var name in instances)
Console.WriteLine($" - {name}");
var interfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(i => i.OperationalStatus == OperationalStatus.Up &&
i.NetworkInterfaceType != NetworkInterfaceType.Loopback);
foreach (var ni in interfaces)
{
var match = instances.FirstOrDefault(inst => inst == ni.Name);
if (match != null)
return match;
}
return instances.FirstOrDefault(i => !i.ToLowerInvariant().Contains("loopback"));
#pragma warning restore CA1416
}
return null;
}
static string? GetGPU3DEngineInstance()
{
if (OperatingSystem.IsWindows())
{
#pragma warning disable CA1416
var category = new PerformanceCounterCategory("GPU Engine");
var instances = category.GetInstanceNames();
#pragma warning restore CA1416
return instances.FirstOrDefault(inst => inst.ToLower().Contains("engtype_3d"));
}
return null;
}
}