public HttpResponseMessage GetNotifySettings()
 {
     try
     {
         log.Debug("GetNotifySettings");
         var healthcheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();
         var sendEmailSms        = 0;
         if (!healthcheckSettings.SendEmail && healthcheckSettings.SendSms)
         {
             sendEmailSms = 1;
         }
         else if (healthcheckSettings.SendEmail && healthcheckSettings.SendSms)
         {
             sendEmailSms = 2;
         }
         log.DebugFormat("Get Notify Settings Success! sendNotify = {0} sendNotify = {1}",
                         healthcheckSettings.SendNotify, sendEmailSms);
         return(ResultHelper.GetContent(new { code = 1, sendNotify = healthcheckSettings.SendNotify, sendEmailSms = sendEmailSms }));
     }
     catch (Exception ex)
     {
         log.ErrorFormat("Error on GetNotifySettings. {0} {1}",
                         ex.ToString(), ex.InnerException != null ? ex.InnerException.Message : string.Empty);
         return(ResultHelper.Error(HealthCheckResource.GetNotifySettingsError));
     }
 }
        public HttpResponseMessage RemoveEmail(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
            {
                throw new ArgumentException("email");
            }
            try
            {
                log.DebugFormat("RemoveEmail email = {0}", email);

                var healthcheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();

                email = email.ToLower();
                if (!healthcheckSettings.Emails.Contains(email))
                {
                    log.DebugFormat("No such email address email = {0}", email);
                    return(ResultHelper.Error(HealthCheckResource.NoSuchEmailAddress));
                }
                healthcheckSettings.Emails.Remove(email);
                HealthCheckSettingsAccessor.SaveHealthCheckSettings(healthcheckSettings);

                log.DebugFormat("Remove Email Success! email = {0}", email);
                return(ResultHelper.Success(HealthCheckResource.RemoveEmailSuccess));
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error on RemoveEmail. email = {0} {1} {2}", email,
                                ex.ToString(), ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                return(ResultHelper.Error(HealthCheckResource.RemoveEmailError));
            }
        }
        public HttpResponseMessage RemovePhone(string phone)
        {
            if (string.IsNullOrWhiteSpace(phone))
            {
                throw new ArgumentException("phone");
            }
            try
            {
                log.DebugFormat("RemovePhone phone = {0}", phone);

                var healthcheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();

                phone = phone.ToLower();
                if (!healthcheckSettings.PhoneNumbers.Contains(phone))
                {
                    log.DebugFormat("No such phone number, phone = {0}", phone);
                    return(ResultHelper.Error(HealthCheckResource.NoSuchPnone));
                }
                healthcheckSettings.PhoneNumbers.Remove(phone);
                HealthCheckSettingsAccessor.SaveHealthCheckSettings(healthcheckSettings);

                log.DebugFormat("Remove Phone Success! phone = {0}", phone);
                return(ResultHelper.Success(HealthCheckResource.RemovePhoneSuccess));
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error on RemovePhone. phone = {0} {1} {2}", phone,
                                ex.ToString(), ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                return(ResultHelper.Error(HealthCheckResource.RemovePhoneError));
            }
        }
