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_UNIX 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, \@computerlist)) { print "Error getting server list: ". Win32::NetAdmin::GetError() . "\n"; } # # Let's build the ignnore computer list. This list will hold names for # computers that don't act "normal" xerox print station, samba server. 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",\@drives)) { # # 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 is returned. unless ( @driveinfo = Win32::AdminMisc::GetDriveSpace("$UNCPath")) { 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, $volume{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"; }