private void PollMemoryUtilization(WmiNode wmiNode) { var node = wmiNode.Node; const string query = @"select AvailableKBytes from Win32_PerfFormattedData_PerfOS_Memory"; using (var q = Wmi.Query(node.Name, query)) { var data = q.GetFirstResult(); if (data == null) return; var available = data.AvailableKBytes * 1024; node.MemoryUsed = node.TotalMemory - available; var utilization = new Node.MemoryUtilization { DateTime = DateTime.UtcNow, MaxMemoryUsed = node.MemoryUsed, AvgMemoryUsed = node.MemoryUsed }; wmiNode.AddMemoryUtilization(utilization); } }
private void PollCpuUtilization(WmiNode wmiNode) { var node = wmiNode.Node; const string query = @"select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor where Name = '_Total'"; using (var q = Wmi.Query(node.Name, query)) { var data = q.GetFirstResult(); if (data == null) return; node.CPULoad = (short)data.PercentProcessorTime; var cpuUtilization = new Node.CPUUtilization { DateTime = DateTime.UtcNow, MaxLoad = node.CPULoad, AvgLoad = node.CPULoad }; wmiNode.AddCpuUtilization(cpuUtilization); } }
/// <summary> /// Make list of nodes as per configuration. /// When adding, a node's ip address is resolved via Dns. /// </summary> private IEnumerable <WmiNode> InitNodeList(IList <string> names) { var nodesList = new List <WmiNode>(names.Count); var exclude = Current.Settings.Dashboard.ExcludePatternRegex; foreach (var nodeName in names) { if (exclude?.IsMatch(nodeName) ?? false) { continue; } var node = new WmiNode(nodeName) { Config = _config, DataProvider = this }; try { var hostEntry = Dns.GetHostEntry(node.Name); if (hostEntry.AddressList.Any()) { node.Ip = hostEntry.AddressList[0].ToString(); node.Status = NodeStatus.Active; } else { node.Status = NodeStatus.Unreachable; } } catch (Exception) { node.Status = NodeStatus.Unreachable; } var staticDataCache = ProviderCache( () => node.PollNodeInfoAsync(), _config.StaticDataTimeoutSeconds, memberName: node.Name + "-Static"); node.Caches.Add(staticDataCache); node.Caches.Add(ProviderCache( () => node.PollStats(), _config.DynamicDataTimeoutSeconds, memberName: node.Name + "-Dynamic")); //Force update static host data, incuding os info, volumes, interfaces. Task.WaitAll(staticDataCache.PollAsync(true)); nodesList.Add(node); } return(nodesList); }
/// <summary> /// Make list of nodes as per configuration. /// When adding, a node's ip address is resolved via Dns. /// </summary> private IEnumerable <WmiNode> InitNodeList(IList <string> names) { var nodesList = new List <WmiNode>(names.Count); foreach (var nodeName in names) { var node = new WmiNode { Config = _config, Id = nodeName.ToLower(), Name = nodeName.ToLower(), DataProvider = this, MachineType = "Windows" }; try { var hostEntry = Dns.GetHostEntry(node.Name); if (hostEntry.AddressList.Any()) { node.Ip = hostEntry.AddressList[0].ToString(); node.Status = NodeStatus.Active; } else { node.Status = NodeStatus.Unreachable; } } catch (Exception) { node.Status = NodeStatus.Unreachable; } var staticDataCache = ProviderCache( () => node.PollNodeInfo(), _config.StaticDataTimeoutSeconds, memberName: node.Name + "-Static"); node.Caches.Add(staticDataCache); node.Caches.Add(ProviderCache( () => node.PollStats(), _config.DynamicDataTimeoutSeconds, memberName: node.Name + "-Dynamic")); //Force update static host data, incuding os info, volumes, interfaces. staticDataCache.Poll(true); nodesList.Add(node); } return(nodesList); }
private Node GetStaticData(WmiNode wmiNode) { try { UpdateNodeData(wmiNode.Node); GetAllVolumes(wmiNode); GetAllInterfaces(wmiNode); } catch (COMException e) { Current.LogException(e); wmiNode.Node.Status = NodeStatus.Unreachable; } return wmiNode.Node; }
private Node GetDynamicData(WmiNode wmiNode) { try { PollCpuUtilization(wmiNode); PollMemoryUtilization(wmiNode); PollNetworkUtilization(wmiNode); } catch (COMException e) { Current.LogException(e); wmiNode.Node.Status = NodeStatus.Unreachable; } return wmiNode.Node; }
public PerfRawData(WmiNode node, ManagementObject data) { _data = data; Classname = data.ClassPath.ClassName; Identifier = (string)_data["Name"]; Timestamp = Convert.ToUInt64(_data["Timestamp_Sys100NS"]); string cacheKey = $"{Classname}.{Identifier}"; // "previous.previousData = null" to cleanup previousData from previousData. Otherwise we get a linked list which never cleans up. node.previousPerfDataCache.AddOrUpdate(cacheKey, s => _previousData = this, (s, previous) => { previous._previousData = null; _previousData = previous; return(this); }); }
public PerfRawData(WmiNode node, dynamic data) : this(node, (ManagementObject)data) { }
/// <summary> /// Make list of nodes as per configuration. /// When adding, a node's IP address is resolved via DNS. /// </summary> /// <param name="nodeNames">The names of the server nodes to monitor.</param> private IEnumerable <WmiNode> InitNodeList(IList <string> nodeNames) { var nodesList = new List <WmiNode>(nodeNames.Count); var exclude = Current.Settings.Dashboard.ExcludePatternRegex; foreach (var nodeName in nodeNames) { if (exclude?.IsMatch(nodeName) ?? false) { continue; } var node = new WmiNode(nodeName) { Config = _config, DataProvider = this }; try { var hostEntry = Dns.GetHostEntry(node.Name); if (hostEntry.AddressList.Length > 0) { node.Ip = hostEntry.AddressList[0].ToString(); node.Status = NodeStatus.Active; } else { node.Status = NodeStatus.Unreachable; } } catch (Exception) { node.Status = NodeStatus.Unreachable; } var staticDataCache = ProviderCache( () => node.PollNodeInfoAsync(), _config.StaticDataTimeoutSeconds.Seconds(), memberName: node.Name + "-Static"); node.Caches.Add(staticDataCache); var dynamicDataCache = ProviderCache( () => node.PollStats(), _config.DynamicDataTimeoutSeconds.Seconds(), memberName: node.Name + "-Dynamic"); node.Caches.Add(dynamicDataCache); nodesList.Add(node); } //// Force update static host data, incuding os info, volumes, interfaces. //{ // var caches = staticDataCaches.ToArray(); // Task.WaitAll(caches); //} //// Force first dynamic polling of utilization. //// This is needed because we use PerfRawData performance counters and we need this as a first starting point for our calculations. //{ // var caches = dynamicDataCaches.ToArray(); // Task.WaitAll(caches); //} return(nodesList); }
/// <summary> /// Make list of nodes as per configuration. /// When adding, a node's ip address is resolved via Dns. /// </summary> private IEnumerable<WmiNode> InitNodeList(IList<string> names, int startId = 1) { var nodesList = new List<WmiNode>(names.Count()); var id = startId; foreach (var nodeName in names) { var node = new Node { Id = id++, Name = nodeName.ToLower(), DataProvider = this, MachineType = "Windows", }; try { var hostEntry = Dns.GetHostEntry(node.Name); if (hostEntry.AddressList.Any()) { node.Ip = hostEntry.AddressList[0].ToString(); node.Status = NodeStatus.Active; } else { node.Status = NodeStatus.Unreachable; } } catch (Exception) { node.Status = NodeStatus.Unreachable; } var wmiNode = new WmiNode(node) { OriginalName = nodeName, Config = _config }; var staticDataCache = ProviderCache( () => GetStaticData(wmiNode), _config.StaticDataTimeoutSec, memberName: wmiNode.Node.Name + "-Static"); wmiNode.Caches.Add(staticDataCache); wmiNode.Caches.Add(ProviderCache( () => GetDynamicData(wmiNode), _config.DynamicDataTimeoutSec, memberName: wmiNode.Node.Name + "-Dynamic")); //Force update static host data, incuding os info, volumes, interfaces. staticDataCache.Poll(true); nodesList.Add(wmiNode); } return nodesList; }
private void GetAllVolumes(WmiNode node) { const string query = @"SELECT Caption, Description, FreeSpace, Name, Size, VolumeSerialNumber FROM Win32_LogicalDisk WHERE DriveType = 3"; //fixed disks using (var q = Wmi.Query(node.Node.Name, query)) { foreach (var disk in q.GetDynamicResult()) { var serial = disk.VolumeSerialNumber; var v = node.Volumes.FirstOrDefault(x => x.Caption == serial); if (v == null) { v = new Volume(); node.Volumes.Add(v); } v.Id = node.Id * 20000 + node.Volumes.Count + 1; v.Available = disk.FreeSpace; v.Caption = disk.VolumeSerialNumber; v.Description = disk.Name + " - " + disk.Description; v.DataProvider = this; v.Name = disk.Name; v.NodeId = node.Id; v.Size = disk.Size; v.Type = "Fixed Disk"; v.Status = NodeStatus.Active; v.Used = v.Size - v.Available; if (v.Size > 0) { v.PercentUsed = (float) (100*v.Used/v.Size); } } } }
private void GetAllInterfaces(WmiNode node) { const string query = @"SELECT NetConnectionID, Description, Name, MACAddress, Speed FROM Win32_NetworkAdapter WHERE NetConnectionStatus = 2"; //connected adapters. //'AND PhysicalAdapter = True' causes exceptions with old windows versions. using (var q = Wmi.Query(node.Node.Name, query)) { foreach (var data in q.GetDynamicResult()) { string name = data.Name; var i = node.Interfaces.FirstOrDefault(x => x.Name == name); if (i == null) { i = new Interface(); node.Interfaces.Add(i); } i.Alias = "!alias"; i.Caption = data.NetConnectionID; i.DataProvider = this; i.FullName = data.Description; i.IfName = data.Name; i.Id = node.Id*10000 + node.Interfaces.Count + 1; i.NodeId = node.Id; i.Index = 0; i.IsTeam = false; i.LastSync = DateTime.UtcNow; i.Name = data.Name; i.NodeId = node.Id; i.PhysicalAddress = data.MACAddress; i.Speed = data.Speed; i.Status = NodeStatus.Active; i.TypeDescription = ""; } } }
private void PollNetworkUtilization(WmiNode node) { const string queryTemplate = @"select BytesReceivedPersec, BytesSentPersec, PacketsReceivedPersec, PacketsSentPersec FROM Win32_PerfFormattedData_Tcpip_NetworkInterface where name = '{name}'"; foreach (var iface in node.Interfaces) { var perfCounterName = iface.Name; //adjust performance counter special symbols for instance name. perfCounterName = perfCounterName.Replace("\\", "_"); perfCounterName = perfCounterName.Replace("/", "_"); perfCounterName = perfCounterName.Replace("(", "["); perfCounterName = perfCounterName.Replace(")", "]"); perfCounterName = perfCounterName.Replace("#", "_"); var query = queryTemplate.Replace("{name}", perfCounterName); using (var q = Wmi.Query(node.Node.Name, query)) { var data = q.GetFirstResult(); if (data == null) continue; iface.InBps = data.BytesReceivedPersec; iface.OutBps = data.BytesSentPersec; iface.InPps = data.PacketsReceivedPersec; iface.OutPps = data.PacketsSentPersec; node.AddNetworkUtilization(iface, new Interface.InterfaceUtilization { DateTime = DateTime.UtcNow, InMaxBps = iface.InBps, OutMaxBps = iface.OutBps }); } } }