Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Trying to get drive space info on remote Windows servers

by Cloudster (Novice)
on Feb 18, 2010 at 07:35 UTC ( #823880=perlquestion: print w/replies, xml ) Need Help??

Cloudster has asked for the wisdom of the Perl Monks concerning the following question:

I want to expand the functionality on a monitoring script that runs every night for me that summarizes my SQL Server logs for the previous day and wanted to add a drive space monitor. I found the Win32::DriveInfo lib and I thought I installed it properly by copying it in to my C:\Perl\lib\Win32 directory. I copied this code with a little modification and it will return correct information for local drives but only returns zero for remote servers. I have no clue what's going on. Any suggestions? Thanks in advance! Here's my program:
use Win32::DriveInfo; print "$s_c\n"; print "Drive Free Tot PctFree\n"; ($SectorsPerCluster, $BytesPerSector, $NumberOfFreeClusters, $TotalNumberOfClusters, $FreeBytesAvailableToCaller, $TotalNumberOfBytes, $TotalNumberOfFreeBytes) = Win32::DriveInfo::DriveSpace('\\\\server01\ +\c\$'); $fb = $TotalNumberOfFreeBytes / 1024 / 1024 / 1024; $tb = $TotalNumberOfBytes / 1024 / 1024 / 1024; #$pctfree = $fb / $tb; print "$s_c: $fb $tb $pctfree\n"; $TotalNumberOfBytes = (Win32::DriveInfo::DriveSpace('\\\\server01\\d\$ +\\'))[5]; $TotalNumberOfFreeBytes = (Win32::DriveInfo::DriveSpace('\\\\server01\ +\d\$\\'))[6]; $fb = $TotalNumberOfFreeBytes / 1024 / 1024 / 1024; $tb = $TotalNumberOfBytes / 1024 / 1024 / 1024; #$pctfree = $fb / $tb; print "$s_d: $fb $tb $pctfree\n";

Replies are listed 'Best First'.
Re: Trying to get drive space info on remote Windows servers
by BrowserUk (Pope) on Feb 18, 2010 at 08:11 UTC
    but only returns zero for remote servers.

    If the total bytes count is zero, it probably means that the call is failing--perhaps the userid you are running under does not have permissions on the remote server. If you check the return code from the call (which you should be doing anyway), then $^E would probably tell you why it is failing.

    BTW: The way you are calling the function twice, with list slices, to get two values that are both returned each time you make the call, is really crap coding. Try something like this:

    use Win32::DriveInfo; use constant { SECTORS_PER_CLUSTER => 0, BYTES_PER_SECTOR => 1, FREE_CLUSTERS => 2, TOTAL_CLUSTERS => 3, BYTES_FREE_4_CALLER => 4, TOTAL_BYTES => 5, TOTAL_FREE => 6, }; my @info = Win32::DriveInfo::DriveSpace('\\\\server01\\d\$\\') or die "DriveSpace failed with error: $^E"; printf "%.3f GB free of total %.3f GB\n", $info[ TOTAL_FREE ] / 1024**3, $info[ TOTAL_BYTES ] / 1024**3;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for the reply. I knew it was crap coding and I recognized that it was calling twice, I just wanted to get the basic functionality going before tightening it up properly. Since I was experimenting trying to get it to work, I wasn't too concerned about the crap at the moment. I'm not a heavy-duty Perl coder, I'm a database admin, and didn't know that I could simply tie an 'or die' to the function call for error trapping, normally I'm quite dogmatic about including that.

      Permissions isn't the issue, I'm using my network admin account when testing this. I was really wondering if I'm missing some sort of a network API call. The framework that this will fit in to reads and filters SQL Server logs every night, but that's just file I/O and works fine.

      Thanks again! I'll plug your code in and give it a shot.
Re: Trying to get drive space info on remote Windows servers
by revhippie (Scribe) on Feb 18, 2010 at 16:52 UTC

    It looks like the backslash before your dollar sign is causing the problem; being in single-quotes, there's nothing special about the dollar sign, so the resulting string is:

    \\server01\c\$

    instead of the more-likely-in-a-windows-environment:

    \\server01\c$

      Still no joy. Here's the current code:
      use Win32::DriveInfo; use constant { SECTORS_PER_CLUSTER => 0, BYTES_PER_SECTOR => 1, FREE_CLUSTERS => 2, TOTAL_CLUSTERS => 3, BYTES_FREE_4_CALLER => 4, TOTAL_BYTES => 5, TOTAL_FREE => 6, }; #$sql = "\\\\server01\\d\$"; $sql = "c:"; print "$sql\n"; print "Drive Free Tot PctFree\n"; my @info = Win32::DriveInfo::DriveSpace($sql) or die "DriveSpace failed with error: $^E"; printf "%.3f GB free of total %.3f GB\n", $info[ TOTAL_FREE ] / 1024**3, $info[ TOTAL_BYTES ] / 1024**3;
      If I run it against the server specified in the line that's commented out, it returns zeroes and no error. If I run it against my C:, it returns accurate numbers. So I don't think it's going out to the network, yet it isn't returning an error.

      Here's the output of two runs:
      c:\Perl>drivelist.pl \\server01\d$ Drive Free Tot PctFree 0.000 GB free of total 0.000 GB c:\Perl>drivelist.pl c: Drive Free Tot PctFree 31.639 GB free of total 74.444 GB

        Try printing $^E unconditionally, immediately after call. It might show an error that the module is failing to act upon.

        Strange; works fine here with all drives/shares I have access to.

        I don't know if it would a prove useful diagnostic, but have you tried checking your C: drive via \\yourpc\c$ instead of c:?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://823880]
Approved by planetscape
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2019-06-26 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (111 votes). Check out past polls.

    Notices?