#!/usr/bin/perl -w # # email-mrtg # John Simpson 2007-08-28 # # Sends an HTML email which shows what the indicated MRTG web page would # look like if viewed in a browser. # ############################################################################### # # Copyright (C) 2007 John Simpson. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License, version 3, as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # ############################################################################### require 5.003 ; use strict ; use MIME::Base64 ; my $dir = "/var/www/html/mrtg" ; my $url = "https://domain.xyz/mrtg" ; my $sender = "Admin " ; my $recip = "Admin " ; ############################################################################### # # read a file from the disk sub readfile($) { my $file = shift ; my $rv = "" ; open ( R , "<$file" ) or die "Can\'t read $file: $!\n" ; local($/) = undef ; # slurp $rv = ; close R ; return $rv ; } ############################################################################### # # get the base64-encoded version of a file from the disk sub readfile_base64($) { return encode_base64 ( readfile ( $_[0] ) ) ; } ############################################################################### # # forward the message as an attachment sub send_mrtg_page($) { my $base = shift ; my $ts = sprintf ( "%08x" , time() ) ; my $text = readfile ( "$dir/$base.html" ) ; my $b64_day = readfile_base64 ( "$dir/$base-day.png" ) ; my $b64_week = readfile_base64 ( "$dir/$base-week.png" ) ; my $b64_month = readfile_base64 ( "$dir/$base-month.png" ) ; my $b64_year = readfile_base64 ( "$dir/$base-year.png" ) ; my $b64_ml = readfile_base64 ( "$dir/mrtg-l.png" ) ; my $b64_mm = readfile_base64 ( "$dir/mrtg-m.png" ) ; my $b64_mr = readfile_base64 ( "$dir/mrtg-r.png" ) ; $text =~ s|($base\-day\.png)|cid:$ts.$1|gi ; $text =~ s|($base\-week\.png)|cid:$ts.$1|gi ; $text =~ s|($base\-month\.png)|cid:$ts.$1|gi ; $text =~ s|($base\-year\.png)|cid:$ts.$1|gi ; $text =~ s|(mrtg\-l\.png)|cid:$ts.$1|gi ; $text =~ s|(mrtg\-m\.png)|cid:$ts.$1|gi ; $text =~ s|(mrtg\-r\.png)|cid:$ts.$1|gi ; my $sep = sprintf ( "mime.%s.%04X.separator" , $ts , $$ ) ; my $now = localtime() ; open ( O , "| /var/qmail/bin/qmail-inject" ) or die "Can\'t run /var/qmail/bin/qmail-inject: $!\n" ; print O <$url/$base.html --$sep Content-Type: text/html Content-Transfer-Encoding: 7bit $text --$sep Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="$base-day.png" Content-ID: <$ts.$base-day.png> $b64_day --$sep Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="$base-week.png" Content-ID: <$ts.$base-week.png> $b64_week --$sep Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="$base-month.png" Content-ID: <$ts.$base-month.png> $b64_month --$sep Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="$base-year.png" Content-ID: <$ts.$base-year.png> $b64_year --$sep Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="mrtg-l.png" Content-ID: <$ts.mrtg-l.png> $b64_ml --$sep Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="mrtg-m.png" Content-ID: <$ts.mrtg-m.png> $b64_mm --$sep Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="mrtg-r.png" Content-ID: <$ts.mrtg-r.png> $b64_mr --$sep-- EOF close O ; } ############################################################################### sub usage() { die <