#!/usr/bin/perl -w
# This script is intended to grep logs for unknown numbers & if one is found -
# Send a report to admin. Prepared for mgetty logs. May be used for other log 
# files w/ slight modifications.
# Best usage: run via logrotate.conf as PREROTATE script. (if rotating daily)
# License: GNU.
# Warranty: None.
# (c) Olli (olli@digger.org.NOSPAMru,remove NOSPAM before mailing)
# greetz: Corvin, CyberPsyhotic, Vika.
# Thanx : Peter V. Chernikoff (peter@digger.org.NOSPAMru,remove NOSPAM before
# mailing) for help in moving my node to Linux. =)

################### Variables definition:
$admin="root";     # who'll get reports. user or full email .
#$mail="/bin/mail -i -s "; # mailing programme
$mail="/usr/bin/inews -h";
$calledlinks="/tmp/LinksCalledUsToday"; # I don't see any need in informing
                                        # admin about know callers, but anyway
					# - it's quite easy to store somewhere
					# this info. I prefer a file in /tmp 
					# now - it will be overwriten each time
					# this script runs.
# mgetty log files which we should scan.
@logs = qw (/var/log/mgetty.ttyS0
          ); 
# insert line below in brackets above if U need 2 scan more logs - 
# just insert paths one per line.


#               4127346 # Konstantin Lisitsin
#		4973015 # Igor Zejnalow
#		9452508 # Valery Terehin
#		4996948 # Peter Chernikoff
#		9494576 # Evgeniy Shurenko
#		9464232 # Andrey Kudryashov
#		4965320 # Alexandr Novikov
#		9476948 # Den Same
#		4998741 # Vladimir Rezinkin
#		9428410 # Kostya Knyasev
#		3054923 # Andrey Fedorov
#		9313746 # Serge N. Pokhodyaev
#		1590817 # Tihon Krasovsky
#		9312950 # Anton_Kazennikov
#		5730651 # Max Maximov
#		4987083 # Dmitry (Guru)			.8
# insert phones of your usual modem callers, one per line, w/o comments.
@known_cids = qw(	4127346
			4973015
			9452508
			4996948
			9494576
			9464232
			4965320
			9476948
			4998741
			9428410
			3054923
			9313746
			1590817
			9312950
			5730651
			4987083
          );

############### Local vars. Do NOT edit below.
local (%opts,$logfile,$number,$found=0,$known,$mail_opened,$cid,@unknown_cids);
use Getopt::Long; # command line processing module.
############### proggie goes:
GetOptions (\%opts, "help|?" ) || die "Try '--help or --?' enstead." ;
if ($opts{help})
{ print "\nThis is log scanner. (c) Olli (olli\@digger.org.ru)\n\n";
  print "License: GNU. Warranty: None.\n";
  print "Made for pointing admin attantion at unknown callers via email.\n";
  print "--help\nGet help.\n\tThis one screen.\n\n";
  print "No other options implemented.\n";
  print "Edit variables definitions inside the script to satisfy your needs.\n";
  exit;
# The following opts declared:
# --help
# --?
}
$mail_opened=0; # We will generate one report for all. If wanna separate
                # report - move it to the place after bracket (3d line lower):
open (LINKS,">$calledlinks") || die "can't open for write $! .";
foreach $logfile (@logs)
 {open(LOGF,"<$logfile");  
  while (<LOGF>)
  {if ( /caller=\'(\d+)\:(\d+)\'/) # sample: caller='9452508:1'
   {$number=$1; $status=$2; # who called & wich priority he(she) has at ATS.
    $found=0;
    foreach $known (@known_cids) 
     {if ("$known" eq "$number") {$found=1; next; } } # found=1 -> known number
    if (!$found)
     {if (!$mail_opened) 
     {if ($mail eq "/bin/mail -i -s ")
       {open( MAIL, "|$mail \"unknown callers\" $admin" ) || die\
      "Can't open MAIL: $!.\n";   $mail_opened=1;
       }
      else 
       {open( MAIL, "|$mail" ) || die\
      "Can't open MAIL: $!.\n";   $mail_opened=1;}
      }
      $uniq=1;
      foreach $cid (@unknown_cids)
      {if ("$cid" eq "$number:$status")  {$uniq=0; next;}}
      if ($uniq) {push (@unknown_cids,"$number:$status");} # admin should know
    if ("$found" eq "yes" ) {print LINKS "$number\n";}
    }
   }
  } 
  close (LOGF);
 }
unless ($mail eq "/bin/mail -i -s ")
{print MAIL "Newsgroups: br.stats\n";
 print MAIL "From: olli\@olli.digger.org.ru\n";
 print MAIL "Subject: ленивцы (ежедневная статистика)\n";
 print MAIL "X-Comment-To: All\n";
 print MAIL "X-Flags: m\n";
 print MAIL "Path: fidogate!not-for-mail\n";
 print MAIL "Content-Type: text/plain; charset=koi8-r\n";
 print MAIL "Content-Transfer-Encoding: 8bit\n\n\n";
 print MAIL "Эти кексы до сих пор не прислали свои телефоны:\n";
}

foreach $cid (@unknown_cids)
{print MAIL "unknown caller $cid !\n";
}
close (MAIL);
close (LINKS);
exit;
