go backarticles

Articles of SQLschool.gr Team

Monitor Disk Space for SQL Server databases

Antonios Chatzipavlis





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



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.


Comments

user-gravatar

On 05 Oct 2018 @ 7:42 PM ΓΙΑΝΝΑΡΟΣ ΔΗΜΗΤΡΗΣ wrote:

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

user-gravatar

On 08 Oct 2018 @ 1:57 PM Vassilis wrote:

Πολύ καλό και εύκολο 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 Ευχαριστώ και πάλι.

user-gravatar

On 08 Oct 2018 @ 2:35 PM Antonios Chatzipavlis wrote:

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

user-gravatar

On 08 Oct 2018 @ 3:04 PM Vassilis wrote:

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

Leave your comment

COMMENT

FULL NAME

EMAIL ADDRESS

We use Gravatar

WEB SITE



captcha


 

Newsletter

If you want to receive updates from us subscribe below with your email.
Follow us in
PASS chapter logo

The Official PASS Local Group for Greece

About us Contact us Terms of Use Privacy Sing in Register
sql school greece logo
© 2010-2020 All rights reserved

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