DBSoft6:OPT#8#67100656GWD#1#0GCL#11#-2147483633WRC#1#0IDS#1#4WRO#3#>|:FON#11#Courier NewFOS#1#9STC#8#16777215STB#7#8388608TBC#0#TBD#1#4EFF#297#17,0,0,16777215,;3,0,16777215,;4,8421504,16777215,;5,0,16777215,;6,255,16777215,;10,16711680,16777215,;11,0,16777215,;12,16711680,16777215,;20,16711680,16777215,;21,255,16777215,;22,255,16777215,;23,16711680,16777215,;40,255,16777215,;42,0,16777215,;43,255,16777215,;45,0,16777215,;46,0,16777215,;GDF#1#0MDF#1#0SDF#1#0BDF#1#0FOC#1#0RMG#1#0LNN#13#MS Sans SerifLNS#1#8LNC#11#-2147483640LNT#1#1LNE#0##END#
cweb
Drive Info - Read Me First
Instructions for installing and running this script
<<Docs: Installing modules necessary for running this script >>
Sample ini file
Driveinfo Script
<< Include Perl modules for reading drive space and sending email (and a couple of others) >>
<<Docs: Installing modules necessary for running this script >>
<< Read in the info from the ini file >>
<< Get the filename of the ini file and put in the $ini_file variable >>
<< Get the drive free space for each drive >>
<< Send the drive space info to a list of people >>
<< Functions >>
<< Notify function - for sending an email message >>
MiscUtil Module
<< Functions >>
<< English Date >>
<< English Time >>
<< Get total size of a list of files >>
<< Load a text file into a scalar variable >>
<< Strip spaces off beginning and end of a string variable >>
<< Join together two hashes >>
<< Add commas to a number >>
@docs
This LEO document contains a script for checking free drive space on all drives on a Win32 machine and optionally emailing this info to a list of addresses.
Extensive documentation for how to use the script, and how the script works is also contained in this document.
The script and this document are copyright 2000 by Joe Orr under the Perl Artistic License. This file and the script it contains may be freely copied and modified as long as this copyright noticed is maintained.
To generate the script, select File->Tangle (with this node selected). This will create the following files:
1. driveinfo.pl (the main script)
2. MiscUtil.pm (set of utility functions, some of which are used in driveinfo.pl)
3. driveinfo_sample.ini (a sample ini file - you must edit this if you want to run the email option)
To run the script, see the "Instructions for installing and running this script".
@docs
This LEO document contains a script named "driveinfo.pl" for checking free drive space on all drives on your Win32 machine and emailing this information to any number of addresses. The LEO document also contains all the information you need to put this script to use. It also contains a description of how the script works.
The driveinfo.pl script is for Win32 systems only (Window 95/98/2000).
This script is written in the Perl programming language. A script is a set of instructions in a text file that execute when run with a program called an "interpreter". A perl interpreter for Win32 is freely available from
http://www.ActiveState.com.
To run this script, first download and install the ActiveState Perl interpreter. Then, "Tangle" the script. "Tangle" means to put together the actual script from the sections in this LEO document where it is embedded. To Tangle the script, click on the "Driveinfo Script" node and choose "Tangle" from the "File" menu. This will create a file named driveinfo.pl in the same folder as this LEO document.
This script requires some extra components which are also freely available. These components are called "modules". To install the modules needed for this script, see the information in the "Installing the modules needed for this script" subsection.
If you have installed Perl and the extra modules, you can now run this script by opening a Command Prompt, using the CD command to change to the directory that this LEO file is in, and typing "perl -w driveinfo.pl".
@docs
driveinfo.pl uses an "ini" file to store some information for use with the email option.
The ini file has two types of information:
1. Information about your smtp server, so that you can send an email.
2. Email addresses of users who should be notified.
Here is a sample ini file (starting from after the "@root").
@root "driveinfo_sample.ini"
#==================================
#
# ini file for driveinfo.pl
#
#==================================
smtp_server: smtp.a001.sprintmail.com
domain: a001.sprintmail.com
smtp_user: username
reply_to: george@jserv.com
notify: george@sprintmail.com
#notify: someone@inconnect.com
@docs
This is a list of the sections in the program. To generate the program ready to run, choose File->Tangle.
For documentation of the code, see the beginning of each section. All of the code for the script is contained in subsections of this section.
@root "driveinfo.pl"
<< Include Perl modules for reading drive space and sending email (and a couple of others) >>
<< Read in the info from the ini file >>
<< Get the drive free space for each drive >>
<< Send the drive space info to a list of people >>
<< Functions >>
@docs
Here we specify a couple of directives, and load a few modules.
This script is for Win32 machines, so we're assuming that you are going to use the perl distribution from http://www.ActiveState.com.
You may need to install the SMTP, AdminMisc and NetAdmin modules. See the subsection "Installing modules necessary for running this script".
@code
<< Docs: Installing modules necessary for running this script >>
use strict; # variables must be declared
use English; # use English names for Perl special variables (like '$_')
use Net::SMTP; # this module allows you to send email
use Win32::AdminMisc; # this module allows you get drive space and other info
use MiscUtil; # a small package containing miscellaneous functions
use FileHandle; # this module allows you to use object-oriented syntax for file functions
@docs
Here is how you can install the necessary modules for this script that using the PPM module manager that is installed by default with the ActiveState distribution:
To install the Net::SMTP module: open a command prompt and type "PPM". This will start PPM. At the PPM prompt, type "install libnet". (Net:: modules are in libnet). The program may ask you some questions. If you don't know the answer to the questions, enter a single space and carriage return.
That's all there is to installing the SMTP module. The AdminMisc module is in the Roth Consulting archive, not in the
ActiveState archive, however. You'll need to tell PPM about the location of this archive. Type the following at the PPM prompt:
set repository RothConsulting http://www.roth.net/perl/packages
set
Now you can type:
install Win32-AdminMisc
and NetAdmin and AdminMisc will be installed on your system.
@docs
Here we read the ini file. The ini file contains info needed for sending an email (like smtp server) and a list of email addresses
for people we will notify about drive space on this machine.
@code
# declare a list of the variable names that will be read in from the ini file
use vars qw($server $ini_file $log_file $smtp_server $smtp_user $reply_to $domain );
<< Get the filename of the ini file and put in the $ini_file variable >>
# now we have the filename, open the file
my $fh;
($fh = new FileHandle $ini_file) or die "can't open $ini_file";
no strict 'refs'; #because we are pulling vars directly from ini file
#go thru the ini file processing one line at a time.
my %notify_flags;
while ($_ = $fh->getline){
chomp;
#skip lines with no letters
unless (/\w/) {next}
#skip comments
next if (/^#/);
#split the line into the variable name and its value
my ($name, $value) = split (/: /);
$$name = &trim($value);
#keep a hash of addresses to send notification email to
if ($name eq 'notify') {
$notify_flags{$$name} = '1';
}
}
@docs
This part of the program gets a list of drives, and
then gets the free space for each drive.
@code
my $hostname;
$hostname = Win32::AdminMisc::GetComputerName;
my (@Drives, $Drive, $Total, $Free);
@Drives = Win32::AdminMisc::GetDrives(DRIVE_FIXED);
my ($msg, $Time);
$msg .= "\nDrive info for $hostname ";
$msg .= &english_date . " " . &english_time . "\n\n";
foreach $Drive (sort(@Drives)){
($Total, $Free) = Win32::AdminMisc::GetDriveSpace($Drive);
$Total = commify($Total);
$Free = commify($Free);
$msg .= "Drive $Drive $Total bytes with $Free bytes available.\n";
}
print "$msg\n";
exit;
@code
my $name;
foreach $name (keys %notify_flags) {
¬ify($name, $msg)
}
@code
<< Notify function - for sending an email message >>
@docs
This function takes an address and a message as parameters,
and sends the message to the address.
The following global variables must already be defined with
meaningful values:
$smtp_server = smtp server name through which the email is to be sent
$smtp_user = user name for the smtp server
You can see that the Net::SMTP module makes sending email very easy. We just create an smtp object and then call some of the object's methods.
@code
sub notify () {
my ($address, $msg) = @_;
unless ($smtp_user and $address and $smtp_server) {return 0}
print "\n";
my $smtp = Net::SMTP->new($smtp_server,
Hello => $domain,
Timeout => 30,
Debug => 1,
);
$msg = "subject: Drive Information - message from DriveInfo\n$msg";
$smtp->mail($reply_to);
$smtp->recipient($address);
$smtp->data($msg);
$smtp->quit();
}
@docs
This module is a package with some miscellaneous functions.
Only a few of these functions are actually used by the driveinfo.pl script.
@root "MiscUtil.pm"
<< Functions >>
@code
package MiscUtil;
use strict;
use Time::localtime;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter AutoLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
grab_file
trim
join_hashes
english_date
english_time
total_size
commify
);
$VERSION = '0.03';
# Preloaded methods go here.
<< English Date >>
<< English Time >>
<< Get total size of a list of files >>
<< Load a text file into a scalar variable >>
<< Strip spaces off beginning and end of a string variable >>
<< Join together two hashes >>
<< Add commas to a number >>
# Autoload methods go after =cut, and are processed by the autosplit program.
1;
__END__
=head1 NAME
misc - Perl extension for misc utilities, used by JSL
=head1 SYNOPSIS
use misc;
=head1 DESCRIPTION
Misc utilities
=head1 AUTHOR
Joe Orr JSL http://www.jserv.com
=head1 SEE ALSO
perl(1).
=cut
@code
my $ini_file = $PROGRAM_NAME;
$ini_file =~ s|.*\\||g; #remove path (everything up to last \)
#remove file extension
$ini_file =~ s/\..*//;
#add ini file extension
$ini_file .= ".ini";
@code
sub english_date () {
my $year = localtime()->year;
no strict;
my $month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)[localtime->mon()];
my $wday = (Mon, Tue, Wed, Thur, Fri, Sat, Sun)[localtime->wday()];
my $mday = localtime->mday();
$year = 1900 + $year;
return "$wday, $month $mday $year";
}
@code
sub english_time () {
my $post;
my $hour = localtime->hour;
my $min = localtime->min;
($min < 10) and $min = "0$min";
my $now_time = "$hour:$min";
}
@code
#get total size of comma delimited list of files
sub total_size () {
my ($files, $pre, $post) = @_;
$files =~ s/ //g;
my @files = map {$pre.$_.$post} split /,/, $files;
my $total;
foreach (@files) {
unless (-e $_) {next}
$total += (-s $_);
}
return $total;
}
@code
#next sub grabs everything in a file and puts it in a scalar variable
sub grab_file {
my $fh = shift;
local $/;
open (FH, $fh) || die "can't open $fh!<p>";
my $l = <FH>;
close (FH);
$l;
}
@code
#cut the spaces off of both sides of a string
sub trim {
my ($l) = shift;
$l =~ s/^\s+//; #ltrim
$l =~ s/\s+$//; #rtrim
return $l
}
@code
#next sub appends multiple hashes
sub join_hashes {
my ($out_hash, @in_hash) = @_;
my ($in_hash, $key);
foreach $in_hash (@in_hash) {
foreach $key (keys %$in_hash) {
$$out_hash{$key} = $$in_hash{$key}
}
}
}
@docs
Insert commas into a number (string). Printf or sprint f could also be used here.
@code
sub commify {
local $_ = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
#$num =~ s/^(\d+)(\d{3})/$1,$2/;
return $_;
}