Perl Help - Use of uninitialised values

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
I have no idea what to try, i have traced the problem to the like

process_log($line);

But have no idea how to fix it as i declare $line before hand :s

The error i get is:
Code:
Use of uninitialized value $sessionState in string eq at parser2.pl line 124.
 at parser2.pl line 124
    main::process_log('Dec 16 07:50:22 vpn-klb-prv 1765156 12/16/2007 07:50:21.110 S...') called at parser2.pl line 54

Use of uninitialized value in string eq at parser2.pl line 130.
 at parser2.pl line 130
    main::process_log('Dec 16 07:50:22 vpn-klb-prv 1765156 12/16/2007 07:50:21.110 S...') called at parser2.pl line 54
so on and so forth..

Code:
#!/usr/bin/perl -w
                  #-w use warnings
                  #-T use Taint to ensure program is secure

#use strict 'vars';                     #Force variable declarations
# Functions to locate errors
use Carp ();
local $SIG{__WARN__} = \&Carp::cluck;


#=== Delcare Varibles ===#
my $line = "empty";             #Line to be processed
my $file = 'testLog.txt';       #Path to log file
my @log = ();                        #Stores lines from log file

# Session Data Array as global (to avoid un-neccesary processing when passed into function)
our @sessions = (
    # Session Data Holding Variables
    {'clientIPAddress' => 'null',    #Address assigned from IP pool
     'currentCode' => 'Initialiser', #Current IKE code being processed
     'connectDate' => 'null',        #Date of connection to access point
     'connectTime' => 'null',        #Time of connection to access point
     'userID' => 'null',             #Username unique to user
     'authenticationTime' => 'null', #Time user's has been authenticated
     'groupID' => 'null',            #VPN group user belongs to (usually Roamnet)
     'platformType' => 'null',       #Platform user is running Windows, Unix etc..
     'clientVersion' => 'null',      #Version of VPN client
     'vpnPoolAddress' => 'null',     #IP address assigned from VPN pool
     'disconnectTime' => 'null',     #Time session was disconnected
     'duration' => 'null',           #Duration of session
     'bytesTx' => 'null',            #Bytes Transmitted
     'bytesRx' => 'null',            #Bytes Recieved
     'reasonForDisconnect' => 'null',#Reason for connection termination
     'errorMessages' => 'null',      #Additional error messages
     'state' => 'Disconnected'       #State of session
   }
);


#=== Get data from file ===
open(LOG, $file) or die "Log file not found";    #Open file under the handle LOG
@log=<LOG>;          #Read file into @log
close(LOG);          #Close the log file

