#! /usr/bin/perl -w
################################################################################
# Copyright 2005-2011 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
# 
# 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.
# 
# 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.
# 
# You should have received a copy of the GNU General Public License along with 
# this program; if not, see <http://www.gnu.org/licenses>.
# 
# Linking this program statically or dynamically with other modules is making a 
# combined work based on this program. Thus, the terms and conditions of the GNU 
# General Public License cover the whole combination.
# 
# As a special exception, the copyright holders of this program give MERETHIS 
# permission to link this program with independent modules to produce an executable, 
# regardless of the license terms of these independent modules, and to copy and 
# distribute the resulting executable under terms of MERETHIS choice, provided that 
# MERETHIS also meet, for each linked independent module, the terms  and conditions 
# of the license of that module. An independent module is a module which is not 
# derived from this program. If you modify this program, you may extend this 
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
# 
# For more information : contact@centreon.com
# 
# SVN : $URL: http://svn.centreon.com/branches/centreon-2.3.x/bin/logAnalyser $
# SVN : $Id: logAnalyser 12238 2011-05-18 08:54:15Z jmathis $
#
####################################################################################

# Message type 
# 0 -> Service Alerts
# 1 -> Host Alerts
# 2 -> Service notification
# 3 -> Host Notification
# 4 -> All nagios Warning
# 5 -> All logs
# 6 -> Service Current State  
# 7 -> Host Current State
# 8 -> Service Initial State  
# 9 -> Host Initial State
# 10 -> Service Acknowledgement
# 11 -> Host Acknowledgement

use strict;
use warnings;
use DBI;
use File::stat;
use Getopt::Long;
use POSIX;

use vars qw($mysql_user $mysql_passwd $mysql_host $mysql_database_oreon $mysql_database_ods $opt_h $opt_a $opt_p $data $LOG);

require "/etc/centreon/conf.pm";

# Define Var lib dir
my $VarLib = "/var/lib/centreon";
$LOG = "/var/log/centreon/logAnalyser.log";

## Init Date
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);

# Write logs in a file.
sub writeLogFile($){
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
    open (LOG, ">> ".$LOG) || print "can't write $LOG: $!";
   
   	# Add initial 0 if value is under 10
    $hour = "0".$hour if ($hour < 10);
    $min = "0".$min if ($min < 10);
    $sec = "0".$sec if ($sec < 10);
   	
    print LOG "$mday/".($mon+1)."/".($year+1900)." $hour:$min:$sec - ".$_[0]."\n";
    close LOG or warn $!;
}

# Init MySQL Connexion
my $dbh = DBI->connect("DBI:mysql:database=".$mysql_database_ods.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 1});

# Init MySQL Connexion
my $dbhoreon = DBI->connect("DBI:mysql:database=".$mysql_database_oreon.";host=".$mysql_host, $mysql_user, $mysql_passwd, {'RaiseError' => 1});

# ##############################
# Lock in MySQL
my $sth = $dbhoreon->prepare("SELECT id, running FROM cron_operation WHERE name LIKE 'logAnalyzer'");
if (!$sth->execute) {
    die "Error:" . $sth->errstr . "\n";
}
$data = $sth->fetchrow_hashref();

########################
# Check if is db layer 
# is centreon broker
########################
my $dbLayer = $dbhoreon->prepare("SELECT `value` FROM `options` WHERE `key` = 'broker'");
if (!$dbLayer->execute()) {
	die "Error:" . $dbLayer->errstr . "\n";
}
my $dataLayer = $dbLayer->fetchrow_hashref();
if ($dataLayer->{'value'} eq "broker") {
	$dbLayer->finish();
	exit;
}
$dbLayer->finish();

my $sthbis;
my $appID;
if (defined($data->{'id'})) {
    if (!defined($data->{"is_running"}) || $data->{"is_running"} == 0) {
        $appID = $data->{"id"};
        $sthbis = $dbhoreon->prepare("UPDATE cron_operation SET running = '1', time_launch = '".time()."' WHERE id = '$appID'");
        if (!$sthbis->execute) {
            die "Error:" . $sthbis->errstr . "\n";
        }
    } else {
        exit(1);
    }
} else {
    my $sthbis = $dbhoreon->prepare("INSERT INTO cron_operation (name, system, activate) VALUES ('logAnalyzer', '1', '1')");
    if (!$sthbis->execute) {
        die "Error:" . $sthbis->errstr . "\n";
    }
}


