public static void Send(string subject, string htmlString, SaveState.Values values) // Email sender incase of unusual behaviour { Output.WriteLine("Sending email notification...", Color.LightBlue); // Unsecure apps access must be enabled for the sender account: https://myaccount.google.com/u/1/lesssecureapps message.From = new MailAddress(values.mailFrom, "Auto Ping Notify"); message.To.Add(new MailAddress(values.mailTo)); message.Subject = subject; message.IsBodyHtml = true; //to make message body as html message.Body = htmlString; smtp.Port = 587; smtp.Host = values.mailSMTP; //for gmail host smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential(values.mailFrom, values.mailPassword); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; try { smtp.Send(message); // Try send email Output.WriteLine("Email notification sent!", Color.LightBlue); } catch (Exception ex) { Output.WriteLine("Email notification failed. Please ensure your mail settings are correct", Color.OrangeRed); Output.WriteLine(ex.Message, Color.OrangeRed); } }
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); }