Example #1
0
        /// <inheritdoc/>
        public override async Task ObserveAsync(CancellationToken token)
        {
            // If set, this observer will only run during the supplied interval.
            // See Settings.xml, CertificateObserverConfiguration section, RunInterval parameter for an example.
            if (this.RunInterval > TimeSpan.MinValue &&
                DateTime.Now.Subtract(this.LastRunDateTime) < this.RunInterval)
            {
                return;
            }

            this.SetErrorWarningThresholds();

            DriveInfo[] allDrives = DriveInfo.GetDrives();
            var         diskUsage = new DiskUsage();

            if (ObserverManager.ObserverWebAppDeployed)
            {
                this.diskInfo = new StringBuilder();
            }

            try
            {
                foreach (var d in allDrives)
                {
                    token.ThrowIfCancellationRequested();

                    if (!d.IsReady)
                    {
                        continue;
                    }

                    // This section only needs to run if you have the FabricObserverWebApi app installed.
                    if (ObserverManager.ObserverWebAppDeployed)
                    {
                        _ = this.diskInfo.AppendFormat("\n\nDrive Name: {0}\n", d.Name);

                        // Logging.
                        _ = this.diskInfo.AppendFormat("Drive Type: {0}\n", d.DriveType);
                        _ = this.diskInfo.AppendFormat("  Volume Label   : {0}\n", d.VolumeLabel);
                        _ = this.diskInfo.AppendFormat("  Filesystem     : {0}\n", d.DriveFormat);
                        _ = this.diskInfo.AppendFormat("  Total Disk Size: {0} GB\n", d.TotalSize / 1024 / 1024 / 1024);
                        _ = this.diskInfo.AppendFormat("  Root Directory : {0}\n", d.RootDirectory);
                        _ = this.diskInfo.AppendFormat("  Free User : {0} GB\n", d.AvailableFreeSpace / 1024 / 1024 / 1024);
                        _ = this.diskInfo.AppendFormat("  Free Total: {0} GB\n", d.TotalFreeSpace / 1024 / 1024 / 1024);
                        _ = this.diskInfo.AppendFormat("  % Used    : {0}%\n", DiskUsage.GetCurrentDiskSpaceUsedPercent(d.Name));
                    }

                    // Setup monitoring data structures.
                    string id = d.Name.Substring(0, 1);

                    // Since these live across iterations, do not duplicate them in the containing list.
                    // Disk space %.
                    if (this.diskSpacePercentageUsageData.All(data => data.Id != id))
                    {
                        this.diskSpacePercentageUsageData.Add(new FabricResourceUsageData <double>(ErrorWarningProperty.DiskSpaceUsagePercentage, id, this.DataCapacity));
                    }

                    if (this.diskSpaceUsageData.All(data => data.Id != id))
                    {
                        this.diskSpaceUsageData.Add(new FabricResourceUsageData <double>(ErrorWarningProperty.DiskSpaceUsageMb, id, this.DataCapacity));
                    }

                    if (this.diskSpaceAvailableData.All(data => data.Id != id))
                    {
                        this.diskSpaceAvailableData.Add(new FabricResourceUsageData <double>(ErrorWarningProperty.DiskSpaceAvailableMb, id, this.DataCapacity));
                    }

                    if (this.diskSpaceTotalData.All(data => data.Id != id))
                    {
                        this.diskSpaceTotalData.Add(new FabricResourceUsageData <double>(ErrorWarningProperty.DiskSpaceTotalMb, id, this.DataCapacity));
                    }

                    // Current disk queue length.
                    if (this.diskAverageQueueLengthData.All(data => data.Id != id))
                    {
                        this.diskAverageQueueLengthData.Add(new FabricResourceUsageData <float>(ErrorWarningProperty.DiskAverageQueueLength, id, this.DataCapacity));
                    }

                    // Generate data over time (monitorDuration.) for use in ReportAsync health analysis.
                    this.stopWatch?.Start();

                    TimeSpan duration = TimeSpan.FromSeconds(10);

                    if (this.MonitorDuration > TimeSpan.MinValue)
                    {
                        duration = this.MonitorDuration;
                    }

                    // Warm up the counters.
                    _ = diskUsage.GetAverageDiskQueueLength(d.Name.Substring(0, 2));

                    while (this.stopWatch?.Elapsed <= duration)
                    {
                        token.ThrowIfCancellationRequested();

                        this.diskSpacePercentageUsageData.FirstOrDefault(
                            x => x.Id == id)
                        ?.Data.Add(DiskUsage.GetCurrentDiskSpaceUsedPercent(id));

                        this.diskSpaceUsageData.FirstOrDefault(
                            x => x.Id == id)
                        ?.Data.Add(diskUsage.GetUsedDiskSpace(id, SizeUnit.Megabytes));

                        this.diskSpaceAvailableData.FirstOrDefault(
                            x => x.Id == id)
                        ?.Data.Add(diskUsage.GetAvailableDiskSpace(id, SizeUnit.Megabytes));

                        this.diskSpaceTotalData.FirstOrDefault(
                            x => x.Id == id)
                        ?.Data.Add(DiskUsage.GetTotalDiskSpace(id, SizeUnit.Megabytes));

                        this.diskAverageQueueLengthData.FirstOrDefault(
                            x => x.Id == id)
                        ?.Data.Add(diskUsage.GetAverageDiskQueueLength(d.Name.Substring(0, 2)));

                        Thread.Sleep(250);
                    }

                    // This section only needs to run if you have the FabricObserverWebApi app installed.
                    if (ObserverManager.ObserverWebAppDeployed)
                    {
                        _ = this.diskInfo.AppendFormat(
                            "{0}",
                            this.GetWindowsPerfCounterDetailsText(
                                this.diskAverageQueueLengthData.FirstOrDefault(
                                    x => x.Id == d.Name.Substring(0, 1))
                                ?.Data,
                                "Avg. Disk Queue Length"));
                    }

                    if (this.stopWatch != null)
                    {
                        this.stopWatch?.Stop();
                        this.RunDuration = this.stopWatch.Elapsed;
                        this.stopWatch.Reset();
                    }
                }
            }
            finally
            {
                diskUsage.Dispose();
            }

            await this.ReportAsync(token).ConfigureAwait(true);

            this.LastRunDateTime = DateTime.Now;
        }
