系统数据监控

系统数据监控。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Arch.CFramework.CLoggingAdapter;
using Ctrip.Mobile.AppDownload.Utility;

namespace Ctrip.Mobile.AppDownload.Service
{
    public class MonitorService : RunningService
    {
        public MonitorService()
        {
            ElapsedMiliseconds = 3 * 60 * 60 * 1000;//每3小时记录一次。
            s_stopwatch.Start();
        }

        public override void LoopLogic()
        {
            string info = GetSystemInfo();
            //Dashboard is better.
            LoggingManager.Instance().Info("MoniterService", info, new Dictionary<string, string>() { { "SystemInfo", "SystemInfo" } });
        }

        Stopwatch s_stopwatch = new Stopwatch();
        private PerformanceCounter _pc;
        private static TimeSpan s_lastTotalProcessTime = TimeSpan.Zero;
        public string GetSystemInfo()
        {
            int timeout;
            int totalThreads;
            int memory;
            int workingSetExecludePrivate;
            int workerThreads;
            int completionPortThreads;
            int maxThreadPoolThreads;
            int maxIOCPThreads;

            string split = ", ";
            StringBuilder sbInfo = new StringBuilder();
            sbInfo.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + split);
            s_stopwatch.Stop();
            Process process = Process.GetCurrentProcess();

            if (_pc == null)
            {
                _pc = new PerformanceCounter("Process", "Working Set - Private", process.ProcessName);
            }

            process.Refresh();

            totalThreads = process.Threads.Count;
            memory = (int)((process.WorkingSet64) / (1024 * 1024));
            workingSetExecludePrivate = (int)(_pc.NextValue() / (1024 * 1024));

            double elapsedProcessTime = (process.TotalProcessorTime - s_lastTotalProcessTime).TotalMilliseconds;
            double elapsedSystemTime = s_stopwatch.ElapsedMilliseconds;
            int cpu = (int)(Math.Round(elapsedProcessTime * 100.0 / elapsedSystemTime / Environment.ProcessorCount));

            s_lastTotalProcessTime = process.TotalProcessorTime;
            s_stopwatch.Restart();

            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);

            ThreadPool.GetMaxThreads(out maxThreadPoolThreads, out maxIOCPThreads);

            sbInfo.AppendFormat("CPU: {0}, ", cpu < 100 ? cpu : 100);
            sbInfo.AppendFormat("worker thread: {0}, ", maxThreadPoolThreads - workerThreads);
            sbInfo.AppendFormat("cpio : {0}, ", maxIOCPThreads - completionPortThreads);
            sbInfo.AppendFormat("total Threads: {0}, ", totalThreads);

            sbInfo.AppendFormat("memory(working): {0}MB, ", memory);
            sbInfo.AppendFormat("memory(private): {0}MB, ", workingSetExecludePrivate);
            return sbInfo.ToString();
        }
    }
}
原文地址:https://www.cnblogs.com/netact/p/3889466.html