using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; namespace PCMonitor { /// /// Monitor Proses yang Sedang Berjalan /// public class ProcessMonitor : IDisposable { private readonly ApiClient apiClient; private readonly ILog logger = new FileLogger(); public ProcessMonitor(ApiClient client) { apiClient = client; } public async Task CollectAsync() { try { var processes = GetProcessInfo(); if (processes.Any()) { var payload = new { processes }; await apiClient.SendAsync("process/batch", "POST", payload); } } catch (Exception ex) { logger.Error($"Error collecting process info: {ex.Message}"); } } private List GetProcessInfo() { var processList = new List(); try { var allProcesses = Process.GetProcesses(); foreach (var process in allProcesses) { try { using (var perfCounter = new PerformanceCounter("Process", "% Processor Time", process.ProcessName, true)) { perfCounter.NextValue(); // First call often returns 0 processList.Add(new { process_name = process.ProcessName, process_id = process.Id, cpu_usage = Math.Round(perfCounter.NextValue() / Environment.ProcessorCount, 2), memory_usage = process.WorkingSet64 / 1024 / 1024, // MB status = process.Responding ? "running" : "not_responding", snapshot_at = DateTime.Now }); } } catch { } } } catch (Exception ex) { logger.Warning($"Error getting process list: {ex.Message}"); } return processList; } public void Dispose() { } } }