#!/usr/bin/perl

# Copyright (C) 2008-2010 NEC Corporation
# All Rights Reserved.

# RCS: $Id: roma_send_mail,v 1.3 2010/03/23 11:03:28 wbmc Exp $

require "/opt/nec/wbmc/lib/wbmc.pl";
require "/opt/nec/wbmc/adm/proxy/common.pl";

#icapmail.confのパス
$icapmail_conf_path ="/etc/opt/nec/wbmc/icapmail.conf";
$icapmail_conf_path_lock ="icapmail.conf";

#mail_logのパス
$mail_log_path = "/var/log/roma/roma_mail_log";

#ネットワーク設定ファイルへのパス
$network_conf_path ="/etc/sysconfig/network";
$network_conf_path_lock = "network.conf";

# 現在時刻の取得
$date_now = time();

#接続不可で最後にスキップした時間
$error_last_skip_time = "";

#タイムアウトで最後にスキップした時間
$timeout_last_skip_time = "";

#FILTER_DOWN_SKIPで最後にスキップした時間
$filter_down_last_skip_time = 0;

#VIRUS_DOWN_SKIPで最後にスキップした時間
$virus_down_last_skip_time = 0;

#最後にFILTERが復旧した時間
$last_restore_time = "";

#最後にVirusが復旧した時間
$virus_last_restore_time = "";

#接続不可でスキップした時間
$error_skip_time = 0;

#タイムアウトでスキップした時間
$timeout_skip_time = 0;

#FILTER_DOWN_SKIPでスキップした時間
$filter_down_skip_time = 0;

#VIRUS_DOWN_SKIPでスキップした時間
$virus_down_skip_time = 0;

#FILTERが復旧した時間
$restore_time = 0;

#VIRUSが復旧した時間
$virus_restore_time = 0;

#ICAPサーバがダウンしたと最後に判断された時間
$icap_server_down_last_time = 0;

#ICAPサーバが復旧したと最後に判断された時間
$icap_server_restore_last_time = 0;

#ICAPサーバがダウンしたと判断された時間
$icap_server_down_time = 0;

#ICAPサーバが復旧したと判断された時間
$icap_server_restore_time = 0;

#メールサーバ
$mail_server = "";

#送信者メールアドレス
$mail_from_addr = "";

#受信者メールアドレス
$mail_to_addr = "";

#ネットワーク設定読み込み
#
if (!&readFile($network_conf_path,$network_conf_path_lock , *nfile) ) {
print <<`EOF`;
	echo $network_conf_path cannot open
EOF
	exit(1);
}

#ネットワーク情報取り出し
for($i=0;$i<@nfile;$i++){
	$tmp = &removeSpace($nfile[$i]);
	#hostnameを取り出す
	if($tmp =~ /^HOSTNAME\s*=\s*(.*)/){
		$hostname = $1;
		last;
	}
}

#2010/01 nakayama
#InterSecVM/CS判定フラグ取得
($err,$N_code) = &get_N_code();
if($err){
        &showErrorPage($err);
        exit(1);
}
$InterSecVM_flag = "";
if($N_code eq "UL4652-001"){
	$InterSecVM_flag = "yes";
}

#通知内容
#InterSecVM/CS判定フラグをチェック 2009/01 nakayama
if($InterSecVM_flag ne "yes"){
	$sub_1 = "InterSec/CS<$hostname>[!!WARNING!!]The ICAP server skip started.";
	$sub_2 = "InterSec/CS<$hostname>[!!WARNING!!]The ICAP server skip started.(Filter)";
	$sub_3 = "InterSec/CS<$hostname>[!!WARNING!!]The ICAP server skip started.(Virus)";
	$sub_4 = "InterSec/CS<$hostname>The ICAP server skip ended.(Filter)";
	$sub_5 = "InterSec/CS<$hostname>The ICAP server skip ended.(Virus)";
	$sub_6 = "InterSec/CS<$hostname>The ICAP server down.";
	$sub_7 = "InterSec/CS<$hostname>The ICAP server success.";
}else{
	$sub_1 = "InterSecVM/CS<$hostname>[!!WARNING!!]The ICAP server skip started.";
	$sub_2 = "InterSecVM/CS<$hostname>[!!WARNING!!]The ICAP server skip started.(Filter)";
	$sub_3 = "InterSecVM/CS<$hostname>[!!WARNING!!]The ICAP server skip started.(Virus)";
	$sub_4 = "InterSecVM/CS<$hostname>The ICAP server skip ended.(Filter)";
	$sub_5 = "InterSecVM/CS<$hostname>The ICAP server skip ended.(Virus)";
	$sub_6 = "InterSecVM/CS<$hostname>The ICAP server is down.";
	$sub_7 = "InterSecVM/CS<$hostname>The ICAP server is success.";
}
$msg_1 = "[Cause]\r\n   Unconnectable to the ICAP server.\r\n   The service working on the ICAP server (such as filtering) cannot be used.\r\n[Reason]\r\n   the ICAP server may not be runnning.\r\n   Or the setting about the ICAP server is wrong.\r\n[Action of user]\r\n   Please start the ICAP server.\r\n   And please confirm the setting about the ICAP server.";
$msg_2 = "[Cause]\r\n   The count of continuous timeout reached a set value.\r\n   The service working on the ICAP server (such as filtering) cannot be used.\r\n[Reason]\r\n   The ICAP server may be down.\r\n[Action of user]\r\n   Please confirm the state of the ICAP server.\r\n   and if it is down , please restart the ICAP server.\r\n   (Before restart, please check a disk space. If there is no free space, make a free space.)";
$msg_3 = "[Cause]\r\n   A normal response returned from the ICAP server.\r\n[Reason]\r\n   The ICAP server is runnning correctly.\r\n[Action of user]\r\n   -";
$msg_4 = "[Cause]\r\n   ICAP server is down, or does not respond.\r\n   The service working on the ICAP server (such as filtering) cannot be used.\r\n[Reason]\r\n   the ICAP server may not be runnning.\r\n   Or the setting about the ICAP server is wrong.\r\n[Action of user]\r\n   Please start the ICAP server.\r\n   And please confirm the setting about the ICAP server.";
$msg_5 = "[Cause]\r\n   A normal response returned from the ICAP server.\r\n[Reason]\r\n   The ICAP server is runnning correctly.\r\n[Action of user]\r\n   -";

