#!/usr/bin/perl
#
#  ----------------------------
# | LogDog Installation Script |
#  ----------------------------
#
# Written by Brandon Zehm
# caspian@linuxfreak.com
#
#




# Clear the screen and get started.
system "clear";
print "Welcome to the installation of LogDog!\n";
print "--------------------------------------\n";
print "\n";
print "The following questions will be used to configure LogDog.\n";
print "When the questioning is complete, a configuration file will\n";
print "be generated and written.\n";
print "\n";
print "Push ENTER to continue.\n";
getc;
system "clear";


# Interval
print "How often should I scan the log files for problems? (in seconds)\n";
print "\n";
print "Interval [30]: "; 
$interval = <>; chomp $interval; 
if ($interval eq '') { $interval = 30; }
print "\n\n";


# Email Server
print "LogDog needs a normal email server (smtp server) to connect\n";
print "to so it can send email.\n";
print "\n";
print "Email server [localhost]: ";
$email_server = <>; chomp $email_server; 
if ($email_server eq '') { $email_server = 'localhost'; }
print "\n\n";


# Email Program
print "LogDog uses sendEmail to send email. (suprise)\n";
print "I use sendEmail because it's small, lightweight, and because\n";
print "most Linux servers don't have sendmail installed on them.\n";
print "I just need to know where sendEmail is.  If you need it\n";
print "you can download it from http://marvin.criadvantage.com/caspian\n";
print "\n";
print "sendEmail [/usr/local/bin/sendEmail]: "; 
$email_program = <>; chomp $email_program; 
if ( $email_program eq '') { $email_program = '/usr/local/bin/sendEmail'; }
print "\n\n";


# Email From
print "Who should the email generated by LogDog come from?\n";
print "\n";
print "From [logdog\@linuxrules.com]: "; 
$email_from = <>; chomp $email_from; 
if ( $email_from eq '') { $email_from = 'logdog@linuxrules.com'; }
print "\n\n";


# After Hours Command
print "This is an optional command that, if defined, will be executed when an alert\n";
print "is generated if the time is after 6pm or the day is Saturday or Sunday.\n";
print "You have one variable for your use, 'CRITICAL_DATA'.  It is the list of lines\n";
print "that are tagged 'critical'. Just comment this line out if you don't need it.\n";
print "\n";
print "I personally use this for sending a page to my cell phone.\n";
print "Example: 'sendSMS.pl 9998887777 2223334444 CRITICAL_DATA'\n";
print "\n";
print "Command []: "; 
$after_hours_command = <>; chomp $after_hours_command; 
print "\n\n";


# Admin List
print "Now I need to know what email addresses I should send alerts to.\n";
print "This should be a comma separated list of email addresses.\n";
print "Example: admin\@company.com,admin2\@company.com\n";
print "\n";
until ( $admin_list[0] ne '' ) {
  print "Notify list: "; 
  @admin_list = split /,/,<>; chomp @admin_list;
}
print "\n\n";


# File List
print "What files should I be watching?\n";  
print "WARNING!!  This program keeps track of what entries have been\n"; 
print "already scanned by the date field found in syslog generated\n"; 
print "log files. If you specify a file which does not have properly\n"; 
print "formated date entries at the begining of each line you will\n"; 
print "have serious problems!\n";
print "This should be a comma separated list of files.\n";
print "\n";
print "File List [/var/log/messages,/var/log/syslog]: "; 
@file_list = split /,/,<>; chomp @file_list;
if ( $file_list[0] eq '') { @file_list = ('/var/log/messages','/var/log/syslog'); }
print "\n\n";


# Warn List
print "What key words/phrases should generate a warning flag?\n";  
print "This should be a comma separated list of words.\n";
print "\n";
print "Warn List [fail,fatal,refuse,breakin,warning,\n";
print "           error,unauthorized,detected,expired\n";
print "           useradd,userdel,passwd]: "; 
@warn_list = split /,/,<>; chomp @warn_list;
if ( $warn_list[0] eq '') { @warn_list = ('fail','fatal','refuse','breakin','warning','error','unauthorized','detected','expired','useradd','userdel','passwd'); }
print "\n\n";



# Ignore List
print "Lines with any of these key words will be totally ignored.\n";  
print "This should be a comma separated list of words.\n";
print "\n";
print "Ignore List [named,sshd]: ";
@ignore_list = split /,/,<>; chomp @ignore_list;
if ( $ignore_list[0] eq '') { @ignore_list = ('named,sshd'); }
print "\n\n";





