Closed wasitthaphon closed 9 months ago
ใช้ Regex Map text response
ตัวอย่าง
using System;
using System.Collections.Generic;
public class WmiMetrics
{
public List<WmiMetric> Metrics { get; set; }
}
public class WmiMetric
{
public string Name { get; set; }
public string Type { get; set; }
public double Value { get; set; }
}
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string responseText = // Your WMI response as a string;
WmiMetrics wmiMetrics = ParseWmiMetrics(responseText);
foreach (var metric in wmiMetrics.Metrics)
{
Console.WriteLine($"Name: {metric.Name}");
Console.WriteLine($"Type: {metric.Type}");
Console.WriteLine($"Value: {metric.Value}");
Console.WriteLine();
}
}
static WmiMetrics ParseWmiMetrics(string responseText)
{
WmiMetrics wmiMetrics = new WmiMetrics();
wmiMetrics.Metrics = new List<WmiMetric>();
string pattern = @"# HELP (?<name>[^\s]+) (?<type>[^\s]+)\s*\n# TYPE (?<name>[^\s]+) (?<type>[^\s]+)\s*\n(?<name>[^\s]+) (?<value>[^\s]+)";
MatchCollection matches = Regex.Matches(responseText, pattern);
foreach (Match match in matches)
{
var metric = new WmiMetric
{
Name = match.Groups["name"].Value,
Type = match.Groups["type"].Value,
Value = double.Parse(match.Groups["value"].Value, System.Globalization.CultureInfo.InvariantCulture)
};
wmiMetrics.Metrics.Add(metric);
}
return wmiMetrics;
}
}
การดึง server status metric เป็นไปได้
ตัวอ่าน CPU จะใช้ Dependency Singleton ช่วยเก็บค่า cpu ก่อนหน้าเพื่อใช้คำนวณเทียบกับการดึงค่าปัจจุบัน
ขั้นตอนการดำเนินงาน
[x] ศึกษาการดึงข้อมูลสถานะจากเซิฟเวอร์
[x] ทำโมเดลค่าที่จะส่งออกไป
[ ] สร้าง api path
[x] all server active