Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- using System.Threading;
- using System.Net.NetworkInformation;
- using System.Data.SqlClient;
- using System.Management;
- using log4net;
- namespace SFDC_MonitoringTool
- {
- class Checks
- {
- private static readonly ILog log = LogManager.GetLogger(typeof(Checks));
- //CPU load check
- public static ReturnType cpuCheck(String passedMachineName)
- {
- ReturnType instance = new ReturnType();
- instance.IsError = false;
- instance.MachineName = passedMachineName;
- instance.TypeOfError = "CPULoadHigh";
- var cpuload = new PerformanceCounter("Processor", "% Processor Time", "_Total", passedMachineName);
- //Calculating the avarage of 3 measures. First is always 0 so it's left out
- var first = cpuload.NextValue(); Thread.Sleep(200);
- var second = cpuload.NextValue(); Thread.Sleep(200);
- var third = cpuload.NextValue(); Thread.Sleep(200);
- var fourth = cpuload.NextValue();
- var cpuLoadValue = (second + third + fourth) / 3; //the avarage of 3 of them
- int cpuLoadInteger = (int)Math.Ceiling(cpuLoadValue);
- if (cpuLoadInteger < Config.cpuThreshhold) //for debugging its < 80
- {
- instance.IsError = false;
- instance.Value = (int)Math.Ceiling(cpuLoadValue);
- return instance;
- }
- else
- {
- instance.IsError = true;
- instance.ErrorMessage = "CPU load is " + (int)Math.Ceiling(cpuLoadValue) + "% on " + instance.MachineName;
- return instance;
- }
- }
- //MEM load check
- public static ReturnType memCheck(String passedMachineName)
- {
- ReturnType instance = new ReturnType();
- instance.IsError = false;
- instance.MachineName = passedMachineName;
- instance.TypeOfError = "MEMLoadHigh";
- var ramCounter = new PerformanceCounter("Memory", "% Committed Bytes In Use", String.Empty, passedMachineName);
- //Calculate avarage load
- var first = Convert.ToInt32(ramCounter.NextValue()); Thread.Sleep(200);
- var second = Convert.ToInt32(ramCounter.NextValue()); Thread.Sleep(200);
- var third = Convert.ToInt32(ramCounter.NextValue());
- int percentage = (first + second + third) / 3; //the avarage of 3 of them
- if (percentage < Config.memThreshhold)
- {
- instance.IsError = false;
- instance.Value = percentage;
- return instance;
- }
- else
- {
- instance.IsError = true;
- instance.ErrorMessage = "MEM load is " + percentage + "% on " + passedMachineName;
- return instance;
- }
- }
- //Ping check -> accessebility
- public static ReturnType pingCheck(String passedMachineName)
- {
- ReturnType instance = new ReturnType();
- instance.IsError = false;
- instance.MachineName = passedMachineName;
- instance.TypeOfError = "PingCheck";
- Ping ping = new Ping();
- try
- {
- ping.Send(passedMachineName, Config.pingTimeout*1000); //convert sec to millisec
- instance.IsError = false;
- return instance;
- }
- catch (Exception ex)
- {
- instance.IsError = true;
- instance.ErrorMessage = passedMachineName +" does not respond within "+ Config.pingTimeout + "sec!!! Errortrace: " + ex.Message;
- return instance;
- }
- }
- //DB check
- public static ReturnType dbCheck()
- {
- ReturnType instance = new ReturnType();
- instance.IsError = false;
- instance.TypeOfError = "DBerror";
- SqlConnection connection = new SqlConnection(Config.connectionString);
- try
- {
- connection.Open();
- SqlCommand cmd = new SqlCommand("SELECT 1",connection);
- cmd.CommandTimeout = Config.dbTimeout;
- cmd.ExecuteNonQuery();
- connection.Close();
- //Successful
- instance.IsError = false;
- return instance;
- }
- catch (Exception ex)
- {
- //ERROR
- instance.IsError = true;
- instance.ErrorMessage = Config.connectionString + " does not respond within " + Config.dbTimeout + "sec. Errortrace: " + ex.Message;
- return instance;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement