forked from nate-hughes/dba-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3884978
commit 2e30a30
Showing
171 changed files
with
12,598 additions
and
5,369 deletions.
There are no files selected for viewing
58 changes: 29 additions & 29 deletions
58
server-config/config-enable-DAC.sql → DAC-config-enable.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
Use master | ||
GO | ||
/* 0 = Allow Local Connection, 1 = Allow Remote Connections*/ | ||
sp_configure 'remote admin connections', 1 | ||
GO | ||
RECONFIGURE | ||
GO | ||
|
||
/* | ||
-- Using DAC with SQLCMD | ||
SQLCMD –S [SQL Server Name] –U [User Name] –P [Password] –A | ||
-- SSMS | ||
-- specify “ADMIN:” before the SQL Server Instance name | ||
*/ | ||
|
||
/* Common DAC requests */ | ||
-- Active Locks | ||
SELECT * FROM sys.dm_tran_locks | ||
GO | ||
-- Cache Status | ||
SELECT * FROM sys.dm_os_memory_cache_counters | ||
GO | ||
-- Active Sessions | ||
SELECT * FROM sys.dm_exec_sessions | ||
GO | ||
-- Requests Status | ||
SELECT * FROM sys.dm_exec_requests | ||
GO | ||
Use master | ||
GO | ||
/* 0 = Allow Local Connection, 1 = Allow Remote Connections*/ | ||
sp_configure 'remote admin connections', 1 | ||
GO | ||
RECONFIGURE | ||
GO | ||
|
||
/* | ||
-- Using DAC with SQLCMD | ||
SQLCMD –S [SQL Server Name] –U [User Name] –P [Password] –A | ||
-- SSMS | ||
-- specify “ADMIN:” before the SQL Server Instance name | ||
*/ | ||
|
||
/* Common DAC requests */ | ||
-- Active Locks | ||
SELECT * FROM sys.dm_tran_locks | ||
GO | ||
-- Cache Status | ||
SELECT * FROM sys.dm_os_memory_cache_counters | ||
GO | ||
-- Active Sessions | ||
SELECT * FROM sys.dm_exec_sessions | ||
GO | ||
-- Requests Status | ||
SELECT * FROM sys.dm_exec_requests | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Who's Using the DAC? | ||
http://www.sqlservercentral.com/articles/Dedicated+Administrator+Connection+(DAC)/174694/ | ||
Who’s Been Sleeping in My DAC? How to Tell Who’s using the Dedicated Admin Connection. | ||
https://www.brentozar.com/archive/2011/08/dedicated-admin-connection-why-want-when-need-how-tell-whos-using/ | ||
*/ | ||
|
||
-- confirm DAC endpoint exists | ||
select * from sys.endpoints where name = 'Dedicated Admin Connection'; | ||
|
||
-- see who's using it | ||
SELECT CASE WHEN s.session_id= @@SPID THEN 'It''s me! ' | ||
ELSE '' | ||
END + coalesce(s.login_name,'???') as WhosGotTheDAC , | ||
s.session_id , | ||
s.login_name , | ||
s.nt_domain , | ||
s.nt_user_name , | ||
s.login_time , | ||
s.host_name , | ||
s.program_name , | ||
s.status , | ||
s.original_login_name | ||
FROM sys.dm_exec_sessions s | ||
INNER JOIN sys.endpoints e ON e.endpoint_id = s.endpoint_id | ||
WHERE e.name='Dedicated Admin Connection'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
DECLARE @SQL NVARCHAR(4000) | ||
,@DefaultBackup NVARCHAR(512); | ||
|
||
EXEC master.dbo.xp_instance_regread | ||
@rootkey='HKEY_LOCAL_MACHINE', | ||
@key='SOFTWARE\Microsoft\MSSQLServer\MSSQLServer', | ||
@value_name='BackupDirectory', | ||
@value=@DefaultBackup OUTPUT; | ||
|
||
SELECT 'EXECUTE [dbo].[DatabaseBackup] | ||
@Databases = ''' + name + ''', | ||
@Directory = ''' + @DefaultBackup + ''', | ||
@BackupType = ''FULL'', | ||
@Compress = ''Y'', | ||
@LogToTable = ''Y'';' | ||
FROM sys.databases | ||
WHERE database_id > 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
DECLARE @ClusterName VARCHAR(128); | ||
|
||
SELECT @ClusterName = cluster_name | ||
FROM master.sys.dm_hadr_cluster | ||
WHERE LEFT(cluster_name, 1) LIKE '[a-z]'; | ||
|
||
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'DBMaint') | ||
IF EXISTS (SELECT 1 FROM DBMaint.sys.tables WHERE name = 'CommandLog') | ||
SELECT @ClusterName AS clustername | ||
,@@SERVERNAME AS ServerName | ||
,DEFAULT_DOMAIN() AS Domain | ||
,DatabaseName | ||
,SchemaName | ||
,ObjectName | ||
,ObjectType | ||
,IndexName | ||
,IndexType | ||
,PartitionNumber | ||
,ExtendedInfo.value('(/ExtendedInfo/PageCount)[1]', 'int') AS PageCount | ||
,ExtendedInfo.value('(/ExtendedInfo/Fragmentation)[1]', 'numeric(9,4)') AS Fragmentation | ||
,TRY_CONVERT(VARCHAR(2000),Command) AS Command | ||
,CommandType | ||
,StartTime | ||
,EndTime | ||
,ErrorNumber | ||
,TRY_CONVERT(VARCHAR(2000),ErrorMessage) AS ErrorMessage | ||
FROM dbo.CommandLog | ||
WHERE CommandType IN ('ALTER_INDEX', 'DBCC_CHECKDB', 'BACKUP_DATABASE') | ||
AND EndTime IS NOT NULL; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
-- RUN ON PRIMARY -- | ||
|
||
DECLARE @IsHadrEnabled TINYINT = CONVERT(TINYINT,SERVERPROPERTY ('IsHadrEnabled')) | ||
,@ServerName NVARCHAR(256) = @@SERVERNAME | ||
,@RoleDesc NVARCHAR(60) = 'PRIMARY' | ||
,@AG_SQL NVARCHAR(MAX) | ||
,@Version VARCHAR(50) = LEFT(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(128)),2); | ||
|
||
DECLARE @AGs TABLE ( | ||
domain VARCHAR(128) NOT NULL | ||
,primaryreplicaservername VARCHAR(128) NOT NULL | ||
,availabilitygroupname VARCHAR(128) NOT NULL | ||
,failure_condition_level INT NOT NULL | ||
,health_check_timeout INT NOT NULL | ||
,automated_backup_preference TINYINT NOT NULL | ||
,listener_name VARCHAR(63) NULL | ||
,listener_port INT NULL | ||
,listener_ip VARCHAR(200) NULL | ||
,dtc_support BIT NULL | ||
,db_failover BIT NULL | ||
); | ||
|
||
IF (@IsHadrEnabled = 1) | ||
AND EXISTS ( | ||
SELECT 1 | ||
FROM sys.dm_hadr_availability_replica_states AS a | ||
JOIN sys.availability_replicas AS b | ||
ON b.replica_id = a.replica_id | ||
WHERE b.replica_server_name = @ServerName | ||
AND a.role_desc = @RoleDesc | ||
) | ||
BEGIN | ||
SELECT * | ||
INTO #availability_groups | ||
FROM sys.availability_groups; | ||
|
||
SELECT group_id, dns_name, port, ip_configuration_string_from_cluster | ||
INTO #availability_group_listeners | ||
FROM sys.availability_group_listeners; | ||
|
||
SET @AG_SQL = ' | ||
SELECT DEFAULT_DOMAIN() as domain | ||
,''' + @ServerName + ''' as primaryreplicaservername | ||
,ag.name | ||
,ag.failure_condition_level | ||
,ag.health_check_timeout | ||
,ag.automated_backup_preference | ||
,l.dns_name | ||
,l.port | ||
,l.ip_configuration_string_from_cluster' | ||
+ CASE WHEN @Version > 11 THEN ' ,ag.dtc_support' ELSE ' ,NULL' END | ||
+ CASE WHEN @Version > 11 THEN ' ,ag.db_failover' ELSE ' ,NULL' END + ' | ||
FROM #availability_groups ag | ||
JOIN #availability_group_listeners l ON ag.group_id = l.group_id' | ||
|
||
INSERT @AGs ( | ||
domain | ||
,primaryreplicaservername | ||
,availabilitygroupname | ||
,failure_condition_level | ||
,health_check_timeout | ||
,automated_backup_preference | ||
,listener_name | ||
,listener_port | ||
,listener_ip | ||
,dtc_support | ||
,db_failover | ||
) | ||
EXEC sp_executesql @AG_SQL; | ||
END; | ||
|
||
SELECT * FROM @AGs; | ||
|
||
IF OBJECT_ID('tempdb..#availability_groups') IS NOT NULL | ||
DROP TABLE #availability_groups; | ||
|
||
IF OBJECT_ID('tempdb..#availability_group_listeners') IS NOT NULL | ||
DROP TABLE #availability_group_listeners; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
|
||
-- RUN ON PRIMARY -- | ||
|
||
DECLARE @IsHadrEnabled TINYINT = CONVERT(TINYINT,SERVERPROPERTY ('IsHadrEnabled')) | ||
,@ServerName NVARCHAR(256) = @@SERVERNAME | ||
,@RoleDesc NVARCHAR(60) = 'PRIMARY' | ||
,@AG_SQL NVARCHAR(MAX) | ||
,@Version VARCHAR(50) = LEFT(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(128)),2); | ||
|
||
DECLARE @AGs TABLE ( | ||
domain VARCHAR(128) NOT NULL | ||
,availabilitygroupname VARCHAR(128) NOT NULL | ||
,replicaservername VARCHAR(128) NOT NULL | ||
,endpoint_url VARCHAR(128) NULL | ||
,availability_mode TINYINT NOT NULL | ||
,failover_mode TINYINT NOT NULL | ||
,session_timeout INT NOT NULL | ||
,primary_role_allow_connections TINYINT NOT NULL | ||
,secondary_role_allow_connections TINYINT NOT NULL | ||
,backup_priority INT NOT NULL | ||
,read_only_routing_url VARCHAR(256) NULL | ||
,seeding_mode TINYINT NULL | ||
,read_only_routing_lists VARCHAR(1000) NULL | ||
); | ||
|
||
IF (@IsHadrEnabled = 1) | ||
AND EXISTS ( | ||
SELECT 1 | ||
FROM sys.dm_hadr_availability_replica_states AS a | ||
JOIN sys.availability_replicas AS b | ||
ON b.replica_id = a.replica_id | ||
WHERE b.replica_server_name = @ServerName | ||
AND a.role_desc = @RoleDesc | ||
) | ||
BEGIN | ||
SELECT * | ||
INTO #availability_read_only_routing_lists | ||
FROM sys.availability_read_only_routing_lists; | ||
|
||
SELECT * | ||
INTO #availability_replicas | ||
FROM sys.availability_replicas; | ||
|
||
SELECT group_id, name | ||
INTO #availability_groups | ||
FROM sys.availability_groups; | ||
|
||
SET @AG_SQL = ' | ||
WITH read_only_routing_lists AS ( | ||
SELECT r.replica_id | ||
,STUFF(( | ||
SELECT '','' + ror.replica_server_name | ||
FROM #availability_read_only_routing_lists l | ||
JOIN #availability_replicas ror ON l.read_only_replica_id = ror.replica_id | ||
WHERE r.replica_id = l.replica_id | ||
ORDER BY l.routing_priority | ||
FOR XML PATH(''''),TYPE).value(''.'',''VARCHAR(MAX)''),1,1,'''') AS read_only_replicas | ||
FROM #availability_replicas r | ||
) | ||
SELECT DEFAULT_DOMAIN() as domain | ||
,ag.name | ||
,r.replica_server_name | ||
,r.endpoint_url | ||
,r.availability_mode | ||
,r.failover_mode | ||
,r.session_timeout | ||
,r.primary_role_allow_connections | ||
,r.secondary_role_allow_connections | ||
,r.backup_priority | ||
,r.read_only_routing_url' | ||
+ CASE WHEN @Version > 11 THEN ' ,r.seeding_mode' ELSE ' ,NULL' END + ' | ||
,ro.read_only_replicas | ||
FROM #availability_groups ag | ||
JOIN #availability_replicas r on ag.group_id = r.group_id | ||
LEFT JOIN read_only_routing_lists ro ON r.replica_id = ro.replica_id' | ||
|
||
INSERT @AGs ( | ||
domain | ||
,availabilitygroupname | ||
,replicaservername | ||
,endpoint_url | ||
,availability_mode | ||
,failover_mode | ||
,session_timeout | ||
,primary_role_allow_connections | ||
,secondary_role_allow_connections | ||
,backup_priority | ||
,read_only_routing_url | ||
,seeding_mode | ||
,read_only_routing_lists | ||
) | ||
EXEC sp_executesql @AG_SQL; | ||
END; | ||
|
||
SELECT * FROM @AGs; | ||
|
||
IF OBJECT_ID('tempdb..#availability_read_only_routing_lists') IS NOT NULL | ||
DROP TABLE #availability_read_only_routing_lists; | ||
|
||
IF OBJECT_ID('tempdb..#availability_replicas') IS NOT NULL | ||
DROP TABLE #availability_replicas; | ||
|
||
IF OBJECT_ID('tempdb..#availability_groups') IS NOT NULL | ||
DROP TABLE #availability_groups; |
Oops, something went wrong.