foreach (@log) {
 $line = $_;
  if($line eq 'empty'){                            #Check to see if line is empty
      print 'Warning Line is Empty <BR>\n';
  }else{
  #===Process line ===
  chomp($line);       #Removes new line character from entry


  process_log($line);                    #Call function to precess log line <--------- line 54
  }

}
line 124
Code:
sub process_log { #Script to parse logs

 #== Parameters passed into function ==
 #$line (@_) line being processed

 #== Declare Variables ==
 my $currentCode = 'null';                          #Code being processed
 my $currentIP = 'null';                            #IP of line being processed
 my $currentUserID = 'null';                        #UserID of line being processed
 my $numberOfSessions = 'null';                     #Number of sessions in the array
 my $data = 'null';                                 #Stores result of regex expressions
 my $currentLine = 'null';                          #Whole Line currently being processed
 my @sepearatedLine = ();                                #Seperated line
 my $sessionExists = 'false';                       #Boolean check for Does session exist

 #=== Store elements from line ===
 $currentLine = shift;                              #Get line passed into function
 @seperatedLine = split(/\s/, $currentLine);        #Split line based on empty space delimitation

 $currentLine =~ m/SEV=\d (.*) RPT/;                #Extract current code from line
 $currentCode = $1;                                 #Get current code
 $currentIP = $seperatedLine[10];                   #Get IP from line
 $numberOfSessions = scalar (@sessions);            #Get number of elements sessions array



 # IKE/DBG64
 if ($currentCode eq 'IKEDBG/64'){
      for (my $i=1; $i <= $numberOfSessions; $i++){               #Check to see if session already exists
        my $sessionIP = $sessions[$i]{"clientIPAddress"};         #Get IP for selected session

        if ($currentIP eq $sessionIP){                            #Compare IP
        #-Session Exists-
          $sessionExists = 'true';
          #Check for illegal connections (session not terminated correctly)
          my $sessionState = $sessions[$i]{"state"};              #Get current state of session
                    if ($sessionState eq 'Disconnected'){         <---------------------------------- line 124
           #--Legal Session--
           $sessions[$i]{"state"} = 'Connecting';                 #Progress state to connecting
           last;                                                  #Break out of loop
           }else{
           #--Illegal Session--
           if ($sessions[$i]{"errorMessages"} eq 'null'){            #Check if there are any error messages
             $sessions[$i]{"errorMessages"} = 'Errors: ';            #Add title
           }
           #Error Message
           $sessions[$i]{"reasonForDisconnect"} = 'Session was not teminated correctly and has been termintated by parser';
           save_session($i);                                       #Call save_session function

           splice(@sessions,$i,1);                                 #Delete the current session
           #Create new session
           create_new_session($i);
           $sessions[$i]{"clientIPAddress"} = $seperatedLine[10];  #Used as key for session
           $sessions[$i]{'currentCode'} = $currentCode;            #Store current code for debugging
           $sessions[$i]{"connectDate"} = $seperatedLine[5];
           $sessions[$i]{"connectTime"} = $seperatedLine[6];
           last;                                                                  #Break out of loop
          }
        }
      }
      if ($sessionExists eq 'false') {
        #-Session doesn't exist-
          #Create new session
          create_new_session($numberOfSessions);                                 #As the array starts from 0, $numberOfSessions will
                                                                                 #always be greater than the index for the last session.
          $sessions[$numberOfSessions]{"clientIPAddress"} =  $seperatedLine[10]; #Used as key for session
          $sessions[$numberOfSessions]{'currentCode'} = $currentCode;            #Store current code
          $sessions[$numberOfSessions]{"connectDate"} = $seperatedLine[5];
          $sessions[$numberOfSessions]{"connectTime"} = $seperatedLine[6];
      }

 }

function create_new_session
Code:
sub create_new_session {            #Script to create a blank session

 my $sessionID = shift;                                  #Session ID
 # Create empty session
 $sessions[$sessionID]{'clientIPAddress'} = 'null';    #Address assigned from IP pool
 $sessions[$sessionID]{'currentCode'} = 'null';        #Current IKE code being processed
 $sessions[$sessionID]{'connectDate'} = 'null';        #Date of connection to access point
 $sessions[$sessionID]{'connectTime'} = 'null';        #Time of connection to access point
 $sessions[$sessionID]{'userID'} = 'null';             #Username unique to user
 $sessions[$sessionID]{'authenticationDate'} = 'null'; #Date user's has been authenticated
 $sessions[$sessionID]{'authenticationTime'} = 'null'; #Time user's has been authenticated
 $sessions[$sessionID]{'groupID'} = 'null';            #VPN group user belongs to (usually Roamnet)
 $sessions[$sessionID]{'platformType'} = 'null';       #Platform user is running Windows, Unix etc..
 $sessions[$sessionID]{'clientVersion'} = 'null';      #Version of VPN client
 $sessions[$sessionID]{'vpnPoolAddress'} = 'null';     #IP address assigned from VPN pool
 $sessions[$sessionID]{'disconnectTime'} = 'null';     #Time session was disconnected
 $sessions[$sessionID]{'duration'} = 'null';           #Duration of session
 $sessions[$sessionID]{'bytesTx'} = 'null';            #Bytes Transmitted
 $sessions[$sessionID]{'bytesRx'} = 'null';            #Bytes Recieved
 $sessions[$sessionID]{'reasonForDisconnect'} = 'null';#Reason for connection termination
 $sessions[$sessionID]{'errorMessages'} = 'null';      #Additional error messages
 $sessions[$sessionID]{'state'} = 'Disconnected';      #State of session
} #End Create empty session

Any help would be much appreciated as i am out of ideas,
or if you cant personally even somewhere to ask

Thanks in advance
 

Azathrim

Fledgling Freddie
Joined
Dec 31, 2003
Messages
1,802
In the process_log sub you use a $i. Where do you declare that?

I haven't run or found the warning message you mention though. Feel free to hand a link to a download though. :

anyways, a few hinters.

undef is a false value. Thus, it's better to do this:
my $bool = undef;
if($bool) {
print 'true';
} else {
print false;
}

than using the 'null' thing you are doing.

Also, you do:

@log = <LOG>;

perhaps try to be explicit, thus doing:
@log = (<LOG>);

Yours does work, but it's implicit.
Instead your loop could be updated to do this instead:

while (my $line = <LOG>) {
next if $line =~ m/^\s*$/;
process_log($line);
}
 

Gahn

Resident Freddy
Joined
Jan 16, 2004
Messages
5,056
If it does work like the Session Variable in C# (as it seems), u need to initialize that $sessionState also in the process_log.
My wide guess not being a Perl programmer, but it does make sense oO
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
thanks all, i searched for ages to see if there was a boolean function in perl and most the guides said there werent which is why i used a scalar and gave it true/false values

:)


Azathrim any chance you could pm me your email and i will send you the whole script
 

phlash

Fledgling Freddie
Joined
Dec 24, 2003
Messages
195
Hi Chronictank - I think I've spotted your problem - you are indexing the @sessions array from 1 -> $numberOfSessions, which isn't valid for Perl, as arrays are zero-based, you should be indexing from 0 -> ($numberOfSessions-1).

Check through your logic for detecting a new session, and adding it to the array as well, that looks suspect ATM.

HTH,
Phil.
 

Gahn

Resident Freddy
Joined
Jan 16, 2004
Messages
5,056
Hi Chronictank - I think I've spotted your problem - you are indexing the @sessions array from 1 -> $numberOfSessions, which isn't valid for Perl, as arrays are zero-based, you should be indexing from 0 -> ($numberOfSessions-1).

Check through your logic for detecting a new session, and adding it to the array as well, that looks suspect ATM.

HTH,
Phil.

Nice spotting, completely overlooked that (it works same way in most dev progrs btw).
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
record 0 is the first record made at the top to initialise all the variables which is why it is skipped in the main loop

Thanks all
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
Thanks again , I will look at the logic when i get some time :D
 

Users who are viewing this thread

Top Bottom