########################################### 
# Generate logdog.conf from received data #
###########################################

&choose_location;

print "Generating logdog.conf...\n\n";  

until ( open (FILE, ">$logdog_conf_file") ) {
  &open_file_error;
}

print FILE <<EOM;
# This is the configuration file for LogDog.
# ------------------------------------------
#
# This file is executed as native Perl code, so syntax
# is extremly important.  For security reasons this file
# should only be writable by root.
#
# This file should be placed in /etc, or you will need
# to specify where it is located on the command line.
# See 'logdog.pl --help' for details.
#


\$interval = $interval;                                        # The number of seconds to sleep between each log check.
\$email_server = '$email_server';          # A normal SMTP email server.
\$email_program = '$email_program';           # Path and name of to the sendEmail program. (www.webpak.net/~god)
\$email_from = '$email_from';  # Email generated by this program will be sent from this email address.

# <after_hours_command>
# This is an optional command that, if defined, will be executed when an aleart
# is generated if the time is after 6pm or the day is Saturday or Sunday.
# You have one variable for your use, 'CRITICAL_DATA'.  It is the list of lines
# that are tagged 'critical'. Just comment this line out if you don't need it.
# I personally use this for sending a page to my cell phone via my sendSMS.pl program.
\$after_hours_command = "$after_hours_command";


# <admin_list>
# This is a list of email addresses to send alerts to.
\@admin_list = (
EOM
for ( @admin_list ) {
  print FILE "                '$_',\n";
}
print FILE <<EOM;
              );


# <file_list>
# This is a list of files to be monitored by this LogDog.
# WARNING!!  This program keeps track of what entries have been already scanned
# by the date field found in syslog generated log files. If you specify a file
# which does not have properly formated date entries at the begining of each
# line you will have serious problems!
\@file_list = (
EOM
for ( @file_list ) {
  print FILE "               '$_',\n";
}
print FILE <<EOM;
             );


# <warn_list>
# This list contains key words, which when found in the log file
# will generate an alert and email the people in @admin_list
\@warn_list = (
EOM
for ( @warn_list ) {
  print FILE "               '$_',\n";
}
print FILE <<EOM;
             );

# <ignore_list>
# Entries from the log file with any of these strings will be totally ignored.
\@ignore_list = (
EOM
for ( @ignore_list ) {
print FILE "                 '$_',\n";
}
print FILE <<EOM;               
               );

EOM
close FILE;



### chmod LogDog.conf
system("chmod 600 $logdog_conf_file");
system "sleep 1";
system "clear";
print "OK, $logdog_conf_file has been written!\n";
print "If you need to change any options you can edit that file\n";
print "or re-run this script at any time.\n";
print "\n";
print "One last thing.  Where should I put logdog.pl?\n";
print "\n";
print "Location [/usr/local/bin]: "; 
$location = <>; chomp $location; 
if ($location eq '') { $location = '/usr/local/bin'; }
print "\n\n";
system("cp logdog.pl $location");


system "clear";
print "Done!  The only thing left for you to do is edit\n";
print "one of your startup scripts and add a line to start\n";
print "LogDog.  Typically you will edit /etc/rc.d/rc.local\n";
print "and add a line like '/usr/local/bin/logdog.pl -d'\n";
print "\n";
print "I hope you find LogDog useful!  If you have any comments\n";
print "or suggestions feel free to contact me.\n";
print "Brandon Zehm <caspian\@linuxfreak.com>\n";
print "\n";
print "\n";

# We are done, so lets exit.
exit;



########################
# DEFINE SUB ROUTINES
########################


# Conf File Location
sub choose_location {
  system "clear";
  print "All options are now defined.\n";
  print "I now need to know where to save the configuration file.\n";
  print "NOTE: If you choose anything other than /etc/logdog.conf\n";
  print "      every time you start logdog you will need to tell\n";
  print "      it where the configuration file is.\n";
  print "      [via command line options]\n";
  print "\n";
  print "Location [/etc/logdog.conf]: "; 
  $logdog_conf_file = <>; chomp $logdog_conf_file; 
  if ($logdog_conf_file eq '') { $logdog_conf_file = '/etc/logdog.conf'; }
  print "\n\n";
  return 1;
}




sub open_file_error {
  print "There was an error opening the file:\n";
  print "$logdog_conf_file\n";
  print "Press [Enter] to return to the 'File Location' dialogue.\n\n";
  <>;
  &choose_location;
  return 1;
}