Getopt::Long::Configure('bundling');
GetOptions("h" => \$opt_h, "help" => \$opt_h,
           "a" => \$opt_a, "archives" => \$opt_a,
		   "p=s" => \$opt_p, "poller" => \$opt_p);

if ($opt_h) {
    print " Usage : $0 :\n";
    print "    -a (--archives) load data from log archives to database\n";
	print "    -p (--poller <value>) load data from log archives to database from specific poller ID\n";
    print "    -h (--help) show help\n";
    exit(0);
}

if (defined($opt_p)) {
	if ($opt_p =~ m/\d+/) {
		my $sth = $dbh->prepare("SELECT `instance_name` FROM `instance` WHERE `instance_id` = '".$opt_p."'");
		if (!$sth->execute) {
			die "Error:" . $sth->errstr . "\n";
		}
		my $result = $sth->rows();
		$sth->finish();
		if ($result ne 1) {
			writeLogFile("Invalid poller ID 1\n");
			exit (1);
		}
	} else {
		writeLogFile("Invalid poller ID\n");
		exit (1);
	}
}

# Get conf Data
my $sth_config = $dbh->prepare("SELECT `archive_log`, `archive_retention`, `nagios_log_file`  FROM `config`");
if (!$sth_config->execute) {
	die "Error:" . $sth_config->errstr . "\n";
}
$data = $sth_config->fetchrow_hashref();
$sth_config->finish();

my $cpt = 0;
my $ctime = 0;
my $last_line_read;

# Parsing nagios.log
sub parseFile ($$) {
    my $instance = $_[1];
	my $logFile = $_[0];
	my $lastCTime = 0;
	my $CTime = 0;
	
    my $historyFile = "$VarLib/log/$instance/.history";
     
    if (!-f "$VarLib/log/") {
        mkdir("$VarLib/log/");
    }

    if (!-f "$VarLib/log/$instance") {
        mkdir("$VarLib/log/$instance");
    } 
     
    # Get History Flag
   	if (!open(FILE, $historyFile)){
		writeLogFile("Cannot open file : $historyFile (READ)\n");
    } else {
    	while (<FILE>) {
    		$lastCTime = $_;
    		$lastCTime =~ s/\n//g;	
    	}
    	close(FILE);
    }
    
    # Get History Flag
   	if (!open(FILE, $logFile)){
		writeLogFile("Cannot open file : $logFile (READ)\n");
		return;
    } else {
    	my @tab;
    	while (<FILE>) {
    		if ($_ =~ m/\[([0-9]*)\]\ /) {
		    	$CTime = $1;
		    	$CTime =~ s/\n//g;	
		    	last;
		    } else {
		    	writeLogFile("Cannot find ctime in first line for poller $instance\n");
		    	last;
		    }
    	}
    	close(FILE);
    }
    
    # Decide if we have to read the nagios.log from the begining
    if ($CTime ne $lastCTime && $CTime ne 0 & $lastCTime ne 0){
    	$last_line_read = 0;
    } else {
		my $sth_flag = $dbh->prepare("SELECT `log_flag` FROM `instance` WHERE `instance_id` = '".$instance."'");
		if (!$sth_flag->execute) {
			die "Error:" . $sth_flag->errstr . "\n";
		}
		$data = $sth_flag->fetchrow_hashref();
		$last_line_read = $data->{'log_flag'};
		
		if (!defined($last_line_read)) {
			writeLogFile("Alert : Cannot get last line read in DB for poller $instance\n");
			return ;
		}
		$sth_flag->finish();
	}
        
    # Open Log File for parsing
    if (!open (FILE, $_[0])){
		writeLogFile("Cannot open file : $_[0]\n");
		return;
    }
    
    # Init Counter
    $cpt = 0;
    
    # Skip old lines (already read)
    if (!$opt_a && $last_line_read) {
		while ($cpt < $last_line_read && <FILE>) {
		    $cpt++;
		}
    }
    #
    # Now Read unknown log lines
    #
    while (<FILE>) {
		if ($_ =~ m/^\[([0-9]*)\]\sSERVICE ALERT\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		    $tab[0] =~ s/\\/\\\\/g;
                    $tab[0] =~ s/\'/\\\'/g;
		    $tab[1] =~ s/\\/\\\\/g;
                    $tab[1] =~ s/\'/\\\'/g;
		    $tab[5] =~ s/\\/\\\\/g; 
		    $tab[5] =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`,`ctime`, `host_name` , `service_description`, `status`, `type`, `retry`, `output`, `instance`) VALUES ('0', '$ctime', '".$tab[0]."', '".$tab[1]."', '".$tab[2]."', '".$tab[3]."','".$tab[4]."','".$tab[5]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sHOST ALERT\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		    $tab[0] =~ s/\\/\\\\/g;
                    $tab[0] =~ s/\'/\\\'/g;
		    if (defined($tab[4]) && $tab[4]) {
			$tab[4] =~ s/\\/\\\\/g; 
			$tab[4] =~ s/\'/\\\'/g;
		    }
		    my $rq = "INSERT INTO `log` (`msg_type`,`ctime`, `host_name` , `status`,  `type`, `retry`, `output`, `instance`) VALUES ('1', '$ctime', '".$tab[0]."', '".$tab[1]."', '".$tab[2]."','".$tab[3]."','".$tab[4]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sSERVICE NOTIFICATION\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		    $tab[2] =~ s/\\/\\\\/g;
            $tab[2] =~ s/\'/\\\'/g;
		    $tab[1] =~ s/\\/\\\\/g;
            $tab[1] =~ s/\'/\\\'/g;
		    if (defined($tab[5])) {
		    	$tab[5] =~ s/\\/\\\\/g; 
		    	$tab[5] =~ s/\'/\\\'/g;
		    } else {
		    	$tab[5] = "";
		    }
		    my $rq = "INSERT INTO `log` (`msg_type`,`ctime`, `host_name` , `service_description`, `status`, `notification_cmd`, `notification_contact`, `output`, `instance`) VALUES ('2', '$ctime', '".$tab[1]."', '".$tab[2]."', '".$tab[3]."', '".$tab[4]."','".$tab[0]."','".$tab[5]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sHOST NOTIFICATION\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		   	if (defined($tab[4])) {
		    	$tab[4] =~ s/\\/\\\\/g; 
		    	$tab[4] =~ s/\'/\\\'/g;
		    } else {
		    	$tab[4] = "";
		    }
		    my $rq = "INSERT INTO `log` (`msg_type`,`ctime`, `notification_contact`, `host_name` , `status`, `notification_cmd`,  `output`, `instance`) VALUES ('3', '$ctime', '".$tab[0]."','".$tab[1]."', '".$tab[2]."', '".$tab[3]."','".$tab[4]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sCURRENT\sHOST\sSTATE\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		    $tab[0] =~ s/\\/\\\\/g;
                    $tab[0] =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`, `ctime`, `host_name` , `status`, `type`, `instance`) VALUES ('7', '$ctime', '".$tab[0]."', '".$tab[1]."', '".$tab[2]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sCURRENT\sSERVICE\sSTATE\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		    $tab[0] =~ s/\\/\\\\/g;
                    $tab[0] =~ s/\'/\\\'/g;
		    $tab[1] =~ s/\\/\\\\/g;
                    $tab[1] =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`, `ctime`, `host_name`, `service_description` , `status`, `type`, `instance`) VALUES ('6', '$ctime', '".$tab[0]."', '".$tab[1]."', '".$tab[2]."', '".$tab[3]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sINITIAL\sHOST\sSTATE\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		    $tab[0] =~ s/\\/\\\\/g;
                    $tab[0] =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`, `ctime`, `host_name` , `status`, `type`, `instance`) VALUES ('9', '$ctime', '".$tab[0]."', '".$tab[1]."', '".$tab[2]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sINITIAL\sSERVICE\sSTATE\:\s(.*)$/){
		    my @tab = split(/;/, $2);
		    $ctime = $1;
		    $tab[0] =~ s/\\/\\\\/g;
                    $tab[0] =~ s/\'/\\\'/g;
		    $tab[1] =~ s/\\/\\\\/g;
                    $tab[1] =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`, `ctime`, `host_name`, `service_description` , `status`, `type`, `instance`) VALUES ('8', '$ctime', '".$tab[0]."', '".$tab[1]."', '".$tab[2]."', '".$tab[3]."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sEXTERNAL\sCOMMAND\:\sACKNOWLEDGE\_SVC\_PROBLEM\;(.*)$/) {
		    $ctime = $1;
		    my @tab = split(/;/, $2);
		    $tab[0] =~ s/\\/\\\\/g;
                    $tab[0] =~ s/\'/\\\'/g;
		    $tab[1] =~ s/\\/\\\\/g;
                    $tab[1] =~ s/\'/\\\'/g;
            if (!defined($tab[6])){
            	$tab[6] = "";
            }
		    $tab[6] =~ s/\\/\\\\/g;
                    $tab[6] =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`, `ctime`, `host_name`, `service_description`, `notification_contact`, `output`, `instance`) VALUES ('10', '$ctime', '".$tab[0]."', '".$tab[1]."', '".$tab[5]."', '".$tab[6]."','".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sEXTERNAL\sCOMMAND\:\sACKNOWLEDGE\_HOST\_PROBLEM\;(.*)$/) {
		    $ctime = $1;
		    my @tab = split(/;/, $2);
		    $tab[0] =~ s/\\/\\\\/g;
			$tab[0] =~ s/\'/\\\'/g;
			$tab[5] =~ s/\\/\\\\/g;
			$tab[5] =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`, `ctime`, `host_name`, `notification_contact`, `output`, `instance`) VALUES ('11', '$ctime', '".$tab[0]."', '".$tab[4]."', '".$tab[5]."','".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!$res);	
		} elsif ($_ =~ m/^\[([0-9]*)\]\sWarning\:\s(.*)$/){
		    my $tab = $2;
		    $ctime = $1;
		    $tab =~ s/\\/\\\\/g; 
		    $tab =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`,`ctime`, `output`, `instance`) VALUES ('4','$ctime', '".$tab."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	
		} elsif ($_ =~ m/^\[([0-9]*)\]\s(.*)$/) {
		    $ctime = $1;
		    my $tab = $2;
		    $tab =~ s/\\/\\\\/g; 
		    $tab =~ s/\'/\\\'/g;
		    my $rq = "INSERT INTO `log` (`msg_type`,`ctime`, `output`, `instance`) VALUES ('5','$ctime', '".$tab."', '".$instance."')";
		    my $res = $dbh->do($rq);
		    writeLogFile("SQL INSERT ERROR :" . $rq. "\n") if (!defined($res));	   
		}
		$cpt++;
    }
    close(FILE);
    
    # Add History
   	if (!open(FILE, "> ".$historyFile)){
		writeLogFile("Cannot open file : $historyFile (WRITE)\n");
		return;
    } else {
    	print FILE $CTime."\n";
    	close(FILE);
    }
    
    return $cpt;
}

