public async Task <CheckerSettings> SetSettingsAsync([FromBody] CheckerSettings value)
        {
            var global = await _settingsHelper.GetGlobal();

            await _settingsHelper.Set(value);

            if (global.TimeZoneId != value.Global.TimeZoneId)
            {
                _logger.LogInformation($"TimeZoneId changed from {global.TimeZoneId} to {value.Global.TimeZoneId}, updating schedules");
                await _manager.UpdateSchedules();
            }
            await _manager.UpdateMaintenanceJobs(value.Global);

            return(await GetSettingsAsync());
        }
Beispiel #2
0
        public async Task Run(Check check, CheckNotification notification, CheckResult result, CheckerSettings settings, ICheckLogger logger)
        {
            _check        = check;
            _notification = notification;
            _result       = result;
            _settings     = settings;
            _logger       = logger;

            logger.Info($"Running {GetType().Name}");
            var message = $"Check {_check.Name} completed in {_result.TimeMS}ms with result status: {_result.Status.Name.ToString()} (Type: {_result.Status.Type.Name})";

            var failed = result.Status.Type.Identifier == nameof(ResultTypeEnum.Failed);

            if (notification.Sent != null && !failed)
            {
                logger.Info("Notification sent and no longer failing, resetting");
                await SendNotification(NotificationType.Fixed, message);

                notification.Sent = null;
            }
            else if (notification.Sent == null && failed)
            {
                var sent = false;
                if (!sent)
                {
                    sent = await CheckCount(message);
                }

                if (!sent)
                {
                    sent = await CheckMinutes(message);
                }

                if (sent)
                {
                    notification.Sent = DateTime.UtcNow;
                }
            }
            else if (notification.Sent != null)
            {
                logger.Info($"Notification already sent at {notification.Sent}");
            }
        }
        public async Task <CheckerSettings> Get()
        {
            var logins = await _logins.GetAll().ToListAsync();

            var connStrings = await _connStrings.GetAll().ToListAsync();

            var environments = await _environments.GetAll().ToListAsync();

            var contacts = await _contacts.GetAll().ToListAsync();

            var checkGroups = await _checkGroups.GetAll().ToListAsync();

            var settings = new CheckerSettings
            {
                Logins       = _mapper.Map <List <LoginDTO> >(logins),
                ConnStrings  = _mapper.Map <List <ConnStringDTO> >(connStrings),
                Environments = _mapper.Map <List <EnvironmentDTO> >(environments),
                Contacts     = _mapper.Map <List <ContactDTO> >(contacts),
                CheckGroups  = _mapper.Map <List <CheckGroupDTO> >(checkGroups),
                Global       = await GetGlobal()
            };

            return(settings);
        }
