DBA_Blocking_Alerts script

 Please find the below mentioned script help you get an alert..


SET NOCOUNT ON

DECLARE @xml nvarchar(max)

SELECT @xml = Cast((SELECT HOST_NAME() AS 'td','',b.session_id AS 'td',

'',

(b.wait_duration_ms/1000)/60 AS 'td',

'',

b.wait_type AS 'td',

'',

b.blocking_session_id AS 'td',

'',

t.text AS 'td'

FROM sys.dm_os_waiting_tasks b inner join sys.dm_exec_requests r on r.session_id= b.session_id 

OUTER APPLY 

sys.dm_exec_sql_text(sql_handle) t 

WHERE b.blocking_session_id <> 0 and b.wait_duration_ms>300000

FOR xml path('tr'), elements) AS NVARCHAR(max))

Declare @body nvarchar(max)

SET @body =

'<html>

 <head>

 <style>

 table, th, td 

 {

 border: 1px solid black;

 border-collapse: collapse;

 text-align: center;

 }

 </style>

 </head>

 <body>

 <H2>

 Blocking queries

 </H2>

 <table> 

 <tr>

 <th>Instance Name</th><th> Blocked Session ID </th> <th> Wating in minutes </th> <th> Wait type</th> 

 <th> Blocking Session ID</th><th>Query waiting to execute</th>

 </tr>'

 SET @body = @body + @xml + '

 </table>

 </body>

</html>'

if(@xml is not null)

BEGIN

EXEC msdb.dbo.Sp_send_dbmail

@profile_name = 'dbmail',

@body = @body,

@body_format ='html',

@recipients = 'ajay@sqldbanow.com',

@copy_recipients ='bandarucreations@sqldbanow.com', -- list of Email recipients

@subject = 'Blocking queries Alert-SQLDBANOW01';

END

SET NOCOUNT OFF

Sql Server : A connection was successfully established with the server

Error Message:

A connection was successfully established with the server, but then an error occurred during the 

login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority 

that is not trusted.) (Microsoft SQL Server, Error: -2146893019)

The certificate chain was issued by an authority that is not trusted



Resolution: 

Go to “Connect to Server” pop-up window >> Options >> Check “Trust server certificate“.


Once selected, SSMS will accept the remote certificate and complete connection.

Hope this helps,

Length specified in network packet payload did not match number of bytes read; the connection has been closed. Please contact the vendor of the client library.

 Here are few more causes based on my search on the internet:

Network team doing Port Scanning

Sometimes Antivirus programs do check the port.

MSSQL server is under a DDoS

You may want to look at the error message and check the IP of the machine and see what that is.

Step 1:

First we need to figure out where is this database connection request coming from. Use DNS look up (nslookup a.b.c.d) command to identify the machine that is sending this connection.

Step 2:

Similarly, use Network command netstat -anp to uncover all the connections coming into the database server; Then filter them down to this particular IP (a.b.c.d) and see what port are they originating from.

Step 3:

Now go to that remote machine and run similar netstat -anp command on it. Now, compare and confirm that the PID for this process.

Step 4:

With the above two steps, you could find out exactly what software in that remote machine is trying to connect to the database server.

Now that you have the exact PID (ProcessID) on the remote machine, go to Task Manager and check under Processes tab to see the originating software name.

Step 5:

See if using Sql Authentication could help in allowing that remote machine to connect successfully (if its a valid login request).

There are some other unconventional approaches to digging in further, but the above steps should be sufficient in troubleshooting.

DDL CREATE_DATABASE EVENT alert via mail

 USE [master]

GO

/****** Object:  DdlTrigger [DDL_CREATE_DATABASE_EVENT]    Script Date: 12/4/2020 3:58:37 AM ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TRIGGER [DDL_CREATE_DATABASE_EVENT]

ON ALL SERVER

FOR CREATE_DATABASE

AS

DECLARE @bd varchar(max)

Declare @tsql varchar(max)

Set @tsql = EVENTDATA().value

        ('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','varchar(max)')

SET @bd = 'UserName: ' + UPPER(SUSER_NAME()) + '

         ServerName: ' + @@SERVERNAME + '

         Time: '   + CONVERT(varchar(25),Getdate()) + '

         HostName: ' + HOST_NAME() + '

         Database: ' + db_name() + '


         T-SQL: ' +  @tsql


BEGIN


PRINT 'Make sure you have informed all DBAs before creating databases. This event has been logged'


EXEC msdb.dbo.sp_send_dbmail


                      @profile_name = 'SQLMail',


                      @recipients = 'bajeyudu@sqldbanow.com',


                      @subject = 'A new database has been created!',


                      @body_format = 'HTML',


        @importance = 'High',


                      @body = @bd


END

Using FILEPROPERTY to check for free space in a database

--Using FILEPROPERTY to check for free space in a database

SELECT DB_NAME() AS DbName,
name AS FileName,
size/128.0 AS CurrentSizeMB,
size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB
FROM sys.database_files;