#!/usr/bin/perl
########################################################################
# filename:     logparse
#
# description:  This script parses the log file for connect/disconnect
#		messages, and prints a message for the current range in
#		the messages file.  It can also accept the name of a rotated
#		messages file on the command line.  If none is present it
#		assumes /home/irlp/log/messages
# history:
# 2004-12-20            original script installed on system
########################################################################

#
# Set the format of the output generated by the script.
#
format =
  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>
  $connected,                             $count
.


format STDOUT_TOP =

  Date (network, direction)            	  # of Connects
  --------------------------------------- -------------
.

$log = $ARGV[0];

if (!$log) {
	$log = "/home/irlp/log/messages";
}

open(FILE, "$log") or die "Can't open $log: $!\n";;
while (<FILE>) {
	$line = $_;

	## Takes the log file and splits it up by whitespaces
	#
	@t = split(/ /, $line);
	chomp($t[7]);

	## Inbound Echolink
	#
	if ($t[6] eq "connected" && $t[7] eq "echolink") {
		$type = "$t[0] $t[1] (echolink in)";
		$connect{$type}++;

		$date = "$t[0] $t[1]";
		$condate{$date}++;
	}

	## Outbound Echolink
	#
        if ($t[6] eq "connected" && $t[7] eq "outbound") {
                $type = "$t[0] $t[1] (echolink out)";
                $connect{$type}++;

                $date = "$t[0] $t[1]";
                $condate{$date}++;
        }

	## Incoming IRLP (Node to Node)
	#
	if ($t[6] eq "connect" && $t[7] eq "ADPCM") {
		$type = "$t[0] $t[1] (irlp in)";
		$connect{$type}++;

                $date = "$t[0] $t[1]";
                $condate{$date}++;
	}

	## Outbound IRLP (Node to Node)
	#
	if ($t[7] eq "initiated") {
		$type = "$t[0] $t[1] (irlp out station)";
		$connect{$type}++;

		$date = "$t[0] $t[1]";
		$condate{$date}++;
	}

	## Outbound IRLP (Node to Reflector)
	#
	if ($t[6] eq "Connected" && $t[8] eq "reflector") {
		$type = "$t[0] $t[1] (irlp out reflector)";
		$connect{$type}++;

		$date = "$t[0] $t[1]";
		$condate{$date}++;
	}

}
close(FILE);

#
## Print the report.
foreach $connected (sort(keys(%connect))) {
	$count = $connect{$connected};
	write;
}
