NAME

RADIUS::XMLParser - Radius log file XML convertor

SYNOPSIS

    use RADIUS::XMLParser;
    use Data::Dumper;
    
    my $radius_file = 'radius.log';
    my @labels = qw(
    	Event-Timestamp
    	User-Name
    	File
    );
    
    my $radius = RADIUS::XMLParser->new(
    	{
    		VERBOSE       => 1,
    		DAYSFORORPHAN => 1,
    		AUTOPURGE     => 0,
    		ALLEVENTS     => 1,
    		XMLENCODING   => "us-ascii",
    		OUTPUTDIR     => '/tmp/',
    		LABELS        => \@labels
    	}
    );
    	
    my $xml_file = $radius->convert($radius_file);
    print Dumper($parser->metadata());

DESCRIPTION

This module will extract and sort any supported events included into a radius log file.
Note that your logfile must contain an empty line at its end otherwise the last event will not be analyzed.
Events will be grouped by their session ID and converted into XML sessions.
At this time, supported events are the following:
START
INTERIM-UPDATE
STOP

On first step, any event will be stored on different hash tables (with SessionID as a unique key). Then, for each STOP event, the respective START and INTERIM will be retrieved

[OPTIONAL] Each found START / INTERIM event will be written, final hash will be empty.
[OPTIONAL] Only the newest START / INTERIM events will be kept. Oldest ones will be considered as orphan events and will be dropped

Final XML will get the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<sessions>
   <session sessionId=$sessionId>
      <start></start>
      <interims>
         <interim id1></interim>
      </interims>
      <stop></stop>
   </session>
</sessions>

CONSTRUCTOR

Usage:

my $parser = RADIUS::XMLParser->new({%params});

Return:

A radius parser blessed reference

Parameters:

Hash reference including below Options

Options:

[optional] VERBOSE

Integer (0 by default) enabling verbose mode.
Regarding the amount of lines in a typical Radius log file (mine were 100MB large), verbose mode is split into several levels (0,1,2,3).

[optional] LABELS

Array reference of labels user would like to see converted into XML.

A reference to below Array passed as an input parameter...

my @labels = qw(
  Acct-Output-Packets
  NAS-IP-Address
  Event-Timestamp
);

...will result on the following XML output

<stop>
	<Acct-Output-Packets></Acct-Output-Packets>
	<NAS-IP-Address></NAS-IP-Address>
	<Event-Timestamp></Event-Timestamp>
</stop>
If LABELS is not supplied, all the found Key / Values will be written.
Else, only the supplied labels will be written
FYI, Gettings few LABELS is significantly faster... Might save precious time when dealing with large files !

[optional] AUTOPURGE

Boolean (0 by default) that will purge stored hash reference (Start + Interim) before being used for Event lookup.
Newest events will be kept, oldest will be dropped.
Threshold is defined by below parameter DAYSFORORPHAN

[optional] DAYSFORORPHAN

Number of days user would like to keep the orphan Start + Interim events.
Default is 1 day; any event older than 1 day will be dropped.
AUTOPURGE must be set to true

[optional] OUTPUTDIR

Output directory where XML file will be created
Default is first temporary directory (returned by File::Spec-tmpdir()>)

[optional] ALLEVENTS

Boolean (0 by default).
If 1, all events will be written, including Start, Interim and Stop "orphan" records. Note that Orphan hash should be empty after processing.
If 0, only the events Stop will be written together with the respective Start / Interims for the same session ID. Note that Orphan hash should not be empty after processing, and therefore should be written on disk (under ORPHANDIR directory)

[optional] XMLENCODING

Only utf-8 and us-ascii are supported
default is utf-8

[optional] ORPHANDIR

Default directory for orphan hash tables stored structure
Default is first temporary directory (returned by File::Spec-tmpdir()>)

METHODS

convert

Description:

The convert will parse and convert provided file $radius_file.
All its events will be retrieved, sorted and grouped by their unique sessionId.
Then, file will be converted into a XML format.

Usage:

my $xml_file = $parser->convert($radius_file);

Parameter:

$radius_file:
Radius log file that will be parsed.

Return:

The name of the XML file that has been created.

flush

Description:

The flush method will cleanup orphanage on demand
Note that this process is already done at startup but might be required some times to times, especially for deamons processes which might never have to rebuild parser (new method)
Oldest orphans are dropped
Need PURGEORPHAN parameter set (optionnally DAYSFORORPHAN)

Usage:

    $parser->flush();

metadata

Description:

The metadata will give you back some useful information about the latest XML conversion
This hash will provide you with the number of found events, processed lines, etc...

Usage:

    Using below code...

    use Data::Dumper;
    print Dumper($parser->metadata());

    ...Will result on the following output

    	$VAR1 = {
    		  'EVENT_INTERIM' 		=> 130,
              'EVENT_START' 		=> 46,
              'EVENT_STOP' 			=> 46,
              'ERRORS' 				=> 0,
              'PROCESSED_LINES' 	=> 5659
            };

Return:

A hash reference including metadata

EXAMPLE:

use RADIUS::XMLParser;

my $radius_file = 'radius.log';
my @labels = qw(
	Event-Timestamp
	User-Name
	File
);

my $radius = RADIUS::XMLParser->new(
	{
		VERBOSE       => 1,
		DAYSFORORPHAN => 1,
		AUTOPURGE     => 0,
		ALLEVENTS     => 1,
		XMLENCODING   => "utf-8",
		OUTPUTDIR     => '/tmp/',
		LABELS        => \@labels
	}
);

my $xml_file = $radius->convert($radius_file);

Here is how the generated XML file will look like

<?xml version="1.0" encoding="UTF-8"?>
<session sessionId="d537cca0d43c95dc">
  <start>
   <Event-Timestamp>1334560899</Event-Timestamp>
   <User-Name>User1</User-Name>
   <File>radius.log</File>
  </start>
  <interims>
   <interim id="1">
    <Event-Timestamp>1334561024</Event-Timestamp>
    <User-Name>User1</User-Name>
    <File>radius.log</File>
   </interim>
   <interim id="2">
    <Event-Timestamp>1334561087</Event-Timestamp>
    <User-Name>User1</User-Name>
    <File>radius.log</File>
   </interim>
  </interims>
  <stop>
   <Event-Timestamp>1334561314</Event-Timestamp>
   <User-Name>User1</User-Name>
   <File>radius.log</File>
  </stop>
 </session>

AUTHOR

Antoine Amend <amend.antoine@gmail.com>

MODIFICATION HISTORY

See the Changes file.

COPYRIGHT AND LICENSE

Copyright (c) 2013 Antoine Amend. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.