added various stuff

This commit is contained in:
taywon18 2024-04-25 22:02:17 +02:00
parent 1c4821f2be
commit 393cbeb5a9
7 changed files with 152 additions and 31 deletions

View File

@ -42,11 +42,17 @@ public class Software
}
public static IEnumerable<Software> FromPath(string path)
{
throw new NotImplementedException();
}
public static Software FromLocal()
{
var mypath = Environment.ProcessPath;
if (Environment.ProcessPath == null)
throw new Exception($"Cannot create process for local non-pathed process.");
return FromLocal(Helper.CreateIdFrom(Helper.GetMachineId(), mypath));
return FromLocal(Helper.CreateIdFrom(Helper.GetMachineId(), Environment.ProcessPath));
}
public static Software FromLocal(string id)
@ -54,7 +60,6 @@ public class Software
var currentProcess = Process.GetCurrentProcess();
var processPath = Environment.ProcessPath;
var soft = new Software(id)
{
ShortName = System.AppDomain.CurrentDomain.FriendlyName,

View File

@ -0,0 +1,47 @@
using Salmon.Core;
using Salmon.Model.Monitor;
using System.Security.Cryptography.X509Certificates;
namespace Salmon.Service;
public class Configuration
{
public Uri? Url { get; set; }
public int? Period { get; set; }
public bool? MonitorHardware { get; set; } = true;
public bool? MonitorLocalSoftware { get; set; } = true;
public List<WatcherConfiguration> Watch { get; set; } = new ();
}
public abstract class WatcherConfiguration
{
public abstract Task<IEnumerable<Element>> ForgeElements();
}
public class ExecutableInstanceWatcherConfiguration
: WatcherConfiguration
{
public string Path { get; set; }
public override async Task<IEnumerable<Element>> ForgeElements()
{
List<Element> ret = new();
foreach (var el in Software.FromPath(Path))
ret.Add(el);
return ret;
}
}
public class HttpPageWatcherConfiguration
: WatcherConfiguration
{
public string Method { get; set; } = "GET";
public string Uri { get; set; }
public override async Task<IEnumerable<Element>> ForgeElements()
{
throw new NotImplementedException();
}
}

View File

@ -1,35 +1,90 @@
using Salmon.Core;
using Salmon.Service;
using System.Diagnostics;
using System.Text.Json;
const string ENV_PERIOD = "SALMON_PERIOD";
const string ENV_URI = "SALMON_URI";
Console.WriteLine($"Salmon.Service initializing...");
string uri_args = args.LastOrDefault();
int? period = null;
Uri? uri = null;
bool? monitor_hardware = null;
bool? monitor_localsoftware = null;
// Arguments parsing
string period_str = Environment.GetEnvironmentVariable("SALMON_PERIOD") ?? "60000";
int period;
if (!int.TryParse(period_str, out period))
// 1: use environment variable
var period_env = Environment.GetEnvironmentVariable(ENV_PERIOD);
if (period_env != null)
{
Console.WriteLine($"Cannot parse env variable SALMON_PERIOD, should be an int in milliseconds.");
if (int.TryParse(period_env, out int p))
period = p;
else
Console.WriteLine($"Cannot use env variable {ENV_PERIOD}, should be an int in milliseconds.");
}
var uri_env = Environment.GetEnvironmentVariable(ENV_URI);
if(uri_env != null)
{
if (Uri.TryCreate(uri_env, UriKind.Absolute, out var u))
uri = u;
else
Console.WriteLine($"Cannot use env variable {ENV_URI}, url is malformated.");
}
// 2: override with configuration file
string configpath = "config.json";
if (File.Exists(configpath))
{
Console.WriteLine("Found a configuration file, parsing...");
string content = File.ReadAllText(configpath);
Configuration? conf = null;
try
{
conf = JsonSerializer.Deserialize<Configuration>(content);
}
catch(Exception e)
{
Console.WriteLine($"Cannot parse config file \"{configpath}\", {e}.");
return 1;
}
string uri_str = uri_args ?? Environment.GetEnvironmentVariable("SALMON_URI");
if (uri_str == null)
if(conf == null)
{
Console.WriteLine($"Cannot find env variable SALMON_URI, should be a valid URL.");
Console.WriteLine($"You may also use cmd line, uri must be the last argument.");
return 2;
Console.WriteLine($"Cannot parse config file \"{configpath}\".");
return 1;
}
Uri uri;
if(!Uri.TryCreate(uri_str, UriKind.Absolute, out uri))
{
Console.WriteLine($"Cannot parse env variable SALMON_URI ({uri_str}), it should be a valid URL.");
return 3;
if(conf.Url != null)
uri = conf.Url;
if(conf.Period != null)
period = conf.Period;
if(conf.MonitorHardware != null)
monitor_hardware = conf.MonitorHardware;
if(conf.MonitorLocalSoftware != null)
monitor_localsoftware = conf.MonitorLocalSoftware;
}
//todo: override with parameter
// 3 : override with argument
// 4 : override with default
period ??= 60000;
monitor_hardware ??= true;
monitor_localsoftware ??= true;
// 5 : Check if valid
if(uri == null)
{
Console.WriteLine($"Cannot start: not any URI defined.");
return 1;
}
// Initialisation
Transmitter transmitter = new();
@ -39,22 +94,30 @@ Console.WriteLine($"Salmon.Service started at {DateTime.Now}.");
while ( true )
{
var hardwares = Salmon.Model.Monitor.Hardware.FromAllHardware();
var software = Salmon.Model.Monitor.Software.FromLocal();
List<Element> tosend = new();
tosend.Add(software);
if(monitor_hardware == true)
{
var hardwares = Salmon.Model.Monitor.Hardware.FromAllHardware();
tosend.AddRange(hardwares);
}
try
{
await transmitter.SendAsync(uri, tosend);
}
catch(HttpRequestException e)
{
Console.WriteLine($"[{e.StatusCode}] Sending data failed: {e.Message}.");
}
catch(Exception e)
{
Console.WriteLine($"Sending data failed: {e}");
}
await Task.Delay(period);
await Task.Delay(period.Value);
}

View File

@ -2,6 +2,7 @@
"profiles": {
"Salmon.Service": {
"commandName": "Project",
"workingDirectory": "$(ProjectDir)/RunEnv",
"environmentVariables": {
"SALMON_URI": "http://localhost:5009/api/Push"
}

View File

@ -0,0 +1,3 @@
{
"Url":"http://localhost:5009/api/Push"
}

View File

@ -0,0 +1,2 @@
cd ..
dotnet run

View File

@ -4,10 +4,10 @@
<h3>Éléments</h3>
<CardGroup>
<div Style="display:flex;flex-wrap:wrap; height:100%;">
@foreach(var e in Elements)
{
<Card Class="col-4" Style="width:18rem;">
<Card Class="col-6" Style="width:18rem;margin:10px;">
<CardHeader>
@e.LastType
</CardHeader>
@ -33,7 +33,7 @@
}
</Card>
}
</CardGroup>
</div>
@code {
List<Element> Elements = new();