#!/usr/bin/perl

# Copyright (C) 2008-2010 NEC Corporation
# All Rights Reserved.
#!/usr/bin/perl

# RCS: $Id: wbmc_mail,v 1.3 2011/06/19 11:01:28 wbmc Exp $

#2008/12 higashi
#send_mail関数読み込み
require "/opt/nec/wbmc/adm/proxy/common_roma.pl";

#2008/12 higashi
#ログ出力のため読み込み
require "/opt/nec/wbmc/bin/time_conv.pl";

#2010/01 nakayama
#InterSecVM/CS判定フラグ取得用
require "/opt/nec/wbmc/lib/wbmc.pl";

#2008/12 higashi
#accesslog_transfer.logへのパス
$accesslog_transfer_log_path = "/var/log/accesslog_transfer/accesslog_transfer.log";

#2008/12 higashi
#accesslog_transfer.logをローテートするサイズ
$accesslog_transfer_log_rotate_size=5242880;

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

#2008/12 higashi
#ネットワーク設定読み込み
#
@nfile = ();
if (!&readFile($network_conf_path,$network_conf_path_lock , *nfile) ) {
print <<`EOF`;
	echo $network_conf_path cannot open
EOF
	if(!&writeLog_rotate("[Error] $network_conf_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
		system("/bin/echo $accesslog_transfer_log_path cannot write");
	}
	exit(1);
}

#ネットワーク情報取り出し
$hostname = "";
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 =~ /^UL\d+/){
	$InterSecVM_flag = "yes";
}

#2008/12 higashi
#メール通知時の件名、本文
#InterSecVM/CS判定フラグをチェック 2009/01 nakayama
if($InterSecVM_flag ne "yes"){
	$mail_success_subject = "InterSec/CS<$hostname>[SUCCESS] Accesslog transfer succeeded";
	$mail_failed_subject  = "InterSec/CS<$hostname>[!!WARNING!!] Accesslog transfer failed";
	$mail_delete_subject  = "InterSec/CS<$hostname>[!!FAILED!!] Accesslog was deleted";
}else{
	$mail_success_subject = "InterSecVM/CS<$hostname>[SUCCESS] Accesslog transfer succeeded";
	$mail_failed_subject  = "InterSecVM/CS<$hostname>[!!WARNING!!] Accesslog transfer failed";
	$mail_delete_subject  = "InterSecVM/CS<$hostname>[!!FAILED!!] Accesslog was deleted";
}
$mail_success_message = "[Cause]\r\n    The transfer of the accesslog succeeded.\r\n[Reason]\r\n    - \r\n[Resolution]\r\n    -";
$mail_failed_message  = "[Cause]\r\n    Failed in transfer of the accesslog.\r\n[Reason]\r\n    There is a possibility that file server or the network doesn't work normally.\r\n    Otherwise the setting of CS may be wrong.\r\n[Resolution]\r\n    Please confirm the status of the file server and the network.\r\n    And, please confirm the setting of CS or file server.";
$mail_delete_message  = "[Cause]\r\n    The oldest accesslog file in retry-queue was deleted.\r\n[Reason]\r\n    Retry-queue exceeded the limit value.\r\n[Resolution]\r\n    Please confirm the status of the file server and the network.\r\n    And, please confirm the setting of CS or file server.";

#2008/12 higashi
#引数のチェック
#$ARGV[0] = 送信者メールアドレス
#$ARGV[1] = 受信者メールアドレス
#$ARGV[2] = メールサーバ
#$ARGV[3] = 送信するメッセージの種類
#           1：転送成功時のメッセージ
#           2: 転送失敗時のメッセージ
#           3: アクセスログを削除した時のメッセージ
if(($ARGV[0] eq "" ) || ($ARGV[1] eq "") || ($ARGV[2] eq "") || ($ARGV[3] eq "")){
    if(!&writeLog_rotate("[Error] Not argument",$accesslog_transfer_log_path,"wbmc_mail",$accesslog_transfer_log_rotate_size)){
        system("/bin/echo $accesslog_transfer_log_path cannot write");
    }
    exit(1);
}

#送信者メールアドレス
$from_mailaddr = $ARGV[0];

#受信者メールアドレス
$to_mailaddr   = $ARGV[1];

#メールサーバ
$mailserver    = $ARGV[2];

#送信するメッセージの種類
$mail_flg      = $ARGV[3];

#メールの件名
$mail_subject = "";

#メールのメッセージ
$mail_message = "";

#送信するメッセージを格納
if($mail_flg eq "1"){
    #転送成功時のメッセージを格納
    $mail_subject = $mail_success_subject;
    $mail_message = $mail_success_message;
}elsif($mail_flg eq "2"){
    #転送失敗時のメッセージを格納
    $mail_subject = $mail_failed_subject;
    $mail_message = $mail_failed_message;
}elsif($mail_flg eq "3"){
    #アクセスログを削除した時のメッセージを格納
    $mail_subject = $mail_delete_subject;
    $mail_message = $mail_delete_message;    
}else{
    #用意していない種類のメッセージ
    if(!&writeLog_rotate("[Error] Unknowon mail message",$accesslog_transfer_log_path,"wbmc_mail",$accesslog_transfer_log_rotate_size)){
        system("/bin/echo $accesslog_transfer_log_path cannot write");
    }
    exit(1);
}

#メールを通知
$mail_log = "";
if(($mail_log = &send_mail($from_mailaddr,$to_mailaddr,$mail_subject,$mail_message,$mailserver)) ne ""){
    #メール通知に失敗したらログを出力する
    if(!&writeLog_rotate("[Error] MailLog:$mail_log",$accesslog_transfer_log_path,"wbmc_mail",$accesslog_transfer_log_rotate_size)){
        system("/bin/echo $accesslog_transfer_log_path cannot write");
    }
}
