Hi Florian,
As mentioned, you would need to transform the rows in to columns to have PRTG process them. See the SQL script below that will do this for you.
USE master
DECLARE @Columns NVARCHAR(MAX)
DECLARE @SQLQuery NVARCHAR(MAX)
SET @Columns = N'';
SELECT @Columns+=N''+QUOTENAME([Name])+'=(SELECT [State] FROM sys.databases WHERE [NAME] = '''+[Name]+'''),' FROM sys.databases ORDER BY [Name];
SET @SQLQuery = N'
SELECT '+@Columns+'TotalDatabases=(SELECT COUNT([Name]) FROM sys.databases)
';
EXEC sp_executesql @SQLQuery
This actually will take the rows you get from sys.databases and use the NAME column to create a new set of columns. It then executes a query per NAME to gather the status of the specific database from sys.databases and executes this generated query giving you a one-row result and the status per column.
- master=0
- model=0
- tempdb=0
etc...
additionally you have a final column that shows the TotalDatabases found on the SQL server.
There is a theoretical flaw that the amount of databases would not change, but a database was added and one was removed - giving you a similar amount of columns and TotalDatabases.
Further is a flaw that if something is added you might need to re-create the sensor, since PRTG has a fixed amount of channels (as of now). You could bypass this by adding dummy-channels int the SET @SQLQuery.
And then there is the issue that the name of each column is the database name, this could end up in some kind of change/mix-up if e.g. a database was removed or added. That's partly why I added the TotalDatabases - but since the whole script will return a dynamic result, there is a risk in it. You don't compare the list against a fixed name-list of databases, what would be possible with a dummy database that holds a compare list etc..
If you would like to go more fancy, you could as well transform the script and use a PRTG parameter to inject the needed SQL database names - this would bypass the initial name query from sys.databases to create the columns, but would mean you need to transform the whole script quite a bit. This is just an idea - it is not how the current SQL query works, but it is possible.
For the most part, this should help you getting the status per database in a single SQL sensor.
Feel free to use the contact form on my web-site - it might be beneficial to directly communicate to speed the solution finding process up. @PRTG team - if this should happen and we find another solution, I would post it here as well - just trying to avoid the lengthy process through the moderated forum..
Let us know if this actually helped you - I am curious - I like the idea of it, for that reason I came up with this script.
Regards
Florian Rossmark
www.it-admins.com
Add comment