#!/usr/bin/perl # # Monitorix - Sends Monitorix monthly reports. # # Copyright (C) 2005-2012 by Jordi Sanfeliu # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # 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, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # require 5.006; no strict "vars"; no warnings "once"; #use warnings; use LWP::UserAgent; use Getopt::Long; use MIME::Lite; use constant SMTP_HOST => "localhost"; use constant FROM => "noreply\@example.com"; use constant PREFIX => "http://127.0.0.1"; use constant SUBJECT => "Monitorix: monthly traffic report"; # ======================================================================== # ====== Probably you don't need to touch anything below this line ====== # ======================================================================== my $url; my $ua; my (undef, undef, undef, undef, $prev_month, $prev_year) = localtime(time - 3600); sub adjust($) { my $bytes = (shift); my $adjust = 0; my $b = " "; if($bytes > 0 && $bytes < 1048576) { $adjust = $bytes/1024; $b = "KB"; } if($bytes > 1048576 && $bytes < 1073741824) { $adjust = $bytes/1024/1024; $b = "MB"; } if($bytes > 1073741824 && $bytes < 1000000000000) { $adjust = $bytes/1024/1024/1024; $b = "GB"; } return sprintf("%3u%s", $adjust, $b); } GetOptions( "host=s" => \$opt_host, "config=s" => \$opt_config, ); if(!$opt_host || !$opt_config) { die("send_reports -h|--host -c|--config \n\n"); } if(!stat($opt_config)) { die("can't open file $opt_config.\n"); } # load configuration file require $opt_config; # get the traffic counters # ----------------------------------------------------------------------------- my @traffic = (); my $tot_in = 0; my $tot_out = 0; my $tot = 0; open(IN, $USAGE_DIR . $opt_host); push(@traffic, "DAY INPUT OUTPUT TOTAL\n"); push(@traffic, "---------------------------------------------------------------\n"); while() { my ($day, $in, $out) = split(' ', $_); chomp($day); chomp($in); chomp($day); $tot_in += $in; $tot_out += $out; $tot = $in + $out; push(@traffic, sprintf("%3u %12u %s %12u %s %15u %s\n", $day, $in, adjust($in), $out, adjust($out), $tot, adjust($tot))); } close(IN); push(@traffic, "---------------------------------------------------------------\n"); $tot = $tot_in + $tot_out; push(@traffic, sprintf("%16u %s %12u %s %15u %s\n", $tot_in, adjust($tot_in), $tot_out, adjust($tot_out), $tot, adjust($tot))); # get the recipient's email address # ----------------------------------------------------------------------------- my %index; @index{@PC_LIST} = (0 .. $#PC_LIST); my $i = $index{$opt_host}; my $to_addr = $PC_LIST[$i]; $to_addr = $PC_DEFAULT_MAIL unless $PC_REPORT_MAIL[$i]; # get the Monitorix logo # ----------------------------------------------------------------------------- $url = PREFIX . $BASE_URL . "/logo_bot.png"; $ua = LWP::UserAgent->new(timeout => 30); my $logo = $ua->request(HTTP::Request->new('GET', $url)); # get the monthly graph # ----------------------------------------------------------------------------- $url = PREFIX . $BASE_CGI . "/monitorix.cgi?mode=pc.$i&graph=all&when=month&color=$THEME_COLOR&silent=imagetagbig"; $ua = LWP::UserAgent->new(timeout => 30); $ua->request(HTTP::Request->new('GET', $url)); $url = PREFIX . $BASE_URL . "/" . $IMGS_DIR . "pc" . $i . ".month.png"; my $graph = $ua->request(HTTP::Request->new('GET', $url)); # create the multipart container, add attachments and send the message # ----------------------------------------------------------------------------- $msg = new MIME::Lite( From => FROM, To => $to_addr, Subject => SUBJECT . " - $opt_host", Type => "multipart/related", Organization => "Monitorix", ); $msg->attach( Type => 'text/html', Path => $PC_REPORT_LANG . '.html', ); $msg->attach( Type => 'image/png', Id => 'image_01', Data => $logo->content, ); $msg->attach( Type => 'image/png', Id => 'image_02', Data => $graph->content, ); $msg->attach( Type => 'text/plain', Id => 'text_01', Data => join("", @traffic), ); $msg->send('smtp', SMTP_HOST, Timeout => 60); # rename $opt_host file to avoid reusing # ----------------------------------------------------------------------------- my $newname = sprintf("%s.%02u-%u", $USAGE_DIR . $opt_host, $prev_month + 1, $prev_year + 1900); rename($USAGE_DIR . $opt_host, $newname);