From 4ce099c5bf61d89c3ff4a92ae392e536d9210d60 Mon Sep 17 00:00:00 2001 From: LORDofDOOM Date: Sun, 26 Aug 2018 10:51:09 +0200 Subject: [PATCH] Add Network (NIC) support (updated) (#52) * [Core] Add Network (NIC) support - All credits to XiaofengdiZhu (https://github.com/XiaofengdiZhu/openhardwaremonitor/commits/EditeByXiaofengdizhu) * [Core] NIC: Rename InternetSpeed to Throughput, drop non physical devices (like loopback), fix alltime stats for adapter --- GUI/HardwareTypeImage.cs | 3 + GUI/MainForm.Designer.cs | 10 +- GUI/MainForm.cs | 7 + GUI/SensorGadget.cs | 37 ++ GUI/SensorNode.cs | 33 ++ GUI/TypeNode.cs | 4 + Hardware/Computer.cs | 830 ++++++++++++++++++---------------- Hardware/IComputer.cs | 1 + Hardware/IHardware.cs | 5 +- Hardware/ISensor.cs | 3 +- Hardware/Nic/Nic.cs | 110 +++++ Hardware/Nic/NicGroup.cs | 81 ++++ OpenHardwareMonitor.csproj | 4 + OpenHardwareMonitorLib.csproj | 2 + Resources/nic.png | Bin 0 -> 15769 bytes Resources/throughput.png | Bin 0 -> 15584 bytes Utilities/HttpServer.cs | 4 + 17 files changed, 730 insertions(+), 404 deletions(-) create mode 100644 Hardware/Nic/Nic.cs create mode 100644 Hardware/Nic/NicGroup.cs create mode 100644 Resources/nic.png create mode 100644 Resources/throughput.png diff --git a/GUI/HardwareTypeImage.cs b/GUI/HardwareTypeImage.cs index 056b81a..1ea5ee3 100644 --- a/GUI/HardwareTypeImage.cs +++ b/GUI/HardwareTypeImage.cs @@ -62,6 +62,9 @@ namespace OpenHardwareMonitor.GUI { case HardwareType.Aquacomputer: image = Utilities.EmbeddedResources.GetImage("acicon.png"); break; + case HardwareType.NIC: + image = Utilities.EmbeddedResources.GetImage("nic.png"); + break; default: image = new Bitmap(1, 1); break; diff --git a/GUI/MainForm.Designer.cs b/GUI/MainForm.Designer.cs index f602030..24898ad 100644 --- a/GUI/MainForm.Designer.cs +++ b/GUI/MainForm.Designer.cs @@ -96,6 +96,7 @@ namespace OpenHardwareMonitor.GUI { this.timer = new System.Windows.Forms.Timer(this.components); this.splitContainer = new OpenHardwareMonitor.GUI.SplitContainerAdv(); this.treeView = new Aga.Controls.Tree.TreeViewAdv(); + this.nicMenuItem = new System.Windows.Forms.MenuItem(); this.logSeparatorMenuItem = new System.Windows.Forms.MenuItem(); this.loggingIntervalMenuItem = new System.Windows.Forms.MenuItem(); this.log1sMenuItem = new System.Windows.Forms.MenuItem(); @@ -247,7 +248,8 @@ namespace OpenHardwareMonitor.GUI { this.ramMenuItem, this.gpuMenuItem, this.fanControllerMenuItem, - this.hddMenuItem}); + this.hddMenuItem, + this.nicMenuItem}); this.menuItem5.Text = "Hardware"; // // mainboardMenuItem @@ -280,6 +282,11 @@ namespace OpenHardwareMonitor.GUI { this.hddMenuItem.Index = 5; this.hddMenuItem.Text = "Hard Disk Drives"; // + // nicItem + // + this.nicMenuItem.Index = 6; + this.nicMenuItem.Text = "Network"; + // // menuItem6 // this.menuItem6.Index = 5; @@ -762,6 +769,7 @@ namespace OpenHardwareMonitor.GUI { private System.Windows.Forms.MenuItem log1hMenuItem; private System.Windows.Forms.MenuItem log2hMenuItem; private System.Windows.Forms.MenuItem log6hMenuItem; + private System.Windows.Forms.MenuItem nicMenuItem; } } diff --git a/GUI/MainForm.cs b/GUI/MainForm.cs index 64f9c82..981ea11 100644 --- a/GUI/MainForm.cs +++ b/GUI/MainForm.cs @@ -58,6 +58,7 @@ namespace OpenHardwareMonitor.GUI { private UserOption readGpuSensors; private UserOption readFanControllersSensors; private UserOption readHddSensors; + private UserOption readNicSensors; private UserOption showGadget; private UserRadioGroup plotLocation; @@ -266,6 +267,12 @@ namespace OpenHardwareMonitor.GUI { computer.HDDEnabled = readHddSensors.Value; }; + readNicSensors = new UserOption("nicMenuItem", true, nicMenuItem, + settings); + readNicSensors.Changed += delegate(object sender, EventArgs e) { + computer.NICEnabled = readNicSensors.Value; + }; + showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem, settings); showGadget.Changed += delegate(object sender, EventArgs e) { diff --git a/GUI/SensorGadget.cs b/GUI/SensorGadget.cs index c0d8833..e7e8cd4 100644 --- a/GUI/SensorGadget.cs +++ b/GUI/SensorGadget.cs @@ -576,6 +576,8 @@ namespace OpenHardwareMonitor.GUI { case SensorType.Data: format = "{0:F1} GB"; break; + case SensorType.SmallData: format = "{0:F0} MB"; + break; case SensorType.Factor: format = "{0:F3}"; break; @@ -585,6 +587,41 @@ namespace OpenHardwareMonitor.GUI { unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) { formatted = string.Format("{0:F1} °F", UnitManager.CelsiusToFahrenheit(sensor.Value)); + } else if(sensor.SensorType == SensorType.Throughput) + { + string result = "-"; + switch (sensor.Name){ + case "Connection Speed": { + switch (sensor.Value){ + case 100000000: + result = "100Mbps"; + break; + case 1000000000: + result = "1Gbps"; + break; + default: { + if (sensor.Value < 1024) + result = string.Format("{0:F0} bps", sensor.Value); + else if(sensor.Value < 1048576) + result = string.Format("{0:F1} Kbps", sensor.Value / 1024); + else if(sensor.Value < 1073741824) + result = string.Format("{0:F1} Mbps", sensor.Value / 1048576); + else + result = string.Format("{0:F1} Gbps", sensor.Value / 1073741824); + } + break; + } + } + break; + default:{ + if (sensor.Value < 1048576) + result = string.Format("{0:F1} KB/s", sensor.Value / 1024); + else + result = string.Format("{0:F1} MB/s", sensor.Value / 1048576); + } + break; + } + formatted = result; } else { formatted = string.Format(format, sensor.Value); } diff --git a/GUI/SensorNode.cs b/GUI/SensorNode.cs index 655dfa5..5c5927e 100644 --- a/GUI/SensorNode.cs +++ b/GUI/SensorNode.cs @@ -29,6 +29,38 @@ namespace OpenHardwareMonitor.GUI { if (sensor.SensorType == SensorType.Temperature && unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) { return string.Format("{0:F1} °F", value * 1.8 + 32); + } else if(sensor.SensorType == SensorType.Throughput){ string result = "-"; + switch (sensor.Name){ + case "Connection Speed": { + switch (value){ + case 100000000: + result = "100Mbps"; + break; + case 1000000000: + result = "1Gbps"; + break; + default: { + if (value < 1024) + result = string.Format("{0:F0} bps", value); + else if (value < 1048576) + result = string.Format("{0:F1} Kbps",value/1024); + else if (value < 1073741824) + result = string.Format("{0:F1} Mbps",value/1048576); + else + result = string.Format("{0:F1} Gbps",value/ 1073741824); + } break; + } + } + break; + default:{ + if (value<1048576) + result = string.Format("{0:F1} KB/s",value/1024); + else + result = string.Format("{0:F1} MB/s",value/1048576); + } + break; + } + return result; } else { return string.Format(format, value); } @@ -55,6 +87,7 @@ namespace OpenHardwareMonitor.GUI { case SensorType.SmallData: format = "{0:F1} MB"; break; case SensorType.Factor: format = "{0:F3}"; break; case SensorType.Frequency: format = "{0:F1} Hz"; break; + case SensorType.Throughput: format = "{0:F1} B/s"; break; } bool hidden = settings.GetValue(new Identifier(sensor.Identifier, diff --git a/GUI/TypeNode.cs b/GUI/TypeNode.cs index 0fc5110..831f791 100644 --- a/GUI/TypeNode.cs +++ b/GUI/TypeNode.cs @@ -73,6 +73,10 @@ namespace OpenHardwareMonitor.GUI { this.Image = Utilities.EmbeddedResources.GetImage("clock.png"); this.Text = "Frequencys"; break; + case SensorType.Throughput: + this.Image = Utilities.EmbeddedResources.GetImage("throughput.png"); + this.Text = "Throughput"; + break; } NodeAdded += new NodeEventHandler(TypeNode_NodeAdded); diff --git a/Hardware/Computer.cs b/Hardware/Computer.cs index 71b697b..fd3a3b4 100644 --- a/Hardware/Computer.cs +++ b/Hardware/Computer.cs @@ -1,400 +1,430 @@ -/* - - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. - - Copyright (C) 2009-2012 Michael Möller - -*/ - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Security.Permissions; -using System.Reflection; - -namespace OpenHardwareMonitor.Hardware { - - public class Computer : IComputer { - - private readonly List groups = new List(); - private readonly ISettings settings; - - private SMBIOS smbios; - - private bool open; - - private bool mainboardEnabled; - private bool cpuEnabled; - private bool ramEnabled; - private bool gpuEnabled; - private bool fanControllerEnabled; - private bool hddEnabled; - - public Computer() { - this.settings = new Settings(); - } - - public Computer(ISettings settings) { - this.settings = settings ?? new Settings(); - } - - private void Add(IGroup group) { - if (groups.Contains(group)) - return; - - groups.Add(group); - - if (HardwareAdded != null) - foreach (IHardware hardware in group.Hardware) - HardwareAdded(hardware); - } - - private void Remove(IGroup group) { - if (!groups.Contains(group)) - return; - - groups.Remove(group); - - if (HardwareRemoved != null) - foreach (IHardware hardware in group.Hardware) - HardwareRemoved(hardware); - - group.Close(); - } - - private void RemoveType() where T : IGroup { - List list = new List(); - foreach (IGroup group in groups) - if (group is T) - list.Add(group); - foreach (IGroup group in list) - Remove(group); - } - - [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] - public void Open() { - if (open) - return; - - this.smbios = new SMBIOS(); - - Ring0.Open(); - Opcode.Open(); - - if (mainboardEnabled) - Add(new Mainboard.MainboardGroup(smbios, settings)); - - if (cpuEnabled) - Add(new CPU.CPUGroup(settings)); - - if (ramEnabled) - Add(new RAM.RAMGroup(smbios, settings)); - - if (gpuEnabled) { - Add(new ATI.ATIGroup(settings)); - Add(new Nvidia.NvidiaGroup(settings)); - } - - if (fanControllerEnabled) { - Add(new TBalancer.TBalancerGroup(settings)); - Add(new Heatmaster.HeatmasterGroup(settings)); - Add(new Aquacomputer.AquacomputerGroup(settings)); - } - - if (hddEnabled) - Add(new HDD.HarddriveGroup(settings)); - - open = true; - } - - public bool MainboardEnabled { - get { return mainboardEnabled; } - - [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] - set { - if (open && value != mainboardEnabled) { - if (value) - Add(new Mainboard.MainboardGroup(smbios, settings)); - else - RemoveType(); - } - mainboardEnabled = value; - } - } - - public bool CPUEnabled { - get { return cpuEnabled; } - - [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] - set { - if (open && value != cpuEnabled) { - if (value) - Add(new CPU.CPUGroup(settings)); - else - RemoveType(); - } - cpuEnabled = value; - } - } - - public bool RAMEnabled { - get { return ramEnabled; } - - [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] - set { - if (open && value != ramEnabled) { - if (value) - Add(new RAM.RAMGroup(smbios, settings)); - else - RemoveType(); - } - ramEnabled = value; - } - } - - public bool GPUEnabled { - get { return gpuEnabled; } - - [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] - set { - if (open && value != gpuEnabled) { - if (value) { - Add(new ATI.ATIGroup(settings)); - Add(new Nvidia.NvidiaGroup(settings)); - } else { - RemoveType(); - RemoveType(); - } - } - gpuEnabled = value; - } - } - - public bool FanControllerEnabled { - get { return fanControllerEnabled; } - - [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] - set { - if (open && value != fanControllerEnabled) { - if (value) { - Add(new TBalancer.TBalancerGroup(settings)); - Add(new Heatmaster.HeatmasterGroup(settings)); - Add(new Aquacomputer.AquacomputerGroup(settings)); - } else { - RemoveType(); - RemoveType(); - RemoveType(); - } - } - fanControllerEnabled = value; - } - } - - public bool HDDEnabled { - get { return hddEnabled; } - - [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] - set { - if (open && value != hddEnabled) { - if (value) - Add(new HDD.HarddriveGroup(settings)); - else - RemoveType(); - } - hddEnabled = value; - } - } - - public IHardware[] Hardware { - get { - List list = new List(); - foreach (IGroup group in groups) - foreach (IHardware hardware in group.Hardware) - list.Add(hardware); - return list.ToArray(); - } - } - - private static void NewSection(TextWriter writer) { - for (int i = 0; i < 8; i++) - writer.Write("----------"); - writer.WriteLine(); - writer.WriteLine(); - } - - private static int CompareSensor(ISensor a, ISensor b) { - int c = a.SensorType.CompareTo(b.SensorType); - if (c == 0) - return a.Index.CompareTo(b.Index); - else - return c; - } - - private static void ReportHardwareSensorTree( - IHardware hardware, TextWriter w, string space) - { - w.WriteLine("{0}|", space); - w.WriteLine("{0}+- {1} ({2})", - space, hardware.Name, hardware.Identifier); - ISensor[] sensors = hardware.Sensors; - Array.Sort(sensors, CompareSensor); - foreach (ISensor sensor in sensors) { - w.WriteLine("{0}| +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", - space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, - sensor.Identifier); - } - foreach (IHardware subHardware in hardware.SubHardware) - ReportHardwareSensorTree(subHardware, w, "| "); - } - - private static void ReportHardwareParameterTree( - IHardware hardware, TextWriter w, string space) { - w.WriteLine("{0}|", space); - w.WriteLine("{0}+- {1} ({2})", - space, hardware.Name, hardware.Identifier); - ISensor[] sensors = hardware.Sensors; - Array.Sort(sensors, CompareSensor); - foreach (ISensor sensor in sensors) { - string innerSpace = space + "| "; - if (sensor.Parameters.Length > 0) { - w.WriteLine("{0}|", innerSpace); - w.WriteLine("{0}+- {1} ({2})", - innerSpace, sensor.Name, sensor.Identifier); - foreach (IParameter parameter in sensor.Parameters) { - string innerInnerSpace = innerSpace + "| "; - w.WriteLine("{0}+- {1} : {2}", - innerInnerSpace, parameter.Name, - string.Format(CultureInfo.InvariantCulture, "{0} : {1}", - parameter.DefaultValue, parameter.Value)); - } - } - } - foreach (IHardware subHardware in hardware.SubHardware) - ReportHardwareParameterTree(subHardware, w, "| "); - } - - private static void ReportHardware(IHardware hardware, TextWriter w) { - string hardwareReport = hardware.GetReport(); - if (!string.IsNullOrEmpty(hardwareReport)) { - NewSection(w); - w.Write(hardwareReport); - } - foreach (IHardware subHardware in hardware.SubHardware) - ReportHardware(subHardware, w); - } - - public string GetReport() { - - using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) { - - w.WriteLine(); - w.WriteLine("Open Hardware Monitor Report"); - w.WriteLine(); - - Version version = typeof(Computer).Assembly.GetName().Version; - - NewSection(w); - w.Write("Version: "); w.WriteLine(version.ToString()); - w.WriteLine(); - - NewSection(w); - w.Write("Common Language Runtime: "); - w.WriteLine(Environment.Version.ToString()); - w.Write("Operating System: "); - w.WriteLine(Environment.OSVersion.ToString()); - w.Write("Process Type: "); - w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit"); - w.WriteLine(); - - string r = Ring0.GetReport(); - if (r != null) { - NewSection(w); - w.Write(r); - w.WriteLine(); - } - - NewSection(w); - w.WriteLine("Sensors"); - w.WriteLine(); - foreach (IGroup group in groups) { - foreach (IHardware hardware in group.Hardware) - ReportHardwareSensorTree(hardware, w, ""); - } - w.WriteLine(); - - NewSection(w); - w.WriteLine("Parameters"); - w.WriteLine(); - foreach (IGroup group in groups) { - foreach (IHardware hardware in group.Hardware) - ReportHardwareParameterTree(hardware, w, ""); - } - w.WriteLine(); - - foreach (IGroup group in groups) { - string report = group.GetReport(); - if (!string.IsNullOrEmpty(report)) { - NewSection(w); - w.Write(report); - } - - IHardware[] hardwareArray = group.Hardware; - foreach (IHardware hardware in hardwareArray) - ReportHardware(hardware, w); - - } - return w.ToString(); - } - } - - public void Close() { - if (!open) - return; - - while (groups.Count > 0) { - IGroup group = groups[groups.Count - 1]; - Remove(group); - } - - Opcode.Close(); - Ring0.Close(); - - this.smbios = null; - - open = false; - } - - public event HardwareEventHandler HardwareAdded; - public event HardwareEventHandler HardwareRemoved; - - public void Accept(IVisitor visitor) { - if (visitor == null) - throw new ArgumentNullException("visitor"); - visitor.VisitComputer(this); - } - - public void Traverse(IVisitor visitor) { - foreach (IGroup group in groups) - foreach (IHardware hardware in group.Hardware) - hardware.Accept(visitor); - } - - private class Settings : ISettings { - - public bool Contains(string name) { - return false; - } - - public void SetValue(string name, string value) { } - - public string GetValue(string name, string value) { - return value; - } - - public void Remove(string name) { } - } - } -} +/* + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + + Copyright (C) 2009-2012 Michael Möller + +*/ + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Security.Permissions; +using System.Reflection; +using System.Net.NetworkInformation; + +namespace OpenHardwareMonitor.Hardware { + + public class Computer : IComputer { + + private readonly List groups = new List(); + private readonly ISettings settings; + + private SMBIOS smbios; + + private bool open; + + private bool mainboardEnabled; + private bool cpuEnabled; + private bool ramEnabled; + private bool gpuEnabled; + private bool fanControllerEnabled; + private bool hddEnabled; + private bool nicEnabled; + private int nicCount; + + public Computer() { + this.settings = new Settings(); + } + + public Computer(ISettings settings) { + this.settings = settings ?? new Settings(); + } + + private void Add(IGroup group) { + if (groups.Contains(group)) + return; + + groups.Add(group); + + if (HardwareAdded != null) + foreach (IHardware hardware in group.Hardware) + HardwareAdded(hardware); + } + + private void Remove(IGroup group) { + if (!groups.Contains(group)) + return; + + groups.Remove(group); + + if (HardwareRemoved != null) + foreach (IHardware hardware in group.Hardware) + HardwareRemoved(hardware); + + group.Close(); + } + + private void RemoveType() where T : IGroup { + List list = new List(); + foreach (IGroup group in groups) + if (group is T) + list.Add(group); + foreach (IGroup group in list) + Remove(group); + } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + public void Open() { + if (open) + return; + + this.smbios = new SMBIOS(); + + Ring0.Open(); + Opcode.Open(); + + if (mainboardEnabled) + Add(new Mainboard.MainboardGroup(smbios, settings)); + + if (cpuEnabled) + Add(new CPU.CPUGroup(settings)); + + if (ramEnabled) + Add(new RAM.RAMGroup(smbios, settings)); + + if (gpuEnabled) { + Add(new ATI.ATIGroup(settings)); + Add(new Nvidia.NvidiaGroup(settings)); + } + + if (fanControllerEnabled) { + Add(new TBalancer.TBalancerGroup(settings)); + Add(new Heatmaster.HeatmasterGroup(settings)); + Add(new Aquacomputer.AquacomputerGroup(settings)); + } + + if (hddEnabled) + Add(new HDD.HarddriveGroup(settings)); + + if (nicEnabled) + { + nicCount = NetworkInterface.GetAllNetworkInterfaces().Length; + Add(new Nic.NicGroup(settings)); + } + + open = true; + } + + public bool MainboardEnabled { + get { return mainboardEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + set { + if (open && value != mainboardEnabled) { + if (value) + Add(new Mainboard.MainboardGroup(smbios, settings)); + else + RemoveType(); + } + mainboardEnabled = value; + } + } + + public bool CPUEnabled { + get { return cpuEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + set { + if (open && value != cpuEnabled) { + if (value) + Add(new CPU.CPUGroup(settings)); + else + RemoveType(); + } + cpuEnabled = value; + } + } + + public bool RAMEnabled { + get { return ramEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + set { + if (open && value != ramEnabled) { + if (value) + Add(new RAM.RAMGroup(smbios, settings)); + else + RemoveType(); + } + ramEnabled = value; + } + } + + public bool GPUEnabled { + get { return gpuEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + set { + if (open && value != gpuEnabled) { + if (value) { + Add(new ATI.ATIGroup(settings)); + Add(new Nvidia.NvidiaGroup(settings)); + } else { + RemoveType(); + RemoveType(); + } + } + gpuEnabled = value; + } + } + + public bool FanControllerEnabled { + get { return fanControllerEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + set { + if (open && value != fanControllerEnabled) { + if (value) { + Add(new TBalancer.TBalancerGroup(settings)); + Add(new Heatmaster.HeatmasterGroup(settings)); + Add(new Aquacomputer.AquacomputerGroup(settings)); + } else { + RemoveType(); + RemoveType(); + RemoveType(); + } + } + fanControllerEnabled = value; + } + } + + public bool HDDEnabled { + get { return hddEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + set { + if (open && value != hddEnabled) { + if (value) + Add(new HDD.HarddriveGroup(settings)); + else + RemoveType(); + } + hddEnabled = value; + } + } + + public bool NICEnabled { + get { return nicEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] + set { + if (open && value != nicEnabled) { + if (value) + Add(new Nic.NicGroup(settings)); + else + RemoveType(); + } + nicEnabled = value; + } + } + + public IHardware[] Hardware { + get { + List list = new List(); + foreach (IGroup group in groups) + foreach (IHardware hardware in group.Hardware) + list.Add(hardware); + return list.ToArray(); + } + } + + private static void NewSection(TextWriter writer) { + for (int i = 0; i < 8; i++) + writer.Write("----------"); + writer.WriteLine(); + writer.WriteLine(); + } + + private static int CompareSensor(ISensor a, ISensor b) { + int c = a.SensorType.CompareTo(b.SensorType); + if (c == 0) + return a.Index.CompareTo(b.Index); + else + return c; + } + + private static void ReportHardwareSensorTree( + IHardware hardware, TextWriter w, string space) + { + w.WriteLine("{0}|", space); + w.WriteLine("{0}+- {1} ({2})", + space, hardware.Name, hardware.Identifier); + ISensor[] sensors = hardware.Sensors; + Array.Sort(sensors, CompareSensor); + foreach (ISensor sensor in sensors) { + w.WriteLine("{0}| +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", + space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, + sensor.Identifier); + } + foreach (IHardware subHardware in hardware.SubHardware) + ReportHardwareSensorTree(subHardware, w, "| "); + } + + private static void ReportHardwareParameterTree( + IHardware hardware, TextWriter w, string space) { + w.WriteLine("{0}|", space); + w.WriteLine("{0}+- {1} ({2})", + space, hardware.Name, hardware.Identifier); + ISensor[] sensors = hardware.Sensors; + Array.Sort(sensors, CompareSensor); + foreach (ISensor sensor in sensors) { + string innerSpace = space + "| "; + if (sensor.Parameters.Length > 0) { + w.WriteLine("{0}|", innerSpace); + w.WriteLine("{0}+- {1} ({2})", + innerSpace, sensor.Name, sensor.Identifier); + foreach (IParameter parameter in sensor.Parameters) { + string innerInnerSpace = innerSpace + "| "; + w.WriteLine("{0}+- {1} : {2}", + innerInnerSpace, parameter.Name, + string.Format(CultureInfo.InvariantCulture, "{0} : {1}", + parameter.DefaultValue, parameter.Value)); + } + } + } + foreach (IHardware subHardware in hardware.SubHardware) + ReportHardwareParameterTree(subHardware, w, "| "); + } + + private static void ReportHardware(IHardware hardware, TextWriter w) { + string hardwareReport = hardware.GetReport(); + if (!string.IsNullOrEmpty(hardwareReport)) { + NewSection(w); + w.Write(hardwareReport); + } + foreach (IHardware subHardware in hardware.SubHardware) + ReportHardware(subHardware, w); + } + + public string GetReport() { + + using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) { + + w.WriteLine(); + w.WriteLine("Open Hardware Monitor Report"); + w.WriteLine(); + + Version version = typeof(Computer).Assembly.GetName().Version; + + NewSection(w); + w.Write("Version: "); w.WriteLine(version.ToString()); + w.WriteLine(); + + NewSection(w); + w.Write("Common Language Runtime: "); + w.WriteLine(Environment.Version.ToString()); + w.Write("Operating System: "); + w.WriteLine(Environment.OSVersion.ToString()); + w.Write("Process Type: "); + w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit"); + w.WriteLine(); + + string r = Ring0.GetReport(); + if (r != null) { + NewSection(w); + w.Write(r); + w.WriteLine(); + } + + NewSection(w); + w.WriteLine("Sensors"); + w.WriteLine(); + foreach (IGroup group in groups) { + foreach (IHardware hardware in group.Hardware) + ReportHardwareSensorTree(hardware, w, ""); + } + w.WriteLine(); + + NewSection(w); + w.WriteLine("Parameters"); + w.WriteLine(); + foreach (IGroup group in groups) { + foreach (IHardware hardware in group.Hardware) + ReportHardwareParameterTree(hardware, w, ""); + } + w.WriteLine(); + + foreach (IGroup group in groups) { + string report = group.GetReport(); + if (!string.IsNullOrEmpty(report)) { + NewSection(w); + w.Write(report); + } + + IHardware[] hardwareArray = group.Hardware; + foreach (IHardware hardware in hardwareArray) + ReportHardware(hardware, w); + + } + return w.ToString(); + } + } + + public void Close() { + if (!open) + return; + + while (groups.Count > 0) { + IGroup group = groups[groups.Count - 1]; + Remove(group); + } + + Opcode.Close(); + Ring0.Close(); + + this.smbios = null; + + open = false; + } + + public event HardwareEventHandler HardwareAdded; + public event HardwareEventHandler HardwareRemoved; + + public void Accept(IVisitor visitor) { + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitComputer(this); + } + + public void Traverse(IVisitor visitor) { + foreach (IGroup group in groups) + foreach (IHardware hardware in group.Hardware) + hardware.Accept(visitor); + int newNiccount = NetworkInterface.GetAllNetworkInterfaces().Length; + if (nicCount != newNiccount) { + nicCount = newNiccount; + NICEnabled = false; + NICEnabled = true; + } + } + + private class Settings : ISettings { + + public bool Contains(string name) { + return false; + } + + public void SetValue(string name, string value) { } + + public string GetValue(string name, string value) { + return value; + } + + public void Remove(string name) { } + } + } +} diff --git a/Hardware/IComputer.cs b/Hardware/IComputer.cs index 56413a1..b3342db 100644 --- a/Hardware/IComputer.cs +++ b/Hardware/IComputer.cs @@ -22,6 +22,7 @@ namespace OpenHardwareMonitor.Hardware { bool GPUEnabled { get; } bool FanControllerEnabled { get; } bool HDDEnabled { get; } + bool NICEnabled { get; } string GetReport(); diff --git a/Hardware/IHardware.cs b/Hardware/IHardware.cs index c9ce79e..522a27c 100644 --- a/Hardware/IHardware.cs +++ b/Hardware/IHardware.cs @@ -22,8 +22,9 @@ namespace OpenHardwareMonitor.Hardware { GpuAti, TBalancer, Heatmaster, - HDD - } + HDD, + NIC + } public interface IHardware : IElement { diff --git a/Hardware/ISensor.cs b/Hardware/ISensor.cs index cf431e8..1760e06 100644 --- a/Hardware/ISensor.cs +++ b/Hardware/ISensor.cs @@ -28,7 +28,8 @@ namespace OpenHardwareMonitor.Hardware { Power, // W Data, // GB = 2^30 Bytes SmallData, // MB = 2^20 Bytes - } + Throughput, // B/s + } public struct SensorValue { private readonly float value; diff --git a/Hardware/Nic/Nic.cs b/Hardware/Nic/Nic.cs new file mode 100644 index 0000000..3c51d32 --- /dev/null +++ b/Hardware/Nic/Nic.cs @@ -0,0 +1,110 @@ +/* + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + + Copyright (C) 2012 Michael Möller + +*/ + +using System; +using System.Diagnostics; +using System.Globalization; +using System.Net.NetworkInformation; + +namespace OpenHardwareMonitor.Hardware.Nic +{ + internal class Nic : Hardware + { + private ISettings settings; + private Sensor dataUploaded; + private Sensor dataDownloaded; + private Sensor uploadSpeed; + private Sensor downloadSpeed; + private Sensor networkUtilization; + private NetworkInterface networkInterface; + private int nicIndex; + private DateTime latesTime; + private DateTime presentBootTime; + + private long bytesUploaded; + private long bytesDownloaded; + private bool shouldTotalFlowUpdate = true; + + public Nic(string name, ISettings Settings, int index, NicGroup nicGroup) + : base(name, new Identifier("NIC",index.ToString(CultureInfo.InvariantCulture)), Settings) + { + settings = Settings; + nicIndex = index; + networkInterface = nicGroup.NetworkInterfaces[index]; + presentBootTime = DateTime.Now.AddMilliseconds(-(double)Environment.TickCount); + dataUploaded = new Sensor("Data Uploaded", 2, SensorType.Data, this, + settings); + ActivateSensor(dataUploaded); + dataDownloaded = new Sensor("Data Downloaded", 3, SensorType.Data, this, + settings); + ActivateSensor(dataDownloaded); + uploadSpeed = new Sensor("Upload Speed", 7, SensorType.Throughput, this, + settings); + ActivateSensor(uploadSpeed); + downloadSpeed = new Sensor("Download Speed", 8, SensorType.Throughput, this, + settings); + ActivateSensor(downloadSpeed); + networkUtilization = new Sensor("Network Utilization", 1, SensorType.Load, this, + settings); + ActivateSensor(networkUtilization); + bytesUploaded = NetworkInterface.GetIPStatistics().BytesSent; + bytesDownloaded = NetworkInterface.GetIPStatistics().BytesReceived; + latesTime = DateTime.Now; + } + + public override HardwareType HardwareType + { + get + { + return HardwareType.NIC; + } + } + + internal NetworkInterface NetworkInterface + { + get { return networkInterface; } + } + + public override void Update() + { + DateTime newTime = DateTime.Now; + float dt = (float)(newTime - latesTime).TotalSeconds; + latesTime = newTime; + IPv4InterfaceStatistics interfaceStats = NetworkInterface.GetIPv4Statistics(); + long dBytesUploaded = interfaceStats.BytesSent - bytesUploaded; + long dBytesDownloaded = interfaceStats.BytesReceived - bytesDownloaded; + uploadSpeed.Value = (float)dBytesUploaded / dt; + downloadSpeed.Value = (float)dBytesDownloaded / dt; + networkUtilization.Value = Clamp((float)Math.Max(dBytesUploaded, dBytesDownloaded) * 800 / NetworkInterface.Speed / dt, 0,100); + bytesUploaded = interfaceStats.BytesSent; + bytesDownloaded = interfaceStats.BytesReceived; + dataUploaded.Value = ((float)bytesUploaded / 1073741824); + dataDownloaded.Value = ((float)bytesDownloaded / 1073741824); + } + + /// + /// Clamps the specified value. + /// + /// The value. + /// The minimum. + /// The maximum. + /// . + private static float Clamp(float value, float min, float max) + { + if (value < min) + return min; + + if (value > max) + return max; + + return value; + } + } +} diff --git a/Hardware/Nic/NicGroup.cs b/Hardware/Nic/NicGroup.cs new file mode 100644 index 0000000..2fc66cd --- /dev/null +++ b/Hardware/Nic/NicGroup.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Net.NetworkInformation; +using System.Text; + +namespace OpenHardwareMonitor.Hardware.Nic +{ + internal class NicGroup : IGroup + { + + private List hardware = new List(); + private NetworkInterface[] _networkInterfaces; + + public NicGroup(ISettings settings) + { + int p = (int)Environment.OSVersion.Platform; + if ((p == 4) || (p == 128)) + { + hardware = new List(); + return; + } + _networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); + for (int i = 0; i < _networkInterfaces.Length; i++) + { + if (_networkInterfaces[i].NetworkInterfaceType != NetworkInterfaceType.Unknown && _networkInterfaces[i].NetworkInterfaceType != NetworkInterfaceType.Loopback && _networkInterfaces[i].NetworkInterfaceType != NetworkInterfaceType.Tunnel) + { + hardware.Add(new Nic(_networkInterfaces[i].Name, settings, i, this)); + } + + } + + } + + public string GetReport() + { + if (NetworkInterfaces == null) + return null; + + var report = new StringBuilder(); + + foreach (Nic hw in hardware) + { + report.AppendLine(hw.NetworkInterface.Description); + report.AppendLine(hw.NetworkInterface.OperationalStatus.ToString()); + report.AppendLine(); + foreach (var sensor in hw.Sensors) + { + report.AppendLine(sensor.Name); + report.AppendLine(sensor.Value.ToString() + sensor.SensorType); + report.AppendLine(); + } + } + + return report.ToString(); + } + + public IHardware[] Hardware + { + get + { + return hardware.ToArray(); + } + } + public NetworkInterface[] NetworkInterfaces + { + get + { + return _networkInterfaces; + } + set + { + _networkInterfaces = value; + } + } + public void Close() + { + foreach (Hardware nic in hardware) + nic.Close(); + } + } +} diff --git a/OpenHardwareMonitor.csproj b/OpenHardwareMonitor.csproj index 09fc055..18fc12a 100644 --- a/OpenHardwareMonitor.csproj +++ b/OpenHardwareMonitor.csproj @@ -173,6 +173,7 @@ + @@ -255,6 +256,9 @@ + + + diff --git a/OpenHardwareMonitorLib.csproj b/OpenHardwareMonitorLib.csproj index c631360..bb720e8 100644 --- a/OpenHardwareMonitorLib.csproj +++ b/OpenHardwareMonitorLib.csproj @@ -66,6 +66,8 @@ + + diff --git a/Resources/nic.png b/Resources/nic.png new file mode 100644 index 0000000000000000000000000000000000000000..17e4d56e081ed065363bcb9a14ab4684cdee81e0 GIT binary patch literal 15769 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 literal 0 HcmV?d00001 diff --git a/Utilities/HttpServer.cs b/Utilities/HttpServer.cs index 4192b1e..90c35d9 100644 --- a/Utilities/HttpServer.cs +++ b/Utilities/HttpServer.cs @@ -477,6 +477,8 @@ namespace OpenHardwareMonitor.Utilities { return "ram.png"; case HardwareType.Aquacomputer: return "acicon.png"; + case HardwareType.NIC: + return "nic.png"; default: return "cpu.png"; } @@ -504,6 +506,8 @@ namespace OpenHardwareMonitor.Utilities { return "level.png"; case SensorType.Power: return "power.png"; + case SensorType.Throughput: + return "internetspeed.png"; default: return "power.png"; }