Following the instructions of both AMtwo and Tibor Karaszi I decided to create an Agent job that verifies if a threshold was crossed and, if yes, would report the top 10 apps consuming the most connections. I can get the info I need with this:
--The number of connections to be considered abnormal for your environment.
--It should be bellow SQL Server max supported 32,767 user connections
DECLARE @ConnectionThreshold int = 1000;
IF @ConnectionThreshold < (SELECT COUNT(*) FROM sys.dm_exec_connections)
--List the top 10 apps using connections
SELECT TOP(10)
s.login_name AS [Login name],
s.host_name AS [Host name],
d.name AS [Database name],
s.program_name AS [Program name],
COUNT(*) AS [Connections]
FROM sys.dm_exec_connections AS c
INNER JOIN sys.dm_exec_sessions AS s
ON c.session_id = s.session_id
INNER JOIN sys.databases AS d
ON s.database_id = d.database_id
GROUP BY s.login_name,
s.host_name,
d.name,
s.program_name
ORDER BY COUNT(*) DESC;
To generate a more informative and well formatted email I borrowed some ideas (not to say most of the code) from AMtwo's Alerting on SQL Server Blocking and the result was this:
--The number of connections to be considered abnormal for the environment.
--It should be bellow SQL Server max supported 32,767 user connections
DECLARE @ConnectionThreshold int = 1000;
IF @ConnectionThreshold < (SELECT COUNT(*) FROM sys.dm_exec_connections)
BEGIN
DECLARE @EmailBody nvarchar(4000);
SELECT @EmailBody = dbo.EmailCSS_Get();
SELECT @EmailBody = @EmailBody + N'<h2>Top 10 apps connected:</h2>' + CHAR(10) +
N'<table><tr>' +
N'<th>Login name</th>' +
N'<th>Host name</th>' +
N'<th>Database name</th>' +
N'<th>Program name</th>' +
N'<th>Connections</th>' +
N'</tr>' +
CAST(( SELECT TOP(10)
td = s.login_name, '',
td = s.host_name, '',
td = d.name, '',
td = s.program_name, '',
td = COUNT(*), ''
FROM sys.dm_exec_connections AS c
INNER JOIN sys.dm_exec_sessions AS s
ON c.session_id = s.session_id
INNER JOIN sys.databases AS d
ON s.database_id = d.database_id
GROUP BY s.login_name,
s.host_name,
d.name,
s.program_name
ORDER BY COUNT(*) DESC
FOR XML PATH ('tr'), ELEMENTS
) AS nvarchar(max)) +
N'</table>';
SELECT @EmailBody = @EmailBody + '<hr>' + dbo.EmailServerInfo_Get();
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'YourMailProfile',
@recipients = 'yourEmail@job.com',
@subject = 'Number of connections above defined threshold',
@body = @EmailBody,
@body_format = 'HTML';
END
This code was added to a job that runs at some interval and sends the alert in a formatted email if needed. It did the trick.
Referenced content:
dbo.EmailCSS_Get()
dbo.EmailServerInfo_Get()