How to get drive space using Queries on SQL Server hosted on Linux

  • Hi,

    I need to get the free drive space details on SQL Server using queries, this server is hosted on a Linux Server. Require a query to give drive free space details.

    Thanks

    Sanz
  • I do not have SQL Server running on linux, but I think there may be better tools to report that data than SQL.  Things like Cacti, which is designed for reporting on things like CPU, memory and disk space of servers, would likely be a better option.

    If you NEED to do it from SQL, you will likely need to make a call to the OS (xp_cmdshell... although not sure that works in linux) and capture the results of df to a table.

    My opinion though, I would use a 3rd party reporting and monitoring solution to handle things like free space on a disk.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Are you asking inside of SQL Server, as in T-SQL, or from the host OS? How do you get this in Windows?

    df in Linux gets space. or du.

     

  • For those curious, in Windows you can still use xp_cmdshell and run fsutil volume diskfree <driveletter> where "<driveletter>" is the drive letter name such as "fsutil volume diskfree C:".

    a lot shorter in Linux with df.  df -h for "human readable" sizes (KB, MB, GB, TB, etc) rather than bytes.

    Without xp_cmdshell, I am not sure how you'd manage that though.

    I do feel like SQL Server is the wrong tool for monitoring free disk space though... If SQL Server is the hammer, not every problem is going to be a nail...

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • Thanks for your comments.

    I was looking for something similar to a xp_fixeddrives to use in T-sql. Guess I will have to do it using from OS side.

    Sanz
  • I could be mistaken, but I thought all of the xp_ stored procedures were deprecated?

    Not sure if this is relevant or not, but:

    http://tomaslind.net/2014/01/28/alternative-xp_fixeddrives/

    relevant section:

    SELECT DISTINCT
    SUBSTRING(volume_mount_point, 1, 1) AS volume_mount_point
    ,total_bytes/1024/1024 AS total_MB
    ,available_bytes/1024/1024 AS available_MB
    FROM
    sys.master_files AS f
    CROSS APPLY
    sys.dm_os_volume_stats(f.database_id, f.file_id);

     

    Not sure if that will work on Linux or not, but could give it a shot?

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • AFAIK, xp_fixeddrives hasn't been supported for some time. Other XPs are.

    https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/general-extended-stored-procedures-transact-sql?view=sql-server-ver15

    dm_os_volume_stats is what I'd use.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply