Brady
A moan, a groan, my life story and how me, a technically minded person just can't seem to fit in with anybody.

Getting Windows HDD usage with PHP and SNMP

At work a couple of months back I was faced with a problem that many of our servers would run out of hard drive space without us knowing and that would cause the server to hang and/or stop responding. So I did a bit of research and came across. This little utilility would run on the server in command line and tell you what drives were available and some info of the drives like space used and available. Well with this I would echo out the data to a text file and send that across to a common holding ground. Then from there get my web server to constantly scan this folder for new logs. This method worked but it had its issues.

Well yesterday at work I was playing around with SNMP and after finding a site with W2K MIBS I found lots of info you can pull from the server. Now I was already familiar with SNMP in PHP thanks to their documentation on their site. So using their snmpwalk function I was able to pull the relevant data to get HDD usage info.

[php]
$names = snmpwalk($row['ip'], “public”, ‘.1.3.6.1.2.1.25.2.3.1.3′);
$used = snmpwalk($row['ip'], “public”, ‘.1.3.6.1.2.1.25.2.3.1.6′);
$total = snmpwalk($row['ip'], “public”, ‘.1.3.6.1.2.1.25.2.3.1.5′);
$alloc = snmpwalk($row['ip'], “public”, ‘.1.3.6.1.2.1.25.2.3.1.4′);
[/php]

Now that would give me an array of drive names, space used on those drives, total space available on those drives and the allocation units. Now the allocation units I didn’t realize I needed as I thought the numbers given in used and total were just measured in bytes but they aren’t. The numbers in used and total are how many blocks of allocation size (if that makes sense). So to get the true byte value from used and total you need to multiply them by the allocation size number.

25599569 * 4096 = 104855834624B = 97.65GB

So there you have it. Now you know how to get HDD usage from your server using PHP and SNMP.