* ウェブページ更新監視プログラム [#wa75a7e6]
#!/usr/bin/perl
# ============================================================================
# ウェブページ更新監視プログラム
# ============================================================================
use strict;
use Text::Diff;
use Data::Dumper;
use IO::File;
use Date::Simple qw(today);
use DateTime;
use LWP::UserAgent;
use Getopt::Long;
use Net::SMTP;
use Jcode;
use Cwd;
# ============================================================================
# 定数
# ============================================================================
# HTMLファイルを保存するディレクトリ
my $save_dir = cwd.'/save';
# 取得するページのURL
my %urls = (
url1 =>
'http://www.example.com/foo/index.html',
url2 =>
'http://www.example.com/var/index.html',
);
# 送信元
my $from_mail = 'admin@'.`hostname`; chomp $from_mail;
my $from_name = 'ADMIN User';
# 送信先
my @to_mails = qw(
hanako@example.com
);
# sendmailのパス
my $sendmail_path = '/usr/sbin/sendmail';
# ============================================================================
# メイン
# ============================================================================
# ----------------------------------------------------------------------------
# 前処理
# ----------------------------------------------------------------------------
umask 0111;
#$ENV{PATH} = "/usr/sbin:".$ENV{PATH};
# ----------------------------------------------------------------------------
# 引数処理
# ----------------------------------------------------------------------------
my $debug;
my $send_mail;
GetOptions('debug' => \$debug,'send_mail' => \$send_mail);
die "usage: $0 [--send_mail] [--debug]\n" if !$debug && !$send_mail;
# ----------------------------------------------------------------------------
# HTMLファイルを取得・保存
# ----------------------------------------------------------------------------
my $today = today();
my $ua = LWP::UserAgent->new;
$ua->agent("example.com");
while (my ($name,$url) = each(%urls)) {
my $req = HTTP::Request->new(get => $url);
my $res = $ua->request($req);
if ($res->is_success) {
my $out = new IO::File "> $save_dir/${name}_${today}.html" or die $!;
print $out $res->content;
$out->close;
} else {
print $res->status_line, "\n";
}
}
# ----------------------------------------------------------------------------
# diffを実行
# ----------------------------------------------------------------------------
my %diff;
while (my ($name,$url) = each(%urls)) {
# 前日以前のHTMLファイルを探し、しばらく前まで日付を遡っても見つからない
# ならエラー
my $hit;
my $yesterday = today() - 1;
for (my $i=2,my $limiter=60;$i<$limiter;$i++,$yesterday = today() - $i) {
if (-f "$save_dir/${name}_${yesterday}.html") {
$hit = 1; last;
}
}
die "sorry. no old day file of $name" unless $hit;
# diffを実行
my $diff = diff(
"$save_dir/${name}_${yesterday}.html",
"$save_dir/${name}_${today}.html",
{ STYLE => "Unified" });
if ($diff) {
$diff{$name} = Jcode->new($diff,'sjis')->euc;
} else {
}
}
# ----------------------------------------------------------------------------
# メール送信
# ----------------------------------------------------------------------------
while (my ($name,$diff) = each(%diff)) {
my $mesg = <<"END";
ウェブページが更新されました。
URL: $urls{$name}
---------------------------------------------------------------------------
$diff
END
if ($send_mail) {
send_mail( (
from_mail => $from_mail,
from_name => $from_name,
to_mail => [@to_mails],
subject => 'ウェブページ更新監視',
mesg => $mesg,
));
} elsif ($debug) {
print "# ======================================================================\n";
print "# $name\n";
print "# $urls{$name}\n";
print "# ======================================================================\n";
print "$diff\n";
}
}
exit 0;
sub send_mail {
my (%arg) = @_;
my $from = sprintf '"%s"<%s>',
jcode($arg{from_name})->mime_encode(),
$arg{from_mail};
my @to = (ref $arg{to_mail} eq 'ARRAY')? @{$arg{to_mail}} : ($arg{to_mail});
my $to = join ',', @to;
my $subject = Jcode->new($arg{subject},'euc')->mime_encode();
my $dt = DateTime->now();
my $date = sprintf "%s, %d %s %04d %02d:%02d:%02d +0900 (JST)",
$dt->day_abbr, $dt->day, $dt->month_abbr, $dt->year,
$dt->hour, $dt->minute, $dt->second;
my $mesg = Jcode->new($arg{mesg},'euc')->jis;
my $smtp = Net::SMTP->new('localhost');
$smtp->mail($from);
$smtp->to($to);
# ヘッダ
$smtp->data();
$smtp->datasend("Date: $date\n");
$smtp->datasend("From: $from\n");
$smtp->datasend("To: $to\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("Content-Transfer-Encoding: 7bit\n");
$smtp->datasend("Content-Type: text/plain;charset=\"ISO-2022-JP\"\n\n");
$smtp->datasend("\n");
# 本文
$smtp->datasend("$mesg\n");
# メール送信
$smtp->dataend();
$smtp->quit;
}