wasitthaphon / journeys

0 stars 0 forks source link

สร้าง api service ส่งผลลัพธ์ค่าของสถานะของ Server #12

Closed wasitthaphon closed 9 months ago

wasitthaphon commented 10 months ago

ขั้นตอนการดำเนินงาน

wasitthaphon commented 10 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;
    }
}
wasitthaphon commented 10 months ago

การดึง server status metric เป็นไปได้

Image

wasitthaphon commented 10 months ago

ตัวอ่าน CPU จะใช้ Dependency Singleton ช่วยเก็บค่า cpu ก่อนหน้าเพื่อใช้คำนวณเทียบกับการดึงค่าปัจจุบัน