static int Main(string[] args) { string configFileName = args[0]; string jsonConfig = String.Empty; try { jsonConfig = File.ReadAllText(configFileName); } catch (Exception ex) { Console.WriteLine($"Cannot read file {configFileName}. {ex.Message}"); return(1); } var config = JsonConvert.DeserializeObject <Config>(jsonConfig); if (config.useREST && !String.IsNullOrWhiteSpace(config.sas)) { EventHubREST.sas = config.sas; Console.WriteLine($"sending updates via HTTP"); sendMethod = "HTTP"; } else { sendMethod = "AMQP"; if (config.sendMaintenance || config.sendConfig) { maintHub = EventHubClient.CreateFromConnectionString(config.maintHubConnectionString); } } if (config.maintInterval == 0) { config.maintInterval = 2000; } MaintenanceInformation.updateStartTime = DateTime.UtcNow; var computerName = Dns.GetHostName(); var nicAdapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (var nic in nicAdapters) { Console.WriteLine($"Network adapter {nic.Name}, id: {nic.Id}"); Console.WriteLine($" Description: {nic.Description}"); Console.WriteLine($" Speed: {nic.Speed} Type: {nic.NetworkInterfaceType}"); } var chosenNic = nicAdapters.Where(p => p.NetworkInterfaceType.ToString() == "Ethernet").Where(p => p.OperationalStatus == OperationalStatus.Up).FirstOrDefault(); string chosenMAC = String.Empty; if (chosenNic != null) { chosenMAC = chosenNic.GetPhysicalAddress().ToString(); Console.WriteLine($"Selected adapter {chosenNic.Name} with MAC {chosenMAC}"); } computerName += $"|{chosenMAC}"; var cp = new Computer() { FanControllerEnabled = true, CPUEnabled = true, GPUEnabled = true, HDDEnabled = true, MainboardEnabled = true, RAMEnabled = true }; cp.Open(); bool report = true; Stopwatch runTime = new Stopwatch(); runTime.Start(); Stopwatch updateTime = new Stopwatch(); updateTime.Start(); var configData = new ConfigurationInformation(computerName, config.venueName); bool firstSend = true; while (true) { var maintData = new MaintenanceInformation(computerName, config.venueName, sendMethod); foreach (var hw in cp.Hardware) { if (report) { Console.WriteLine(hw.GetReport()); } if (config.sendConfig) { hw.AddHardware(configData); } hw.Update(); foreach (var hwSensor in hw.Sensors) { if (report) { Console.WriteLine($" HW: Sensor {hwSensor.SensorType} : {hwSensor.Name} : {hwSensor.Value}"); } hwSensor.AddSensor(maintData.details); hwSensor.UpdateGenericMaintenanceData(hw, maintData); foreach (var par in hwSensor.Parameters) { if (report) { Console.WriteLine($" HW: Sensor: Param {par.Name} {par.Value}"); } } foreach (var val in hwSensor.Values) { if (report) { Console.WriteLine($" HW: Sensor: Value {val.Time} {val.Value}"); } } } foreach (var subhw in hw.SubHardware) { if (report) { Console.WriteLine(subhw.GetReport()); } subhw.Update(); if (config.sendConfig) { subhw.AddHardware(configData); } foreach (var subhwSensor in subhw.Sensors) { if (report) { Console.WriteLine($" HW: Sensor {subhwSensor.SensorType} : {subhwSensor.Name} : {subhwSensor.Value}"); } subhwSensor.AddSensor(maintData.details); subhwSensor.UpdateGenericMaintenanceData(subhw, maintData); foreach (var par in subhwSensor.Parameters) { if (report) { Console.WriteLine($" HW: Sensor: Param {par.Name} {par.Value}"); } } foreach (var val in subhwSensor.Values) { if (report) { Console.WriteLine($" HW: Sensor: Value {val.Time} {val.Value}"); } } } } } report = false; string j = String.Empty; if (config.sendConfig) { configData.Resolve(); j = JsonConvert.SerializeObject(configData); configSize = SendToMaintHub(j, configData.systemID, config.maintHubName, configData.recordType); Console.WriteLine($"{configData.recordType} data size: {AutoFormatValue(configSize, 2)}"); File.WriteAllText(@".\HWconfig-information.json", j); // just once config.sendConfig = false; } if (config.sendMaintenance) { maintData.Resolve(); j = JsonConvert.SerializeObject(maintData); maintSize = SendToMaintHub(j, maintData.systemID, config.maintHubName, maintData.recordType); } MaintenanceInformation.updateStartTime = DateTime.UtcNow; double sinceLastUpdate = updateTime.Elapsed.TotalSeconds; if (firstSend || (sinceLastUpdate > 60.0)) { if (firstSend) { Console.WriteLine($"{maintData.recordType} data size: {AutoFormatValue(maintSize, 2)}"); File.WriteAllText(@".\HWmaint-information.json", j); firstSend = false; } double bytesSecond = (double)updateSentBytes / sinceLastUpdate; string autoValue = AutoFormatValue(totalSentBytes, 2); string autoBytesSecond = AutoFormatValue((ulong)bytesSecond, 2); Console.WriteLine($"{computerName}: at {maintData.utcTime} : (via {sendMethod}) interval: {config.maintInterval} {sentCount} sends, {autoValue} ({autoBytesSecond}/Sec)"); updateTime.Restart(); updateSentBytes = 0; } Thread.Sleep(config.maintInterval); } return(0); }
public static void UpdateGenericMaintenanceData(this ISensor sensor, IHardware hardware, MaintenanceInformation maintInfo) { switch (hardware.HardwareType) { case HardwareType.SuperIO: case HardwareType.Mainboard: switch (sensor.SensorType) { case SensorType.Fan: if (sensor.Value > maintInfo.MainboardFanSpeedMax) { maintInfo.MainboardFanSpeedMax = (double)sensor.Value; } if (sensor.Value < maintInfo.MainboardFanSpeedMin) { maintInfo.MainboardFanSpeedMin = (double)sensor.Value; } maintInfo.MainboardFanSpeedAvg += (double)sensor.Value; maintInfo.MainboardFanCount++; break; case SensorType.Temperature: if (sensor.Value > maintInfo.MainboardTemperatureMax) { maintInfo.MainboardTemperatureMax = (double)sensor.Value; } if (sensor.Value < maintInfo.MainboardTemperatureMin) { maintInfo.MainboardTemperatureMin = (double)sensor.Value; } maintInfo.MainboardTemperatureAvg += (double)sensor.Value; maintInfo.MainboardTempCount++; break; } break; case HardwareType.CPU: switch (sensor.SensorType) { case SensorType.Fan: if (sensor.Value > maintInfo.CPUFanSpeedMax) { maintInfo.CPUFanSpeedMax = (double)sensor.Value; } if (sensor.Value < maintInfo.CPUFanSpeedMin) { maintInfo.CPUFanSpeedMin = (double)sensor.Value; } maintInfo.CPUFanSpeedAvg += (double)sensor.Value; maintInfo.CPUFanCount++; break; case SensorType.Temperature: if (sensor.Value > maintInfo.CPUTemperatureMax) { maintInfo.CPUTemperatureMax = (double)sensor.Value; } if (sensor.Value < maintInfo.CPUTemperatureMin) { maintInfo.CPUTemperatureMin = (double)sensor.Value; } maintInfo.CPUTemperatureAvg += (double)sensor.Value; maintInfo.CPUTempCount++; break; case SensorType.Power: if (sensor.Value > maintInfo.CPUPowerMax) { maintInfo.CPUPowerMax = (double)sensor.Value; } if (sensor.Value < maintInfo.CPUPowerMin) { maintInfo.CPUPowerMin = (double)sensor.Value; } maintInfo.CPUPowerAvg += (double)sensor.Value; maintInfo.CPUPowerCount++; break; } break; case HardwareType.GpuAti: case HardwareType.GpuNvidia: switch (sensor.SensorType) { case SensorType.Fan: if (sensor.Value > maintInfo.GPUFanSpeedMax) { maintInfo.GPUFanSpeedMax = (double)sensor.Value; } if (sensor.Value < maintInfo.GPUFanSpeedMin) { maintInfo.GPUFanSpeedMin = (double)sensor.Value; } maintInfo.GPUFanSpeedAvg += (double)sensor.Value; maintInfo.GPUFanCount++; break; case SensorType.Temperature: if (sensor.Value > maintInfo.GPUTemperatureMax) { maintInfo.GPUTemperatureMax = (double)sensor.Value; } if (sensor.Value < maintInfo.GPUTemperatureMin) { maintInfo.GPUTemperatureMin = (double)sensor.Value; } maintInfo.GPUTemperatureAvg += (double)sensor.Value; maintInfo.GPUTempCount++; break; case SensorType.Power: if (sensor.Value > maintInfo.GPUPowerMax) { maintInfo.GPUPowerMax = (double)sensor.Value; } if (sensor.Value < maintInfo.GPUPowerMin) { maintInfo.GPUPowerMin = (double)sensor.Value; } maintInfo.GPUPowerAvg += (double)sensor.Value; maintInfo.GPUPowerCount++; break; } break; } }