|
@@ -22,6 +22,16 @@ namespace VeloeMonitorDataCollector
|
|
Serilog.ILogger _logger;
|
|
Serilog.ILogger _logger;
|
|
|
|
|
|
Computer? _computerHardware;
|
|
Computer? _computerHardware;
|
|
|
|
+ float prevIdle = 0f;
|
|
|
|
+ float prevTotal = 0f;
|
|
|
|
+ List<DriveInfo> _drives;
|
|
|
|
+ Dictionary<string, int> _deviceLoadSensorIndex = new()
|
|
|
|
+ {
|
|
|
|
+ {"cpuload", -1},
|
|
|
|
+ {"ramavailable", -1},
|
|
|
|
+ {"ramused", -1},
|
|
|
|
+ {"ramload", -1}
|
|
|
|
+ };
|
|
|
|
|
|
private List<IDataSendable> _sendToDb; //TODO and here
|
|
private List<IDataSendable> _sendToDb; //TODO and here
|
|
|
|
|
|
@@ -86,12 +96,13 @@ namespace VeloeMonitorDataCollector
|
|
case "MySQL":
|
|
case "MySQL":
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- MySqlConnector mySqlDb = new MySqlConnector(dbSection, logger);
|
|
|
|
|
|
+ var mySqlDb = new VeloeMonitorDataCollector.DatabaseConnectors.MySqlConnector(dbSection, logger);
|
|
_sendToDb.Add(mySqlDb);
|
|
_sendToDb.Add(mySqlDb);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
logger.Warning("Database connector not confugured properly. It won't be added in working configuration.");
|
|
logger.Warning("Database connector not confugured properly. It won't be added in working configuration.");
|
|
|
|
+ logger.Error(ex.Message);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
@@ -104,6 +115,7 @@ namespace VeloeMonitorDataCollector
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
logger.Warning("SignalR connector not confugured properly. It won't be added in working configuration.");
|
|
logger.Warning("SignalR connector not confugured properly. It won't be added in working configuration.");
|
|
|
|
+ logger.Error(ex.Message);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
@@ -163,12 +175,24 @@ namespace VeloeMonitorDataCollector
|
|
{
|
|
{
|
|
IsCpuEnabled = true,
|
|
IsCpuEnabled = true,
|
|
IsMemoryEnabled = true,
|
|
IsMemoryEnabled = true,
|
|
- IsStorageEnabled = true,
|
|
|
|
};
|
|
};
|
|
|
|
+ _drives = new List<DriveInfo>();
|
|
|
|
+
|
|
|
|
+ DriveInfo[] allDrives = DriveInfo.GetDrives();
|
|
|
|
+
|
|
|
|
+ foreach (DriveInfo d in allDrives)
|
|
|
|
+ {
|
|
|
|
+ if (d.IsReady == true && (d.DriveType is DriveType.Fixed or DriveType.Network) && d.TotalSize != 0 && !(d.Name.StartsWith("/boot") || d.Name.StartsWith("/dev")))
|
|
|
|
+ {
|
|
|
|
+ _drives.Add(d);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
_computerHardware.Open();
|
|
_computerHardware.Open();
|
|
- SetValuesTimeZero(); //why
|
|
|
|
-
|
|
|
|
|
|
+ SetValuesTimeZero(); //no history
|
|
|
|
+
|
|
|
|
+ CreateHardwareInfoFile();
|
|
|
|
+
|
|
//check database table configuration
|
|
//check database table configuration
|
|
foreach (var database in _sendToDb)
|
|
foreach (var database in _sendToDb)
|
|
{
|
|
{
|
|
@@ -281,6 +305,75 @@ namespace VeloeMonitorDataCollector
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private void CreateHardwareInfoFile()
|
|
|
|
+ {
|
|
|
|
+ File.Create("sysinfo.txt").Close();
|
|
|
|
+
|
|
|
|
+ StreamWriter sw = File.AppendText("sysinfo.txt");
|
|
|
|
+
|
|
|
|
+ if (OperatingSystem.IsLinux())
|
|
|
|
+ {
|
|
|
|
+ var cpuLine = File
|
|
|
|
+ .ReadAllLines("/proc/stat")
|
|
|
|
+ .First()
|
|
|
|
+ .Split(' ', StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
+ .Skip(1)
|
|
|
|
+ .Select(float.Parse)
|
|
|
|
+ .ToArray();
|
|
|
|
+
|
|
|
|
+ var idle = cpuLine[3];
|
|
|
|
+ var total = cpuLine.Sum();
|
|
|
|
+
|
|
|
|
+ var percent = 100 * (1.0f - (idle - prevIdle) / (total - prevTotal));
|
|
|
|
+ sw.WriteLine("CPU Load: {0}", percent);
|
|
|
|
+
|
|
|
|
+ prevIdle = idle;
|
|
|
|
+ prevTotal = total;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ foreach (var hardware in _computerHardware.Hardware)
|
|
|
|
+ {
|
|
|
|
+ hardware.Update();
|
|
|
|
+ //foreach (var sensor in hardware.Sensors)
|
|
|
|
+ // _logger.Warning("{0}: {1}", sensor.Name, sensor.Value);
|
|
|
|
+ sw.WriteLine(hardware.Name);
|
|
|
|
+ sw.WriteLine(" Type: {0}", hardware.HardwareType);
|
|
|
|
+ sw.WriteLine(" Type: {0}", hardware.Identifier);
|
|
|
|
+ sw.WriteLine(" Sensors:");
|
|
|
|
+ foreach (var sensor in hardware.Sensors)
|
|
|
|
+ {
|
|
|
|
+ sw.WriteLine(" {0,-25} {1,-15} {2,-15}",sensor.Name, sensor.SensorType, sensor.Value);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ foreach (DriveInfo d in DriveInfo.GetDrives())
|
|
|
|
+ {
|
|
|
|
+ if (d.IsReady == true && (d.DriveType is DriveType.Fixed or DriveType.Network) && d.TotalSize != 0 && !(d.Name.StartsWith("/boot") || d.Name.StartsWith("/dev")))
|
|
|
|
+ {
|
|
|
|
+ sw.WriteLine("Drive {0}", d.Name);
|
|
|
|
+ sw.WriteLine(" File type: {0}", d.DriveType);
|
|
|
|
+
|
|
|
|
+ sw.WriteLine(" Volume label: {0}", d.VolumeLabel);
|
|
|
|
+ sw.WriteLine(" File system: {0}", d.DriveFormat);
|
|
|
|
+ sw.WriteLine(
|
|
|
|
+ " Available space to current user:{0, 15} bytes",
|
|
|
|
+ d.AvailableFreeSpace);
|
|
|
|
+
|
|
|
|
+ sw.WriteLine(
|
|
|
|
+ " Total available space: {0, 15} bytes",
|
|
|
|
+ d.TotalFreeSpace);
|
|
|
|
+
|
|
|
|
+ sw.WriteLine(
|
|
|
|
+ " Total size of drive: {0, 15} bytes ",
|
|
|
|
+ d.TotalSize);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sw.Close();
|
|
|
|
+ }
|
|
private Dictionary<string, float> UpdateHardware()
|
|
private Dictionary<string, float> UpdateHardware()
|
|
{
|
|
{
|
|
Dictionary<string, float> output = new Dictionary<string, float>();
|
|
Dictionary<string, float> output = new Dictionary<string, float>();
|
|
@@ -288,31 +381,102 @@ namespace VeloeMonitorDataCollector
|
|
if (_computerHardware is null)
|
|
if (_computerHardware is null)
|
|
return output;
|
|
return output;
|
|
|
|
|
|
|
|
+ if (OperatingSystem.IsLinux())
|
|
|
|
+ {
|
|
|
|
+ var cpuLine = File
|
|
|
|
+ .ReadAllLines("/proc/stat")
|
|
|
|
+ .First()
|
|
|
|
+ .Split(' ', StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
+ .Skip(1)
|
|
|
|
+ .Select(float.Parse)
|
|
|
|
+ .ToArray();
|
|
|
|
+
|
|
|
|
+ var idle = cpuLine[3];
|
|
|
|
+ var total = cpuLine.Sum();
|
|
|
|
+
|
|
|
|
+ var percent = 100 * (1.0f - (idle - prevIdle) / (total - prevTotal));
|
|
|
|
+ //for the first calc number can be inf or nan
|
|
|
|
+ if (float.IsFinite(percent))
|
|
|
|
+ output.Add("cpuload", percent);
|
|
|
|
+
|
|
|
|
+ prevIdle = idle;
|
|
|
|
+ prevTotal = total;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
foreach (var hardware in _computerHardware.Hardware)
|
|
foreach (var hardware in _computerHardware.Hardware)
|
|
{
|
|
{
|
|
hardware.Update();
|
|
hardware.Update();
|
|
-
|
|
|
|
- if (hardware.HardwareType is HardwareType.Cpu)
|
|
|
|
- output.Add("cpuload", hardware.Sensors[8].Value.GetValueOrDefault());
|
|
|
|
- if (hardware.HardwareType is HardwareType.Memory)
|
|
|
|
- {
|
|
|
|
- output.Add("ramavailable", hardware.Sensors[1].Value.GetValueOrDefault());
|
|
|
|
- output.Add("ramused", hardware.Sensors[0].Value.GetValueOrDefault());
|
|
|
|
- output.Add("ramload", hardware.Sensors[2].Value.GetValueOrDefault());
|
|
|
|
- }
|
|
|
|
|
|
+ //foreach (var sensor in hardware.Sensors)
|
|
|
|
+ // _logger.Warning("{0}: {1}", sensor.Name, sensor.Value);
|
|
|
|
+
|
|
|
|
+ if (hardware.HardwareType is HardwareType.Cpu && OperatingSystem.IsWindows())
|
|
|
|
+ if (_deviceLoadSensorIndex["cpuload"] is not -1)
|
|
|
|
+ output.Add("cpuload", hardware.Sensors[_deviceLoadSensorIndex["cpuload"]].Value.GetValueOrDefault());
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ for(int i = 0; i < hardware.Sensors.Length; i++)
|
|
|
|
+ if (hardware.Sensors[i].SensorType is SensorType.Load && hardware.Sensors[i].Name == "CPU Total")
|
|
|
|
+ {
|
|
|
|
+ output.Add("cpuload", hardware.Sensors[i].Value.GetValueOrDefault());
|
|
|
|
+ _deviceLoadSensorIndex["cpuload"] = i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- if (hardware.HardwareType is HardwareType.Storage)
|
|
|
|
|
|
+ if (hardware.HardwareType is HardwareType.Memory)
|
|
{
|
|
{
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
|
- foreach (char c in hardware.Name.ToLower())
|
|
|
|
|
|
+ //output.Add("ramavailable", hardware.Sensors[1].Value.GetValueOrDefault());
|
|
|
|
+ if (_deviceLoadSensorIndex["ramavailable"] is not -1)
|
|
|
|
+ output.Add("ramavailable", hardware.Sensors[_deviceLoadSensorIndex["ramavailable"]].Value.GetValueOrDefault());
|
|
|
|
+ else
|
|
{
|
|
{
|
|
- if (c is not (' ' or '/' or '-'))
|
|
|
|
- { sb.Append(c); }
|
|
|
|
|
|
+ for (int i = 0; i < hardware.Sensors.Length; i++)
|
|
|
|
+ if (hardware.Sensors[i].SensorType is SensorType.Data && hardware.Sensors[i].Name == "Memory Available")
|
|
|
|
+ {
|
|
|
|
+ output.Add("ramavailable", hardware.Sensors[i].Value.GetValueOrDefault());
|
|
|
|
+ _deviceLoadSensorIndex["ramavailable"] = i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //output.Add("ramused", hardware.Sensors[0].Value.GetValueOrDefault());
|
|
|
|
+ if (_deviceLoadSensorIndex["ramused"] is not -1)
|
|
|
|
+ output.Add("ramused", hardware.Sensors[_deviceLoadSensorIndex["ramused"]].Value.GetValueOrDefault());
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ for (int i = 0; i < hardware.Sensors.Length; i++)
|
|
|
|
+ if (hardware.Sensors[i].SensorType is SensorType.Data && hardware.Sensors[i].Name == "Memory Used")
|
|
|
|
+ {
|
|
|
|
+ output.Add("ramused", hardware.Sensors[i].Value.GetValueOrDefault());
|
|
|
|
+ _deviceLoadSensorIndex["ramused"] = i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //output.Add("ramload", hardware.Sensors[2].Value.GetValueOrDefault());
|
|
|
|
+ if (_deviceLoadSensorIndex["ramload"] is not -1)
|
|
|
|
+ output.Add("ramload", hardware.Sensors[_deviceLoadSensorIndex["ramload"]].Value.GetValueOrDefault());
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ for (int i = 0; i < hardware.Sensors.Length; i++)
|
|
|
|
+ if (hardware.Sensors[i].SensorType is SensorType.Load && hardware.Sensors[i].Name == "Memory")
|
|
|
|
+ {
|
|
|
|
+ output.Add("ramload", hardware.Sensors[i].Value.GetValueOrDefault());
|
|
|
|
+ _deviceLoadSensorIndex["ramload"] = i;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-
|
|
|
|
- output.Add(sb.ToString(), hardware.Sensors[1].Value.GetValueOrDefault());
|
|
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < _drives.Count; i++)
|
|
|
|
+ {
|
|
|
|
+ _drives[i] = new DriveInfo(_drives[i].Name);
|
|
|
|
+ var name = "";
|
|
|
|
|
|
|
|
+ if (OperatingSystem.IsWindows())
|
|
|
|
+ name = _drives[i].Name.ToLower().Replace(":\\", "").Replace('\\','_');
|
|
|
|
+ else
|
|
|
|
+ if (_drives[i].Name == "/")
|
|
|
|
+ name = "root_directory";
|
|
|
|
+ else
|
|
|
|
+ name = _drives[i].Name.ToLower().Replace('/','_');
|
|
|
|
+ output.Add(name , (float)_drives[i].TotalFreeSpace / _drives[i].TotalSize);
|
|
}
|
|
}
|
|
return output;
|
|
return output;
|
|
}
|
|
}
|
|
@@ -416,13 +580,13 @@ namespace VeloeMonitorDataCollector
|
|
}
|
|
}
|
|
catch (System.Net.Sockets.SocketException ex)
|
|
catch (System.Net.Sockets.SocketException ex)
|
|
{
|
|
{
|
|
- _logger.Debug(ex.Message);
|
|
|
|
|
|
+ _logger.Debug("{0}:{1} {2}", ip, port, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch(System.Net.Sockets.SocketException ex)
|
|
catch(System.Net.Sockets.SocketException ex)
|
|
{
|
|
{
|
|
- _logger.Debug(ex.Message);
|
|
|
|
|
|
+ _logger.Debug("{0}:{1} {2}", ip, port, ex.Message);
|
|
}
|
|
}
|
|
}, _token));
|
|
}, _token));
|
|
|
|
|
|
@@ -455,13 +619,13 @@ namespace VeloeMonitorDataCollector
|
|
}
|
|
}
|
|
catch (System.Net.Sockets.SocketException ex)
|
|
catch (System.Net.Sockets.SocketException ex)
|
|
{
|
|
{
|
|
- _logger.Debug(ex.Message);
|
|
|
|
|
|
+ _logger.Debug("{0}:{1} {2}", ip, port, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (System.Net.Sockets.SocketException ex)
|
|
catch (System.Net.Sockets.SocketException ex)
|
|
{
|
|
{
|
|
- _logger.Debug(ex.Message);
|
|
|
|
|
|
+ _logger.Debug("{0}:{1} {2}", ip, port, ex.Message);
|
|
}
|
|
}
|
|
}, _token));
|
|
}, _token));
|
|
|
|
|
|
@@ -478,7 +642,7 @@ namespace VeloeMonitorDataCollector
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
- _logger.Debug(ex.Message);
|
|
|
|
|
|
+ _logger.Debug("{0}:{1} {2}", ip, port, ex.Message);
|
|
}
|
|
}
|
|
|
|
|
|
if (status != null)
|
|
if (status != null)
|
|
@@ -515,12 +679,12 @@ namespace VeloeMonitorDataCollector
|
|
}
|
|
}
|
|
catch (TimeoutException ex)
|
|
catch (TimeoutException ex)
|
|
{
|
|
{
|
|
- _logger.Debug($"{{0}}:{{1}} {ex.Message}", ip, port);
|
|
|
|
|
|
+ _logger.Debug("{0}:{1} {2}", ip, port, ex.Message);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
- _logger.Debug(ex.Message);
|
|
|
|
|
|
+ _logger.Debug("{0}:{1} {2}", ip, port, ex.Message);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|