未加星标

Simple Free Disk Space HTML report for SQL Server

字体大小 | |
[数据库(mssql) 所属分类 数据库(mssql) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
Problem

Automated SQL Server database monitoring is a very important task for the DBA, because information about the availability and health of the environment (instances availability, use of data files and transaction logs, resource consumption: CPU, disk and memory, etc.) is obtained when the professional is not in his or her working hours.

Obviously, this data is used to alert the administrator of a problem that is occurring or to prevent a problem that may occur. This tip will discuss the combination of T-SQL scripts that generate HTML reports for an environment analysis, SQL Server Agent to schedule this analysis at regular intervals, and Database Mail to send the generated report.

Solution

To resolve the issue, let’s take the following steps:

Step 1 - Profile configuration and account in Database Mail

You must first configureDatabase Mail.

When you setup Database Mail, you need to configure a Profile to be used for sending emails. The profile must have a name. After naming the profile, you must add a SMTP account with the authentication data. For this, it is necessary to know the data of the SMTP server: its name, port and if it requires SSL (i.e. a secure connection).

In the image below, the profile was configured with the name SQLAgentDBA and the SMTP account is already added:


Simple Free Disk Space HTML report for SQL Server

The SMTP account also requires a name. In this case, DBA - Monitoring.

We will also have the authentication settings: windows (credentials configured for the SQL Server service), basic and anonymous.

The Windows Authentication option causes Database Mail to use the user name and password that starts the SQL Server service. This user must have an associated e-mail address and the settings are set on the domain controller (Active Directory). In Basic authentication , the user name and password associated with the server is defined in the SMTP settings that are specified. And Anonymous authentication does not require a user name and password, but the SMTP server must be configured to not require authentication.
Simple Free Disk Space HTML report for SQL Server
Step 2 - SQL Server T-SQL Disk Space Monitoring Script

The following query will pull the free disk space information and below I explain how this was put together and how this is used.

We will initially create a query against tempdb.sys.tables to check if the temporary table named #Drives exists. If it exists, it is deleted and created with the drive, freespace, totalsize, and percentfree fields along with their respective data types. Remembering that the tempdb database stores the temporary tables and these are deleted when the connection is closed.

In the second block the information in the temporary table is inserted from the sys.dm_os_volume_stats DMF query. Calculation is done on the available_bytes and total_bytes columns to get the values in megabytes. Then, the HTML is assembled and treated within a SELECT statement with the CASE expression that defines the color of the fields in red when the free percentage is less than or equal to 15% of the total space of each unit.

In the sequence, the sp_monitor_disk procedure was created within the master system database, where we have the following parameters: @to variable will receive the recipient's email, the variable @subject will be responsible for the subject of the email and @PercentFree will determine how the report returns the data based on the actual percentage free values compared to this value.

USE [master]
GO
CREATE PROCEDURE [dbo].[sp_monitor_disk_free_space]
@to varchar(200),
@subject varchar(100),
@PercentFree int
AS
SET NOCOUNT ON
-- Create the temp table #Drives
IF EXISTS ( SELECT nameFROM tempdb.sys.tablesWHERE name LIKE '#Drives%' )
DROP TABLE #Drives
CREATE TABLE #Drives
(
Drive CHAR(3) PRIMARY KEY ,
FreeSpace INT NULL ,
TotalSize INT NULL ,
[PercentFree] AS ( CONVERT(FLOAT, FreeSpace) / TotalSize ) * 100
)
-- Insert the info on the table by DMF sys.dm_os_volume_stats
INSERT #Drives
( Drive ,
FreeSpace ,
TotalSize
)
SELECT DISTINCT dovs.volume_mount_point , CONVERT(INT, dovs.available_bytes / 1048576.0) , CONVERT(INT, dovs.total_bytes / 1048576.0)
FROM sys.master_files mf CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.file_id) dovs
ORDER BY dovs.volume_mount_point ASC
-- Variables for mail send
DECLARE @tableHTML NVARCHAR(MAX)
-- Creates the HTML to the report sending with the total free space info
SET @tableHTML = N'<H1>Disk usage</H1>' + N'<table border="1">'
+ N'<tr><th>Drive</th><th>Total (MB)</th><th>Free (MB)</th><th>% Free</th></tr>'
+ CAST(( SELECT CASE WHEN [PercentFree] <= @PercentFree THEN '#FF0000' END AS 'td/@BGCOLOR' , td = Drive , '' , 'right' AS 'td/@align' , CASE WHEN [PercentFree] <= @PercentFree THEN '#FF0000' END AS 'td/@BGCOLOR' , td = TotalSize , '' , 'right' AS 'td/@align' , CASE WHEN [PercentFree] <= @PercentFree THEN '#FF0000' END AS 'td/@BGCOLOR' , td = FreeSpace , '' , 'right' AS 'td/@align' , CASE WHEN [PercentFree] <= @PercentFree THEN '#FF0000' END AS 'td/@BGCOLOR' , td = CONVERT (NUMERIC(10, 2), [PercentFree]) FROM #Drives
FOR XML PATH('tr') , TYPE
) AS NVARCHAR(MAX)) + N'</table>';
-- Calls the procedure sp_send_dbmail to send the email using the SQLAgentDBA profile created on
SET @Subject = @Subject + @@Servername
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'SQLAgentDBA',
@recipients = @to, @body = @tableHTML,
@subject = @Subject, @body_format = 'HTML'
RETURN 0
GO Step 3 - Create a New SQL Server Agent Job

In SQL Server Agent, a new job must be created. In this example, the DBA - Disk Space job is created .


Simple Free Disk Space HTML report for SQL Server

In the Steps tab we will add a new step that will receive the script.

The type, in this case, must be a T-SQL script and the database must be set to the master.

In the Schedules tab, the scheduling must be configured according to each environment. In our case, the job is recurring and it will run every day, every hour between 00:00 to 23:59. You can configure as needed in your environment.


Simple Free Disk Space HTML report for SQL Server

That's all there is to it. This is a pretty simple and straightforward way to automate an email to alert you about available disk space.

Next Steps Make sure database mail is enabled on the SQL Server where this script is executed. Check out some other related content: Troubleshoot SQL Server Ag

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

tags: lt,gt,SQL,PercentFree,td,will,name,In,Server,must
分页:12
转载请注明
本文标题:Simple Free Disk Space HTML report for SQL Server
本站链接:https://www.codesec.net/view/586925.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mssql) | 评论(0) | 阅读(61)