示例#1
0
        private void GetISMHealthData()
        {
            if (TheCommonUtils.cdeIsLocked(GetHealthLock) || !AreCounterInit)
            {
                return;
            }
            lock (GetHealthLock)
            {
                MyHealthData.StationWatts     = LastStationWatts;
                MyHealthData.QSenders         = TheCDEKPIs.QSenders;
                MyHealthData.QSReceivedTSM    = TheCDEKPIs.QSReceivedTSM;
                MyHealthData.QSSendErrors     = TheCDEKPIs.QSSendErrors;
                MyHealthData.QSInserted       = TheCDEKPIs.QSInserted;
                MyHealthData.QSQueued         = TheCDEKPIs.QSQueued;
                MyHealthData.QSRejected       = TheCDEKPIs.QSRejected;
                MyHealthData.QSSent           = TheCDEKPIs.QSSent;
                MyHealthData.QKBSent          = Math.Floor(TheCDEKPIs.QKBSent > 0 ? TheCDEKPIs.QKBSent / 1024 : 0);
                MyHealthData.QKBReceived      = Math.Floor(TheCDEKPIs.QKBReceived > 0 ? TheCDEKPIs.QKBReceived / 1024 : 0);
                MyHealthData.QSLocalProcessed = TheCDEKPIs.QSLocalProcessed;

                MyHealthData.CCTSMsRelayed      = TheCDEKPIs.CCTSMsRelayed;
                MyHealthData.CCTSMsReceived     = TheCDEKPIs.CCTSMsReceived;
                MyHealthData.CCTSMsEvaluated    = TheCDEKPIs.CCTSMsEvaluated;
                MyHealthData.EventTimeouts      = TheCDEKPIs.EventTimeouts;
                MyHealthData.TotalEventTimeouts = TheCDEKPIs.TotalEventTimeouts;
                MyHealthData.KPI1        = TheCDEKPIs.KPI1;
                MyHealthData.KPI2        = TheCDEKPIs.KPI2;
                MyHealthData.KPI3        = TheCDEKPIs.KPI3;
                MyHealthData.KPI4        = TheCDEKPIs.KPI4;
                MyHealthData.KPI5        = TheCDEKPIs.KPI5;
                MyHealthData.KPI6        = TheCDEKPIs.KPI6;
                MyHealthData.KPI7        = TheCDEKPIs.KPI7;
                MyHealthData.KPI8        = TheCDEKPIs.KPI8;
                MyHealthData.KPI9        = TheCDEKPIs.KPI9;
                MyHealthData.KPI10       = TheCDEKPIs.KPI10;
                MyHealthData.HTCallbacks = TheCDEKPIs.HTCallbacks;
                TheCDEKPIs.Reset();

                MyHealthData.HostAddress  = System.Environment.MachineName;
                MyHealthData.FriendlyName = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false);
                MyHealthData.HostVersion  = TheBaseAssets.MyServiceHostInfo.Version;
                MyHealthData.LastUpdate   = DateTimeOffset.Now;
                DiskInfo di = GetDiskInfo();
                MyHealthData.DiskSpaceFree = di.TotalFreeSpace;
                if (di.TotalSize > 0 && di.TotalFreeSpace <= di.TotalSize)
                {
                    MyHealthData.DiskSpaceUsage = ((di.TotalSize - di.TotalFreeSpace) / di.TotalSize) * 100;
                }
                else
                {
                    MyHealthData.DiskSpaceUsage = 0;
                }

                try
                {
                    if (wmiObjectWin32 != null)
                    {
                        var tRed         = wmiObjectWin32.Get().Cast <ManagementObject>();
                        var memoryValues = tRed.Select(mo => new
                        {
                            FreePhysicalMemory     = TheCommonUtils.CDbl(mo["FreePhysicalMemory"].ToString()),
                            TotalVisibleMemorySize = TheCommonUtils.CDbl(mo["TotalVisibleMemorySize"].ToString())
                        }).FirstOrDefault();

                        if (memoryValues != null)
                        {
                            MyHealthData.RAMAvailable = memoryValues.FreePhysicalMemory;
                            //var percent = ((memoryValues.TotalVisibleMemorySize - memoryValues.FreePhysicalMemory) / memoryValues.TotalVisibleMemorySize) * 100;
                        }
                    }
                }
                catch (Exception e)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(8007, new TSM("jcHealth", "wmiObject-Warning (Not Supported on XP)", eMsgLevel.l2_Warning, e.ToString()));
                }

                try
                {
                    MeasureProcessCPU();
                    MyHealthData.cdeLoad = cpuUsageSinceLastMeasure; // Can also use cpuUsageSinceStart, depending on purpose of metric
                }
                catch (Exception e)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(8007, new TSM("jcHealth", "Could not get CPU usage for process", eMsgLevel.l2_Warning, e.ToString()));
                }

                try
                {
                    if (MyCPULoadCounter != null)
                    {
                        MyHealthData.CPULoad = TheCommonUtils.CDbl(MyCPULoadCounter.NextValue()).cdeTruncate(2);
                    }
                }
                catch (Exception e)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(8007, new TSM("jcHealth", "PerfCounter-Warning (Not Supported on XP)", eMsgLevel.l2_Warning, e.ToString()));
                    MyCPULoadCounter = new PerformanceCounter
                    {
                        CategoryName = "Processor Information",
                        CounterName  = "% Processor Time",
                        InstanceName = "_Total"
                    };
                }
                try
                {
                    //if (MyHealthData != null && MyCDELoadCounter != null)
                    //    MyHealthData.cdeLoad = MyCDELoadCounter.NextValue().cdeTruncate(2);
                }
                catch (Exception e)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(8007, new TSM("jcHealth", "PerfCounter-Warning (Not Supported on XP)", eMsgLevel.l2_Warning, e.ToString()));
                    MyCDELoadCounter = new PerformanceCounter
                    {
                        CategoryName = "Process",
                        CounterName  = "% Processor Time",
                        InstanceName = Process.GetCurrentProcess().ProcessName
                    };
                }

                if (UseNicState)
                {
                    UpdateNetworkInterface();
                }
                else
                {
                    if (MyCounter?.Count > 0)
                    {
                        foreach (var t in MyCounter)
                        {
                            try
                            {
                                MyHealthData.SetProperty(t.PropertyName, t.PerfCounter.NextValue());
                            }
                            catch (Exception)
                            {
                                TheBaseAssets.MySYSLOG.WriteToLog(8007, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("jcHealth", $"PerfCounter: {t.PropertyName} could not be read", eMsgLevel.l2_Warning));
                            }
                        }
                    }
                }

                try
                {
                    MyHealthData.cdeCTIM    = DateTimeOffset.Now;
                    MyHealthData.LastUpdate = DateTimeOffset.Now;
                    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select CreationDate from Win32_Process where Name='System'"))
                    {
                        foreach (ManagementObject share in searcher.Get())
                        {
                            foreach (PropertyData pData in share.Properties)
                            {
                                if (pData.Name.Equals("CreationDate"))
                                {
                                    DateTime dc = ManagementDateTimeConverter.ToDateTime(TheCommonUtils.CStr(pData.Value));
                                    //dc = dc.AddTicks(-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Ticks).ToLocalTime();
                                    TimeSpan ts = DateTime.Now.Subtract(dc);
                                    MyHealthData.PCUptime = ts.TotalMinutes;
                                }
                            }
                        }
                    }


                    /// cde Process Specific Information
                    int nProcessID = Process.GetCurrentProcess().Id;
                    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select CreationDate,WorkingSetSize,HandleCount,ThreadCount from Win32_Process where ProcessID='" + nProcessID + "'"))
                    {
                        foreach (ManagementObject share in searcher.Get())
                        {
                            foreach (PropertyData PC in share.Properties)
                            {
                                string objName  = PC.Name;
                                string objValue = "";
                                if (PC.Value != null && PC.Value.ToString() != "")
                                {
                                    switch (PC.Value.GetType().ToString())
                                    {
                                    case "System.String[]":
                                        string[] str = (string[])PC.Value;
                                        foreach (string st in str)
                                        {
                                            objValue += st + " ";
                                        }
                                        break;

                                    case "System.UInt16[]":
                                        ushort[] shortData = (ushort[])PC.Value;
                                        foreach (ushort st in shortData)
                                        {
                                            objValue += st.ToString() + " ";
                                        }
                                        break;

                                    default:
                                        objValue = PC.Value.ToString();
                                        break;
                                    }
                                }
                                switch (objName)
                                {
                                case "CreationDate":
                                    DateTime dc = ManagementDateTimeConverter.ToDateTime(PC.Value.ToString());
                                    //dc=dc.AddTicks(-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Ticks).ToLocalTime();
                                    TimeSpan ts = DateTime.Now.Subtract(dc);
                                    MyHealthData.cdeUptime = ts.TotalMinutes;
                                    break;

                                case "WorkingSetSize":
                                    MyHealthData.cdeWorkingSetSize = TheCommonUtils.CLng(objValue) / 1024;
                                    break;

                                case "HandleCount":
                                    MyHealthData.cdeHandles = TheCommonUtils.CInt(objValue);
                                    break;

                                case "ThreadCount":
                                    MyHealthData.cdeThreadCount = TheCommonUtils.CInt(objValue);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    //if (!WasRunBefore)
                    UpdateDiagInfo(MyHealthData);
                }
                catch (Exception e)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(8008, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM("jcHealth", "Ring0-Warning", eMsgLevel.l2_Warning, e.ToString()));
                }
            }
        }
示例#2
0
        void OnThingUpdate(ICDEThing thing, object param)
        {
            try
            {
                if (IsDisabled)
                {
                    StopVerifier();
                    return;
                }
                {
                    var property = param as cdeP;

                    if (property == null)
                    {
                        return;
                    }

                    string genName  = property.Name;
                    var    indexGen = genName.IndexOf("Gen_");
                    if (indexGen >= 0)
                    {
                        genName = property.Name.Substring(indexGen + "Gen_".Length);
                    }

                    switch (genName)
                    {
                    case "Stats_PropertyCounter":
                        long generatorPropertyCounter = TheCommonUtils.CLng(property.Value);
                        if (generatorPropertyCounter + this.Config_NumberOfActiveProperties < lastGeneratorPropertyCounter)
                        {
                            this.Stats_PropertyCounter = generatorPropertyCounter;
                            thingStats = new ConcurrentDictionary <string, Stats>();
                            TheBaseAssets.MySYSLOG.WriteToLog(700, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Data Verifier", "Verifier", eMsgLevel.l6_Debug, "Resetting property counter"));
                        }
                        lastGeneratorPropertyCounter = generatorPropertyCounter;
                        break;

                    case "Config_NumberOfActiveProperties":
                    case "Stats_PropertiesPerSecond":
                    case "Config_PropertyUpdateInterval":
                    case "Stats_UpdateTime":
                    case "Config_StatsUpdateInterval":
                        break;

                    case "Value":
                        MyBaseThing.SetProperty("Value", property.Value);
                        System.Threading.Interlocked.Increment(ref count);
                        break;

                    default:
                        if (!genName.StartsWith("Prop"))
                        {
                            TheBaseAssets.MySYSLOG.WriteToLog(700, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Data Verifier", "Verifier", eMsgLevel.l6_Debug, String.Format("Unexpected property: {0}", property.Name)));
                        }
                        System.Threading.Interlocked.Increment(ref count);
                        break;
                    }

                    thingStats.AddOrUpdate(property.Name, new Stats {
                        count = 1
                    },
                                           (name, currentStats) =>
                    {
                        currentStats.count++;
                        return(currentStats);
                    }
                                           );

                    var lastUpdate = property.cdeCTIM;

                    var now     = DateTime.UtcNow;
                    var latency = (now.Ticks - lastUpdate.Ticks);
                    if (latency >= 0)
                    {
                        if (latency > _maxLatency)
                        {
                            _maxLatency = latency;
                        }
                        if (latency < _minLatency)
                        {
                            _minLatency = latency;
                        }
                    }
                    else
                    {
                        TheBaseAssets.MySYSLOG.WriteToLog(700, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Data Verifier", "Verifier", eMsgLevel.l6_Debug, "Negative latency!!??"));
                    }
                }
                if (g_sw.ElapsedMilliseconds > 5000)
                {
                    long elapsed  = 0;
                    long newCount = -1;
                    lock (g_sw)
                    {
                        elapsed = g_sw.ElapsedMilliseconds;
                        if (elapsed > 5000)
                        {
                            newCount = System.Threading.Interlocked.Exchange(ref count, 0);
                            g_sw.Restart();
                        }
                    }


                    if (elapsed > 5000)
                    {
                        this.Stats_PropertyCounter    += newCount;
                        this.Stats_PropertiesPerSecond = newCount / (elapsed / 1000.0);
                        // this.MinLatency = _minLatency / 1000000;
                        // this.MaxLatency = _maxLatency / 1000000;
                        this.MinLatency = _minLatency / 100000000;
                        this.MaxLatency = _maxLatency / 100000000;
                        _maxLatency     = 0;
                        _minLatency     = long.MaxValue;
                        TheBaseAssets.MySYSLOG.WriteToLog(700, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM("Data Verifier", "Verifier", eMsgLevel.l6_Debug, String.Format("Verify Rate: {0,11:N2} properties/s", this.Stats_PropertiesPerSecond)));
                    }
                }
            }
            catch (Exception)
            {
            }
        }