Exemple #4
0
        private static void HasAttempt(ServiceEnum serviceName)
        {
            log.DebugFormat("HasAttempt: service = {0}", serviceName);
            if (serviceRepository.ShouldRestart(serviceName))
            {
                log.DebugFormat("HasAttempt: ShouldRestart is true, service = {0}", serviceName);
                var healthCheckServiceManager = new HealthCheckServiceManager(ServiceRepository);
                healthCheckServiceManager.StopService(serviceName);
                Thread.Sleep(5);
                healthCheckServiceManager.StartService(serviceName);
                return;
            }
            if (!serviceRepository.HasAtempt(serviceName))
            {
                // Send sms and e-mail about service/site/zone
                var service = ServiceRepository.GetService(serviceName);
                log.ErrorFormat("There are some problem with serviceName = {0}, status = {1}, Message = {2}.",
                                serviceName, service.Status, service.Message);

                var healthCheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();

                if (healthCheckSettings.SendNotify)
                {
                    if (healthCheckSettings.SendEmail)
                    {
                        log.Debug("Send email notification.");
                        SendEmail(String.Format("Onlyoffice {0} problem.", serviceName),
                                  String.Format(HealthCheckResource.ServiceProblem, serviceName, service.Status, service.Message),
                                  healthCheckSettings);
                    }
                    else
                    {
                        log.Debug("Email isn't sent. Email notification disabled.");
                    }
                    if (healthCheckSettings.SendSms)
                    {
                        log.Debug("Send SMS notification.");
                        SendSms(String.Format(HealthCheckResource.ServiceProblem,
                                              serviceName, service.Status, service.Message), healthCheckSettings);
                    }
                    else
                    {
                        log.Debug("SMS isn't sent. SMS notification disabled.");
                    }
                }
                else
                {
                    log.Debug("Notification isn't sent. Notification disabled.");
                }
            }
        }
        public HttpResponseMessage GetNotifiers()
        {
            try
            {
                log.Debug("GetNotifiers");

                var healthcheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();

                return(ResultHelper.GetContent(new { code = 1, numbers = healthcheckSettings.PhoneNumbers, emails = healthcheckSettings.Emails }));
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error! {0}", ex.ToString());
                return(ResultHelper.Error(HealthCheckResource.GetNotifiersError));
            }
        }
        public HttpResponseMessage SetNotifySettings(bool sendNotify, int sendEmailSms)
        {
            try
            {
                log.DebugFormat("SetNotifySettings sendNotify = {0}, sendEmailSms = {1}", sendNotify, sendEmailSms);
                var healthcheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();
                healthcheckSettings.SendNotify = sendNotify;
                switch (sendEmailSms)
                {
                case 0:
                    healthcheckSettings.SendEmail = true;
                    healthcheckSettings.SendSms   = false;
                    break;

                case 1:
                    healthcheckSettings.SendEmail = false;
                    healthcheckSettings.SendSms   = true;
                    break;

                case 2:
                    healthcheckSettings.SendEmail = true;
                    healthcheckSettings.SendSms   = true;
                    break;

                default:
                    log.ErrorFormat("Error on SetNotifySettings. Wrong sendEmailSms = {0}", sendEmailSms);
                    return(ResultHelper.Error(HealthCheckResource.SetNotifySettingsError));
                }
                HealthCheckSettingsAccessor.SaveHealthCheckSettings(healthcheckSettings);

                log.DebugFormat("Set Notify Settings Success! sendNotify = {0} sendNotify = {1}", sendNotify, sendEmailSms);
                return(ResultHelper.Success());
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error on SetNotifySettings. {0} {1}",
                                ex.ToString(), ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                return(ResultHelper.Error(HealthCheckResource.SetNotifySettingsError));
            }
        }
Exemple #7
0
        private static int GetFakeTenant()
        {
            var fakeTenant = CoreContext.TenantManager.GetTenant(FakeTenantAlias);

            /*
             * var tenants = CoreContext.TenantManager.GetTenants();
             *
             * if (fakeTenant != null)
             * {
             * tenants = tenants.Where(t => t.TenantId != fakeTenant.TenantId).ToList();
             * }
             *
             * if (tenants.Count() <= 0 || !CoreContext.TenantManager.GetTenantQuota(tenants.First().TenantId).HealthCheck)
             * {
             * log.Debug("Service wasn't started. There is no correct license for HealthCheck.");
             * return;
             * }*/

            if (fakeTenant == null)
            {
                fakeTenant = new Tenant
                {
                    TenantAlias = FakeTenantAlias
                };
                fakeTenant = CoreContext.TenantManager.SaveTenant(fakeTenant);
            }

            if (fakeTenant != null)
            {
                var healthcheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();
                healthcheckSettings.FakeTenantId = fakeTenant.TenantId;
                HealthCheckSettingsAccessor.SaveHealthCheckSettings(healthcheckSettings);

                CoreContext.TenantManager.SetCurrentTenant(fakeTenant.TenantId);

                return(fakeTenant.TenantId);
            }

            return(0);
        }
        public HttpResponseMessage AddPhone(string phone)
        {
            if (string.IsNullOrWhiteSpace(phone))
            {
                throw new ArgumentException("phone");
            }
            try
            {
                log.DebugFormat("AddPhone phone = {0}", phone);

                var healthcheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();

                phone = phone.ToLower();
                if (IsValidPhone(phone))
                {
                    if (healthcheckSettings.PhoneNumbers.Contains(phone))
                    {
                        log.DebugFormat("PhoneNumbers already contains this phone! phone = {0}", phone);
                        return(ResultHelper.Error(HealthCheckResource.AlreadyContainsPhone));
                    }
                    healthcheckSettings.PhoneNumbers.Add(phone);
                    HealthCheckSettingsAccessor.SaveHealthCheckSettings(healthcheckSettings);

                    log.DebugFormat("Add Phone Success! phone = {0}", phone);
                    return(ResultHelper.Success(HealthCheckResource.AddPhoneSuccess));
                }

                log.DebugFormat("Wrong Phone Number! phone = {0}", phone);
                return(ResultHelper.Error(HealthCheckResource.WrongPhone));
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error on AddPhone. phone = {0} {1} {2}", phone,
                                ex.ToString(), ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                return(ResultHelper.Error(HealthCheckResource.AddPhoneError));
            }
        }
