refactor (networking)
This commit is contained in:
parent
febc4d9488
commit
450a6f2796
153 changed files with 7834 additions and 8004 deletions
|
|
@ -1,106 +1,105 @@
|
|||
using Insight.Agent.Extensions;
|
||||
using Insight.Agent.Messages;
|
||||
using Insight.Domain.Messages.Agent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Insight.Agent.Services
|
||||
namespace Insight.Agent.Services;
|
||||
|
||||
public partial class CollectorService
|
||||
{
|
||||
public partial class CollectorService
|
||||
public OperationSystem? GetOperatingSystem()
|
||||
{
|
||||
public OperationSystem? GetOperatingSystem()
|
||||
{
|
||||
Logger.LogTrace("GetOperatingSystem");
|
||||
Logger.LogTrace("GetOperatingSystem");
|
||||
|
||||
var os = new OperationSystem();
|
||||
var os = new OperationSystem();
|
||||
|
||||
// get uptime
|
||||
var output = string.Empty;
|
||||
// get uptime
|
||||
var output = string.Empty;
|
||||
|
||||
// read file
|
||||
using var stream = File.OpenText(@"/proc/uptime");
|
||||
output = stream.ReadToEnd();
|
||||
// read file
|
||||
using var stream = File.OpenText(@"/proc/uptime");
|
||||
output = stream.ReadToEnd();
|
||||
|
||||
// clean output
|
||||
var clean = Regex
|
||||
.Replace(output
|
||||
// clean output
|
||||
var clean = Regex
|
||||
.Replace(output
|
||||
.Trim()
|
||||
.Replace("\t", " "), @"[ ]{2,}", " ");
|
||||
|
||||
var elements = clean.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
// assign values
|
||||
//os.Uptime = DateTime.Now - TimeSpan.FromSeconds(double.Parse(elements.ElementAt(0)));
|
||||
|
||||
// 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
|
||||
.Trim()
|
||||
.Replace("\t", " "), @"[ ]{2,}", " ");
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var elements = clean.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries);
|
||||
// assign values
|
||||
os.Virtual = lines
|
||||
.Any(l => l
|
||||
.StartsWith("Virtualization:")) && !string.IsNullOrEmpty(lines
|
||||
.Where(l => l
|
||||
.StartsWith("Virtualization:"))
|
||||
.First()
|
||||
.Split("Virtualization:")[1]
|
||||
.Trim());
|
||||
|
||||
// assign values
|
||||
//os.Uptime = DateTime.Now - TimeSpan.FromSeconds(double.Parse(elements.ElementAt(0)));
|
||||
// OS
|
||||
// request data with process
|
||||
output = "hostnamectl".Bash();
|
||||
|
||||
// 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();
|
||||
|
||||
// linebreak list conversion
|
||||
var 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;
|
||||
|
||||
// assign values
|
||||
os.Virtual = lines
|
||||
.Any(l => l
|
||||
.StartsWith("Virtualization:")) && !string.IsNullOrEmpty(lines
|
||||
.Where(l => l
|
||||
.StartsWith("Virtualization:"))
|
||||
.First()
|
||||
.Split("Virtualization:")[1]
|
||||
.Trim());
|
||||
os.Version = lines
|
||||
.Any(l => l
|
||||
.StartsWith("Kernel:")) ? lines
|
||||
.Where(l => l
|
||||
.StartsWith("Kernel:"))
|
||||
.First()
|
||||
.Split("Kernel:")[1]
|
||||
.Trim() : string.Empty;
|
||||
|
||||
// OS
|
||||
// request data with process
|
||||
output = "hostnamectl".Bash();
|
||||
var architecture = lines
|
||||
.Any(l => l
|
||||
.StartsWith("Architecture:")) ? lines
|
||||
.Where(l => l
|
||||
.StartsWith("Architecture:"))
|
||||
.First()
|
||||
.Split("Architecture:")[1]
|
||||
.Trim() : string.Empty;
|
||||
|
||||
// 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;
|
||||
}
|
||||
return os;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue