Monitoring SQL Server database status changes using T SQL and PowerShell scripts

Monitoring SQL Server database status changes using T SQL and PowerShell scripts

Monitoring SQL Server database status changes using T-SQL and PowerShell scripts

SQLShack

SQL Server training Español

Monitoring SQL Server database status changes using T-SQL and PowerShell scripts

October 17, 2017 by Prashanth Jayaram Monitoring a SQL Server database is a critical component of database administration. Ninety percent of the organizations expect the number of databases to increase over the next twelve months. An increase in data volumes can have negative effects on the availability of databases. Hence, SQL Server database monitoring is considered a critical responsibility of a database administrator. Organizations tend to spend a lot of their funds towards enterprise solutions. And due to the sensitive and growing nature of business and user needs, application availability is very important nowadays. It’s also equally important to get alerted or notified when a database is inaccessible, due to any of the myriad of reasons we can think of. This article deals with one of the solutions to monitor the status of the SQL Server database using T-SQL or PowerShell with the help of the native programming and alerting techniques. Some of us may have many questions regarding this: How do we decide on the monitoring strategies for different environments? How do we automate and manage SQL Server databases? Can the monitoring task be automated using T-SQL or PowerShell or any other tools or techniques? Does the SQL engine provide the required capabilities to schedule a job and run it across multiple servers? What are the customization options available? Do we have a robust method to perform the backup activity? The monitoring techniques using T-SQL, XML, and PowerShell are used to monitor a SQL Server database using native methods are discussed in this article. Topics covered in this resource include: Discuss the implementation architecture XML string manipulation Prepare the SQL and PowerShell scripts Create SQL Jobs And more…

Data Flow Diagram

Let’s look at the high-level view of this implementation: A dedicated SQL Instance has the SQL Server DB Mail configured In the dedicated instance, a SQL Server agent job is used to schedule SQL Jobs It contains the stored procedure to perform XML data transformation. Also, it takes care of sending the email to intended recipients, using SMTP The SQL file that contains the code to be executed on the listed SQL instance is placed in a shared path. The path should be accessible from all the SQL instances. The instance details are the source for this implementation. The server details are stored in a text file. On the dedicated instance, the SQL job is scheduled to traverse across the entire list of servers. The SQL file is copied to a shared path and is executed on each server to generate the XML data for only the offline databases. The generated XML data is then fed as an input parameter to the stored procedure for further data manipulation. In the Stored Procedure, using dynamic SQL, an HTML is prepared and is sent as an email to the DBA’s. The implementation can also be done using JSON transformation. JSON is supported starting from SQL 2016. Since JSON penetration is not good enough at the time, XML is the preferred choice for data interchange format for now—until JSON takes over as the de-facto format.

Prepare the script