Exemple #9
0
        private static void IdleTimeout(object e)
        {
            var healthCheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();

            /*
             * var tenants = CoreContext.TenantManager.GetTenants().Where(t => t.TenantId != healthCheckSettings.FakeTenantId).ToList();
             * if (tenants.Count() <= 0 || !CoreContext.TenantManager.GetTenantQuota(tenants.First().TenantId).HealthCheck)
             * {
             *  log.Debug("Service wasn't started. There is no correct license for HealthCheck.");
             *  return;
             * }*/
            log.Debug("Begin IdleTimeout");

            CoreContext.TenantManager.SetCurrentTenant(healthCheckSettings.FakeTenantId);
            Thread.CurrentThread.CurrentCulture = CoreContext.TenantManager.GetCurrentTenant().GetCulture();

            try
            {
                var serviceName = (ServiceEnum)e;

                ServiceStatus serviceStatus;
                using (var xplatServiceController = XplatServiceController.GetXplatServiceController(serviceName))
                {
                    serviceStatus = xplatServiceController.GetServiceStatus();
                }

                switch (serviceStatus)
                {
                case ServiceStatus.StartPending:
                    // wait till started
                    break;

                case ServiceStatus.Running:
                    var result = serviceRepository.GetService(serviceName).Check(healthCheckSettings.FakeTenantId);

                    if (result != null)
                    {
                        serviceRepository.SetStates(serviceName,
                                                    (result == string.Empty
                                    ? ServiceStatus.Running
                                    : ServiceStatus.NotFound).GetStringStatus(), result);
                        //if (result != string.Empty)
                        //{
                        //    HasAttempt(serviceName);
                        //}
                        //else
                        //{
                        //    serviceRepository.DropAttempt(serviceName);
                        //}
                    }
                    break;

                case ServiceStatus.NotFound:
                    serviceRepository.SetStates(serviceName, ServiceStatus.NotFound.GetStringStatus(), ServiceStatus.NotFound.GetMessageStatus());
                    //HasAttempt(serviceName);
                    break;

                default:
                    serviceRepository.SetStates(serviceName, ServiceStatus.Stopped.GetStringStatus(), ServiceStatus.Stopped.GetMessageStatus());
                    //HasAttempt(serviceName);
                    break;
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error on IdleTimeout. {0} {1}", ex.ToString(),
                                ex.InnerException != null ? ex.InnerException.Message : string.Empty);
            }
            finally
            {
                log.Debug("End IdleTimeout");
            }
        }
Exemple #10
0
        private static void IdleTimeoutDriveSpace(object e)
        {
            try
            {
                var healthCheckSettings = HealthCheckSettingsAccessor.GetHealthCheckSettings();

                /*var tenants = CoreContext.TenantManager.GetTenants().Where(t => t.TenantId != healthCheckSettings.FakeTenantId).ToList();
                 * if (tenants.Count() <= 0 || !CoreContext.TenantManager.GetTenantQuota(tenants.First().TenantId).HealthCheck)
                 * {
                 *  log.Debug("Service wasn't started. There is no correct license for HealthCheck.");
                 *  return;
                 * }*/
                log.Debug("Begin IdleTimeoutDriveSpace");

                CoreContext.TenantManager.SetCurrentTenant(healthCheckSettings.FakeTenantId);
                Thread.CurrentThread.CurrentCulture = CoreContext.TenantManager.GetCurrentTenant().GetCulture();

                IDriveSpaceChecker spaceChecker = new DriveSpaceChecker();
                spaceChecker.GetTotalAndFreeSpace();
                var freeSpace = spaceChecker.TotalFreeSpace;
                if (freeSpace == -1)
                {
                    log.Error("Not found drive name.");
                    return;
                }
                var space = freeSpace / OneMb + " Mb";
                var driveSpaceThreashold = HealthCheckCfgSectionHandler.Instance.DriveSpaceThreashold;
                if (freeSpace >= driveSpaceThreashold)
                {
                    log.DebugFormat("It is enough free disk space, {0}.", space);
                    alreadyNotifyAboutSmallDiskSpace = false;
                    return;
                }

                // Send sms and e-mail about small free disk space
                log.WarnFormat("It isn't enough free disk space, {0}.", space);
                if (alreadyNotifyAboutSmallDiskSpace)
                {
                    return;
                }

                if (!healthCheckSettings.SendNotify || (!healthCheckSettings.SendEmail && !healthCheckSettings.SendSms))
                {
                    log.Debug("Notification isn't sent. Notifications disabled.");
                    return;
                }

                alreadyNotifyAboutSmallDiskSpace = true;

                if (healthCheckSettings.SendEmail)
                {
                    log.Debug("Send email notification");
                    SendEmail("Onlyoffice:" + HealthCheckResource.SmallDriveSpace + ": " + space,
                              HealthCheckResource.SmallDriveSpace + ": " + space, healthCheckSettings);
                }
                else
                {
                    log.Debug("Email isn't sent. Email notification disabled.");
                }

                if (healthCheckSettings.SendSms)
                {
                    log.Debug("Send SMS notification");
                    SendSms(HealthCheckResource.SmallDriveSpace + ": " + space, healthCheckSettings);
                }
                else
                {
                    log.Debug("SMS isn't sent. SMS notifications disabled.");
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error on IdleTimeoutDriveSpace. {0} {1} {2}",
                                ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : string.Empty);
            }
            finally
            {
                log.Debug("End IdleTimeoutDriveSpace");
            }
        }