use msdb; Begin Transaction Begin Try Declare @collection_set_id_17 int Declare @collection_set_uid_18 uniqueidentifier EXEC [dbo].[sp_syscollector_create_collection_set] @name=N'Disk Usage', @collection_mode=1, @description=N'Collects data about the disk and log usage for all databases.', @target=N'', @logging_level=1, @days_until_expiration=730, @proxy_name=N'', @schedule_name=N'CollectorSchedule_Every_6h', @collection_set_id=@collection_set_id_17 OUTPUT, @collection_set_uid=@collection_set_uid_18 OUTPUT Select @collection_set_id_17, @collection_set_uid_18 Declare @collector_type_uid_19 uniqueidentifier Select @collector_type_uid_19 = collector_type_uid From [dbo].[syscollector_collector_types] Where name = N'Generic T-SQL Query Collector Type'; Declare @collection_item_id_20 int EXEC [dbo].[sp_syscollector_create_collection_item] @name=N'Disk Usage - Data Files', @parameters=N' DECLARE @dbsize bigint DECLARE @logsize bigint DECLARE @ftsize bigint DECLARE @reservedpages bigint DECLARE @pages bigint DECLARE @usedpages bigint SELECT @dbsize = SUM(convert(bigint,case when type = 0 then size else 0 end)) ,@logsize = SUM(convert(bigint,case when type = 1 then size else 0 end)) ,@ftsize = SUM(convert(bigint,case when type = 4 then size else 0 end)) FROM sys.database_files SELECT @reservedpages = SUM(a.total_pages) ,@usedpages = SUM(a.used_pages) ,@pages = SUM(CASE WHEN it.internal_type IN (202,204) THEN 0 WHEN a.type != 1 THEN a.used_pages WHEN p.index_id < 2 THEN a.data_pages ELSE 0 END) FROM sys.partitions p JOIN sys.allocation_units a ON p.partition_id = a.container_id LEFT JOIN sys.internal_tables it ON p.object_id = it.object_id SELECT @dbsize as ''dbsize'', @logsize as ''logsize'', @ftsize as ''ftsize'', @reservedpages as ''reservedpages'', @usedpages as ''usedpages'', @pages as ''pages'' disk_usage', @collection_item_id=@collection_item_id_20 OUTPUT, @frequency=5, @collection_set_id=@collection_set_id_17, @collector_type_uid=@collector_type_uid_19 Select @collection_item_id_20 Declare @collector_type_uid_21 uniqueidentifier Select @collector_type_uid_21 = collector_type_uid From [dbo].[syscollector_collector_types] Where name = N'Generic T-SQL Query Collector Type'; Declare @collection_item_id_22 int EXEC [dbo].[sp_syscollector_create_collection_item] @name=N'Disk Usage - Log Files', @parameters=N' -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @tran_log_space_usage table( database_name sysname , log_size_mb float , log_space_used float , status int ); INSERT INTO @tran_log_space_usage EXEC(''DBCC SQLPERF (LOGSPACE) WITH NO_INFOMSGS''); SELECT database_name, log_size_mb, log_space_used, status FROM @tran_log_space_usage log_usage', @collection_item_id=@collection_item_id_22 OUTPUT, @frequency=5, @collection_set_id=@collection_set_id_17, @collector_type_uid=@collector_type_uid_21 Select @collection_item_id_22 Commit Transaction; End Try Begin Catch Rollback Transaction; DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; DECLARE @ErrorNumber INT; DECLARE @ErrorLine INT; DECLARE @ErrorProcedure NVARCHAR(200); SELECT @ErrorLine = ERROR_LINE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(), @ErrorNumber = ERROR_NUMBER(), @ErrorMessage = ERROR_MESSAGE(), @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-'); RAISERROR (14684, @ErrorSeverity, 1 , @ErrorNumber, @ErrorSeverity, @ErrorState, @ErrorProcedure, @ErrorLine, @ErrorMessage); End Catch; GO