X-Git-Url: https://git.llucax.com/z.facultad/75.08/llamadas.git/blobdiff_plain/de75f70bd685c8851fcf6ad66d86d53e4b2739ed..8924bf6b7d40b1b543706926d806e4104db61393:/inst/antifraude.pl diff --git a/inst/antifraude.pl b/inst/antifraude.pl index 8417306..6583252 100644 --- a/inst/antifraude.pl +++ b/inst/antifraude.pl @@ -1,5 +1,29 @@ #!/usr/bin/perl +sub is_lock { + if ( -e "$ENV{'HOME'}/.antifraude/lock/antifraude.pid" ) { + # Lock file encontrado + return 1; + } + # No hay lock file! + return 0; +} + +sub unlock { + unlink ("$ENV{'HOME'}/.antifraude/lock/antifraude.pid"); +} + +sub lock { + if (!is_lock()) { + local $lfile = "$ENV{'HOME'}/.antifraude/lock/antifraude.pid"; + open(LOCKFILE,">>$lfile"); + print LOCKFILE $$; + close(LOCKFILE); + return 1; + } + return 0; +} + # Comparador de fecha para los archivos de llamada sub byDate { ($year1,$mon1,$day1,$hr1,$min1) = $a =~ /([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/; @@ -40,7 +64,8 @@ sub getConfVar { do { $line = } until $. == $linenumber; close(CONFIGFILE); chop($line); - ($confvar = $line) =~ s/^.*= (.*)/$1/; + ($confvar = $line) =~ s/"(.*)"$/$1/; + $confvar =~ s/^.*=//; return $confvar; } @@ -51,15 +76,29 @@ sub logEntry { my $log = "$CONFDATA{logdir}/$CONFDATA{logfile}"; # Fetch date and Format it ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); - $year += 1900; $year = sprintf("%02d", $year % 100); ++$mon; - $user = getlogin || "Unidentified"; - $commonstring = "[$mday/$mon/$year $hour:$min:$sec|$user|antifraude]"; + $mon =~ s/^(\d)$/0$1/; + $mday =~ s/^(\d)$/0$1/; + $hour =~ s/^(\d)$/0$1/; + $min =~ s/^(\d)$/0$1/; + $year += 1900; ++$mon; + $user = "$ENV{USER}"; + $commonstring = "$mday/$mon/$year-$hour:$min $user antifraude:"; # Append to log open(LOGFILE,">>$log") or die "No se pudo abrir el archivo de log"; seek(LOGFILE,0,2); - print LOGFILE "$commonstring - $logentry\n"; + print LOGFILE "$commonstring \"$logentry\"\n"; + $filesize = tell(LOGFILE); close(LOGFILE); + # Chequeo el logsize y su lo supero me quedo con 100 lineas + if ($filesize > $CONFDATA{logsize}) + { + open(LOGFILE,"+<$log"); + do ($line = ) until $. == 100; + truncate(LOGFILE,tell(LOGFILE)); + close(LOGFILE); + } + if ($consoleout) { print("$logentry\n"); } } @@ -72,8 +111,8 @@ sub badCall { } sub checkUmbrales { - local(*callfields) = @_[0]; - my $callsfile = @_[1]; + local(*callfields) = $_[0]; + my $callsfile = $_[1]; my $matchedUmbral = 0; my $i = 0; @@ -98,13 +137,19 @@ sub checkUmbrales { if ($matchedUmbral > 0) { # Obtengo algunos datos ($central = $callsfile) =~ s/^.*\.//; - $user = getlogin || 'Unidentified'; + $user = "$ENV{USER}"; # Fetch date and Format it ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; ++$mon; - $date = "$year$mon$mday"; - $time = "$hour$min$sec"; + $mon =~ s/^(\d)$/0$1/; + $mday =~ s/^(\d)$/0$1/; + $hour =~ s/^(\d)$/0$1/; + $min =~ s/^(\d)$/0$1/; + $sec =~ s/^(\d)$/0$1/; + $user = "$ENV{USER}"; + $date = "${year}${mon}${mday}"; + $time = "${hour}${min}${sec}"; # Preparo el registro de alarma $alarmEntry = "$callfields[7];$central;$callfields[0];$callfields[1];$regid;$callfields[2];$callfields[3];$user;$date;$time"; @@ -122,31 +167,22 @@ sub checkUmbrales { } # --------- MAIN CODE -------- # -# En la version final, recibo por param el directorio del .conf, por ahora -# recibo un dir donde tengo archivos de llamadas -if ((!$ARGV[0]) || (! -d $ARGV[0])) { - print("No se ha ingresado un directorio fuente de llamadas\n"); - exit 1; -} +# File locking.. +if (is_lock()) { exit 1; } +lock(); + # Defino some GLOBALS -$CONFDIR = shift; -$CONFDATA{logdir} = getConfVar("$CONFDIR/afinstal.conf",12); -$CONFDATA{logfile} = getConfVar("$CONFDIR/afinstal.conf",13); -$CONFDATA{logsize} = getConfVar("$CONFDIR/afinstal.conf",14); -$CONFDATA{datadir} = getConfVar("$CONFDIR/afinstal.conf",15); -$CONFDATA{alarmlog} = "$CONFDIR/alarmas/alarmas.txt"; +$CONFDIR = "$ENV{'HOME'}/.antifraude"; +$CONFDATA{logdir} = getConfVar("$CONFDIR/conf/antifraude.conf",2); +$CONFDATA{logfile} = getConfVar("$CONFDIR/conf/antifraude.conf",3); +$CONFDATA{logsize} = getConfVar("$CONFDIR/conf/antifraude.conf",4); +$CONFDATA{datadir} = getConfVar("$CONFDIR/conf/antifraude.conf",5); # Cargo los UMBRALES en memoria open(PARAMFILE,"$CONFDIR/umbrales.param") or die "No se pudo abrir el archivo de umbrales"; @UMBRALES = ; close(PARAMFILE); -# For Debug Only -print("\nLogdir: $CONFDATA{logdir}\n"); -print("Logfile: $CONFDATA{logfile}\n"); -print("Logsize: $CONFDATA{logsize}\n"); -print("Datadir: $CONFDATA{datadir}\n\n"); - # Proceso los archivos de llamadas @archivos = getCallFiles(); FILE: foreach $filename (@archivos) { @@ -186,3 +222,6 @@ FILE: foreach $filename (@archivos) { logEntry("Fin proceso de: $filename",1); } + +# Unlocking +unlock();