Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f477e9c

Browse files
Adding scripts for Grafana Monitoring
Adding scripts for Grafana Monitoring
1 parent e8cb01e commit f477e9c

13 files changed

+490
-190
lines changed

‎.vs/SQLDBA-SSMS Solution/v15/.ssms_suo

-2.5 KB
Binary file not shown.

‎Baselining/perfmon-collector-logman.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
cls
22

3-
$collector_root_directory = 'D:\MSSQL15.MSSQLSERVER\SQLWATCH';
4-
$data_collector_template_path = "$collector_root_directory\DBA_PerfMon_Collector-Template.xml";
3+
$collector_root_directory = 'D:\MSSQL15.MSSQLSERVER\MSSQL\Perfmon';
4+
$data_collector_set_name = 'DBA';
5+
$data_collector_template_path = "$collector_root_directory\DBA_PerfMon_NonSQL_Collector_Template.xml";
56
$log_file_path = "$collector_root_directory\$($env:COMPUTERNAME)__"
6-
$data_collector_set_name = 'DBA_PerfMon_Collector';
77
$file_rotation_time = '04:00:00'
88
$sample_interval = '00:00:05'
99
$version_format = 'yyyyMMdd\-HHmmss'
Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
Import-Module dbatools;
2-
$collector_root_directory = 'D:\MSSQL15.MSSQLSERVER\SQLWATCH';
3-
$data_collector_template_path = "$collector_root_directory\DBA_PerfMon_Collector-Template.xml";
4-
$log_file_path = "$collector_root_directory\$($env:COMPUTERNAME)__"
5-
$data_collector_set_name = 'DBA_PerfMon_Collector';
6-
$dsn = 'DBAInventory';
2+
$collector_root_directory = 'D:\MSSQL15.MSSQLSERVER\MSSQL\Perfmon';
3+
$data_collector_set_name = 'DBA';
4+
$dsn = 'LocalSqlServer';
75
$DBAInventory = 'MSI';
8-
$last_log_file_imported = Invoke-DbaQuery -SqlInstance $DBAInventory -Query 'select top 1 DisplayString from DBA.dbo.DisplayToID order by LogStopTime desc' | Select-Object -ExpandProperty DisplayString;
6+
7+
$data_collector_template_path = "$collector_root_directory\DBA_PerfMon_NonSQL_Collector_Template.xml";
8+
$log_file_path = "$collector_root_directory\$($env:COMPUTERNAME)__"
9+
10+
$tsql_last_log_file_imported = @"
11+
if OBJECT_ID('DBA.dbo.DisplayToID') is null
12+
select CAST(NULL AS varchar(1024)) AS DisplayString;
13+
else
14+
select a.DisplayString
15+
from (
16+
select top 1 DisplayString
17+
from DBA.dbo.DisplayToID --full outer join (select CAST(NULL AS varchar(1024)) AS D) as D on 1 = 1
18+
where DisplayString like 'D:\MSSQL15.MSSQLSERVER\MSSQL\Perfmon%'
19+
order by LogStopTime desc
20+
) as a
21+
full outer join (select CAST(NULL AS varchar(1024)) AS DisplayString) as d on 1 = 1;
22+
"@
23+
$last_log_file_imported = Invoke-DbaQuery -SqlInstance $DBAInventory -Query $tsql_last_log_file_imported | Select-Object -ExpandProperty DisplayString;
924

1025
$current_collector_state = logman -n $data_collector_set_name;
1126
$location_line = $current_collector_state | Where-Object {$_ -like 'Output Location:*'}
@@ -14,7 +29,7 @@ $current_log_file = $location_line.Replace("Output Location:",'').trim();
1429
$current_log_file_status = $status_line.Replace("Status:",'').trim();
1530

1631
$perfmonfiles = Get-ChildItem -Path $collector_root_directory -Filter *.blg |
17-
Where-Object {$_.FullName -gt $last_log_file_imported -or $last_log_file_imported -eq $null}
32+
Where-Object {$_.FullName -gt $last_log_file_imported -or $last_log_file_imported -eq $null-or [String]::IsNullOrEmpty($last_log_file_imported)}
1833

1934
if($current_log_file_status -eq 'Running') {
2035
logman stop -name "$data_collector_set_name"
@@ -29,6 +44,7 @@ foreach($perfmonfile in $perfmonfiles)
2944

3045
$AllArgs = @($sourceBlg, '-f', 'SQL', '-o', $sqlDSNconection)
3146
$relog_result = relog $AllArgs
47+
Write-Output "Importing -> $sourceBlg"
3248
}
3349

