如何在SQL Server中添加WMI alert
SQL Server可以支持WMI alert,因此我们可以使用WMI event 来监控SQL Server中的某些事件发生,并在此时出发SQL Server alert,指定执行我们需要的语句。 以下是WMI alert的一个示例脚本: IF OBJECT_ID('DeadlockEvents', 'U') IS NOT NULL BEGIN DROP TABL
SQL Server可以支持WMI alert,因此我们可以使用WMI event 来监控SQL Server中的某些事件发生,并在此时出发SQL Server alert,指定执行我们需要的语句。
以下是WMI alert的一个示例脚本:
IF OBJECT_ID('DeadlockEvents', 'U') IS NOT NULL
BEGIN
DROP TABLE DeadlockEvents ;
END ;
GO
CREATE TABLE DeadlockEvents
(AlertTime DATETIME, DeadlockGraph XML) ;
GO
-- Add a job for the alert to run.
EXEC msdb.dbo.sp_add_job @job_name=N'Capture Deadlock Graph',
@enabled=1,
@description=N'Job for responding to DEADLOCK_GRAPH events' ;
GO
-- Add a jobstep that inserts the current time and the deadlock graph into
-- the DeadlockEvents table.
EXEC msdb.dbo.sp_add_jobstep
@job_name = N'Capture Deadlock Graph',
@step_name=N'Insert graph into LogEvents',
@step_id=1,
@on_success_action=1,
@on_fail_action=2,
@subsystem=N'TSQL',
@command= N'INSERT INTO DeadlockEvents
(AlertTime, DeadlockGraph)
VALUES (getdate(), N''$(ESCAPE_SQUOTE(WMI(TextData))))',
@database_name=N'AdventureWorks' ;
GO
-- Set the job server for the job to the current instance of SQL Server.
EXEC msdb.dbo.sp_add_jobserver @job_name = N'Capture Deadlock Graph' ;
GO
-- Add an alert that responds to all DEADLOCK_GRAPH events for
-- the default instance. To monitor deadlocks for a different instance,
-- change MSSQLSERVER to the name of the instance.
EXEC msdb.dbo.sp_add_alert @name=N'Respond to DEADLOCK_GRAPH',
@wmi_namespace=N'\\.\root\Microsoft\SqlServer\ServerEvents\MSSQLSERVER',
@wmi_query=N'SELECT * FROM DEADLOCK_GRAPH',
@job_name='Capture Deadlock Graph' ;
GO
这个脚本是用WMI事件来监控SQL Server有没有deadlock 发生,如果发生了deadlock,就调用一个job,把相关的内如写入我们事先创建好的表中。
在SSMS 里面用图形界面创建WMI alert如下:
这两种方式都需要指定SQL Server WMI的namespace。
我们可以从这里得到当前SQL实例的WMI namespace的路径:
那么,创建WMI alert的过程究竟是怎样的呢?
SQL Server的WMI event provider 包含在Sqlwep100.dll 中。在我们创建WMI alert时,WMI service需要先将Sqlwep100.dll 装载并且初始化。
当SQL Server 调用WMI query的时候,会有一个对应的WMIPRVSE.exe 被启动。
WMIPRVSE.exe 启动后,装载Sqlwep100.dll并且做provider 初始化,初始化包含以下几个过程:
a. sqlwep100!CSQLServerEventProvider::Initialize --开始初始化
b. sqlwep100!CSQLServerEventProvider::HrConnectToSQL---连接SQL Server master database
c. sqlwep100!CSQLServerEventProvider::FIsServiceBrokerEnabled –检查 MSDB上”broker enabled”有没有启用
d. sqlwep100!CSBDeployment::CreateDeploymentIfNecessary –创建WMI alert 在SQL Server中所需要的对象。
对于我们文中的实例,需要创建以下对象:
IF NOT EXISTS(select * from sys.service_queues where name='WMIEventProviderNotificationQueue') CREATE QUEUE WMIEventProviderNotificationQueue;
IF NOT EXISTS(select * from sys.services where name='SQL/Notifications/ProcessWMIEventProviderNotification/v1.0') CREATE SERVICE [SQL/Notifications/ProcessWMIEventProviderNotification/v1.0] ON QUEUE WMIEventProviderNotificationQueue( [http://schemas.microsoft.com/SQL/Notifications/PostEventNotification] );
IF NOT EXISTS(select * from sys.server_event_notifications where name='SQLWEP_RECHECK_SUBSCRIPTIONS') CREATE EVENT NOTIFICATION SQLWEP_RECHECK_SUBSCRIPTIONS ON SERVER WITH FAN_IN FOR ALTER_LOGIN, DROP_LOGIN, ALTER_USER, DROP_USER, ADD_ROLE_MEMBER, DROP_ROLE_MEMBER, ADD_SERVER_ROLE_MEMBER, DROP_SERVER_ROLE_MEMBER, DENY_SERVER, REVOKE_SERVER, DENY_DATABASE, REVOKE_DATABASE TO SERVICE 'SQL/Notifications/ProcessWMIEventProviderNotification/v1.0', 'current database';
SELECT service_broker_guid FROM sys.databases WHERE name='msdb'
从windows的task manager中我们可以观察到,SQL Server对应的WMIPRVSE.exe 的启动用户是system. 从SQL Server 2008 开始, builtin\administrator 用户组默认已经从SQL Server的login中移除了。 在上述列出的a, b, c, d 四个步骤中,b, d 两个步骤都可能会遇到权限问题。
b. 连接SQL Server master database—我们需要将NT AUTHORITY\SYSTEM 加入到SQL login中,并且grant “public”用户组
c. MSDB的”broker enabled” 启用:
d. 执行创建对象的脚本需要给用户NT AUTHORITY\SYSTEM赋予以下权限:
use [master]
GO
GRANT ALTER ANY EVENT NOTIFICATION TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT AUTHENTICATE SERVER TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT CONTROL SERVER TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT CREATE DDL EVENT NOTIFICATION TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT CREATE TRACE EVENT NOTIFICATION TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT VIEW ANY DEFINITION TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT CONTROL ON LOGIN::[ SQL_starting_account] TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT IMPERSONATE ON LOGIN::[ SQL_starting_account] TO [NT AUTHORITY\SYSTEM]
GO
use [master]
GO
GRANT VIEW DEFINITION ON LOGIN::[ SQL_starting_account] TO [NT AUTHORITY\SYSTEM]
GO

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Microsoft SQL Server is a relational database management system launched by Microsoft. It is a comprehensive database platform that uses integrated business intelligence (BI) tools to provide enterprise-level data management. It is easy to use, has good scalability, and has a high degree of integration with related software. High advantages. The SQL Server database engine provides more secure and reliable storage functions for relational data and structured data, allowing users to build and manage highly available and high-performance data applications for business.

SQLServer or MySQL? The latest research reveals the best database selection. In recent years, with the rapid development of the Internet and big data, database selection has become an important issue faced by enterprises and developers. Among many databases, SQL Server and MySQL, as the two most common and widely used relational databases, are highly controversial. So, between SQLServer and MySQL, which one should you choose? The latest research sheds light on this problem for us. First, let

With the popularity of the Internet, website and application development has become the main business of many companies and individuals. PHP and SQLServer database are two very important tools. PHP is a server-side scripting language that can be used to develop dynamic websites; SQL Server is a relational database management system developed by Microsoft and has a wide range of application scenarios. In this article, we will discuss the development of PHP and SQL Server, as well as their advantages, disadvantages and application methods. First, let's

In web development, the combination of PHP and MySQL is very common. However, in some cases, we need to connect to other types of databases, such as SQL Server. In this article, we will cover five different ways to connect to SQL Server using PHP.

Introduction to how to use PDO to connect to a Microsoft SQL Server database: PDO (PHPDataObjects) is a unified interface for accessing databases provided by PHP. It provides many advantages, such as implementing an abstraction layer of the database and making it easy to switch between different database types without modifying a large amount of code. This article will introduce how to use PDO to connect to a Microsoft SQL Server database and provide some related code examples. step

With the continuous development of the Internet, database selection has become increasingly important. Among the many databases, SQLServer and MySQL are two high-profile options. SQLServer is a relational database management system developed by Microsoft, while MySQL is an open source relational database management system. So how to choose the best database solution between SQLServer and MySQL? First, we can compare these two databases in terms of performance. SQLServer is processing

SQL Server vs. MySQL: Which database is more suitable for high availability architecture? In today's data-driven world, high availability is one of the necessities for building reliable and stable systems. As the core component of data storage and management, the database's high availability is crucial to the business operation of the enterprise. Among the many databases, SQLServer and MySQL are common choices. So in terms of high availability architecture, which database is more suitable? This article will compare the two and give some suggestions.

SQLServer and MySQL are currently two very popular relational database management systems (RDBMS). They are both powerful tools for storing and managing large-scale data. However, they have some differences in handling large-scale data. This article will compare SQL Server and MySQL, focusing on their suitability for large-scale data processing. First, let us understand the basic characteristics of SQLServer and MySQL. SQLServer is developed by Microsoft