Let’s now go through the steps to create the script. It’s good to have an understanding of the database states, to understand the script. 1234567891011121314151617 select a.name, a.state_desc, case When a.state = 0 then 'ONLINE' when a.state = 1 then 'RESTORING' when a.state = 2 then 'RECOVERING' when a.state = 3 then 'RECOVERY_PENDING' when a.state = 4 then 'SUSPECT' when a.state = 5 then 'EMERGENCY' when a.state = 6 then 'OFFLINE' when a.state = 7 then 'COPYING - SQL AZURE' when a.state = 10 then 'OFLINE_SECONDARY - SQL AZURE' end from sys.databases a
Step 1: Prepare XML Data The XML data is generated by querying the sys.databases system object for offline databases. The query results are then transformed as nested XML elements. The simple AUTO option is very useful in generating the raw data. The below T-SQL statements are placed in an SQL file. This file will be executed across all the SQL instances and the output is then fed to the stored procedure. 1234567891011121314151617181920212223242526272829303132333435 --declare table variable to hold multiple resultsetDECLARE @T table (X nvarchar(MAX));--Variable declaration to hold XML data DECLARE @XML nvarchar(MAX)DECLARE @XML_content xml --Declare table variable DECLARE @DatabaseState table (DBName varchar(100),DBState varchar(50))INSERT INTO @DatabaseState select name, state_desc from sys.databases IF (SELECT COUNT(*) FROM @DatabaseState WHERE DBState <> 'Online') > 0 BEGIN WITH SQLShackDemoCTE AS ( select @@servername ServerName,DBName, DBState FROM @DatabaseState status where DBState <> 'Online' ) insert into @T(X) select ( select * from SQLShackDemoCTE FOR XML AUTO ); select @xml=X from @TENDSET @xml = N''+@XML+''--print @xmlSET @XML_content=@XMLSELECT @XML_content
When you click the above XML tag, the data shown like below Step 2: Create Stored Procedure USP_InsertDBStatus The XML raw output from the SQL File is fed as an input parameter to the stored procedure. This stored procedure has two sections: Preparing the XML The XML raw-data is processed using the OPENXML clause. The OPENXML clause uses xml-document ID and XML text as inputs and uses the MSXL parser to parse the text and return each element for further transformation or data manipulation. Preparing the Email The FOR XML PATH clause is used to generate an HTML table of data. The XML tags are then transformed as SQL columns. The data is then parsed as valid HTML tags. Using Database Mail, an email is sent to intended recipients with the data. Note: The same output can also derived by using the concept of XML nodes for efficient data processing using the OPENXML clause. 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 CREATE PROCEDURE [dbo].[USP_InsertDBStatus]( @XmlData XML )ASBEGIN SET NOCOUNT ON; DECLARE @hdoc AS INT;declare @State varchar(10)declare @Database_Name varchar(100)declare @Server_Name varchar(100)declare @EmailBody varchar(100)declare @DBName varchar(max)declare @Html nvarchar(max)set @State = '' declare @DBStatusCheck table( [Server] varchar(25) NOT NULL, [DBStatusID] INT IDENTITY(1, 1) NOT NULL ,[StatusCheckDateTime] DATETIME NOT NULL ,[DBName] VARCHAR(128) NULL ,[DBState] VARCHAR(128) NULL) --Create an internal representation of the XML document. EXECUTE [dbo].[sp_xml_preparedocument] @hdoc OUTPUT, @XmlData; INSERT INTO @DBStatusCheck ( [Server] ,[StatusCheckDateTime] ,[DBName] ,[DBState] ) SELECT [ServerName]=DBstatus.[Servername] ,[CheckDate] = CURRENT_TIMESTAMP ,[DBName] = DBstatus.[DBName] ,[DBState] = DBstatus.[DBState] FROM OPENXML(@hdoc,'//SQLShackDemoCTE')WITH (ServerName varchar(100), DBName VARCHAR(130), DBState VARCHAR(130)) AS DBstatus; EXECUTE [dbo].[sp_xml_removedocument] @hdoc; select @Server_Name= server from @DBStatusCheck where [DBStatusID]=1 if (select COUNT(*) from @DBStatusCheck) > 0 begin SET @HTML = N'

Databases not online on '+@Server_Name+'

' + N'' + N'' + CAST ( ( SELECT td= Server, '', td = DBName, '', td = DBState, '', td=StatusCheckDateTime, '' FROM @DBStatusCheck FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) + N'
Server Name Database Name State StatusCheck DateTime
' ; EXEC msdb.dbo.sp_send_dbmail @profile_name = 'PowerSQL', @recipients = '[email protected]', @body = @Html, @body_format = 'HTML', @subject = 'HIGH Alert : Database Not Online';EndEND;

Define the SQL Server Agent Job

