2023-09-21 18:58:32 +02:00
|
|
|
|
using Insight.Agent.Extensions;
|
2023-11-17 17:12:41 +01:00
|
|
|
|
using Insight.Domain.Network.Agent.Messages;
|
2023-09-21 18:58:32 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
2023-09-21 22:10:55 +02:00
|
|
|
|
namespace Insight.Agent.Services;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class CollectorService
|
2023-09-21 18:58:32 +02:00
|
|
|
|
{
|
2023-09-21 22:10:55 +02:00
|
|
|
|
public OperationSystem? GetOperatingSystem()
|
2023-09-21 18:58:32 +02:00
|
|
|
|
{
|
2023-09-21 22:10:55 +02:00
|
|
|
|
Logger.LogTrace("GetOperatingSystem");
|
|
|
|
|
|
|
|
|
|
|
|
var os = new OperationSystem();
|
|
|
|
|
|
|
|
|
|
|
|
// get uptime
|
|
|
|
|
|
var output = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
// read file
|
|
|
|
|
|
using var stream = File.OpenText(@"/proc/uptime");
|
|
|
|
|
|
output = stream.ReadToEnd();
|
2023-09-21 18:58:32 +02:00
|
|
|
|
|
2023-09-21 22:10:55 +02:00
|
|
|
|
// clean output
|
|
|
|
|
|
var clean = Regex
|
|
|
|
|
|
.Replace(output
|
|
|
|
|
|
.Trim()
|
|
|
|
|
|
.Replace("\t", " "), @"[ ]{2,}", " ");
|
2023-09-21 18:58:32 +02:00
|
|
|
|
|
2023-09-21 22:10:55 +02:00
|
|
|
|
var elements = clean.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
|
2023-09-21 18:58:32 +02:00
|
|
|
|
|
2023-09-21 22:10:55 +02:00
|
|
|
|
// assign values
|
|
|
|
|
|
//os.Uptime = DateTime.Now - TimeSpan.FromSeconds(double.Parse(elements.ElementAt(0)));
|
2023-09-21 18:58:32 +02:00
|
|
|
|
|
2023-09-21 22:10:55 +02:00
|
|
|
|
// request data with process
|
|
|
|
|
|
output = "hostnamectl".Bash();
|
|
|
|
|
|
|
|
|
|
|
|
// linebreak list conversion
|
|
|
|
|
|
var lines = new List<string>(output
|
|
|
|
|
|
.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
|
|
|
|
|
|
.Select(l =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return Regex
|
|
|
|
|
|
.Replace(l
|
2023-09-21 18:58:32 +02:00
|
|
|
|
.Trim()
|
|
|
|
|
|
.Replace("\t", " "), @"[ ]{2,}", " ");
|
2023-09-21 22:10:55 +02:00
|
|
|
|
})
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// assign values
|
|
|
|
|
|
os.Virtual = lines
|
|
|
|
|
|
.Any(l => l
|
|
|
|
|
|
.StartsWith("Virtualization:")) && !string.IsNullOrEmpty(lines
|
|
|
|
|
|
.Where(l => l
|
|
|
|
|
|
.StartsWith("Virtualization:"))
|
|
|
|
|
|
.First()
|
|
|
|
|
|
.Split("Virtualization:")[1]
|
|
|
|
|
|
.Trim());
|
|
|
|
|
|
|
|
|
|
|
|
// OS
|
|
|
|
|
|
// request data with process
|
|
|
|
|
|
output = "hostnamectl".Bash();
|
|
|
|
|
|
|
|
|
|
|
|
// linebreak list conversion
|
|
|
|
|
|
lines = new List<string>(output
|
|
|
|
|
|
.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
|
|
|
|
|
|
.Select(l =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return Regex
|
|
|
|
|
|
.Replace(l
|
|
|
|
|
|
.Trim()
|
|
|
|
|
|
.Replace("\t", " "), @"[ ]{2,}", " ");
|
|
|
|
|
|
})
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// assign values
|
|
|
|
|
|
os.Name = lines
|
|
|
|
|
|
.Any(l => l
|
|
|
|
|
|
.StartsWith("Operating System:")) ? lines
|
|
|
|
|
|
.Where(l => l
|
|
|
|
|
|
.StartsWith("Operating System:"))
|
|
|
|
|
|
.First()
|
|
|
|
|
|
.Split("Operating System:")[1]
|
|
|
|
|
|
.Trim() : string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
os.Version = lines
|
|
|
|
|
|
.Any(l => l
|
|
|
|
|
|
.StartsWith("Kernel:")) ? lines
|
|
|
|
|
|
.Where(l => l
|
|
|
|
|
|
.StartsWith("Kernel:"))
|
|
|
|
|
|
.First()
|
|
|
|
|
|
.Split("Kernel:")[1]
|
|
|
|
|
|
.Trim() : string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
var architecture = lines
|
|
|
|
|
|
.Any(l => l
|
|
|
|
|
|
.StartsWith("Architecture:")) ? lines
|
|
|
|
|
|
.Where(l => l
|
|
|
|
|
|
.StartsWith("Architecture:"))
|
|
|
|
|
|
.First()
|
|
|
|
|
|
.Split("Architecture:")[1]
|
|
|
|
|
|
.Trim() : string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
return os;
|
2023-09-21 18:58:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|