use strict;
use warnings;
use Time::Local;
use Net::Ping;
use Win32::AdminMisc;
use Win32::NetAdmin;
use Win32::Lanman;
#
# Debug anybody?
my $debug = 0;
my (@unixlist, %ignorelist);
my (@computerlist, @drives, @driveinfo);
my $p = Net::Ping->new('icmp',3);
#
# For now the Samba machines can get excluded by the SV_TYPE_SERVER_UN
+IX option
# of the GetServers command. But in case that changes....
my @sambasvrs;
#
# Do we need to exclude a computer for some reason? Add an entry here
+.
my @exclude;
#
# Let's start the report.
my $outfile = "spacecheck.rpt";
open (OUT, ">$outfile")or die ("Could not create $outfile\n");
#
# Display a header
&print_header();
#
# Make a workstation list using NetAdmin::GetDomainController for the
+PDC
# and NetAdmin::GetServers() for the workstations
my $PDC;
if (Win32::NetAdmin::GetDomainController("","",$PDC)) {
if ($debug) { print "The PDC is $PDC\n"; }
my $domain = Win32::DomainName or die "Unable to obtain the domain
+ name\n";
unless (Win32::NetAdmin::GetServers($PDC, $domain, SV_TYPE_NT, \@c
+omputerlist)) {
print "Error getting server list: ". Win32::NetAdmin::GetError
+() . "\n";
}
#
# Let's build the ignnore computer list. This list will hold name
+s for
# computers that don't act "normal" xerox print station, samba ser
+ver.
unless (Win32::NetAdmin::GetServers($PDC, $domain, SV_TYPE_SERVER_
+UNIX, \@unixlist)) {
print "Unable to get the unix lists.\n";
}
my @buildlist = (@unixlist, @exclude, @sambasvrs );
foreach (@buildlist) {
$ignorelist{$_} = 1;
}
} else {
print Win32::FormatMessage( Win32::NetAdmin::GetError());
}
#
# Let's start checking computers!
foreach my $computer(@computerlist) {
#
# Let's see if the computer is up.
if ($p->ping($computer)) {
unless (Win32::Lanman::NetServerDiskEnum("\\\\$computer",\@dri
+ves)) {
#
# Is this a computer to skip?
if (exists $ignorelist{$computer}) {
next;
} else {
print "\nCan't get disk list for $computer: ". Win32::
+NetAdmin::GetError() . "\n\n";
next;
}
}
#
# Lets get to work!
printf("%-16s\n", $computer);
printf(OUT "%-16s\n", $computer);
foreach my $drive ( @drives ) {
if ($debug) { print "Drive says $drive\n"; }
$drive =~ s/://g;
my $UNCPath = "\\\\$computer\\$drive\$\\";
#
# Let's get the space. If it is an invalid drive, undef i
+s returned.
unless ( @driveinfo = Win32::AdminMisc::GetDriveSpace("$UN
+CPath")) {
if ($debug) { print "Skipping $drive since it is empty
+\n"; }
next;
}
if ($debug ) {
print "Capacity is $driveinfo[0]\n";
print "Freespace is $driveinfo[1]\n";
}
my %volume = Win32::AdminMisc::GetVolumeInfo("$UNCPath");
+
my $capacity = sprintf("%5.2f",($driveinfo[0]/1073741824))
+;
#
# We want to skip any removable media.
next if ($capacity < 0.1);
my $freespace = sprintf("%5.2f",($driveinfo[1]/1073741824)
+);
my $used = $capacity - $freespace;
my $percentfree = ($freespace/$capacity)*100;
printf("%15s:%6s %8.2f %8.2f %8.2f %6d %%\n", $drive, $vol
+ume{FileSystemName}, $capacity, $used, $freespace, $percentfree);
printf(OUT "%15s:%6s %8.2f %8.2f %8.2f %6d %%\n", $drive,
+$volume{FileSystemName}, $capacity, $used, $freespace, $percentfree);
}
} else {
print "\nCan't Ping $computer!\n\n";
printf OUT "\nCan't Ping $computer!\n\n";
}
}
close (OUT);
#
#
#
# Just a simplistic header for both the display and report.
sub print_header {
print "System/Drives FS Total Used Free % Free\
+n";
print "------------- ------ ------ ------ ------ ------\
+n";
print OUT " Drive Space Report (Gigabytes) \n\n";
print OUT " " . scalar localtime;
print OUT "\n\nSystem/Drives FS Total Used Free
+% Free\n";
print OUT "------------- ------ ------ ------ ------ ----
+--\n";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.