#!/usr/bin/perl

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

# RCS: $Id: wbmc_mail,v 1.1 2008/12/24 10:31:52 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";

#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;
	}
}

#2008/12 higashi
#᡼λη̾ʸ
$mail_success_subject = "InterSec/CS<$hostname>[SUCCESS] Accesslog transfer succeeded";
$mail_success_message = "[Cause]\r\n    The transfer of the accesslog succeeded.\r\n[Reason]\r\n    - \r\n[Resolution]\r\n    -";
$mail_failed_subject  = "InterSec/CS<$hostname>[!!WARNING!!] Accesslog transfer failed";
$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_subject  = "InterSec/CS<$hostname>[!!FAILED!!] Accesslog was deleted";
$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");
    }
}
