-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatherer.cs
More file actions
46 lines (40 loc) · 1.78 KB
/
gatherer.cs
File metadata and controls
46 lines (40 loc) · 1.78 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
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main ( )
{
// Gather system health information (for example, memory and processor details):
var memoryUsage = GetMemoryUsage ( ) ;
var processorUsage = GetProcessorUsage ( ) ;
// Prepare the data as a JSON-like string
var systemHealthData = "{\n \"MemoryUsage\": \"" + memoryUsage + "\",\n \"ProcessorUsage\": \"" + processorUsage + "\"\n}";
// Save the data to data.json file in the current directory
var fileName = "data.json" ;
var filePath = Path.Combine( Environment.CurrentDirectory , fileName ) ;
SaveDataToJsonFile ( filePath, systemHealthData ) ;
}
static string GetMemoryUsage ( )
{
// Code to get memory usage details from the system
PerformanceCounter ramCounter = new PerformanceCounter ( "Memory" , "Available MBytes" ) ;
float availableMemoryInMB = ramCounter.NextValue ( ) ;
return availableMemoryInMB + " MB" ;
}
static string GetProcessorUsage ( )
{
// Code to get processor usage details from the system
PerformanceCounter cpuCounter = new PerformanceCounter( "Processor" , "% Processor Time" , "_Total" ) ;
cpuCounter.NextValue() ;
System.Threading.Thread.Sleep(1000) ; // Wait for a second to get a valid reading
float processorUsage = cpuCounter.NextValue() ;
return processorUsage + "%" ;
}
static void SaveDataToJsonFile (string filePath , string jsonData )
{
// Save the data to data.json file in the specified path
File.WriteAllText ( filePath , jsonData ) ;
Console.WriteLine( "System health data saved to " + filePath + "." ) ;
}
}