Discussion (4)2
Log in or sign up to continue

To create a task in ObjectScript to monitor the free space for each namespace and to alert when close to being full, you can use the SYS.Database FreeSpace query in combination with custom logic for thresholds and alerting. Below is a sample code implementation:

Class MyNamespace.SpaceMonitorTask
{
ClassMethod MonitorFreeSpace(threshold As %Integer = 10) As %Status
{
    Try {
        Set dbList = ##class(%SQL.Statement).%New()
        Set sc = dbList.%PrepareClassQuery("SYS.Database", "FreeSpace")
        If 'sc Quit sc

        Set resultSet = dbList.%Execute()

        // Loop through each database entry and check free space
        While resultSet.%Next() {
            Set namespace = resultSet.%Get("DatabaseName")
            Set freeSpacePercent = resultSet.%Get("Free")
            // Alert if free space is below threshold
            If freeSpacePercent < threshold {
                Write "Alert: Namespace "_namespace_" is below the free space threshold with "_freeSpacePercent_"% remaining!", !
            }
        }

        Quit $$$OK
    } Catch ex {
        Write "Error: ", ex.DisplayString(), !
        Quit ex.AsStatus()
    }
}
}

This method does the following:
1. Queries the SYS.Database for free space details using the FreeSpace query [1].
2. Iterates over the records and checks if the free space percentage is below a given threshold (default set to 10%).
3. Generates an alert for namespaces with insufficient free space.

You can invoke this class method on a schedule (e.g., via a scheduled task in your environment) to continuously monitor namespace disk usage and receive alerts.

Sources:

@Nikhil Pawaria Code here is not entirely correct.

Try this

ClassMethod MonitorFreeSpace(threshold As %Integer = 10) As %Status
{
    Try {
        zn "%SYS"
        Set dbList = ##class(%SQL.Statement).%New()
        Set sc = dbList.%PrepareClassQuery("SYS.Database", "FreeSpace")
        If 'sc Quit

        Set resultSet = dbList.%Execute()

        // Loop through each database entry and check free space
        While resultSet.%Next() {
            Set namespace = resultSet.%Get("DatabaseName")
            Set freeSpacePercent = resultSet.%Get("Free")
            // Alert if free space is below threshold
            If freeSpacePercent < threshold {
                Write "Alert: Namespace "_namespace_" is below the free space threshold with "_freeSpacePercent_"% remaining!", !
            }
        }

        Return 1
    } Catch ex {
        Write "Error: ", ex.DisplayString(), !
        Return 0
    }

    Return 0
}

And link to the related article is correct you can use it