#!/usr/bin/perl

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2001-2006 by moleSoftware GmbH - http://www.molesoftware.com
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp-srv-traff 2546 2010-01-18 13:40:14Z benedikt $
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "VHCS - Virtual Hosting Control System".
#
# The Initial Developer of the Original Code is moleSoftware GmbH.
# Portions created by Initial Developer are Copyright (C) 2001-2006
# by moleSoftware GmbH. All Rights Reserved.
# Portions created by the ispCP Team are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#

use FindBin;
use lib "$FindBin::Bin/..";
require 'ispcp_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::ispcp_srv_traff_el) {

		$rs = del_file($main::ispcp_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
		domain_id,
		domain_name,
		domain_gid,
		domain_uid,
		domain_admin_id,
		domain_created_id,
		domain_created,
		domain_last_modified,
		domain_mailacc_limit,
		domain_ftpacc_limit,
		domain_traffic_limit,
		domain_sqld_limit,
		domain_sqlu_limit,
		domain_status,
		domain_alias_limit,
		domain_ip_id,
		domain_disk_limit,
		domain_disk_usage,
		domain_php,
		domain_cgi
	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/ispcp-iptables-input.log",
			"$main::log_dir/ispcp-iptables-output.log"
		);

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

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

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

		$rs = sys_command("export COLUMNS=120;$main::cfg{CMD_IPTABLES} -nvxL ISPCP_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 =~ /[d,s]pt\:(\d+) *$/);

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

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

				$smtp_in += $bytes if ($port == 465);

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

				$pop3_in += $bytes if ($port == 995);

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

				$imap_in += $bytes if ($port == 993);

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

				$http_in += $bytes if ($port == 443);

			} 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 =~ /[d,s]pt\:(\d+) *$/);

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

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

				$smtp_out += $bytes if ($port == 465);

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

				$pop3_out += $bytes if ($port == 995);

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

				$imap_out += $bytes if ($port == 993);

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

				$http_out += $bytes if ($port == 443);

			} else {

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

			}

		}

		$pop3_out += $imap_out;

		my $timestamp = time;

		# timestamp has floor down for the last half'n our to make timestamps in
		# server_traffic and domain_traffic the same
		$timestamp -= ($timestamp % 1800);

		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 ISPCP_INPUT");

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


		$rs = sys_command("$main::cfg{CMD_IPTABLES} -Z ISPCP_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::ispcp_srv_traff_el);

	srv_traff_shut_down();

	exit 1;

}


$rs = srv_traff_engine();

if ($rs != 0) {

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

	srv_traff_shut_down();

	exit 1;

}


$rs = srv_traff_shut_down();

if ($rs != 0) {

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

	exit 1;

}

exit 0;
