Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for agent host #259

Merged
merged 15 commits into from
Nov 3, 2023
16 changes: 16 additions & 0 deletions Consul.Test/AgentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -993,5 +994,20 @@ await _client.Agent.ServiceRegister(new AgentServiceRegistration
Assert.Single(actual);
Assert.Equal(checkName, actual.Values.First().Name);
}

[Fact]
public async Task Agent_HostInfo()
{
var hostInfo = await _client.Agent.GetAgentHostInfo();
Assert.NotNull(hostInfo.Response.Host);
Assert.NotNull(hostInfo.Response.Memory);
Assert.NotNull(hostInfo.Response.CPU);
sammychinedu2ky marked this conversation as resolved.
Show resolved Hide resolved
Assert.NotNull(hostInfo.Response.Disk);
Assert.True(hostInfo.Response.CollectionTime > 0);
Assert.True(agentVersion.Response.Host.Os.Contains("windows")
|| agentVersion.Response.Host.Os.Contains("linux")
|| agentVersion.Response.Host.Os.Contains("mac")
|| agentVersion.Response.Host.Os.Contains("darwin"));
sammychinedu2ky marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
121 changes: 121 additions & 0 deletions Consul/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,118 @@ public enum LogLevel
Err
}

/// <summary>
/// MemoryInfo represents the memory statistics for the agent
/// </summary>
public class MemoryInfo
{
public long Total { get; set; }
public long Available { get; set; }
public long Used { get; set; }
public double UsedPercent { get; set; }
public long Free { get; set; }
public long Active { get; set; }
public long Inactive { get; set; }
public long Wired { get; set; }
public long Laundry { get; set; }
public long Buffers { get; set; }
public long Cached { get; set; }
public long WriteBack { get; set; }
public long Dirty { get; set; }
public long WriteBackTmp { get; set; }
public long Shared { get; set; }
public long Slab { get; set; }
public long Sreclaimable { get; set; }
public long Sunreclaim { get; set; }
public long PageTables { get; set; }
public long SwapCached { get; set; }
public long CommitLimit { get; set; }
public long CommittedAS { get; set; }
public long HighTotal { get; set; }
public long HighFree { get; set; }
public long LowTotal { get; set; }
public long LowFree { get; set; }
public long SwapTotal { get; set; }
public long SwapFree { get; set; }
public long Mapped { get; set; }
public long VmallocTotal { get; set; }
public long VmallocUsed { get; set; }
public long VmallocChunk { get; set; }
public long HugePagesTotal { get; set; }
public long HugePagesFree { get; set; }
public long HugePageSize { get; set; }
}

/// <summary>
/// CPUInfo represents the CPU statistics for the agent
/// </summary>
public class CPUInfo
{
public int Cpu { get; set; }
public string VendorId { get; set; }
public string Family { get; set; }
public string Model { get; set; }
public int Stepping { get; set; }
public string PhysicalId { get; set; }
public string CoreId { get; set; }
public int Cores { get; set; }
public string ModelName { get; set; }
public long Mhz { get; set; }
public int CacheSize { get; set; }
public List<string> Flags { get; set; }
public string Microcode { get; set; }
}

/// <summary>
/// HostInfo represents the host information for the agent
/// </summary>
public class HostInfo
{
public string Hostname { get; set; }
public long Uptime { get; set; }
public long BootTime { get; set; }
public int Procs { get; set; }
public string Os { get; set; }
public string Platform { get; set; }
public string PlatformFamily { get; set; }
public string PlatformVersion { get; set; }
public string KernelVersion { get; set; }
public string KernelArch { get; set; }
public string VirtualizationSystem { get; set; }
public string VirtualizationRole { get; set; }
public string HostId { get; set; }
}

/// <summary>
/// DiskInfo represents the disk statistics for the agent
/// </summary>
public class DiskInfo
{
public string Path { get; set; }
public string Fstype { get; set; }
public long Total { get; set; }
public long Free { get; set; }
public long Used { get; set; }
public double UsedPercent { get; set; }
public long InodesTotal { get; set; }
public long InodesUsed { get; set; }
public long InodesFree { get; set; }
public double InodesUsedPercent { get; set; }
}

/// <summary>
/// AgentHostInfo represents the host information for the agent
/// </summary>
public class AgentHostInfo
{
public MemoryInfo Memory { get; set; }
public List<CPUInfo> CPU { get; set; }
public HostInfo Host { get; set; }
public DiskInfo Disk { get; set; }
public long CollectionTime { get; set; }
public object Errors { get; set; }
sammychinedu2ky marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
/// Agent can be used to query the Agent endpoints
/// </summary>
Expand Down Expand Up @@ -839,6 +951,15 @@ public async Task<QueryResult<LocalServiceHealth>> GetLocalServiceHealthByID(str
return await GetLocalServiceHealthByID(serviceID, QueryOptions.Default, ct).ConfigureAwait(false);
}

/// <summary>
/// GetAgentHostInfo returns the host info of the agent
/// </summary>
/// <param name="ct"></param>
/// <returns>Agent Host Information</returns>
public async Task<QueryResult<AgentHostInfo>> GetAgentHostInfo(CancellationToken ct = default)
{
return await _client.Get<AgentHostInfo>($"v1/agent/host").Execute(ct).ConfigureAwait(false);
}
/// <summary>
/// Log streamer
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Consul/Interfaces/IAgentEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface IAgentEndpoint
Task<QueryResult<string>> GetWorstLocalServiceHealth(string serviceName, CancellationToken ct = default);
Task<QueryResult<LocalServiceHealth>> GetLocalServiceHealthByID(string serviceID, QueryOptions q, CancellationToken ct = default);
Task<QueryResult<LocalServiceHealth>> GetLocalServiceHealthByID(string serviceID, CancellationToken ct = default);

Task<QueryResult<AgentHostInfo>> GetAgentHostInfo(CancellationToken ct = default);
Task<WriteResult> Leave(string node, CancellationToken ct = default);
Task<WriteResult> Reload(string node, CancellationToken ct = default);
}
Expand Down