#!/usr/bin/perl

# VHCS(tm) - Virtual Hosting Control System
# Copyright (c) 2001-2004 by moleSoftware GmbH
# http://www.molesoftware.com
#
#
# License:
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the MPL Mozilla Public License
#    as published by the Free Software Foundation; either version 1.1
#    of the License, or (at your option) any later version.
#
#    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
#    MPL Mozilla Public License for more details.
#
#    You may have received a copy of the MPL Mozilla Public License
#    along with this program.
#
#    An on-line copy of the MPL Mozilla Public License can be found
#    http://www.mozilla.org/MPL/MPL-1.1.html
#
#
# The VHCS Home Page is at:
#
#    http://www.vhcs.net


use FindBin;
use lib "$FindBin::Bin/..";
require 'vhcs2_common_code.pl';

use strict;

use warnings;

sub srv_traff_start_up {

    my ($rs, $rdata) = (undef, undef);

    push_el(\@main::el, 'srv_traff_start_up()', 'Starting...');

    # Let's clear Execution Logs, if any.

    if (-e $main::vhcs2_srv_traff_el) {

        $rs = del_file($main::vhcs2_srv_traff_el);

        return $rs if ($rs != 0);

    }

    # config check;

    $rs = get_conf();

    return $rs if ($rs != 0);

    #
    # getting initial data also must be done here;
    #

    my $sql = "select * from domain;";

    ($rs, $rdata) = doSQL($sql);

    return $rs if ($rs != 0);

    push_el(\@main::el, 'srv_traff_start_up()', 'Ending...');

    return 0;

}


sub srv_traff_shut_down {

    my $rs = undef;

    push_el(\@main::el, 'srv_traff_shut_down()', 'Starting...');

    push_el(\@main::el, 'srv_traff_shut_down()', 'Ending...');

    return 0;

}

sub srv_traff_engine {

    if ($main::cfg{'CMD_IPTABLES'} ne 'no') {

    my ($rs, $ref) = (undef, undef);

    my ($input_log, $output_log) = (
                                    "$main::log_dir/vhcs2-iptables-input.log",
                                    "$main::log_dir/vhcs2-iptables-output.log"
                                   );

    push_el(\@main::el, 'srv_traff_engine()', 'Starting...');

    $rs = sys_command("export COLUMNS=120;$main::cfg{CMD_IPTABLES} -nvxL INPUT 1>$input_log");

    return $rs if ($rs != 0);

    $rs = sys_command("export COLUMNS=120;$main::cfg{CMD_IPTABLES} -nvxL OUTPUT 1>$output_log");

    return $rs if ($rs != 0);

    my $input_line = get_file($input_log); my @input_rows = split(/\n/, $input_line);

    my ($smtp_in, $pop3_in, $imap_in, $http_in, $all_in) = (0, 0, 0, 0, 0);

    my ($line, $bytes, $port) = (undef, undef, undef);

    foreach (@input_rows) {

        $line = $_."\n"; $bytes = 0; $port = 0;

        $bytes = $2 if ($line =~ /^ *(\d+) *(\d+) */);

        $port = $1 if ($line =~ /dpt\:(\d+) *$/);

        if (defined($port) && $port > 0) {

            $smtp_in = $bytes if ($port == 25);

            $pop3_in = $bytes if ($port == 110);

            $imap_in = $bytes if ($port == 143);

            $http_in = $bytes if ($port == 80);

        } else {

            $all_in = $bytes if (defined($bytes));

        }

    }

    $pop3_in += $imap_in;

    my $output_line = get_file($output_log); my @output_rows = split(/\n/, $output_line);

    my ($smtp_out, $pop3_out, $imap_out, $http_out, $all_out) = (0, 0, 0, 0, 0);

    foreach (@output_rows) {

        $line = $_."\n"; $bytes = 0; $port = 0;

        $bytes = $2 if ($line =~ /^ *(\d+) *(\d+) */);

        $port = $1 if ($line =~ /spt\:(\d+) *$/);

        if (defined($port) && $port > 0) {

            $smtp_out = $bytes if ($port == 25);

            $pop3_out = $bytes if ($port == 110);

            $imap_out = $bytes if ($port == 143);

            $http_out = $bytes if ($port == 80);

        } else {

            $all_out = $bytes if (defined($bytes));

        }

    }

    $pop3_out += $imap_out;

    my $timestamp = time;

    my $sql = "insert into server_traffic (traff_time, bytes_in, bytes_out, bytes_mail_in, bytes_mail_out, bytes_pop_in, bytes_pop_out, bytes_web_in, bytes_web_out) values ($timestamp, $all_in, $all_out, $smtp_in, $smtp_out, $pop3_in, $pop3_out, $http_in, $http_out);";

    ($rs, $ref) = doSQL($sql);

    return $rs if ($rs != 0);


    $rs = sys_command("$main::cfg{CMD_IPTABLES} -Z INPUT");

    return $rs if ($rs != 0);


    $rs = sys_command("$main::cfg{CMD_IPTABLES} -Z OUTPUT");

    return $rs if ($rs != 0);

    push_el(\@main::el, 'srv_traff_engine()', 'Ending...');

    return 0;

    } else {

    return 0;

    }

}


my $rs = undef;


$rs = srv_traff_start_up();

if ($rs != 0) {

    dump_el(\@main::el, $main::vhcs2_srv_traff_el);

    srv_traff_shut_down();

    exit 1;

}


$rs = srv_traff_engine();

if ($rs != 0) {

    dump_el(\@main::el, $main::vhcs2_srv_traff_el);

    srv_traff_shut_down();

    exit 1;

}


$rs = srv_traff_shut_down();

if ($rs != 0) {

    dump_el(\@main::el, $main::vhcs2_srv_traff_el);

    exit 1;

}

exit 0;