34-
#Add-OdbcDsn -Name "DBAInventory" -DriverName "SQL Server" -DsnType "System" -SetPropertyValue @("Server=MSI", "Trusted_Connection=Yes", "Database=DBA")
50+
#Add-OdbcDsn -Name "LocalSqlServer" -DriverName "SQL Server" -DsnType "System" -SetPropertyValue @("Server=MSI", "Trusted_Connection=Yes", "Database=DBA")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Import-Module dbatools;
2+
3+
$tsql_get_files_imported = "select /* Delete files older than 12 hours */ DisplayString from dbo.DisplayToID where LogStopTime < DATEADD(hour,-1,getdate())";
4+
5+
$files_to_delete = Invoke-DbaQuery -SqlInstance msi -Database DBA -Query $tsql_get_files_imported | Select-Object -ExpandProperty DisplayString;
6+
foreach($file in $files_to_delete)
7+
{
8+
if([System.IO.File]::Exists($file)) {
9+
Write-Output "Deleting file -> $file";
10+
del $file
11+
}
12+
}
13+
14+

‎SQLDBATools-Inventory/Grafana-Queries.sql

Lines changed: 272 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,288 @@ FROM sys.dm_exec_cached_plans
241241
--WHERE objtype = 'Adhoc' AND usecounts = 1
242242
GROUP BY objtype, cacheobjtype;
243243

244+
-- ====================================================================================================
244245

245246
select top 1 collection_time as time
246247
,available_physical_memory_gb*1024 as decimal(20,0)) as available_physical_memory
247248
from DBA.[dbo].[dm_os_sys_memory]
248249
order by collection_time desc
249250

250-
select top 1 collection_time as time, (free_memory_kb*1.0)/1024 as [Available Memory]
251+
-- ====================================================================================================
252+
DECLARE @server_name varchar(256);
253+
set @server_name = 'MSI';
254+
255+
select collection_time as time, (available_physical_memory_kb*1.0)/1024 as [Available Memory]
251256
from DBA.[dbo].[dm_os_sys_memory]
257+
where 1 = 1
258+
--and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
259+
--and pc.server_name = @server_name
252260
order by collection_time desc
253261

262+
-- ====================================================================================================
263+
254264
select *
255265
from DBA..WhoIsActive_ResultSets as r
256266
where r.collection_time >= '2020年08月23日 16:30:00'
257267

