#!/usr/local/bin/perl
#
# Name		: wrapcol.pl
# 
# Version	: $Header: /cvsroot/ipfc/ipfc/src/wrapper/perl-skeleton/wrapcol-skeleton.pl,v 1.2 2002/01/13 19:07:40 adulau Exp $
#
# Purpose	: It's a simple collector wrapper acting as a general 
#	          collector and generating xml files into the transac 
#		  directory.
#      
# Author	: Alexandre Dulaunoy <alex@thinkingsecure.com>

# Copyright (C) 2000,2001 Alexandre Dulaunoy <adulau@thinkingsecure.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Alexandre Dulaunoy <adulau@conostix.com>

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

require $ENV{IPFCDIR} . "/lib/libipfc.pl";
require Event::timer;

use Event::File;

my ( @ipfc_data, $cpt, $data, $cpt2);
&ipfc_init;

#
# We read for example an apache log files
# located in /usr/local/apache/logs/access_log
#
# You can create as much as you want events for reading files for example.

Event::File->tail(
    cb => sub {
        my ( $me, $line ) = @_;

        ##print "Got: $line\n"; ## for debug
        &ipfc_call_log("Event react for syslog with : $line");
        @ipfc_data2[$cpt2] = $line;
        $cpt2++;
    },
    file       => '/usr/local/apache/logs/access_log',
    endfile_cb => sub {
        print "end of file.\n";
    },

);

$ipfc_status = Event->timer( interval =>  $ipfc_conf_status_retrytime, parked => 1 );
$ipfc_status->cb( \&ipfc_status_call );
$ipfc_status->start;

$ipfc_pusher = Event->timer( interval => 10, parked => 1 );
$ipfc_pusher->cb( \&ipfc_pusher_call );
$ipfc_pusher->start;

&ipfc_going_daemon;

my $output_event = Event::loop;

&ipfc_call_log($output_event);

sub ipfc_pusher_call() {
    $ipfc_date = &ipfc_get_date;
    &ipfc_call_log("The EVENTS pusher is working at $ipfc_date");

    if ( !( $cpt2 == 0 ) ) {
        foreach $entry2_line (@ipfc_data2) {
            $data2 .= $entry2_line . "\n";
        }
        &ipfc_call_log("The data are : $data2");
        &IpfcGenMessage($data2,"17","","events","log","common-log","log-line");
	$cpt2 = 0;
        undef @ipfc_data2;
        undef $data2;
    } 

}

sub ipfc_status_call() {
    &ipfc_call_log("The STATUS pusher is working at $ipfc_date");
    &ipfc_gen_transacid;
    &ipfc_gen_xml("status");

  # If the type is not specified, by default is a status type. (in this case with agentid 1) 
    &IpfcGenMessage("","1");

}
