sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Monitor Disk Space for SQL Server databases

Antonios Chatzipavlis
Thursday 16 August 2018





Job Code

declare @threshold_space_pct decimal(5,2) = 99.000;
declare @ebody nvarchar(max) ='';
with r as (
select distinct     v.volume_mount_point as drive
                ,   v.total_bytes as drive_size_in_bytes
                ,   v.available_bytes as drive_free_space_in_bytes
                ,   p.drive_free_space_pct
                ,   iif (p.drive_free_space_pct<=@threshold_space_pct,1,0) as low_space
from sys.master_files AS f  
cross apply sys.dm_os_volume_stats(f.database_id, f.file_id) as v
cross apply ( values (cast ( (v.available_bytes * 1.0 ) / (v.total_bytes * 1.0 ) as  decimal(5,2) )  * 100.0 ) ) as p(drive_free_space_pct)
)
select @ebody += drive + ' detected with low space ' + CHAR(10)+CHAR(13) from r where low_space = 1 ; 
if ( len(@ebody) > 0 )
begin
    declare @subject nvarchar(1024) = 'Low disk space detected in SQL Server instance ' +  @@Servername  
    EXEC msdb.dbo.sp_send_dbmail 
                @profile_name = 'YourProfile',
                @recipients = 'dba@sqlschool.gr',
                @body = @ebody,
                @subject = @subject
end


//antonch


Comments

05 Oct 2018 @ 7:42 PM

user-gravatar

ΓΙΑΝΝΑΡΟΣ ΔΗΜΗΤΡΗΣ

Είναι μια θαυμάσια προσπάθεια αυτά τα καινούργια Vlogs.Πολύ βολικό και και για DBA's αλλά σωτήριο για τους "accidental DBA's"

08 Oct 2018 @ 1:57 PM

user-gravatar

Vassilis

Πολύ καλό και εύκολο vlog.Να πώω μόνο ότι για να μου παίξει έπρεπε να αλλάξω το παρακάτω: -- , iif (p.drive_free_space_pct<=@threshold_space_pct,1,0) as low_spaceμε το παρακάτω: ,case when p.drive_free_space_pct<=@threshold_space_pct then 1 else 0 end as low_space Ευχαριστώ και πάλι.

08 Oct 2018 @ 2:35 PM

user-gravatar

Antonios Chatzipavlis

@Vassilis Προφανώς είσαι σε έκδοση του SQL Server πριν το 2012 όπου δεν υπάρχει η IIF. Γιατί?

08 Oct 2018 @ 3:04 PM

user-gravatar

Vassilis

@Antonios Γιατί : "αφού δουλεύει γιατί να το αλλάξω" Με αποδείξεις σε lab για το πόσο ποιο γρήγορα θα έτρεχε μία νέα version και πόσο ποιο εύχρηστο θα γίνονταν το Reporting με τα SSRS . Ευτυχώς που σταματάει η υποστήριξη του χρόνου και βγήκαν και τα "ψηφιακά άλματα" και οι "ποιοτικοί εκσυγχρονισμοί" μπας και τρέξουμε κανένα σύγχρονο query. Ειδικά τα data-driven subscriptions τα περιμένω και ξερογλείφομαι.

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Tip

What's New in SQL Server 2022 - Episodes

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2024 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.