Ce script TRANSACT / SQL ci-dessous va vous permettre de "surveiller" l'espace disponible de votre base de données et de vous alerter par mail si le "seuil" minimal est atteint.
Nous utiliserons le mail manager propre à SQL Server.
Vous pouvez "encapsuler" cette requête dans un Job SQL Server avec une planification associée.
Voici le script :
FOR READ ONLY
DECLARE @database_name sysname
OPEN db_cur
FETCH NEXT FROM db_cur INTO @database_name
WHILE @@fetch_status = 0
BEGIN
DELETE @database_file_space
SET @exec_sql = 'EXEC ' + @database_name + '.dbo.sp_executeSQL N''' + @sql + ''
BEGIN TRY
INSERT @database_file_space
EXEC (@exec_sql)
END TRY
BEGIN CATCH
PRINT @database_name + ' : ' + ERROR_MESSAGE()
END CATCH
INSERT @instance_database_file_space
SELECT @database_name
, file_logical_name
, file_physical_name
, file_growth
, file_size_MB
, file_used_space_MB
, file_size_MB - file_used_space_MB AS file_free_space_MB
, CAST((CAST(file_used_space_MB AS numeric(14, 2)) / file_size_MB) * 100 AS numeric(14, 2)) AS file_used_space_percent
, CAST((CAST(file_size_MB - file_used_space_MB AS numeric(14, 2)) / file_size_MB) * 100 AS numeric(14, 2)) AS file_free_space_percent
FROM @database_file_space
WHERE file_logical_name = 'ma_base'
FETCH NEXT FROM db_cur INTO @database_name
END
CLOSE db_cur
DEALLOCATE db_cur
-- xx exprimé en MB
if exists (
SELECT *
FROM @instance_database_file_space WHERE file_free_space_MB <= xx
)
BEGIN
exec msdb.dbo.sp_send_dbmail
@profile_name = 'mon_profil_mail',
@recipients = 'mon_adresse_mail_en_copie',
@body = 'mon text',
@subject = 'mon sujet',
@body_format = 'TEXT';
END
Aucun commentaire:
Enregistrer un commentaire