Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Win32::Daemon and Outlook

by BatGnat (Scribe)
on Nov 24, 2000 at 02:47 UTC ( [id://43174]=perlquestion: print w/replies, xml ) Need Help??

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

Howdy all. Here is the crux of problem. I have a script running on my NT box that sits and monitors our main NT file server, when free space drops below 2gb it sends out an email via Win32::OLE and Outlook. Actually I have 3 similar programs running that email when something goes wrong, but I starting with this one. So being the good little Perl hacker that I am, I was always looking for ways to improve my Code. I was getting sick of have the script visibly running on the desktop. One word: "SERVICE". So I modified the code, it now runs as a service. But when the error condition is matched, it opens outlook in a different (non visible) session, but does't actually email. Please help JAPH's, your my only Hope. BatGnat

Replies are listed 'Best First'.
(sendmail) Re: Win32::Daemon and Outlook
by mwp (Hermit) on Nov 24, 2000 at 03:10 UTC
    This may not be an option, but I recommend using the sendmail MTA as an alternative to mucking about with Outlook and OLE. Now you may say to me, "Sendmail? Isn't that a Unix thing?" and I'd say "Yes, but" there is a sendmail binary included in the NT Resource Kit that you can use! Convenient, no?

    Try this:

    # I forget the exact path to sendmail in NT. # -t tells sendmail to scan the message for To:, From:, etc. $sendmail = 'c:\bin\sendmail.exe -t'; open(MAIL, ">$sendmail") || die 'Could not load sendmail!'; print MAIL <<EOF To: batgnat@hiscorp.com From: fileserver@hiscorp.com Subject: 2GB Warning on File Server Your fileserver has less than 2GB of free disk space. EOF close(MAIL);
    Don't forget the extra break after the Subject line.

    Hope this helps!

    Friar 'kaboo
    avoiding NT problems with archaic Unix solutions since 1996 {g}

      Unfortunatly my Exchanges SMTP is disabled, (GE security Policy). And even if it was, there is a current project to outsource email for the region. I have no idea whether they will have SMTP, but until i find out, I am stuck.
      It does have an IMAP port open, does anyone have any modules for that??? I did a quick search and found none.
(jcwren) Re: Win32::Daemon and Outlook
by jcwren (Prior) on Nov 24, 2000 at 08:29 UTC
    Actually, if your Outlook server is configured to accept SMTP connections (and it's an unusual one that isn't), I'd use Net::SMTP, or MIME::Lite, and connect to the mail server that way.

    --Chris

    e-mail jcwren
Re: Win32::Daemon and Outlook
by BatGnat (Scribe) on Nov 27, 2000 at 04:51 UTC
    Here is the current version of the source If you want the other version, it used Win32:Console to make it pretty. I also have a CGI script to read the log file and convert it into a into a graph using OLE, and saves a PNG/GIF etc. and for web display. As always any comment and suggestions welcome.
    # SPACEd # Daemon to check free Space on DAT01 my $ver = "v2.0.0beta1"; use warnings; use MySubs; use Win32::AdminMisc; use Win32::OLE; use Win32::Daemon; use Win32::EventLog; START_DAEMON(); my $MB = 1024 ** 2; my $hour = 60*60; my $SERVERPATH = uc(shift) || "\\\\SERVER\\SHARE\\"; my $ALERT = (shift) * $MB || 60000 * $MB; # for testing email #my $ALERT = (shift) * $MB || 2000 * $MB; if ($SERVERPATH !~ /\\$/) { $SERVERPATH=$SERVERPATH.'\\'; }; while (1) { my $sleep_time = ($hour*2); if ( GET_DRIVES($SERVERPATH) ) { $sleep_time = ($hour/2); } for (1 .. int($sleep_time/60)) { if ( Win32::Daemon::State() eq SERVICE_STOP_PENDING ) { Win32::Daemon::StopService(); die; } sleep 60; } } sub GET_DRIVES { my $OK; my $drive = shift; my $TIME = localtime; my ($sectors_per_cluster, $bytes_per_cluster, $free_clusters, $tot +al_clusters) = Win32::AdminMisc::GetDriveGeometry($drive); my $free = $sectors_per_cluster * $bytes_per_cluster * $free_clust +ers; my $total = $sectors_per_cluster * $bytes_per_cluster * $total_clu +sters; LOG_CSV($free); print $drive,"\n"; print $TIME,"\n"; if ($free) { my $align = 42; if ($free < $ALERT) { undef $OK; email($drive,$free,$total); } else { $OK = 1; } } else { undef $OK; } return $OK; } sub email { my $path = shift; my $free = shift; my $total = shift; my $TIME = localtime; my @TOLIST = ('bowersm'); # for testing # my @TOLIST =('Aust ISD Hardware/Software Support','Aust ISD Syste +ms Admin','toglej'); my $message = "<HTML><FONT SIZE=6><B><I><FONT COLOR=NAVY>LOW SPACE + on ".$path."<\/FONT COLOR><\/B><\/I><HR color=red><FONT SIZE=5>There + is currently <FONT COLOR=RED>".&COMMA(int($free/1048576))."<\/FONT C +OLOR>Mb free of <FONT COLOR=RED>".&COMMA(int($total/1048576))."<\/FON +T COLOR>Mb total on <FONT COLOR=NAVY>".$path.".<\/FONT COLOR><BR>This + is below the alert mark of <FONT COLOR=RED>".&COMMA(int($ALERT/10485 +76))."<\/FONT COLOR>Mb.<BR>CSC may want to look at this!!!<HR color=r +ed>".$TIME."\n"; eval {$OUTLOOK = Win32::OLE->GetActiveObject('Outlook.Application' +)}; die "Outlook not installed" if $@; unless (defined $OUTLOOK) { $OUTLOOK = Win32::OLE->new('Outlook.Application', sub {$_[0]-> +Quit;}) or die "Oops, cannot start Outlook"; } $mailitem = $OUTLOOK->CreateItem(0); map {$mailitem->Recipients->Add($_)} @TOLIST; $mailitem->{"Subject"} = "LOW SPACE on ".$path." \@ ".$TIME; $mailitem->{"Categories"} = 'LOW SPACE!!!' ; $mailitem->{"HTMLBody"} = $message; $mailitem->Send(); undef $OUTLOOK,@TOLIST, $message, $TIME, $path, $free, $total; } sub LOG_CSV { my $NEW; my $NOW = localtime; my $NOW_CONV = CONV_LOCALTIME($NOW); my $FREE = shift; my %LOG->{$NOW_CONV } = $FREE; my @days = qw/MON TUE WED THU FRI SAT SUN/; if (open (CSV, "<d:/LOG_v2.CSV")) { foreach (<CSV>) { next if /Date/; chomp; my ($DATE, $SIZE) = split /,/, $_; %LOG->{$DATE} = $SIZE; } close CSV; } open (CSV, ">d:/LOG_v2.CSV"); print CSV "Date,Bytes,Megabytes\n"; foreach $key (sort keys %LOG) { print CSV "$key,".%LOG->{$key}.",".(%LOG->{$key}/1048576)."\n" +; } close CSV; } sub CONV_LOCALTIME { my %MONTHS = ( "Jan" => '01', "Feb" => '02', "Mar" => '03', "Apr" => '04', "May" => '05', "Jun" => '06', "Jul" => '07', "Aug" => '08', "Sep" => '09', "Oct" => '10', "Nov" => '11', "Dec" => '12' ); my ($DAY, $MON, $DATE, $TIME, $YEAR) = split /\s+/, shift; my $CONV = join(" ",$YEAR, %MONTHS->{$MON}, sprintf("%02d",$DATE), + $TIME); return $CONV; } sub START_DAEMON { # Tell the OS to start processing the service... Win32::Daemon::StartService(); # Wait until the service manager is ready for us to continue... while( SERVICE_START_PENDING != Win32::Daemon::State() ) { sleep( 1 ); } # Now let the service manager know that we are running... Win32::Daemon::State( SERVICE_RUNNING ); # EVENT(); } sub EVENT { #### Adding Event log entries #### Not implemented %EVENT = ( Computer => '', Source => '', EventType => EVENTLOG_INFORMATION_TYPE, Catergory => '1048576', # EventID => '', Strings => 'TEST' ); $handle=Win32::EventLog->new("Application"); $handle->Report(\%EVENT); }
Re: Win32::Daemon and Outlook
by royalanjr (Chaplain) on Nov 24, 2000 at 23:08 UTC
    I am very curious as to how exactly you are monitoring the freespace....

    Roy Alan

      Here is the graph code.
      use strict; use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Excel'; use CGI::Pretty qw/:standard *table *th *Tr/; use MySubs; my $name="SPACEd Graph"; my $TIME = localtime($^T); $Win32::OLE::Warn = 3; # die on errors... my $filename = 'd:\bin\scripts\perlscripts\spaced\log.csv'; my $filter = 'PNG'; # can be GIF, JPG, JPEG or PNG my $count = 0; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32: +:OLE->new('Excel.Application', 'Quit'); # use the Excel application +if it's open, otherwise open new $Excel->{Visible} = 0; my $Book = $Excel->Workbooks->Open( $filename ); # open the file my $Sheet = $Book->Worksheets(1); my $Range = $Sheet->Range('A:A,C:C'); my $Chart = $Excel->Charts->Add; $Chart->{ChartType} = xlAreaStacked; $Chart->SetSourceData({Source => $Range, PlotBy => xlColumns}); $Chart->{HasLegend} = 0; $Chart->{HasTitle} = 1; $Chart->ChartTitle->{Text} = "Free Space in Megabytes on \\\\SERVER\\S +HARE\n$TIME"; my $savename = 'd:\inetpub\wwwroot\spacedgr\graph.'.$filter; $Chart->Export($savename, $filter, 0); $Book->Close(0); print header; print start_html(-title=>"$name", -author=>'martin_bowers@avcofinance.com.au', -base=>'true', -meta=>{'keywords'=>'menu', 'copyright'=>'copyright 2000 Marty'}, -style=>{'src'=>'/style.css'}, -dtd); print h1($name,br,font({-size=>3},"by Martin Bowers"),hr); print img({src=>'graph.png'}); print end_table, br, hr, &COUNTER, end_html;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-25 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found