258-
select getdate()
268+
-- ====================================================================================================
269+
270+
DECLARE @server_name varchar(256);
271+
set @server_name = 'MSI';
272+
273+
select master.dbo.local2utc(pc.collection_time) as time, pc.cntr_value as [Available MBytes]
274+
from DBA.dbo.dm_os_performance_counters_nonsql as pc
275+
where 1 = 1
276+
--and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
277+
and pc.server_name = @server_name
278+
and pc.[object_name] = 'Memory'
279+
and pc.counter_name = 'Available MBytes'
280+
281+
-- ====================================================================================================
282+
283+
DECLARE @server_name varchar(256);
284+
set @server_name = 'MSI';
285+
286+
select master.dbo.local2utc(pc.collection_time) as time, pc.counter_name, CEILING(pc.cntr_value) as cntr_value
287+
from DBA.dbo.dm_os_performance_counters_nonsql as pc
288+
where 1 = 1
289+
--and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
290+
and pc.server_name = @server_name
291+
and pc.[object_name] = 'Memory'
292+
and pc.counter_name in ('Pages Input/Sec','Pages/Sec')
293+
294+
-- ====================================================================================================
295+
296+
DECLARE @server_name varchar(256);
297+
set @server_name = 'MSI';
298+
299+
select master.dbo.local2utc(pc.collection_time) as time, pc.counter_name, CEILING(pc.cntr_value) as cntr_value
300+
from DBA.dbo.dm_os_performance_counters_nonsql as pc
301+
where 1 = 1
302+
--and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
303+
and pc.server_name = @server_name
304+
and pc.[object_name] = 'Paging File'
305+
and pc.counter_name in ('% Usage','% Usage Peak')
306+
307+
308+
select top 1 collection_time as time, (available_physical_memory_kb*1.0)/1024 as [Available Memory]
309+
from DBA.[dbo].[dm_os_sys_memory]
310+
order by collection_time desc
311+
312+
-- ====================================================================================================
313+
314+
DECLARE @server_name varchar(256);
315+
set @server_name = 'MSI';
316+
317+
select master.dbo.local2utc(pc.collection_time) as time, pc.instance_name + '\ --- ' + pc.counter_name as instance_name, CAST(pc.cntr_value AS FLOAT) as cntr_value
318+
from DBA.dbo.dm_os_performance_counters_nonsql as pc
319+
where 1 = 1
320+
--and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
321+
and collection_time >= DATEADD(hour,-2,getdate())
322+
and pc.server_name = @server_name
323+
and pc.[object_name] = 'LogicalDisk'
324+
and counter_name in ('Avg. Disk sec/Read','Avg. Disk sec/Write')
325+
and ( instance_name <> '_Total' and instance_name not like 'HarddiskVolume%' )
326+
327+
-- ====================================================================================================
328+
329+
select * from [dbo].[CounterDetails]
330+
where ObjectName = 'Network Interface'
331+
and CounterName in ('Avg. Disk sec/Read','Avg. Disk sec/Write')
332+
333+
-- ====================================================================================================
334+
335+
DECLARE @server_name varchar(256);
336+
set @server_name = 'MSI';
337+
338+
select --master.dbo.local2utc(pc.collection_time) as time, pc.instance_name + '\ --- ' + pc.counter_name as instance_name, CAST(pc.cntr_value AS FLOAT) as cntr_value
339+
*
340+
from DBA.dbo.dm_os_performance_counters_nonsql as pc
341+
where 1 = 1
342+
--and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
343+
and collection_time >= DATEADD(hour,-2,getdate())
344+
and pc.server_name = @server_name
345+
and pc.[object_name] = 'LogicalDisk'
346+
and counter_name in ('Disk Bytes/sec')
347+
and ( instance_name <> '_Total' and instance_name not like 'HarddiskVolume%' )
348+
349+
350+
-- ====================================================================================================
351+
352+
DECLARE @server_name varchar(256);
353+
set @server_name = 'MSI';
354+
355+
select master.dbo.local2utc(pc.collection_time) as time, pc.instance_name --+ '\ --- ' + pc.counter_name as instance_name
356+
,CAST(pc.cntr_value AS FLOAT) as cntr_value
357+
--*
358+
from DBA.dbo.dm_os_performance_counters_nonsql as pc
359+
where 1 = 1
360+
--and collection_time BETWEEN master.dbo.utc2local($__timeFrom()) AND master.dbo.utc2local($__timeTo())
361+
and collection_time >= DATEADD(hour,-2,getdate())
362+
and pc.server_name = @server_name
363+
and pc.[object_name] = 'Network Interface'
364+
and counter_name in ('Bytes Total/sec')
365+
--and ( instance_name <> '_Total' and instance_name not like 'HarddiskVolume%' )
366+
367+
-- ====================================================================================================
368+
GO
369+
370+
371+
--set nocount on;
372+
DECLARE @server_name varchar(256);
373+
DECLARE @start_time datetime2;
374+
DECLARE @end_time datetime2;
375+
set @server_name = 'MSI';
376+
--set @start_time = master.dbo.utc2local($__timeFrom());
377+
--set @end_time = master.dbo.utc2local($__timeTo());
378+
set @start_time = DATEADD(MINUTE,-60,GETDATE());
379+
set @end_time = GETDATE();
380+
381+
DECLARE @verbose bit = 1;
382+
383+
if OBJECT_ID('tempdb..#wait_stats_range') is not null
384+
drop table tempdb..#wait_stats_range;
385+
386+
select IDENTITY(int,1,1) as id, point_in_time
387+
into #wait_stats_range
388+
from (
389+
SELECT si.wait_stats_cleared_time as point_in_time
390+
FROM [DBA].dbo.dm_os_sys_info AS si
391+
WHERE 1 = 1
392+
--and pc.server_name = @server_name
393+
and collection_time BETWEEN @start_time AND @end_time
394+
--
395+
union
396+
--
397+
select point_in_time
398+
from (values (@start_time),(@end_time)) Times (point_in_time)
399+
) as ranges
400+
order by point_in_time asc;
401+
402+
if @verbose = 1
403+
begin
404+
select * from #wait_stats_range;
405+
select count(distinct CollectionTime) as WaitStats_Sample_Counts from [DBA].[dbo].[WaitStats];
406+
end
407+
408+
IF OBJECT_ID('tempdb..#Wait_Stats_Delta') IS NOT NULL
409+
DROP TABLE #Wait_Stats_Delta;
410+
CREATE TABLE #Wait_Stats_Delta
411+
(
412+
--[id_T1] [int] NULL,
413+
--[id_T2] [int] NULL,
414+
--[point_in_time_T1] [datetime2](7) NULL,
415+
--[point_in_time_T2] [datetime2](7) NULL,
416+
--[CollectionTime_T1] [datetime2](7) NULL,
417+
--[CollectionTime_T2] [datetime2](7) NULL,
418+
[CollectionTime] [datetime2](7) NOT NULL,
419+
[CollectionTime_Duration_Seconds] [int] NOT NULL,
420+
[WaitType] [nvarchar](120) NOT NULL,
421+
[Wait_S] [decimal](15, 2) NULL,
422+
[Resource_S] [decimal](15, 2) NULL,
423+
[Signal_S] [decimal](15, 2) NULL,
424+
[WaitCount] [bigint] NULL,
425+
[Percentage] [decimal](10,2) NULL,
426+
[AvgWait_S] [decimal](35, 22) NULL,
427+
[AvgRes_S] [decimal](35, 22) NULL,
428+
[AvgSig_S] [decimal](35, 22) NULL
429+
)
430+
431+
declare @l_id int
432+
,@l_point_in_time datetime2
433+
,@l_counter int = 1
434+
,@l_counter_max int;
435+
436+
select @l_counter_max = max(id) from #wait_stats_range;
437+
438+
--if @verbose = 1
439+
-- select [@l_counter] = @l_counter, [@l_counter_max] = @l_counter_max;
440+
441+
while @l_counter < @l_counter_max -- execute for N-1 times
442+
begin
443+
select @l_point_in_time = point_in_time from #wait_stats_range as tr where tr.id = @l_counter;
444+
445+
if @verbose = 1
446+
select [@l_counter] = @l_counter, [@l_counter_max] = @l_counter_max, [@l_point_in_time] = @l_point_in_time;
447+
448+
;WITH T1 AS (
449+
select id = tr.id, point_in_time = tr.point_in_time,
450+
ws.CollectionTime,
451+
ws.WaitType,
452+
ws.Wait_S, ws.Resource_S, ws.Signal_S, ws.WaitCount, ws.Percentage, ws.AvgWait_S, ws.AvgRes_S, ws.AvgSig_S
453+
from [DBA].[dbo].[WaitStats] AS ws with (nolock) full join #wait_stats_range as tr on tr.id = @l_counter
454+
where ws.CollectionTime = ( case when @l_counter = 1
455+
then (select MIN(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime >= tr.point_in_time)
456+
when @l_counter = @l_counter_max - 1
457+
then ISNULL( (select MIN(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime >= tr.point_in_time),
458+
(select MAX(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime <= tr.point_in_time)
459+
)
460+
else (select MIN(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime > tr.point_in_time)
461+
end
462+
)
463+
)
464+
,T2 AS (
465+
select id = tr.id, point_in_time = tr.point_in_time,
466+
ws.CollectionTime,
467+
ws.WaitType,
468+
ws.Wait_S, ws.Resource_S, ws.Signal_S, ws.WaitCount, ws.Percentage, ws.AvgWait_S, ws.AvgRes_S, ws.AvgSig_S
469+
from [DBA].[dbo].[WaitStats] AS ws with (nolock) full join #wait_stats_range as tr on tr.id = @l_counter+1
470+
where ws.CollectionTime = ( case when @l_counter = 1 AND (@l_counter <> (@l_counter_max - 1))
471+
then (select MAX(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime < tr.point_in_time)
472+
when @l_counter = 1 AND (@l_counter = (@l_counter_max - 1))
473+
then (select MAX(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime <= tr.point_in_time)
474+
when @l_counter = @l_counter_max - 1
475+
then (select MAX(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime <= tr.point_in_time)
476+
else (select MAX(wsi.CollectionTime) as CollectionTime from [DBA].[dbo].[WaitStats] as wsi with (nolock) where wsi.CollectionTime < tr.point_in_time)
477+
end
478+
)
479+
)
480+
,T_Waits_Delta AS (
481+
SELECT CollectionTime, CollectionTime_Duration_Seconds, WaitType, Wait_S, Resource_S, Signal_S, WaitCount,
482+
[Percentage],
483+
AvgWait_S, AvgRes_S, AvgSig_S
484+
--INTO tempdb..Wait_Stats_Delta
485+
FROM (
486+
SELECT --id_T1 = T1.id, id_T2 = T2.id,
487+
--point_in_time_T1 = T1.point_in_time, point_in_time_T2 = T2.point_in_time,
488+
--CollectionTime_T1 = T1.CollectionTime, CollectionTime_T2 = T2.CollectionTime,
489+
CollectionTime = T2.CollectionTime,
490+
CollectionTime_Duration_Seconds = DATEDIFF(second,T1.CollectionTime,T2.CollectionTime),
491+
WaitType = COALESCE(T1.WaitType,T2.WaitType),
492+
Wait_S = ISNULL(T2.Wait_S,0.0) - ISNULL(T1.Wait_S,0.0),
493+
Resource_S = ISNULL(T2.Resource_S,0.0) - ISNULL(T1.Resource_S,0.0),
494+
Signal_S = ISNULL(T2.Signal_S,0.0) - ISNULL(T1.Signal_S,0.0),
495+
WaitCount = ISNULL(T2.WaitCount,0.0) - ISNULL(T1.WaitCount,0.0),
496+
[Percentage] = NULL, --ISNULL(T2.Wait_S,0.0) - ISNULL(T1.Wait_S,0.0),
497+
AvgWait_S = CASE WHEN (ISNULL(T2.WaitCount,0.0) - ISNULL(T1.WaitCount,0.0)) = 0 THEN (ISNULL(T2.Wait_S,0.0) - ISNULL(T1.Wait_S,0.0))
498+
ELSE (ISNULL(T2.Wait_S,0.0) - ISNULL(T1.Wait_S,0.0)) / (ISNULL(T2.WaitCount,0.0) - ISNULL(T1.WaitCount,0.0))
499+
END,
500+
AvgRes_S = CASE WHEN (ISNULL(T2.WaitCount,0.0) - ISNULL(T1.WaitCount,0.0)) = 0 THEN (ISNULL(T2.Resource_S,0.0) - ISNULL(T1.Resource_S,0.0))
501+
ELSE (ISNULL(T2.Resource_S,0.0) - ISNULL(T1.Resource_S,0.0)) / (ISNULL(T2.WaitCount,0.0) - ISNULL(T1.WaitCount,0.0))
502+
END,
503+
AvgSig_S = CASE WHEN (ISNULL(T2.WaitCount,0.0) - ISNULL(T1.WaitCount,0.0)) = 0 THEN (ISNULL(T2.Signal_S,0.0) - ISNULL(T1.Signal_S,0.0))
504+
ELSE (ISNULL(T2.Signal_S,0.0) - ISNULL(T1.Signal_S,0.0)) / (ISNULL(T2.WaitCount,0.0) - ISNULL(T1.WaitCount,0.0))
505+
END
506+
FROM T1 full outer join T2 on T2.WaitType = T1.WaitType
507+
) as waits
508+
WHERE 1 = 1
509+
AND CollectionTime_Duration_Seconds > 0.0
510+
--AND Wait_S >= 0.0
511+
)
512+
INSERT #Wait_Stats_Delta
513+
SELECT CollectionTime, CollectionTime_Duration_Seconds, WaitType, Wait_S, Resource_S, Signal_S, WaitCount,
514+
[Percentage] = (Wait_S*100.0)/Total_Wait_S,
515+
AvgWait_S, AvgRes_S, AvgSig_S
516+
FROM T_Waits_Delta as d
517+
JOIN (select sum(i.Wait_S) as Total_Wait_S from T_Waits_Delta as i) as t ON 1 = 1
518+
ORDER BY Wait_S DESC;
519+
520+
set @l_counter += 1;
521+
end
522+
523+
select CollectionTime as time, CollectionTime_Duration_Seconds as Duration_S, WaitType, Wait_S, Resource_S, Signal_S, WaitCount, [Percentage], AvgWait_S, AvgRes_S, AvgSig_S
524+
from #Wait_Stats_Delta;
525+
526+
527+
GO
528+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cls
2+
3+
$collector_root_directory = 'D:\MSSQL15.MSSQLSERVER\SQLWATCH';
4+
$data_collector_set_name = 'DBA';
5+
$data_collector_template_path = "$collector_root_directory\DBA_PerfMon_NonSQL_Collector_Template.xml";
6+
$log_file_path = "$collector_root_directory\$($env:COMPUTERNAME)__"
7+
$file_rotation_time = '04:00:00'
8+
$sample_interval = '00:00:05'
9+
$version_format = 'yyyyMMdd\-HHmmss'
10+
11+
logman import -name "$data_collector_set_name" -xml "$data_collector_template_path"
12+
logman update -name "$data_collector_set_name" -f bin -cnf "$file_rotation_time" -o "$log_file_path" -si "$sample_interval"
13+
logman start -name "$data_collector_set_name"
14+
15+
<#
16+
logman stop -name "$data_collector_set_name"
17+
logman delete -name "$data_collector_set_name"
18+
#>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /