Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Finding Drive usage info with Win32

by TeraMarv (Beadle)
on May 17, 2006 at 03:36 UTC ( [id://549937]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I'm looking to create a script than monitors drive usage on various w2k3 boxes.
It will be running from a w2k3 box itself and needs to gather the following info over the network:

Fixed Drives in servers
Drive capacity
Used capacity

I have performed some tests with Win32::AdminMisc and Win32::DriveInfo but have not managed to get them to work over the network.
use strict; use Win32::DriveInfo; use Win32::AdminMisc; use Data::Dumper; ################# my $target = '\\\\server\\c\\'; my @diskinfo = Win32::DriveInfo::DriveSpace($target); print Dumper \@diskinfo; ################# @diskinfo = Win32::AdminMisc::GetDriveGeometry($target); print Dumper \@diskinfo; exit 0;
All tests result in undefined arrays.

Am I even using the correct modules? Is my code correct? Any help would be grately appreciated.

Thanks,

TeraMarv.

UPDATE:

I have finally got it to work thus:

use strict; use Win32::DriveInfo; use Win32::NetAdmin; use Data::Dumper; my $capacity_href; my @servers = ('hqteradcm1','hqteradcm2','hcodevj248'); for my $server (@servers) { my @disks; my $target = '\\\\'.$server.'\\'; # Find all drives on server $target Win32::NetAdmin::GetServerDisks($target, \@disks); # Change array elements from C: to \\\\server\\c$\\ map { s/(.*):/$target\L$1\$\\/; } @disks; for my $drive (@disks) { # Find drive types my $type = Win32::DriveInfo::DriveType($drive); # Test for valid drive types - if($type =~ /(?:3|4)/) { # Find total and free space on drive my ($total,$free) = (Win32::DriveInfo::DriveSpace($driv +e))[5,6]; $drive =~ s/^\\\\.*\\(.)\$\\/\U$1:\//; $capacity_href->{$server}->{$drive} = { total => $total +, free => $free }; } } } print Dumper $capacity_href; exit 0;


Ouput from Dumper:

---------- Capture Output ---------- > "C:\Perl\bin\wperl.exe" DriveSpace.pl $VAR1 = { 'hqteradcm1' => { 'E:/' => { 'free' => '10177232896', 'total' => '18449301504' }, 'C:/' => { 'free' => 3358816256, 'total' => '18457527808' } }, 'hcodevj248' => { 'E:/' => { 'free' => '5279924224', 'total' => '18449301504' }, 'C:/' => { 'free' => '11348357632', 'total' => '18457527808' } }, 'hqteradcm2' => { 'E:/' => { 'free' => '16571375616', 'total' => '36964372480' }, 'C:/' => { 'free' => 718196224, 'total' => '8595417088' }, 'F:/' => { 'free' => '26558570496', 'total' => '28311412736' } } }; > Terminated with exit code 0.


Replies are listed 'Best First'.
Re: Finding Drive usage info with Win32
by GrandFather (Saint) on May 17, 2006 at 03:55 UTC
      Thank you GrandFather.
Re: Finding Drive usage info with Win32 ($^E?)
by tye (Sage) on May 17, 2006 at 04:17 UTC

    Unfortunately, Win32::AdminMisc isn't on CPAN so I'm not going to go googling in hopes of find it and documentation for it. But there is a reasonable chance that if the call fails, then you can look at $^E to get an explanation as to why it failed. If that doesn't work, then see the module's documentation for how you are supposed to get the failure reason.

    Note that \\server won't have a 'c' shared drive unless you shared it, but will have a 'c$' automatically, though only accessible to administrators. So one possiblity is that you dropped the '$', though that is, of course, just a guess on my part.

    - tye        

      I found Win32::AdminMisc on the Roth Consulting site. The Win32::AdminMisc FAQ, which appears to also contain the documentation, provides a link to the ftp site that can be used to download the latest version of the module (currently 20030714). This module is available from the Roth PPM repository (http://www.roth.net/perl/packages/).

      According to the FAQ, the GetDriveSpace function returns nothing if the call was unsuccessful, or the total drive capacity and the available space on the drive if the call was successful.

      A list of Roth Consulting's Perl Extensions is available at http://www.roth.net/perl/#Installation.

      Update: This post was in response to tye's "Unfortunately, Win32::AdminMisc isn't on CPAN so I'm not going to go googling in hopes of find it and documentation for it" rather than his comment regarding the reason of failure. I just thought quick access to the links above would be useful in future searches. For the record, I didn't see anything in the docs about failure reasons and I didn't examine the code.

        Note that this didn't answer the question I had about the module. I already knew that "nothing" was returned for failure. What would be nice to know, is how to get the failure reason. This is often hard to find in the documentation so I often just look at the source... which is conveniently made available by http://search.cpan.org/... except that this module isn't on CPAN and I suspect the author doesn't make the source available anywhere else (this is certainly the case for some modules from this author). So the next step might be "try it and see" if $^E works for that, but I avoid installing non-CPAN modules so I won't (the original problem was solved anyway).

        - tye        

      Tye,

      You are right! I have added the '$' and it works now.
      I am part of the adminstrators group so access is not a problem.

      Thanks.
Re: Finding Drive usage info with Win32
by Discipulus (Canon) on May 17, 2006 at 07:11 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://549937]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found