my $retention = $data->{'archive_retention'};

sub parseArchive($$) {
    my ($instance, $localhost) = @_;

    my $archives;
    if ($localhost){
		my $sth = $dbhoreon->prepare("SELECT `log_archive_path` FROM `cfg_nagios`, `nagios_server` WHERE `nagios_server_id` = '".$_[0]."' AND `nagios_server`.`id` = `cfg_nagios`.`nagios_server_id` AND `nagios_server`.`ns_activate` = 1 AND `cfg_nagios`.`nagios_activate` = '1'");
		if (!$sth->execute()) {
		    die "Error:" . $sth->errstr . "\n";
		}
		$data = $sth->fetchrow_hashref();
		$archives = $data->{'log_archive_path'};
		$sth->finish();
    } else {
		$archives = "$VarLib/log/$instance/archives/";
    }

    $archives .= "/" if (!($archives =~ /\/$/));

    my @log_files = split /\s/,`ls $archives`;
    my $last_log = time() - ($retention * 24 * 60 * 60);
    foreach (@log_files) {
		$_ =~ /nagios\-([0-9\-]+).log/;
		my @time = split /\-/, $1;
		my $temp = $time[0]."/".$time[1]."/".$time[2];
		$temp = `date -d $temp +%s`;
		if ($temp > $last_log) {
		    if (!(-r $archives.$_)) {
				writeLogFile("Error : cannot read file $archives$_\n");
		    } else {
				writeLogFile $archives.$_."\n";
				parseFile($archives.$_, $instance);
		    }
		}
    }
}

