| 1 |
#!/usr/bin/perl |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use warnings; |
|---|
| 5 |
|
|---|
| 6 |
use Unix::Syslog qw(:subs); |
|---|
| 7 |
use Unix::Syslog qw(:macros); |
|---|
| 8 |
use Date::Manip; |
|---|
| 9 |
|
|---|
| 10 |
my $IRC_HOME = "/home/ircbot"; |
|---|
| 11 |
my $BOT_DIR = "$IRC_HOME/Bots"; |
|---|
| 12 |
|
|---|
| 13 |
my $PAGE_EMAIL = "page\@example.org"; |
|---|
| 14 |
|
|---|
| 15 |
{ |
|---|
| 16 |
my %messageHistory; |
|---|
| 17 |
|
|---|
| 18 |
sub WarnLog ($$) { |
|---|
| 19 |
my($now, $message) = @_; |
|---|
| 20 |
|
|---|
| 21 |
syslog LOG_INFO, $message; |
|---|
| 22 |
|
|---|
| 23 |
my $lastTime = $messageHistory{$message}; |
|---|
| 24 |
|
|---|
| 25 |
my $sendIt = 0; |
|---|
| 26 |
if (not defined $lastTime) { |
|---|
| 27 |
$sendIt = 1; |
|---|
| 28 |
} else { |
|---|
| 29 |
my $err; |
|---|
| 30 |
my $sinceLast = DateCalc($lastTime,"+ 10 minutes",\$err); |
|---|
| 31 |
$sendIt = 1 if ($now gt $sinceLast); |
|---|
| 32 |
} |
|---|
| 33 |
if ($sendIt) { |
|---|
| 34 |
open(SENDMAIL, "|/usr/sbin/sendmail -f root\@example.org -t"); |
|---|
| 35 |
print SENDMAIL "To: $PAGE_EMAIL\nFrom: root\@example.org\n", |
|---|
| 36 |
"Subject: IRC, LOG\n\n$message\n.\n"; |
|---|
| 37 |
close(SENDMAIL); |
|---|
| 38 |
if ($? != 0) { |
|---|
| 39 |
syslog LOG_INFO, "Unable to perform sendmail: $!"; |
|---|
| 40 |
} else { |
|---|
| 41 |
$messageHistory{$message} = $now; |
|---|
| 42 |
} |
|---|
| 43 |
} |
|---|
| 44 |
} |
|---|
| 45 |
} |
|---|
| 46 |
############################################################################### |
|---|
| 47 |
%ENV = (); |
|---|
| 48 |
$ENV{PATH} = "/bin:/usr/bin"; |
|---|
| 49 |
$ENV{HOME} = "$IRC_HOME"; |
|---|
| 50 |
$ENV{USER} = $ENV{LOGNAME} ='ircbot'; |
|---|
| 51 |
$ENV{SHELL} = "/bin/bash"; |
|---|
| 52 |
|
|---|
| 53 |
chdir("$BOT_DIR/Scripts") |
|---|
| 54 |
or WarnLog(ParseDate("today"), "unable to change to $BOT_DIR: $!"); |
|---|
| 55 |
|
|---|
| 56 |
my %bots = (log => { pidFile => "$IRC_HOME/pids/log-bot.pid", |
|---|
| 57 |
scriptFile => "$BOT_DIR/Scripts/logging-bot.plx" }, |
|---|
| 58 |
tem => { pidFile => "$IRC_HOME/pids/temp-bot.pid", |
|---|
| 59 |
scriptFile => "$BOT_DIR/Scripts/temp-bot.plx" }, |
|---|
| 60 |
tim => { pidFile => "$IRC_HOME/pids/time-bot.pid", |
|---|
| 61 |
scriptFile => "$BOT_DIR/Scripts/time-bot.plx" }, |
|---|
| 62 |
sflc_log => { pidFile => "$IRC_HOME/pids/freenode-log-bot.pid", |
|---|
| 63 |
scriptFile => "$BOT_DIR/Scripts/freenode-logging-bot.plx" } |
|---|
| 64 |
); |
|---|
| 65 |
|
|---|
| 66 |
foreach my $key (keys %bots) { |
|---|
| 67 |
my $pid; |
|---|
| 68 |
if (-f $bots{$key}{pidFile}) { |
|---|
| 69 |
open(PID_FILE, "<$bots{$key}{pidFile}") |
|---|
| 70 |
or WarnLog(ParseDate("today"), "unable to open $bots{$key}{pidFile}: $!"); |
|---|
| 71 |
|
|---|
| 72 |
$pid = <PID_FILE>; |
|---|
| 73 |
close PID_FILE; |
|---|
| 74 |
chomp $pid; |
|---|
| 75 |
|
|---|
| 76 |
system("/bin/ps -p $pid > /dev/null 2>&1"); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
if ( (not defined $pid) or $pid eq "" or $? != 0) { |
|---|
| 80 |
unlink($bots{$key}{pidFile}); |
|---|
| 81 |
system("/usr/bin/nohup /usr/bin/perl -I $BOT_DIR/Modules -I $BOT_DIR/Bot-BasicBot/lib $bots{$key}{scriptFile} >> $IRC_HOME/logs/${key}.log 2>&1 &"); |
|---|
| 82 |
WarnLog(ParseDate("today"), "unable to launch $key bot: $!") |
|---|
| 83 |
unless ($? == 0); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
############################################################################### |
|---|
| 88 |
# |
|---|
| 89 |
# Local variables: |
|---|
| 90 |
# compile-command: "perl -c bot-run-check.plx" |
|---|
| 91 |
# End: |
|---|
| 92 |
# |
|---|