Here are some of the points we need to keep in mind: The input SQL file is placed on the shared path so that Invoke-SQLcmd can be executed to generate XML raw-data The PowerShell script is used to call the stored procedures which have already been created in a dedicated SQL instance. Save the below PowerShell script as DBstatus.PS1 1234567891011121314151617181920 Import-csv \\hq6021\C$\InputServer.csv%{If ((Test-Connection $_.ServerName -count 1 -quiet)) { write-host $_.Servername $sqlcmd=Invoke-Sqlcmd -InputFile \\hqdbt01\F$\PowerSQL\DBStatusSQLQuery.sql -ServerInstance $_.Servername -Database master $xmldata=$sqlcmd.Column1 if($xmldata -ne '') { $SqlConnection = New-Object System.Data.SqlClient.SqlConnection; $SqlConnection.ConnectionString = "Server=HQDBT01;Database=SQLShackDemo;Integrated Security=TRUE;"; $SqlConnection.Open(); $SqlCommand = New-Object System.Data.SqlClient.SqlCommand; $SqlCommand.CommandTimeout = 120; $SqlCommand.Connection = $SqlConnection; $SqlCommand.CommandText = "EXECUTE [dbo].[USP_InsertDBStatus] @XmlData = N'$XmlData';"; $Result = $SqlCommand.ExecuteNonQuery(); $SqlConnection.Close(); } Create the SQL Job Follow the job creation process steps to create the SQL job. Enter the Step name and choose Transact-SQL Script (T-SQL) from the Type dropdown In the Command, call the PowerShell function Click OK The aforementioned steps result in a Multi-Server-DBStatus-Check-Alert job. Right-click and run the job. You can also schedule it to run across all the instances, at a ten-minute frequency. The output reveals shows the statuses of the databases.

Wrap Up

In an environment that relies on SQL-managed native methodologies, we could use PowerShell scripts using SMO or T-SQL. Using native methods, sometimes it’s tedious to measure every aspect of the SQL Server database status monitoring. The native methods have a few limitations, which would have to be kept in mind when proceeding. In some cases, this leads to substantial reliance on scripting, which may not be every administrator’s cup of tea. However, there are several third party monitoring solutions available in today’s market, which integrate alerts to ticketing system or send email notifications when the state of a database is not online. It really is about what suits our requirements. We must judge the environment, and work through it. I hope the script in the article helps towards getting an availability report about the managed databases.
Author Recent Posts Prashanth JayaramI’m a Database technologist having 11+ years of rich, hands-on experience on Database technologies. I am Microsoft Certified Professional and backed with a Degree in Master of Computer Application.

My specialty lies in designing & implementing High availability solutions and cross-platform DB Migration. The technologies currently working on are SQL Server, PowerShell, Oracle and MongoDB.

View all posts by Prashanth Jayaram Latest posts by Prashanth Jayaram (see all) Stairway to SQL essentials - April 7, 2021 A quick overview of database audit in SQL - January 28, 2021 How to set up Azure Data Sync between Azure SQL databases and on-premises SQL Server - January 20, 2021

Related posts

What is causing database slowdowns? How to analyze SQL Server database performance using T-SQL Top SQL Server Books Reporting and alerting on job failure in SQL Server Removing the risk from important maintenance tasks in SQL Server 14,597 Views

Follow us

Popular

SQL Convert Date functions and formats SQL Variables: Basics and usage SQL PARTITION BY Clause overview Different ways to SQL delete duplicate rows from a SQL Table How to UPDATE from a SELECT statement in SQL Server SQL Server functions for converting a String to a Date SELECT INTO TEMP TABLE statement in SQL Server SQL WHILE loop with simple examples How to backup and restore MySQL databases using the mysqldump command CASE statement in SQL Overview of SQL RANK functions Understanding the SQL MERGE statement INSERT INTO SELECT statement overview and examples SQL multiple joins for beginners with examples Understanding the SQL Decimal data type DELETE CASCADE and UPDATE CASCADE in SQL Server foreign key SQL Not Equal Operator introduction and examples SQL CROSS JOIN with examples The Table Variable in SQL Server SQL Server table hints – WITH (NOLOCK) best practices

Trending

SQL Server Transaction Log Backup, Truncate and Shrink Operations Six different methods to copy tables between databases in SQL Server How to implement error handling in SQL Server Working with the SQL Server command line (sqlcmd) Methods to avoid the SQL divide by zero error Query optimization techniques in SQL Server: tips and tricks How to create and configure a linked server in SQL Server Management Studio SQL replace: How to replace ASCII special characters in SQL Server How to identify slow running queries in SQL Server SQL varchar data type deep dive How to implement array-like functionality in SQL Server All about locking in SQL Server SQL Server stored procedures for beginners Database table partitioning in SQL Server How to drop temp tables in SQL Server How to determine free space and file size for SQL Server databases Using PowerShell to split a string into an array KILL SPID command in SQL Server How to install SQL Server Express edition SQL Union overview, usage and examples

Solutions

Read a SQL Server transaction logSQL Server database auditing techniquesHow to recover SQL Server data from accidental UPDATE and DELETE operationsHow to quickly search for SQL database data and objectsSynchronize SQL Server databases in different remote sourcesRecover SQL data from a dropped table without backupsHow to restore specific table(s) from a SQL Server database backupRecover deleted SQL data from transaction logsHow to recover SQL Server data from accidental updates without backupsAutomatically compare and synchronize SQL Server dataOpen LDF file and view LDF file contentQuickly convert SQL code to language-specific client codeHow to recover a single table from a SQL Server database backupRecover data lost due to a TRUNCATE operation without backupsHow to recover SQL Server data from accidental DELETE, TRUNCATE and DROP operationsReverting your SQL Server database back to a specific point in timeHow to create SSIS package documentationMigrate a SQL Server database to a newer version of SQL ServerHow to restore a SQL Server database backup to an older version of SQL Server

Categories and tips

►Auditing and compliance (50) Auditing (40) Data classification (1) Data masking (9) Azure (295) Azure Data Studio (46) Backup and restore (108) ►Business Intelligence (482) Analysis Services (SSAS) (47) Biml (10) Data Mining (14) Data Quality Services (4) Data Tools (SSDT) (13) Data Warehouse (16) Excel (20) General (39) Integration Services (SSIS) (125) Master Data Services (6) OLAP cube (15) PowerBI (95) Reporting Services (SSRS) (67) Data science (21) ►Database design (233) Clustering (16) Common Table Expressions (CTE) (11) Concurrency (1) Constraints (8) Data types (11) FILESTREAM (22) General database design (104) Partitioning (13) Relationships and dependencies (12) Temporal tables (12) Views (16) ►Database development (418) Comparison (4) Continuous delivery (CD) (5) Continuous integration (CI) (11) Development (146) Functions (106) Hyper-V (1) Search (10) Source Control (15) SQL unit testing (23) Stored procedures (34) String Concatenation (2) Synonyms (1) Team Explorer (2) Testing (35) Visual Studio (14) DBAtools (35) DevOps (23) DevSecOps (2) Documentation (22) ETL (76) ►Features (213) Adaptive query processing (11) Bulk insert (16) Database mail (10) DBCC (7) Experimentation Assistant (DEA) (3) High Availability (36) Query store (10) Replication (40) Transaction log (59) Transparent Data Encryption (TDE) (21) Importing, exporting (51) Installation, setup and configuration (121) Jobs (42) ▼Languages and coding (686) Cursors (9) DDL (9) DML (6) JSON (17) PowerShell (77) Python (37) R (16) SQL commands (196) SQLCMD (7) String functions (21) T-SQL (275) XML (15) Lists (12) Machine learning (37) Maintenance (99) Migration (50) Miscellaneous (1) ▼Performance tuning (869) Alerting (8) Always On Availability Groups (82) Buffer Pool Extension (BPE) (9) Columnstore index (9) Deadlocks (16) Execution plans (125) In-Memory OLTP (22) Indexes (79) Latches (5) Locking (10) Monitoring (100) Performance (196) Performance counters (28) Performance Testing (9) Query analysis (121) Reports (20) SSAS monitoring (3) SSIS monitoring (10) SSRS monitoring (4) Wait types (11) ►Professional development (68) Professional development (27) Project management (9) SQL interview questions (32) Recovery (33) Security (84) Server management (24) SQL Azure (271) SQL Server Management Studio (SSMS) (90) SQL Server on Linux (21) ►SQL Server versions (177) SQL Server 2012 (6) SQL Server 2016 (63) SQL Server 2017 (49) SQL Server 2019 (57) SQL Server 2022 (2) ►Technologies (334) AWS (45) AWS RDS (56) Azure Cosmos DB (28) Containers (12) Docker (9) Graph database (13) Kerberos (2) Kubernetes (1) Linux (44) LocalDB (2) MySQL (49) Oracle (10) PolyBase (10) PostgreSQL (36) SharePoint (4) Ubuntu (13) Uncategorized (4) Utilities (21) Helpers and best practices BI performance counters SQL code smells rules SQL Server wait types © 2022 Quest Software Inc. ALL RIGHTS RESERVED. GDPR Terms of Use Privacy
Share:
0 comments

Comments (0)

Leave a Comment

Minimum 10 characters required

* All fields are required. Comments are moderated before appearing.

No comments yet. Be the first to comment!