Example #2
0
        public override async Task ObserveAsync(CancellationToken token)
        {
            // If set, this observer will only run during the supplied interval.
            // See Settings.xml, CertificateObserverConfiguration section, RunInterval parameter for an example.
            if (RunInterval > TimeSpan.MinValue &&
                DateTime.Now.Subtract(LastRunDateTime) < RunInterval)
            {
                return;
            }

            SetErrorWarningThresholds();

            DriveInfo[] allDrives = DriveInfo.GetDrives();

            if (ObserverManager.ObserverWebAppDeployed)
            {
                this.diskInfo = new StringBuilder();
            }

            foreach (var d in allDrives)
            {
                token.ThrowIfCancellationRequested();

                if (!DiskUsage.ShouldCheckDrive(d))
                {
                    continue;
                }

                // This section only needs to run if you have the FabricObserverWebApi app installed.
                if (ObserverManager.ObserverWebAppDeployed)
                {
                    _ = this.diskInfo.AppendFormat("\n\nDrive Name: {0}\n", d.Name);

                    // Logging.
                    _ = this.diskInfo.AppendFormat("Drive Type: {0}\n", d.DriveType);
                    _ = this.diskInfo.AppendFormat("  Volume Label   : {0}\n", d.VolumeLabel);
                    _ = this.diskInfo.AppendFormat("  Filesystem     : {0}\n", d.DriveFormat);
                    _ = this.diskInfo.AppendFormat("  Total Disk Size: {0} GB\n", d.TotalSize / 1024 / 1024 / 1024);
                    _ = this.diskInfo.AppendFormat("  Root Directory : {0}\n", d.RootDirectory);
                    _ = this.diskInfo.AppendFormat("  Free User : {0} GB\n", d.AvailableFreeSpace / 1024 / 1024 / 1024);
                    _ = this.diskInfo.AppendFormat("  Free Total: {0} GB\n", d.TotalFreeSpace / 1024 / 1024 / 1024);
                    _ = this.diskInfo.AppendFormat("  % Used    : {0}%\n", DiskUsage.GetCurrentDiskSpaceUsedPercent(d.Name));
                }

                // Setup monitoring data structures.
                string id = d.Name;

                // Since these live across iterations, do not duplicate them in the containing list.
                // Disk space %.
                if (this.DiskSpaceUsagePercentageData.All(data => data.Id != id) && (this.DiskSpacePercentErrorThreshold > 0 || this.DiskSpacePercentWarningThreshold > 0))
                {
                    this.DiskSpaceUsagePercentageData.Add(new FabricResourceUsageData <double>(ErrorWarningProperty.DiskSpaceUsagePercentage, id, DataCapacity));
                }

                // Current disk queue length.
                if (this.DiskAverageQueueLengthData.All(data => data.Id != id) && (this.AverageQueueLengthErrorThreshold > 0 || this.AverageQueueLengthWarningThreshold > 0))
                {
                    this.DiskAverageQueueLengthData.Add(new FabricResourceUsageData <float>(ErrorWarningProperty.DiskAverageQueueLength, id, DataCapacity));
                }

                // This data is just used for Telemetry today.
                if (this.DiskSpaceAvailableMbData.All(data => data.Id != id))
                {
                    this.DiskSpaceAvailableMbData.Add(new FabricResourceUsageData <double>(ErrorWarningProperty.DiskSpaceAvailableMb, id, DataCapacity));
                }

                // This data is just used for Telemetry today.
                if (this.DiskSpaceTotalMbData.All(data => data.Id != id))
                {
                    this.DiskSpaceTotalMbData.Add(new FabricResourceUsageData <double>(ErrorWarningProperty.DiskSpaceTotalMb, id, DataCapacity));
                }

                // It is important to check if code is running on Windows, since d.Name.Substring(0, 2) will fail on Linux for / (root) mount point.
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (this.AverageQueueLengthErrorThreshold > 0 || this.AverageQueueLengthWarningThreshold > 0)
                    {
                        // Warm up counter.
                        _ = DiskUsage.GetAverageDiskQueueLength(d.Name.Substring(0, 2));

                        this.DiskAverageQueueLengthData.Single(x => x.Id == id).Data.Add(DiskUsage.GetAverageDiskQueueLength(d.Name.Substring(0, 2)));
                    }
                }

                if (this.DiskSpacePercentErrorThreshold > 0 || this.DiskSpacePercentWarningThreshold > 0)
                {
                    this.DiskSpaceUsagePercentageData.Single(x => x.Id == id).Data.Add(DiskUsage.GetCurrentDiskSpaceUsedPercent(id));
                }

                this.DiskSpaceAvailableMbData.Single(x => x.Id == id).Data.Add(DiskUsage.GetAvailableDiskSpace(id, SizeUnit.Megabytes));
                this.DiskSpaceTotalMbData.Single(x => x.Id == id).Data.Add(DiskUsage.GetTotalDiskSpace(id, SizeUnit.Megabytes));

                // This section only needs to run if you have the FabricObserverWebApi app installed.
                if (ObserverManager.ObserverWebAppDeployed && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    _ = this.diskInfo.AppendFormat(
                        "{0}",
                        GetWindowsPerfCounterDetailsText(
                            this.DiskAverageQueueLengthData.FirstOrDefault(
                                x => x.Id == d.Name.Substring(0, 1))
                            ?.Data,
                            "Avg. Disk Queue Length"));
                }

                RunDuration = this.stopWatch.Elapsed;
            }

            await ReportAsync(token).ConfigureAwait(true);

            LastRunDateTime = DateTime.Now;
        }