if(-e $icapmail_conf_path){

	#icapmail.confファイルの読込み
	if (!&readFile($icapmail_conf_path, $icapmail_conf_path_lock, *file) ) {
print <<`EOF`;
		echo $icapmail_conf_path cannot open
EOF
		exit(1);
	}

	for ($i = 0; $i < @file; $i++) {

		# コメントならば読み飛ばし
		if ($file[$i] =~ /\s*#/) {
			next;
		}
	        # 接続不可で最後にスキップした時間
	        if ($file[$i] =~ /^\s*ICAP_ERROR_LAST_SKIP_TIME\s*=\s*(.*)\s*$/) {
	                $error_last_skip_time = $1;
			next;
	        }
	        # タイムアウトで最後にスキップした時間
	        if ($file[$i] =~ /^\s*ICAP_TIMEOUT_LAST_SKIP_TIME\s*=\s*(.*)\s*$/) {
	                $timeout_last_skip_time = $1;
			next;
	        }
	        # FILTER_DOWN_SKIP(all)で最後にスキップした時間
	        if ($file[$i] =~ /^\s*ICAP_FILTER_DOWN_SKIP_LAST_TIME\s*=\s*(.*)\s*$/) {
	                $filter_down_last_skip_time = $1;
			next;
	        }
	        # VIRUS_DOWN_SKIP(all)で最後にスキップした時間
	        if ($file[$i] =~ /^\s*ICAP_VIRUS_DOWN_SKIP_LAST_TIME\s*=\s*(.*)\s*$/) {
	                $virus_down_last_skip_time = $1;
			next;
	        }
	        # FILTER(all)が最後に復旧した時間
	        if ($file[$i] =~ /^\s*ICAP_LAST_RESTORE_TIME\s*=\s*(.*)\s*$/) {
	                $last_restore_time = $1;
			next;
	        }
	        # VIRUS(all)が最後に復旧した時間
	        if ($file[$i] =~ /^\s*ICAP_VIRUS_LAST_RESTORE_TIME\s*=\s*(.*)\s*$/) {
	                $virus_last_restore_time = $1;
			next;
	        }
	        # ICAPサーバ(個々)がダウンしたと最後に判断された時間
	        if ($file[$i] =~ /^\s*ICAP_SERVER_DOWN_LAST_TIME\s*=\s*(.*)\s*$/) {
	                $icap_server_down_last_time = $1;
			next;
	        }
	        # ICAPサーバ(個々)が復旧したと最後に判断された時間
	        if ($file[$i] =~ /^\s*ICAP_SERVER_RESTORE_LAST_TIME\s*=\s*(.*)\s*$/) {
	                $icap_server_restore_last_time = $1;
			next;
	        }
		# メールサーバ
		if ($file[$i] =~ /^\s*ICAP_MAIL_SERVER\s*=\s*(.*)\s*$/) {
	                $mail_server = $1;
			next;
	        }
		# 送信者のメールアドレス
		if ($file[$i] =~ /^\s*ICAP_MAIL_FROM_ADDR\s*=\s*(.*)\s*$/) {
	                $mail_from_addr = $1;
			next;
	        }
		# 受信者のメールアドレス
		if ($file[$i] =~ /^\s*ICAP_MAIL_TO_ADDR\s*=\s*(.*)\s*$/) {
	                $mail_to_addr = $1;
			next;
	        }
	}

	my($log_time);
	my($log_msg);

	# roma_mail_logより、ICAP通信をスキップするログを探す（接続不可）
	if(open(IN,"grep \"Error:FIRST_CONNECT_ERROR(ICAP_SKIP)\" $mail_log_path 2>&1 |")){
		@skip1=<IN>;
		close (IN);
		for($i=0;$i<@skip1;$i++){
			chomp $skip1;
			($log_time, $log_msg) = split (/\s+/, $skip1[$i]);
			if($error_last_skip_time < $log_time){
				$error_skip_time = $log_time;
				last;
			}
		}
	}

	# roma_mail_logより、ICAP通信をスキップするログを探す（タイムアウト）
	if(open(IN,"grep \"Error:TIMEOUT(ICAP_SKIP)\" $mail_log_path 2>&1 |")){
		@skip2=<IN>;
		close (IN);
		for($i=0;$i<@skip2;$i++){
			chomp $skip2;
			($log_time, $log_msg) = split (/\s+/, $skip2[$i]);
			if($timeout_last_skip_time < $log_time){
				$timeout_skip_time = $log_time;
				last;
			}
		}
	}

	# roma_mail_logより、ICAP通信をスキップするログを探す（FILTER_DOWN_SKIP）
	if(open(IN,"grep \"Error:ICAP_FILTER_DOWN_SKIP\" $mail_log_path 2>&1 |")){
		@skip3=<IN>;
		close (IN);
		for($i=0;$i<@skip3;$i++){
			chomp $skip3;
			($log_time, $log_msg) = split (/\s+/, $skip3[$i]);
			if($filter_down_last_skip_time < $log_time){
				$filter_down_skip_time = $log_time;
				last;
			}
		}
	}

	# roma_mail_logより、ICAP通信をスキップするログを探す（VIRUS_DOWN_SKIP）
	if(open(IN,"grep \"Error:ICAP_VIRUS_DOWN_SKIP\" $mail_log_path 2>&1 |")){
		@skip4=<IN>;
		close (IN);
		for($i=0;$i<@skip4;$i++){
			chomp $skip4;
			($log_time, $log_msg) = split (/\s+/, $skip4[$i]);
			if($virus_down_last_skip_time < $log_time){
				$virus_down_skip_time = $log_time;
				last;
			}
		}
	}

	# roma_mail_logより、ICAPサーバが復旧したことを示すログを探す
	if(open(IN,"grep \"Success:ICAP_FILTER(restore_check)\" $mail_log_path 2>&1 |")){
		@restore=<IN>;
		close (IN);
		for($i=0;$i<@restore;$i++){
			chomp $restore;
			($log_time, $msg) = split (/\s+/, $restore[$i]);
			if($last_restore_time < $log_time){
				$restore_time = $log_time;
				last;
			}
		}
	}

	# roma_mail_logより、VIRUSが復旧したことを示すログを探す
	if(open(IN,"grep \"Success:ICAP_VIRUS(restore_check)\" $mail_log_path 2>&1 |")){
		@restore=<IN>;
		close (IN);
		for($i=0;$i<@restore;$i++){
			chomp $restore;
			($log_time, $msg) = split (/\s+/, $restore[$i]);
			if($virus_last_restore_time < $log_time){
				$virus_restore_time = $log_time;
				last;
			}
		}
	}

	# roma_mail_logより、ICAPサーバがダウンしたと判断されたことを示すログを探す
	@svdown_mail = ();
	if(open(IN,"grep \"Down:server skip\" $mail_log_path 2>&1 |")){
		@svdown=<IN>;
		close (IN);
		$log_time = 0;
		for($i=0;$i<@svdown;$i++){
			chomp $svdown[$i];
			($log_time, $msg) = split (/\s+/, $svdown[$i]);
			if($icap_server_down_last_time < $log_time){
				push(@svdown_mail,$svdown[$i]);
			}
		}
		if($icap_server_down_last_time < $log_time){
			$icap_server_down_time = $log_time;
		}
	}

	# roma_mail_logより、ICAPサーバが復旧したと判断されたことを示すログを探す
	@svrestore_mail = ();
	if(open(IN,"grep \"Success:server restore\" $mail_log_path 2>&1 |")){
		@svrestore=<IN>;
		close (IN);
		$log_time = 0;
		for($i=0;$i<@svrestore;$i++){
			chomp $svrestore[$i];
			($log_time, $msg) = split (/\s+/, $svrestore[$i]);
			if($icap_server_restore_last_time < $log_time){
				push(@svrestore_mail,$svrestore[$i]);
			}
		}
		if($icap_server_restore_last_time < $log_time){
			$icap_server_restore_time = $log_time;
		}
	}

NEXT:

	#ヒットした対象のメールをCLに送信し、icapmail.confの対象箇所を更新する
	$flg=0;
	if($error_skip_time ne "0"){
		# メール送信
		$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_1, $msg_1, $mail_server);
		if($tmpMes ne ""){
			$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
			system("echo \"$linestr\" >> $mail_log_path");
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_ERROR_LAST_SKIP_TIME/){
				$file[$i]="ICAP_ERROR_LAST_SKIP_TIME=$error_skip_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_ERROR_LAST_SKIP_TIME=$error_skip_time\n";
		}
	}
	$flg=0;
	if($timeout_skip_time ne "0"){
		# メール送信
		$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_1, $msg_2, $mail_server);
		if($tmpMes ne ""){
			$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
			system("echo \"$linestr\" >> $mail_log_path");
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_TIMEOUT_LAST_SKIP_TIME/){
				$file[$i]="ICAP_TIMEOUT_LAST_SKIP_TIME=$timeout_skip_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_TIMEOUT_LAST_SKIP_TIME=$timeout_skip_time\n";
		}
	}
	$flg=0;
	if($filter_down_skip_time ne "0"){
		# メール送信
		$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_2, $msg_1, $mail_server);
		if($tmpMes ne ""){
			$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
			system("echo \"$linestr\" >> $mail_log_path");
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_FILTER_DOWN_SKIP_LAST_TIME/){
				$file[$i]="ICAP_FILTER_DOWN_SKIP_LAST_TIME=$filter_down_skip_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_FILTER_DOWN_SKIP_LAST_TIME=$filter_down_skip_time\n";
		}
	}
	$flg=0;
	if($virus_down_skip_time ne "0"){
		# メール送信
		$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_3, $msg_1, $mail_server);
		if($tmpMes ne ""){
			$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
			system("echo \"$linestr\" >> $mail_log_path");
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_VIRUS_DOWN_SKIP_LAST_TIME/){
				$file[$i]="ICAP_VIRUS_DOWN_SKIP_LAST_TIME=$virus_down_skip_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_VIRUS_DOWN_SKIP_LAST_TIME=$virus_down_skip_time\n";
		}
	}
	$flg=0;
	if($restore_time ne "0"){
		# メール送信
		$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_4, $msg_3, $mail_server);
		if($tmpMes ne ""){
			$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
			system("echo \"$linestr\" >> $mail_log_path");
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_LAST_RESTORE_TIME/){
				$file[$i]="ICAP_LAST_RESTORE_TIME=$restore_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_LAST_RESTORE_TIME=$restore_time\n";
		}
	}
	$flg=0;
	if($virus_restore_time ne "0"){
		# メール送信
		$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_5, $msg_3, $mail_server);
		if($tmpMes ne ""){
			$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
			system("echo \"$linestr\" >> $mail_log_path");
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_VIRUS_LAST_RESTORE_TIME/){
				$file[$i]="ICAP_VIRUS_LAST_RESTORE_TIME=$virus_restore_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_VIRUS_LAST_RESTORE_TIME=$virus_restore_time\n";
		}
	}
	$flg=0;
	if($icap_server_down_time ne "0"){
		# メール送信
		for($i=0;$i<@svdown_mail;$i++){
			chomp $svdown_mail[$i];
			($log_time, $msg, $msg2) = split (/\s+/, $svdown_mail[$i]);
			$msg2 =~ /.*(\(.+\))/;
			$sv_hostname = $1;
			$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_6.$sv_hostname, $msg_4, $mail_server);
			if($tmpMes ne ""){
				$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
				system("echo \"$linestr\" >> $mail_log_path");
			}
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_SERVER_DOWN_LAST_TIME/){
				$file[$i]="ICAP_SERVER_DOWN_LAST_TIME=$icap_server_down_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_SERVER_DOWN_LAST_TIME=$icap_server_down_time\n";
		}
	}
	$flg=0;
	if($icap_server_restore_time ne "0"){
		# メール送信
		for($i=0;$i<@svrestore_mail;$i++){
			chomp $svrestore_mail[$i];
			($log_time, $msg, $msg2) = split (/\s+/, $svrestore_mail[$i]);
			$msg2 =~ /.*(\(.+\))/;
			$sv_hostname = $1;
			$tmpMes = &send_mail($mail_from_addr, $mail_to_addr, $sub_7.$sv_hostname, $msg_5, $mail_server);
			if($tmpMes ne ""){
				$linestr = sprintf("%s [WbMC]MailErrLog:%s",$date_now,$tmpMes);
				system("echo \"$linestr\" >> $mail_log_path");
			}
		}
		# icapmail.conf編集
		for($i=0;$i<@file;$i++){
			if($file[$i] =~ /^ICAP_SERVER_RESTORE_LAST_TIME/){
				$file[$i]="ICAP_SERVER_RESTORE_LAST_TIME=$icap_server_restore_time\n";
				$flg=1;
				last;
			}
		}
		if($flg==0){
			$file[$i]="ICAP_SERVER_RESTORE_LAST_TIME=$icap_server_restore_time\n";
		}
	}
	# icapmail.conf更新
	if (!&writeFile($icapmail_conf_path, $icapmail_conf_path_lock, @file)) {
print<<`EOF`;
		echo $icapmail_conf_path cannot write
EOF
		exit(1);
	}
exit;

}
