public static void StartScaning(string hostsFile, string localhostsFile) { while (running) { //Start pinging string[] hostList = File.ReadAllLines(hostsFile); if (hostList.Length > 0) { foreach (string host in hostList) { Output.WriteLine("Pinging " + host, Color.LightBlue); String message = Monitor.PingHost(host); if (message.Contains("Error")) { if (Monitor.errorCount >= 3) { //Host is offline Email.Send("APN: Host: " + host + " is offline", message, SaveState.values); } } else if (Monitor.avgReplyTime > SaveState.values.replyTimeThreshold) { //Host response is slow Email.Send("APN: Host: " + host + " ping theshold reached", host + " responded with an average response time of: " + Monitor.avgReplyTime + "ms", SaveState.values); } } } else { Output.LogAppend("hosts.txt file is empty! Please add hosts to ping. \n", Color.White); } string[] localhostList = File.ReadAllLines(localhostsFile); if (hostList.Length > 0) { foreach (string localhost in localhostList) { string message = Monitor.CPU(localhost, SaveState.values); // Measure resource usage if (message.Contains("Error")) // Send email for errors { Email.Send("APN: Local Host: " + localhost + " is offline", message, SaveState.values); } else if (Monitor.cpuUsage > SaveState.values.cpuUsageThreshold) // Send email for high CPU usage { Email.Send("APN: Local Host: " + localhost + " CPU usage is higher than theshold", message, SaveState.values); } } } // Wait to ping again Output.WriteLine("Waiting " + SaveState.values.pingInterval / 1000 + " seconds... \n", Color.LightBlue); Thread.Sleep(SaveState.values.pingInterval); } } // Ping and get CPU loop reads hosts.txt and localhosts.txt
//AutoSave values public static async void AutoSaveValues() { while (true) { while (AutoSave) { SaveConfig(); Output.LogAppend("Config has been saved. \n", Color.White); await Task.Delay(AutoSaveInterval); } await Task.Delay(AutoSaveInterval); } }
public static String CPU(string localhost, SaveState.Values values) // Method to measure resource usage { string Message = ""; string localhosts = File.ReadAllText("localhosts.txt"); if (localhosts.Length > 0) { if (localhosts.Contains(localhost)) { try { Output.WriteLine("Measuring local host CPU usage for: " + localhost, Color.LightBlue); // Initialise cpu counter cpucounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true); cpucounter.MachineName = localhost; cpucounter.NextValue(); Thread.Sleep(1000); // One second of CPU time cpuUsage = Convert.ToDouble(Math.Round(cpucounter.NextValue(), 2)); Output.WriteLine("CPU Usage: " + cpuUsage + "%", Color.LightBlue); if (cpuUsage > values.cpuUsageThreshold) { Output.WriteLine("CPU usage is higher than theshold of " + values.cpuUsageThreshold + "%", Color.OrangeRed); } } catch (Exception ex) { Message = "Error: " + ex.Message; Output.WriteLine(Message, Color.OrangeRed); } } } else { Output.LogAppend("localhosts.txt file is empty! Please add local hosts to check their resources. \n", Color.White); } return(Message); }