public async Task <MonitoringResult> Handle(MonitoringCommand command)
        {
            Models.Status status;
            string        message;

            using (var conn = new SqlConnection(command.Connection))
            {
                try
                {
                    await conn.OpenAsync();

                    status  = Models.Status.Success;
                    message = "OK";
                }
                catch (SqlException ex)
                {
                    status  = Models.Status.Danger;
                    message = ex.GetBaseException().Message;
                }

                return(new MonitoringResult
                {
                    Environment = command.Environment,
                    Group = command.Group,
                    Source = command.Source,
                    Type = MonitoringType.Database,
                    Name = command.Name,
                    Status = status,
                    Message = message
                });
            }
        }
        public async Task <MonitoringResult> Handle(MonitoringCommand command)
        {
            var result = new PerformanceMonitoringResult
            {
                Environment = command.Environment,
                Group       = command.Group,
                Source      = command.Source,
                Name        = command.Name,
                Type        = MonitoringType.CPUUsage,
            };

            try
            {
                var scope = new ManagementScope($@"\\{command.Environment}\root\cimv2");
                var query = new SelectQuery("SELECT * FROM Win32_Process where Name = 'w3wp.exe'");
                int?processId;

                using (var searcher = new ManagementObjectSearcher(scope, query))
                {
                    processId = (from p in searcher.Get().Cast <ManagementObject>()
                                 let commandLine = p["CommandLine"]
                                                   where commandLine != null && _regex.IsMatch(commandLine.ToString())
                                                   select int.Parse(p["ProcessId"].ToString())).FirstOrDefault();
                }

                if (processId.HasValue)
                {
                    var processInstanceName = GetProcessInstanceName(processId.Value);
                    var cpuCounter          = new PerformanceCounter("Process", "% Processor Time", processInstanceName, true);
                    cpuCounter.NextValue();
                    await Task.Delay(1000);

                    result.Value   = cpuCounter.NextValue();
                    result.Message = $"% Processor Time: {result.Value:0.00}";
                    if (result.Value < 80)
                    {
                        result.Status = Models.Status.Success;
                    }
                    else if (result.Value < 90)
                    {
                        result.Status = Models.Status.Warning;
                    }
                    else
                    {
                        result.Status = Models.Status.Danger;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = $"Unable to monitor CPU usage for {result.Environment}\\{result.Source}. Error is '{ex.Message}'";
                result.Status  = Models.Status.Danger;
            }

            return(result);
        }
 public async Task <MonitoringResult> Handle(MonitoringCommand command)
 {
     using (var httpClient = new HttpClient())
     {
         using (var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, command.Connection)))
         {
             return(new MonitoringResult
             {
                 Environment = command.Environment,
                 Group = command.Group,
                 Source = command.Source,
                 Type = MonitoringType.Website,
                 Name = command.Name,
                 Status = response.IsSuccessStatusCode ? Models.Status.Success : Models.Status.Danger,
                 Message = response.ReasonPhrase
             });
         }
     }
 }