Some checks are pending
CI / Integration (mcr.microsoft.com/mssql/server:2017-latest) (push) Blocked by required conditions
CI / Integration (mcr.microsoft.com/mssql/server:2019-latest) (push) Blocked by required conditions
CI / Integration (mcr.microsoft.com/mssql/server:2022-latest) (push) Blocked by required conditions
CI / Integration (mcr.microsoft.com/mssql/server:2025-latest) (push) Blocked by required conditions
CI / Build og unit tests (push) Waiting to run
Collectors (SQL Server 2000+, version-specific SQL, least privilege): server, configuration, trace flags, databases (settings, backups in UTC, CHECKDB, VLFs, Query Store, files, tempdb), suspect pages, plan cache, wait stats and file I/O as deltas with restart/clear detection, performance counters, CPU from the scheduler monitor ring buffer, blocking (sleeping head blockers with open transactions), and deadlocks from system_health continuing from the last file offset. Rules: configuration (memory, MAXDOP per Microsoft NUMA guidance, cost threshold, ad hoc bloat, risky options, pending changes), databases (state, settings, backups, log backups, CHECKDB, suspect pages, VLFs, autogrowth, max size, Query Store, tempdb), waits (THREADPOOL, signal waits, locks, I/O, WRITELOG, parallelism, PAGELATCH), I/O latency, CPU, memory grants, blocking and deadlocks. Missing data makes a rule not measurable with the collector's reason. Integration tests run every collector as the least-privilege login and reproduce real blocking and a real deadlock; green on 2017 and 2022. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E3EExwndy8Z22EK2Qpt6BK
65 lines
2.7 KiB
C#
65 lines
2.7 KiB
C#
using SQLInsight.Core.Data;
|
|
|
|
namespace SQLInsight.Collectors.Instance;
|
|
|
|
public sealed class ServerCollector : Collector<ServerSnapshot>
|
|
{
|
|
public override string Name => "server";
|
|
public override TimeSpan DefaultInterval => TimeSpan.FromMinutes(15);
|
|
|
|
protected override async Task<ServerSnapshot> CollectAsync(CollectorContext context, ServerSnapshot? previous)
|
|
{
|
|
var version = context.Instance.Version;
|
|
var ct = context.CancellationToken;
|
|
|
|
if (version.Major < 9)
|
|
{
|
|
return await CollectSql2000Async(context).ConfigureAwait(false);
|
|
}
|
|
|
|
var memory = version.Major >= 11 ? "physical_memory_kb / 1024" : "physical_memory_in_bytes / 1048576";
|
|
var vmType = version >= new Core.Instances.SqlServerVersion(10, 50, 2500, 0) ? "virtual_machine_type_desc" : "CAST(NULL AS nvarchar(60))";
|
|
var sql = $"""
|
|
SELECT
|
|
{Db.StartTimeExpression(version)},
|
|
cpu_count,
|
|
(SELECT COUNT(*) FROM sys.dm_os_schedulers WHERE status = N'VISIBLE ONLINE'),
|
|
(SELECT COUNT(DISTINCT parent_node_id) FROM sys.dm_os_schedulers WHERE status = N'VISIBLE ONLINE'),
|
|
{memory},
|
|
{vmType}
|
|
FROM sys.dm_os_sys_info
|
|
""";
|
|
|
|
var snapshot = await Db.QuerySingleAsync(context.Connection, sql, r => new ServerSnapshot
|
|
{
|
|
CapturedAt = context.Now,
|
|
SqlServerStartTime = r.DateTime(0),
|
|
LogicalCpuCount = r.Int(1),
|
|
VisibleOnlineSchedulers = r.Int(2),
|
|
NumaNodeCount = version.Major >= 10 ? r.Int(3) : null,
|
|
PhysicalMemoryMb = r.Long(4),
|
|
VirtualMachineType = r.NullableString(5),
|
|
}, ct).ConfigureAwait(false);
|
|
|
|
return snapshot ?? throw new InvalidOperationException("sys.dm_os_sys_info returned no rows.");
|
|
}
|
|
|
|
private static async Task<ServerSnapshot> CollectSql2000Async(CollectorContext context)
|
|
{
|
|
var ct = context.CancellationToken;
|
|
var startTime = await Db.StartTimeAsync(context.Connection, context.Instance.Version, ct).ConfigureAwait(false);
|
|
|
|
// xp_msver: Index, Name, Internal_Value, Character_Value.
|
|
var msver = await Db.QueryAsync(context.Connection, "EXEC master.dbo.xp_msver", r => (Name: r.String(1), Value: r.NullableLong(2)), ct)
|
|
.ConfigureAwait(false);
|
|
long? Get(string name) => msver.FirstOrDefault(m => string.Equals(m.Name, name, StringComparison.OrdinalIgnoreCase)).Value;
|
|
|
|
return new ServerSnapshot
|
|
{
|
|
CapturedAt = context.Now,
|
|
SqlServerStartTime = startTime,
|
|
LogicalCpuCount = (int)(Get("ProcessorCount") ?? 0),
|
|
PhysicalMemoryMb = Get("PhysicalMemory"),
|
|
};
|
|
}
|
|
}
|