Nice one Chris! I developed a slightly different SQL query for 'low disk' issues - one that processes BOTH by % free and absolute bytes free. My code helps deal with things like very large and very small disks much better, since EITHER or BOTH conditions trigger alerts. Without this, things like very small flash drives and very large disks (Terabyte sized) don't tend to trigger the warnings you need a the right times.
The variable 'rank' is used to rate each machine by severity, where rank 1 = most urgent, down to rank 7 (no issues). you can adjust the thresholds yourself as you see fit.
select machName, groupName, DriveLetter, TotalSpace, UsedSpace, FreeSpace,
(cast(freespace as float)/cast(totalspace as float))*100 as percFree,
rank = case
when FreeSpace < 1024 and (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 1
when FreeSpace < 1024 or (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 2
when FreeSpace < 10240 and (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 3
when FreeSpace < 10240 or (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 4
when FreeSpace < 20480 and (cast(freespace as float)/cast(totalspace as float))*100 < 20 then 5
when FreeSpace < 20480 or (cast(freespace as float)/cast(totalspace as float))*100 < 20 then 6
else 7
end
from dbo.vCurrDiskInfo
join vdb_Scopes_Machines foo on foo.agentGuid = dbo.vCurrDiskInfo.agentGuid
where DriveType = 'Fixed' and TotalSpace >0 and case
when FreeSpace < 1024 and (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 1
when FreeSpace < 1024 or (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 2
when FreeSpace < 10240 and (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 3
when FreeSpace < 10240 or (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 4
when FreeSpace < 20480 and (cast(freespace as float)/cast(totalspace as float))*100 < 20 then 5
when FreeSpace < 20480 or (cast(freespace as float)/cast(totalspace as float))*100 < 20 then 6
else 7
end <7
order by rank, FreeSpace
and the PHP Code:
<?php
function formatBytes($size, $precision = 2)
{
$base = log($size) / log(1024);
$suffixes = array('b', ' kB', ' MB', ' GB', ' TB');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
$pageContent = null;
ob_start();
include 'dblogin.php';
$tsql = "select machName, groupName, DriveLetter, TotalSpace, UsedSpace, FreeSpace,
(cast(freespace as float)/cast(totalspace as float))*100 as percFree,
rank = case
when FreeSpace < 1024*10 and (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 1
when FreeSpace < 1024*10 or (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 2
when FreeSpace < 1024*15 and (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 3
when FreeSpace < 1024*15 or (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 4
else 5
end
from dbo.vCurrDiskInfo
join vdb_Scopes_Machines foo on foo.agentGuid = dbo.vCurrDiskInfo.agentGuid
where DriveType = 'Fixed' and TotalSpace >0
and case
when FreeSpace < 1024*10 and (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 1
when FreeSpace < 1024*10 or (cast(freespace as float)/cast(totalspace as float))*100 < 10 then 2
when FreeSpace < 1024*15 and (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 3
when FreeSpace < 1024*15 or (cast(freespace as float)/cast(totalspace as float))*100 < 15 then 4
else 5
end <5
order by rank, FreeSpace";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}
echo "<div class=\"heading\">";
Echo "Agents with Low Disk Space</br>";
echo "</div>";
echo "<table id=\"lowdisklist\">";
echo "<tr><th class=\"colL\">Machine Name</th><th class=\"colM\">Drive</th><th class=\"colR\">Free Space</th><th class=\"colR\">Total Space</th><th class=\"colM\">Free Space %</th><th class=\"colM\">Urgency</th></tr>";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
echo "<tr><td class=\"colL\">".$row['machName'].".".$row['groupName']."</td><td class=\"colM\">".$row['DriveLetter'].":</td><td class=\"colR\">";
if ($row['FreeSpace'] < 1024*15) {
if ($row['FreeSpace'] < 1024*10) {
echo "<font color=red>";
} else {
echo "<font color=orange>";
}
echo formatBytes($row['FreeSpace']*1024*1024)."</font></td>";
} else { echo formatBytes($row['FreeSpace']*1024*1024)."</td>"; }
echo "<td class=\"colR\">".formatBytes($row['TotalSpace']*1024*1024)."</td><td class=\"colM\">";
$perc = round(($row['percFree']),2);
if ($perc < 15) {
if ($perc < 10) {
echo "<font color=red>";
} else {
echo "<font color=orange>";
}
echo $perc."%</font></td>";
} else { echo $perc."%</td>"; }
echo "<td class=\"colM\">".$row['rank']."</td></tr>";
}
echo "</table>";
$pageContent = ob_get_contents(); // collect above content and store in variable
ob_end_clean();
echo $pageContent;
?>