简体   繁体   中英

SQL Server Status Monitor

My application connects to 3 SQL servers and 5 databases simultaneously. I need to show statuses {running|stopped|not found} on my status bar.

Any idea/code sample that I can use here? This code should not affect the speed of application or a overhead to SQL server.

Buddhi

"This code should not affect the speed of application or a overhead to SQL server"

This is a Schroedinger's Cat scenario: in order to know the current status of a given remote service or process, you must serialize a message onto the network, await a response, de-serialize the response and act upon it. All of which will require some work and resources from all machines involved.

However, that work might be done in a background thread on the caller and if not called too often, may not impact the target server(s) in any measurable way.

You can use SMO (SQL Server Management Objects) to connect to a remote server and do pretty much anything you can do through the SQL admin tools since they use SMO to work their magic too. It's a pretty simple API and can be very powerful in the right hands.

SMO does, unsurprisingly, require that your have appropriate rights to the boxes you want to monitor. If you don't/can't have sufficient rights, you might want to ask your friendly SQL amin team to publish a simple data feed exposing some of the data you need.

HTH.

连接(验证连接)或无法连接(验证没有连接)时,应用程序中会有一些开销,但您可以通过异常检查来防止等待时间。

I think you should use WMI (using the ServiceController class (with this constructor). You basically query the server where the sql server resides and check its status.

The example below is assuming your application is written in c#:

ServiceController sc = new ServiceController("MSSQLSERVER", serverName); 
string status = sc.Status.ToString();

Full Disclosure, I am the founder of Cotega

If you are interested in a service to do this, our service allows for monitoring of SQL Server uptime and performance. In addition you can set notifications for when the database is not available, performance degrades, database size or user count issues occur, etc.

We use the following SQL query to check the status of a particular database

SELECT 'myDatabase status is:' AS Description, ISNULL((select state_desc FROM sys.databases WITH (NOLOCK) WHERE name ='myDatabase'),'Not Found') AS [DBStatus]

This should have very little overhead, especially when paired with best practices like background or asynchronous threads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM