Code format

This commit is contained in:
2025-10-05 17:55:26 +02:00
parent 2eac3d2610
commit e90eacd065

View File

@@ -3,8 +3,7 @@ using System.Text;
using LibreHardwareMonitor.Hardware;
var port = Environment.GetEnvironmentVariable("PORT");
if (!int.TryParse(port, out var httpPort))
{
if (!int.TryParse(port, out var httpPort)) {
httpPort = 9646; // default port
}
@@ -12,33 +11,26 @@ var listener = new HttpListener();
listener.Prefixes.Add($"http://*:{httpPort}/");
listener.Start();
var computer = new Computer
{
IsCpuEnabled = true,
IsGpuEnabled = true,
IsMemoryEnabled = true,
IsMotherboardEnabled = true,
IsControllerEnabled = true,
IsNetworkEnabled = true,
IsStorageEnabled = true
};
var computer =
new Computer { IsCpuEnabled = true, IsGpuEnabled = true,
IsMemoryEnabled = true, IsMotherboardEnabled = true,
IsControllerEnabled = true, IsNetworkEnabled = true,
IsStorageEnabled = true };
computer.Open();
Console.WriteLine($"LibreHardwareMonitorExporter listening on http://localhost:{httpPort}/metrics");
Console.WriteLine(
$"LibreHardwareMonitorExporter listening on http://localhost:{httpPort}/metrics");
while (true)
{
while (true) {
var context = await listener.GetContextAsync();
if (context.Request.HttpMethod != "GET")
{
if (context.Request.HttpMethod != "GET") {
context.Response.StatusCode = 405;
context.Response.Close();
continue;
}
if (context.Request.Url?.AbsolutePath != "/metrics")
{
if (context.Request.Url?.AbsolutePath != "/metrics") {
context.Response.StatusCode = 404;
context.Response.Close();
continue;
@@ -55,32 +47,27 @@ while (true)
context.Response.Close();
}
static void AppendMetrics(StringBuilder output, Computer computer)
{
static void AppendMetrics(StringBuilder output, Computer computer) {
var updateVisitor = new UpdateVisitor();
computer.Accept(updateVisitor);
// Basic example metrics for temperature, load, clock, power, fan
foreach (var hardware in computer.Hardware)
{
foreach (var hardware in computer.Hardware) {
EmitHardwareMetrics(output, hardware);
foreach (var sub in hardware.SubHardware)
{
foreach (var sub in hardware.SubHardware) {
EmitHardwareMetrics(output, sub);
}
}
}
static void EmitHardwareMetrics(StringBuilder output, IHardware hardware)
{
static void EmitHardwareMetrics(StringBuilder output, IHardware hardware) {
var hardwareLabel = SanitizeLabel(hardware.Name);
foreach (var sensor in hardware.Sensors)
{
if (sensor.Value is null) continue;
foreach (var sensor in hardware.Sensors) {
if (sensor.Value is null)
continue;
var sensorLabel = SanitizeLabel(sensor.Name);
var value = sensor.Value.Value;
var metricName = sensor.SensorType switch
{
var metricName = sensor.SensorType switch {
SensorType.Temperature => "lhm_temperature_celsius",
SensorType.Load => "lhm_load_percent",
SensorType.Clock => "lhm_clock_mhz",
@@ -96,48 +83,41 @@ static void EmitHardwareMetrics(StringBuilder output, IHardware hardware)
_ => null
};
if (metricName == null) continue;
if (metricName == null)
continue;
output.Append(metricName);
output.Append("{");
output.Append($"hardware=\"{hardwareLabel}\",sensor=\"{sensorLabel}\",type=\"{sensor.SensorType}\"");
output.Append(
$"hardware=\"{hardwareLabel}\",sensor=\"{sensorLabel}\",type=\"{sensor.SensorType}\"");
output.Append("} ");
output.AppendLine(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
output.AppendLine(
value.ToString(System.Globalization.CultureInfo.InvariantCulture));
}
}
static string SanitizeLabel(string raw)
{
static string SanitizeLabel(string raw) {
var sb = new StringBuilder(raw.Length);
foreach (var ch in raw)
{
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == ':' || ch == '.')
{
foreach (var ch in raw) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') || ch == '_' || ch == ':' || ch == '.') {
sb.Append(ch);
}
else
{
} else {
sb.Append('_');
}
}
return sb.ToString();
}
sealed class UpdateVisitor : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
sealed class UpdateVisitor : IVisitor {
public void VisitComputer(IComputer computer) { computer.Traverse(this); }
public void VisitHardware(IHardware hardware)
{
public void VisitHardware(IHardware hardware) {
hardware.Update();
foreach (var sub in hardware.SubHardware)
{
foreach (var sub in hardware.SubHardware) {
sub.Accept(this);
}
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
public void VisitSensor(ISensor sensor) {}
public void VisitParameter(IParameter parameter) {}
}