public MonitorHealthCheck(HealthCheckSetting setting, HealthMonitorResult result, HttpClient client, ILogger logger)
 {
     _setting = setting;
     _result  = result;
     _client  = client;
     _logger  = logger;
 }
예제 #2
0
        static void Main(string[] args)
        {
            Log.Debug("Start Application");
            if (HealthChecksSection == null || HealthChecksSection.HealthChecks == null || HealthChecksSection.HealthChecks.Count == 0)
            {
                Log.Error(SystemConstants.MISSING_HEALTH_CHECK_SECTION);
                return;
            }
            if (args.Length == 0)
            {
                Console.WriteLine("Enter one of HealthChecksSection Names:");
                foreach (object key in HealthChecksSection.HealthChecks)
                {
                    var healthCheck = key as HealthCheck;
                    Console.WriteLine($"\"{healthCheck.Name}\"");
                }
            }
            else if (args.Length > 1)
            {
                Console.WriteLine($"Expected only 1 argument but found {args.Length}");
            }
            else
            {
                string healthCheckName = args[0];
                Log.DebugFormat($"Run The Health Check: \"{args[0]}\"");
                try
                {
                    HealthChecksSection       config = HealthChecksSection.GetConfig();
                    IEnumerable <BoundsLimit> bounds = config.HealthChecks[healthCheckName].HealthCheckParameters.ReturnAll()
                                                       .Select(boundsLimit =>
                                                               new BoundsLimit(boundsLimit.Name, boundsLimit.Type, boundsLimit.Value));
                    IKernel kernel             = new BindKernelwithHealthChecks(new StandardKernel(new ServiceModule()), HealthChecksSection.HealthChecks).Bind();
                    ApplicationHealthCheck app = kernel.Get <ApplicationHealthCheck>(healthCheckName);
                    if (app == null)
                    {
                        throw new ArgumentException($"{healthCheckName} is not a valid Health Check Name");
                    }
                    HealthMonitorResult healthMonitorResult = app.DoHealthCheck(bounds);

                    Log.DebugFormat(healthMonitorResult.ToString());

                    if (healthMonitorResult.IsSerious)
                    {
                        //SendNotification(kernel, healthMonitorResult, config.HealthChecks[healthCheckName]);
                        Console.WriteLine(healthMonitorResult);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
                finally
                {
                    Log.DebugFormat($"Finished health check: {healthCheckName}");
                }
            }
        }
예제 #3
0
        public async Task ReturnHealthyCodeOnly()
        {
            var client = MockHttpClientFactory.Client(HttpStatusCode.OK);
            var result = new HealthMonitorResult();

            var healthCheck = new MonitorHealthCheck(new HealthCheckSetting {
                Uri = "test"
            }, result, client, _logger);
            await healthCheck.Check(default);
        public HealthMonitorResult StartServices(ServiceController[] services)
        {
            HealthMonitorResult result = new HealthMonitorResult(Name, HealthType, ResultStatus.Information);

            result.MessageBuilder.AppendNewLine($"Start windows services count: {services.Length}");
            foreach (ServiceController service in services)
            {
                try
                {
                    result.MessageBuilder.AppendNewLine($"Starting the windows service: {service.DisplayName}");
                    service.Start();
                    //give a few seconds
                    service.WaitForStatus(ServiceControllerStatus.Running, ServiceTimeout);
                    result.MessageBuilder.AppendNewLine($"Windows service: {service.DisplayName} - has been started");
                }
                catch (Exception e)
                {
                    result.Status = ResultStatus.Error;
                    result.MessageBuilder.Append(e.ToLogString());
                    return(result);
                }
            }
            return(result);
        }
        public HealthMonitorResult RestartServices(ServiceController[] services)
        {
            HealthMonitorResult resultToStop = StopServices(services);

            resultToStop.MessageBuilder.Insert(0, Environment.NewLine);
            resultToStop.MessageBuilder.Insert(0, "The Result of Stopping Services:");
            if (resultToStop.IsSerious)
            {
                return(resultToStop);
            }
            HealthMonitorResult resultToStart = StartServices(services);

            resultToStart.MessageBuilder.Insert(0, Environment.NewLine);
            resultToStart.MessageBuilder.Insert(0, "The Result of Starting Services:");
            if (resultToStart.IsSerious)
            {
                return(resultToStart);
            }
            HealthMonitorResult result = new HealthMonitorResult(Name, HealthType, ResultStatus.Warning, resultToStop.ToString());

            result.MessageBuilder.Append(Environment.NewLine);
            result.MessageBuilder.Append(resultToStart.ToString());
            return(result);
        }
        /// <summary>
        /// Iterate through all prescribed directories and look to optimeze by either deleting or
        /// archiveing/zipping files
        /// </summary>
        /// <param name="healthCheckParameters">parameters that include size of file before archiving and directories to work on</param>
        /// <returns> object</returns>
        public override HealthMonitorResult DoHealthCheck(IEnumerable <BoundsLimit> healthCheckParameters)
        {
            const string maxDays = "maxDays";
            const string maxSize = "maxSize";

            HealthMonitorResult result = new HealthMonitorResult(Name, HealthType, ResultStatus.Information);

            string[]      spaceOptimizeCheckParams = new[] { maxDays, maxSize };
            BoundsLimit[] checkParameters          = healthCheckParameters as BoundsLimit[] ?? healthCheckParameters.ToArray();
            EnsureAllParametersArePresent(spaceOptimizeCheckParams, checkParameters);
            int  daysOld  = int.Parse(checkParameters.Single(x => x.Name.Equals(maxDays)).Value);
            long fileSize = long.Parse(checkParameters.Single(x => x.Name.Equals(maxSize)).Value);

            spaceOptimizationService.MaxDays = daysOld;
            spaceOptimizationService.MaxSize = fileSize;
            BoundsLimit boundsLimit = checkParameters.FirstOrDefault(x => x.Name.ToLower().Equals(ServiceTimeoutMillisecondsParameter.ToLower()));

            if (boundsLimit != null)
            {
                int serviceTimeoutMilliseconds = int.Parse(boundsLimit.Value);
                ServiceTimeout = TimeSpan.FromMilliseconds(serviceTimeoutMilliseconds);
            }
            //the list of folder(s) to work on are of type 'folder'
            List <string> directories = checkParameters.Where(x => x.Type.Equals("folder"))
                                        .Select(y => y.Value)
                                        .ToList();

            ServiceController[] services = checkParameters.Where(x => x.Type.Equals("service"))
                                           .Select(y => new ServiceController(y.Value))
                                           .ToArray();
            HealthMonitorResult resultToStop = StopServices(services);

            result.MessageBuilder.AppendNewLine(resultToStop.MessageBuilder.ToString());
            List <SpaceOptimizationSummary> optimizations = new List <SpaceOptimizationSummary>();

            //iterete through directories
            foreach (string path in directories)
            {
                try
                {
                    DirectoryInfo directory = new DirectoryInfo(path);
                    archiveService.ArchiveFileName = Path.Combine(path, ZIP_FILE_NAME);
                    SpaceOptimizationSummary summary = new SpaceOptimizationSummary(DateTime.Now,
                                                                                    new List <FileOptimized>(),
                                                                                    path,
                                                                                    directory.GetCurrentSizeExceptFiles(".zip"));
                    result.MessageBuilder.AppendNewLine(DeleteOldArchivedFiles(summary, daysOld));
                    result.MessageBuilder.AppendNewLine(DeleteOldArchivedFiles(summary, path));
                    summary.CurrentSize = directory.GetCurrentSizeExceptFiles(".zip");
                    result.MessageBuilder.AppendNewLine(summary.ToString());
                    optimizations.Add(summary);
                }
                catch (Exception e)
                {
                    result.MessageBuilder.AppendNewLine(e.ToLogString());
                    result.Status = ResultStatus.Warning;
                }
            }
            HealthMonitorResult resultToStart = StartServices(services);

            result.MessageBuilder.AppendNewLine(resultToStart.MessageBuilder.ToString());
            string spaceSavedMessage = string.Format(SystemConstants.TOTAL_FILE_SIZE_MSG,
                                                     FileSizeUtils.FileSizeDescription(optimizations.Sum(x => x.SpaceSaved)));

            result.MessageBuilder.AppendNewLine(spaceSavedMessage);
            return(result);
        }
예제 #7
0
 public RollingFileDateLog()
 {
     Name       = "Delete RollingFile Date Log";
     HealthType = HealthType.WindowsService;
     Result     = new HealthMonitorResult(Name, HealthType, ResultStatus.Information);
 }