Logging

Create a DropBox account and install the commandline tools for DropBox

http://xmodulo.com/access-dropbox-command-line-linux.html
Create a directory for logs, e.g. /home/logging/logs and a script directory e.g. /home/logging/script

Create the „houskeeping.pl“ script:

#!/usr/bin/perl -w
use warnings;

my $debug =0;
# Unix Commands
my $ldate = "/bin/date";
my $lgrep = "/bin/grep";
my $lzgrep = "/bin/zgrep";
my $lhost = "/usr/bin/host";
my $lgzip = "/bin/gzip";
my $lzip = "/usr/bin/zip";
my $ldropb = "/home/logging/script/dropbox_uploader.sh upload";


# Directories and Filenames
my $varlog = "/var/log";
my $slogfiles = "syslog*";
my $mlogfiles = "messages*";
my $ChillyNCon = "ChillyNCon*";
my $BackupDir = "/home/logging/logs";
my $ZIPPW = "<????????>";
my $dropdir = "logs";

my $UAMLOGIN = "'UAM login'";
my $MACIP = "'Client MAC='";
my $MESSMACIP = "'Granted MAC='";

# Variable declaration
my ($YESTERDAYMD,$YESTERDAYM,$YESTERDAYD,$YESTERDAY,$index,$line,$linep1,$linep2,$linep3,$filename);
my @MATCH=();
my @Match2=();
my @REVIP=();
my @LINEPARTS=();
my @LINEPARTS2=();


$YESTERDAYMD = `$ldate -d '1 day ago' +'%b\ %e'`; 
chomp ($YESTERDAYMD);
$YESTERDAYMD = "'$YESTERDAYMD'";
$YESTERDAYM = `$ldate -d '1 day ago' +'%b'`;
chomp ($YESTERDAYM);
$YESTERDAYM = "'$YESTERDAYM'";
$YESTERDAYD = `$ldate -d '1 day ago' +'%d'`;
chomp ($YESTERDAYD);
$YESTERDAYD = "'$YESTERDAYD'";
$YESTERDAY = `$ldate -d '1 day ago' +'%Y%m%d'`;
chomp ($YESTERDAY);
if ($debug) {print "Command: $lzgrep $UAMLOGIN $varlog/$slogfiles | $lgrep $YESTERDAYMD \n";}

$filename="$BackupDir"."/"."$YESTERDAY".".log";
open(DAILYLOG, '>', $filename) or die "Couldn't open file $filename, $!";

# Get Mac to IP
@MATCH = `$lzgrep $MACIP $varlog/$slogfiles | $lgrep $YESTERDAYMD`;

printf DAILYLOG "Got: %d logins from syslog at: $YESTERDAY\n", $#MATCH + 1;
foreach $line (@MATCH)
{
  chomp($line);
  print DAILYLOG "$line\n";
}

@MATCH=();
print DAILYLOG "\n\n--------------------------\n\n";
@MATCH = `$lzgrep $MESSMACIP $varlog/$mlogfiles | $lgrep $YESTERDAYMD`;

printf DAILYLOG "Got: %d logins from messages at: $YESTERDAY\n", $#MATCH + 1;
foreach $line (@MATCH)
{
  chomp($line);
  print DAILYLOG "$line\n";
}

print DAILYLOG "#########################\n\n";
@MATCH=();

# Get UAM Logins
@MATCH = `$lzgrep $UAMLOGIN $varlog/$slogfiles | $lgrep $YESTERDAYMD`;

printf DAILYLOG "Got: %d logins at: $YESTERDAY\n", $#MATCH + 1;
foreach $line (@MATCH)
{
  chomp($line);
  print DAILYLOG "$line\n";
}
print DAILYLOG "#########################\n\n";
@MATCH=();


@Match2 = `$lzgrep $YESTERDAYMD $varlog/$ChillyNCon`;
foreach $linep2 (@Match2)
{
  if ($debug) {print "$linep2\n";}
  # drop port 53 (DNS) DPT=53

  $index = index($linep2, "DPT=");
  $index = $index + 4;
  $linep3 = substr($linep2,$index,2);
  chomp ($linep3);
  $linep3 =~ s/^\s+|\s+$//g;
  if ($linep3 == 53)
  {
    if ($debug) {print "DROP LINE DNS !! index: $index Port: $linep3\n"; }
    next; 
  }

  $index = index($linep2, "DST=");
  $linep3 = substr($linep2,$index,22);
  if ($debug) {print "index: $index IP: $linep3\n"; }
  @LINEPARTS2= split(" ",$linep3); 
  $linep3 =  substr($LINEPARTS2[0],4);
  if ($debug) {print "Dest IP: $linep3\n";}
  @IPREV = split (/\./,$linep3);
  $linep3="";
  $linep3 = "$IPREV[3]"."."."$IPREV[2]"."."."$IPREV[1]"."."."$IPREV[0]";
  if ($debug) {print "Command: $lhost $linep3\n";}
  $linep4 = `$lhost $linep3`; 
  chomp($linep2);
  chomp($linep4);
  print DAILYLOG "$linep2 $linep4\n";
  @LINEPARTS2=();
  @IPREV=();
}
@Match2=();  
close DAILYLOG;
$line = `$lgzip $filename`;

# Upload Dropbox
$filename = "$filename".".gz";
$line = `$ldropb $filename $dropdir`;
exit 0;
## end

Configure CRON to execute the houskeeping script every night at 10 Minutes past 3 o'clock.

crontab -l
#*     *     *   *    *        command to be executed
#-     -     -   -    -
#|     |     |   |    |
#|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
#|     |     |   +------- month (1 - 12)
#|     |     +--------- day of        month (1 - 31)
#|     +----------- hour (0 - 23)
#+------------- min (0 - 59)
10 3 * * * /home/logging/script/housekeeping.pl > /home/logging/logs/houskeeping.log 2>&1

Back to HotSpot Overview-Page

f11/technik/raspberrypi/hotspotdm3kb/rasplogging.txt · Zuletzt geändert: 2019/09/29 12:03 (Externe Bearbeitung)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0