sub parseLogFile($$) {
    # Get parameters
    my ($instance, $localhost) = @_;

    my $LOG_FILE;
    if ($localhost){
		my $sth = $dbhoreon->prepare("SELECT `log_file` FROM `cfg_nagios`, `nagios_server` WHERE `nagios_server_id` = '".$_[0]."' AND `nagios_server`.`id` = `cfg_nagios`.`nagios_server_id` AND `nagios_server`.`ns_activate` = 1");
		if (!$sth->execute()) {
			die "Error:" . $sth->errstr . "\n";
		}
		$data = $sth->fetchrow_hashref();
		$LOG_FILE = $data->{'log_file'};
		if (!(-r $LOG_FILE)) {
		    writeLogFile("Error : cannot open $LOG_FILE\n");
		    exit(0);
		}
		$sth->finish();
    } else {
		$LOG_FILE = "$VarLib/log/$instance/nagios.log";
    }

    # Parse -> 

    parseFile($LOG_FILE, $instance);

    ##############################
    # Update statistics and flags
    my $rq = "UPDATE `instance` SET `log_flag` = '".$cpt."' WHERE `instance_id` = '".$instance."'";
    my $res = $dbh->do($rq);
    if (!defined($res)) {
    	die "SQL UPDATE Error:" . $rq . "\n";
    }
    ##############################
}

exit() if (!$data->{'archive_log'});

my $sth2 = $dbhoreon->prepare("SELECT `id`, `name`, `localhost` FROM `nagios_server` WHERE `ns_activate` = 1");
if (!$sth2->execute) {
    die "Error:" . $sth2->errstr . "\n";
} else { 
	my $flag = 0;
	my $res ;
	
	if (defined($opt_p)) {
		$res = $dbh->do("UPDATE `instance` SET `log_flag` = '0' WHERE `instance_id` = '".$opt_p."'");
		if (!defined($res)) {die "SQL UPDATE Error : UPDATE `instance` SET `log_flag` = '0'\n";};
		
		$res = $dbh->do("DELETE FROM `log` WHERE instance = '".$opt_p."'");
		writeLogFile("SQL INSERT ERROR :TRUNCATE TABLE `log`\n") if (!defined($res));	   
		parseArchive($opt_p, '0');
	} else {
		while (my $ns_server = $sth2->fetchrow_hashref()) {
			my $sth1 = $dbh->prepare("SELECT `instance_name` FROM `instance` WHERE `instance_id` = '".$ns_server->{'id'}."' LIMIT 1");
			if (!$sth1->execute) {
				die "Error:" . $sth1->errstr . "\n";
			}
			if (!$sth1->rows()){
				my $sthinsert = $dbh->prepare("INSERT INTO `instance` (`instance_id` , `instance_name` , `log_flag`) VALUES ('".$ns_server->{'id'}."', '".$ns_server->{'name'}."', '0');");
				if (!$sthinsert->execute) {
					die "Error:" . $sthinsert->errstr . "\n";
				}
				$sthinsert->finish();			
			} else {
				my $sthupdate = $dbh->prepare("UPDATE `instance` SET `instance_name` = '".$ns_server->{'name'}."' WHERE `instance_id` = '".$ns_server->{'id'}."' LIMIT 1");
				if (!$sthupdate->execute) {
					die "Error:" . $sthupdate->errstr . "\n";
				}
				$sthupdate->finish();			
			}
			$sth1->finish();
			
			if ($opt_a) {
				# Empty line counter
				$res = $dbh->do("UPDATE `instance` SET `log_flag` = '0'");
				if (!defined($res)) {die "SQL UPDATE Error : UPDATE `instance` SET `log_flag` = '0'\n";}
		
				if (!$flag) {
					$res = $dbh->do("TRUNCATE TABLE `log`");
					writeLogFile("SQL INSERT ERROR :TRUNCATE TABLE `log`\n") if (!defined($res));	   
					$flag = 1;
				}
				parseArchive($ns_server->{'id'}, $ns_server->{'localhost'});
			} else {
				parseLogFile($ns_server->{'id'}, $ns_server->{'localhost'});
			}
		}
	}
    $sth2->finish();
}

# Remove Lock
if ($appID) {
	$sth = $dbhoreon->prepare("UPDATE cron_operation SET running = '0', last_execution_time = '" . time() . "' WHERE id = '$appID'");
	if (!$sth->execute) {
	    die "Error:" . $sth->errstr . "\n";
	}
	undef($appID);
}

$dbh->disconnect();
$dbhoreon->disconnect();

exit;