Beispiel #4
0
 public async Task <CheckerSettings> SetSettingsAsync(CheckerSettings value)
 {
     return(await _client.CallAsync <CheckerSettings>(HttpMethod.Post, "settings", value));
 }
        public async Task Set(CheckerSettings settings)
        {
            var connStrings = await _connStrings.GetAll().ToListAsync();

            foreach (var connString in connStrings.Where(x => !settings.ConnStrings.Exists(y => y.ID == x.ID)))
            {
                _connStrings.Delete(connString);
            }
            _mapper.Map(settings.ConnStrings, connStrings);
            foreach (var connString in connStrings)
            {
                if (connString.ID == 0)
                {
                    _connStrings.Add(connString);
                }
            }

            var logins = await _logins.GetAll().ToListAsync();

            foreach (var login in logins.Where(x => !settings.Logins.Exists(y => y.ID == x.ID)))
            {
                _logins.Delete(login);
            }
            _mapper.Map(settings.Logins, logins);
            foreach (var login in logins)
            {
                if (login.ID == 0)
                {
                    _logins.Add(login);
                }
            }

            var environments = await _environments.GetAll().ToListAsync();

            foreach (var environment in environments.Where(x => !settings.Environments.Exists(y => y.ID == x.ID)))
            {
                _environments.Delete(environment);
            }
            _mapper.Map(settings.Environments, environments);
            foreach (var environment in environments)
            {
                if (environment.ID == 0)
                {
                    _environments.Add(environment);
                }
            }

            var contacts = await _contacts.GetAll().ToListAsync();

            foreach (var contact in contacts.Where(x => !settings.Contacts.Exists(y => y.ID == x.ID)))
            {
                _contacts.Delete(contact);
            }
            _mapper.Map(settings.Contacts, contacts);
            foreach (var contact in contacts)
            {
                if (contact.ID == 0)
                {
                    _contacts.Add(contact);
                }
            }

            var checkGroups = await _checkGroups.GetAll().ToListAsync();

            foreach (var group in checkGroups.Where(x => !settings.CheckGroups.Exists(y => y.ID == x.ID)))
            {
                _checkGroups.Delete(group);
            }
            _mapper.Map(settings.CheckGroups, checkGroups);
            foreach (var group in checkGroups)
            {
                if (group.ID == 0)
                {
                    _checkGroups.Add(group);
                }
            }

            await SetGlobal(settings.Global);

            await _logins.SaveChangesAsync();

            await _connStrings.SaveChangesAsync();

            await _environments.SaveChangesAsync();

            await _contacts.SaveChangesAsync();

            await _checkGroups.SaveChangesAsync();
        }
Beispiel #6
0
        public async Task Run(Check check, CheckSchedule schedule, ICheckLogger logger)
        {
            _check    = check;
            _schedule = schedule;
            _logger   = logger;

            var result = new CheckResult
            {
                CheckID = check.ID
            };

            try
            {
                await _helper.LoadResultStatuses();

                _settings = await _helper.GetSettings();

                if (_schedule?.SkipPublicHolidays ?? false)
                {
                    if (!Enum.TryParse <CountryCode>(_settings.Global.CountryCode, out var countryCode))
                    {
                        throw new Exception($"Please configure country code in global settings to use {nameof(schedule.SkipPublicHolidays)}");
                    }
                    if (DateSystem.IsPublicHoliday(DateTime.Now, countryCode))
                    {
                        logger.Info("Check cancelled as it is a public holiday");
                        return;
                    }
                }
                result.Status = _helper.GetResultStatus(ResultStatusEnum.Success);
                logger.Info($"Starting check using {GetType().Name}");
                result = await PerformCheck(result);
            }
            catch (SubCheckException e)
            {
                result.Status = _helper.GetResultStatus(ResultStatusEnum.SubCheckFailed);
                logger.Error("Failed to run check - sub-check failed");
                logger.Error(e.ToString());
            }
            catch (Exception e)
            {
                result.Status = _helper.GetResultStatus(ResultStatusEnum.Failed);
                logger.Error("Failed to run check");
                logger.Error(e.ToString());
            }
            Action <string> log = logger.Error;

            switch (result.Status?.Type.Identifier)
            {
            case nameof(ResultTypeEnum.Success):
                log = logger.Info;
                break;

            case nameof(ResultTypeEnum.Warning):
                log = logger.Warn;
                break;

            case nameof(ResultTypeEnum.Failed):
                log = logger.Error;
                break;
            }
            log($"Result: {result.Status?.Name ?? "Unknown"} (Type: {result.Status?.Type.Name ?? "Unknown"})");
            if (result.TimeMS > 0)
            {
                log($"TimeMS: {result.TimeMS}");
            }
            result.DTS = DateTime.UtcNow;

            try
            {
                await _helper.SaveResult(result);
            }
            catch (Exception e)
            {
                logger.Error("Failed to save check result");
                logger.Error(e.ToString());
            }

            try
            {
                await _helper.RunNotifiers(check, result, _settings, logger);
            }
            catch (Exception e)
            {
                logger.Error("Failed to run notifiers");
                logger.Error(e.ToString());
            }

            logger.Done("Check completed");
        }