#!/usr/bin/perl

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

require "/opt/nec/wbmc/language.pl";
if($mes_language eq "Japanese"){
        require "/opt/nec/wbmc/bin/lang_j.pl";                  # 日本語メッセージ
} else {
        require "/opt/nec/wbmc/bin/lang_e.pl";                  # 英語メッセージ
}

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

#2008/08 higashi
#タイムスタンプ変換の関数読み込み
require "/opt/nec/wbmc/bin/time_conv.pl";

#accesslog.confへのパス
$access_path ="/etc/opt/nec/wbmc/accesslog.conf";
$access_path_lock ="accesslog.conf";

#2008/08 higashi
#ftp_commandへのパス
$ftp_command_path = "/etc/opt/nec/wbmc/ftp_command";
$ftp_command_path_lock = "ftp_command";

#2008/08 higashi
#ftp_commandへのパス
$ftp_command_time_path = "/etc/opt/nec/wbmc/ftp_command_time";
$ftp_command_time_path_lock = "ftp_command_time";

#2008/09 higashi
#タイマー監視を行うため転送終了時間と転送コマンド
#を記述したファイル(time)へのパス
$time_path = "/var/log/accesslog_transfer/tmp_time";
$time_path_lock = "tmp_time";

#2008/09 higashi
#send_keep_statへのパス
$send_keep_stat_path = "/var/log/accesslog_transfer/send_keep_stat";
$send_keep_stat_path_lock = "send_keep_stat";

#2008/09 higashi
#転送コマンド(FTP,smbclient)の結果を出力するファイルへのパス
$send_path = "/var/log/accesslog_transfer/send.log";
$send_path_lock = "send.log";

#2008/09 higashi
#転送失敗したログを書き込むファイルへのパス
$send_fail_log_path="/var/log/accesslog_transfer/send_fail.log";

#2008/09 higashi
#転送成功したログリネームするファイルへのパス
$send_true_log_path="/var/log/accesslog_transfer/send_true.log";

#2008/10 higashi
#転送処理のログを出力するファイルへのパス
$accesslog_transfer_log_path = "/var/log/accesslog_transfer/accesslog_transfer.log";

#2008/09 higashi
#send_fail_log,accesslog_transfer.logをローテートするサイズ
$send_fail_log_rotate_size=5242880;

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

#2008/09 higashi
#PUT_ACCESSLOGを起動せせるwbmc_minuteが
#重複起動しないためPUT_ACCESSLOGの
#重複起動の処理を削除

#2008/08 higashi
#引数のチェック
if($ARGV[0] eq ""){
        if(!&writeLog_rotate("[Error] Not argument",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
                system("/bin/echo $accesslog_transfer_log_path cannot write");
        }
	exit(1);
}
$log_file_name=$ARGV[0];

#2008/11 higashi
#accesslog_transferディレクトリの作成
if(!(-d "/var/log/accesslog_transfer")){
	system("/bin/mkdir /var/log/accesslog_transfer");
}

#2008/08 higashi
#リトライ処理
#リトライキューが「0」に設定されている場合リトライは行わない
do{

	#2008/11 higashi
	#前回の設定が残っているなら読み込み

	#転送ファイルの状態
	$send_stat = "SEND";

	#転送先のファイルサーバで一番新しいファイル
	$send_file_name = "";

	#転送先のファイルサーバで一番新しいファイルのタイムスタンプ
	$send_time = "";

	#前回転送行った設定
	$log_save_interval_old              = 1;
	$log_trance_type_old                = 0;
	$log_trance_machine_old             = "";
	$log_workgroup_old                  = "";
	$log_common_old                     = "";
	$log_user_old                       = "";

	@file_send_keep_read = ();
	if(-e $send_keep_stat_path){
		if (!&readFile("$send_keep_stat_path", $send_keep_stat_path_lock, *file_send_keep_read) ) {
			system("/bin/echo $send_keep_stat_path cannot read");
			if(!&writeLog_rotate("[Warning] $send_keep_stat_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);
		}else{
			for($i= 0;$i < 	@file_send_keep_read;$i++){
				#転送ファイルの状態
				if($file_send_keep_read[$i] =~ /^\s*SEND_STAT\s*=\s*(.*)\s*$/){
					$send_stat = $1;
				}

				#転送先で一番新しいファイル名
				if($file_send_keep_read[$i] =~ /^\s*SEND_FILE_NAME\s*=\s*(.*)\s*$/){
					$send_file_name = $1;
				}

				#転送先で一番新しいファイルのタイムスタンプ
				if($file_send_keep_read[$i] =~ /^\s*SEND_TIME\s*=\s*(.*)\s*$/){
					$send_time = $1;
				}
		        	# 最大世代数
		        	if ($file_send_keep_read[$i] =~ /^\s*LOG_SAVE_INTERVAL\s*=\s*(.*)\s*$/) {
		        	        $log_save_interval_old = $1;
		        	}
		        	# 転送方式
		        	if ($file_send_keep_read[$i] =~ /^\s*LOG_TRANCE_TYPE\s*=\s*(.*)\s*$/) {
		        	        $log_trance_type_old = $1;
		        	} 
		        	# 転送先マシン名 
		        	if ($file_send_keep_read[$i] =~ /^\s*LOG_TRANCE_MACHINE\s*=\s*(.*)\s*$/) {
		        	        $log_trance_machine_old = $1;
		        	}
		        	# ワークグループ名 
		        	if ($file_send_keep_read[$i] =~ /^\s*LOG_WORKGROUP\s*=\s*(.*)\s*$/) {
		        		if ("$1" ne "") {
		        	        	$log_workgroup_old = "-W $1";
		        		}
		        	}
		        	# 共有名
		        	if ($file_send_keep_read[$i] =~ /^\s*LOG_COMMON\s*=\s*(.*)\s*$/) {
		        	        $log_common_old = $1;
		        	}
		        	# ユーザ名
		        	if ($file_send_keep_read[$i] =~ /^\s*LOG_USER\s*=\s*(.*)\s*$/) {
		        	        $log_user_old = $1;
				}
			}
		}
	}

	#       accesslog.ocnfファイルの読込み
	if (!&readFile("$access_path", $access_path_lock, *file) ) {
		system("/bin/echo $access_path cannot read");
		if(!&writeLog_rotate("[Error] $access_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);
	}

	# 設定ファイルの内容を読み出す処理
	$log_save_interval              = 0;
	$log_get_generation_stat        = "OFF";
	$log_trance_type                = 0;
	$log_trance_machine             = "";
	$log_workgroup                  = "";
	$log_common                     = "";
	$log_user                       = "";
	$log_passwd                     = "";

	#2008/12 higashi
	#FTPの転送モード選択の追加設定
	$log_ftp_trance_mode	= 0;

       	#2008/8 higashi
	#メール通知機能の追加設定
	$log_mail_stat                  = 0;
       	$log_mail_server                = "";
       	$log_from_mail_addr             = "";
       	$log_to_mail_addr               = "";

       	#2008/8 higashi
	#リトライ機能の追加設定
       	$log_max_que                    = 0;
       	$log_timeout                    = 15 * 60;
       	$log_retry_interval             = 10 * 60;

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

	        # コメントならば読み飛ばし
        	if ($file[$i] =~ /\s*#/) {
        	        next;
        	}
        	# 最大世代数
        	if ($file[$i] =~ /^\s*LOG_SAVE_INTERVAL\s*=\s*(.*)\s*$/) {
        	        $log_save_interval = $1;
        	}
		#2008/11 higashi
        	# サーバから取得したタイムスタンプを使用
        	if ($file[$i] =~ /^\s*LOG_GET_GENERATION_STAT\s*=\s*(.*)\s*$/) {
        	        $log_get_generation_stat = $1;
        	}
        	# 転送方式
        	if ($file[$i] =~ /^\s*LOG_TRANCE_TYPE\s*=\s*(.*)\s*$/) {
        	        $log_trance_type = $1;
        	} 
		#2008/12 higashi
		# FTP転送モード
		if ($file[$i] =~ /^\s*LOG_FTP_TRANCE_MODE\s*=\s*(.*)\s*$/) {
			$log_ftp_trance_mode = $1;
		}
        	# 転送先マシン名 
        	if ($file[$i] =~ /^\s*LOG_TRANCE_MACHINE\s*=\s*(.*)\s*$/) {
        	        $log_trance_machine = $1;
        	}
        	# ワークグループ名 
        	if ($file[$i] =~ /^\s*LOG_WORKGROUP\s*=\s*(.*)\s*$/) {
        		if ("$1" ne "") {
        	        	$log_workgroup = "-W $1";
        		}
        	}
        	# 共有名
        	if ($file[$i] =~ /^\s*LOG_COMMON\s*=\s*(.*)\s*$/) {
        	        $log_common = $1;
        	}
        	# ユーザ名
        	if ($file[$i] =~ /^\s*LOG_USER\s*=\s*(.*)\s*$/) {
        	        $log_user = $1;
		}
        	# パスワード
        	if ($file[$i] =~ /^\s*LOG_PASSWD\s*=\s*(.*)\s*$/) {
        	        $log_passwd = $1;
        	}
		# 世代数 
        	if ($file[$i] =~ /^\s*LOG_GENERATION\s*=\s*(.*)\s*$/) {
        	        $log_generation = $1;
		}
		# ファイル名
        	if ($file[$i] =~ /^\s*LOG_METHOD\s*=\s*(.*)\s*$/) {
        	        $log_method = $1;
		}
		#2008/8 higashi
		# 最大リトライキュー
	       	if ($file[$i] =~ /^\s*LOG_MAX_QUE\s*=\s*(.*)\s*$/) {
	       	        $log_max_que = $1;
		}
		#2008/8 higashi
		# タイムアウト時間
	       	if ($file[$i] =~ /^\s*LOG_TIMEOUT\s*=\s*(.*)\s*$/) {
	      	        $log_timeout = $1 * 60;
		}
		#2008/8 higashi
		# リトライ間隔
	       	if ($file[$i] =~ /^\s*LOG_RETRY_INTERVAL\s*=\s*(.*)\s*$/) {
	       	        $log_retry_interval = $1 * 60;
		}
		#2008/8 higashi
		# 管理者へのアクセスログ転送結果を通知
        	if ($file[$i] =~ /^\s*LOG_MAIL_STAT\s*=\s*(.*)\s*$/) {
			$log_mail_stat = $1;
		}
		#2008/8 higashi
		# メールサーバ
        	if ($file[$i] =~ /^\s*LOG_MAIL_SERVER\s*=\s*(.*)\s*$/) {
        	        $log_mail_server = $1;
		}
		#2008/8 higashi
		# 送信者のメールアドレス
	       	if ($file[$i] =~ /^\s*LOG_FROM_MAIL_ADDR\s*=\s*(.*)\s*$/) {
	       	        $log_from_mail_addr = $1;
		}
		#2008/8 higashi
		# 受信者のメールアドレス
	       	if ($file[$i] =~ /^\s*LOG_TO_MAIL_ADDR\s*=\s*(.*)\s*$/) {
	       	        $log_to_mail_addr = $1;
		}
	}

	#2008/11 higashi
	#リトライ処理なのか確認
	if(-e $send_keep_stat_path){
		#最大世代数,転送先が変わっていなければリトライ処理とする
		if(($send_stat ne "SEND") && ($log_save_interval ne $log_save_interval_old || $log_trance_type ne $log_trance_type_old || $log_trance_machine ne $log_trance_machine_old || $log_workgroup ne $log_workgroup_old || $log_common ne $log_common_old || $log_user ne $log_user_old)){
			$send_stat = "RETRY";
		}else{
			$send_stat = "SEND";
		}
	}

	#2008/09 higashi
	#転送用の名前にリネーム
	if($log_generation eq ""){
		$log_generation = "1";
	}

	#2008/09 higashi
	#作成したファイル名を格納する
	$log_file_name_new = "";

	#2008/09 higashi
	#ファイルリスト取得結果フラグ
	$log_access_flg    = 0;

	#2008/10 higashi
	#転送処理に失敗した結果を格納する
	@send_fail_log_work=();

	if($log_method eq "date"){
		#2008/09 higashi
		#タイムスタンプを付加したファイル名を作成
		use POSIX 'strftime';
		$log_file_name_new=sprintf("/var/log/roma/access%010d.log",strftime("%y%m%d%H%M",localtime((stat("$log_file_name"))[9])));
		$log_access_flg=1;
	}else{
		#2008/09 higashi
		#転送先ファイルサーバから取得したアクセスログリストの結果タイムスタンプをUTC形式に変換した
		#リストを作成

		if($log_trance_type eq "0"){
			#転送方式がFTPの場合

			#lsコマンドをファイルサーバに実行するコマンド作成
			@ftp_command_time_file=();
			push(@ftp_command_time_file,"user $log_user $log_passwd\n");
			if($log_common ne ""){
				push(@ftp_command_time_file,"cd $log_common\n");
			}
			push(@ftp_command_time_file,"ls access*.log\n");
			push(@ftp_command_time_file,"quit\n");
			if(&writeFile($ftp_command_time_path, $ftp_command_time_path_lock, @ftp_command_time_file)){
				system("ftp -i -n -d $log_trance_machine < $ftp_command_time_path >$send_path 2>&1");
				if(&readFile($send_path,$send_path_lock,*log_send_file)){
					$ls_flg=0;
					@send_fail_log_work=@log_send_file;
					for($i=0;$i < @log_send_file;$i++){
						if ($log_send_file[$i] =~ /LIST\s*access\*\.log/) {
							$ls_flg=1;
							next;
			       			}
						if($log_send_file[$i] =~ /QUIT/ && $ls_flg eq "1"){
							$log_access_flg = 1;
							last;
						}
					}
				}else{
					system("/bin/echo $send_path cannot read");
					if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						system("/bin/echo $accesslog_transfer_log_path cannot write");
					}
				}
			}else{
				system("/bin/echo $ftp_command_time_path cannot write");
				if(!&writeLog_rotate("[Error] $ftp_command_time_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}else{
			#転送方式がSAMBAの場合
			system("/usr/bin/smbclient //$log_trance_machine/$log_common $log_passwd -U $log_user $log_workgroup -c 'ls access*.log' 2>$send_path 1>&2");
			if(&readFile($send_path,$send_path_lock,*log_send_file)){
				@send_fail_log_work=@log_send_file;
				for($i=0;$i < @log_send_file;$i++){
					if ($log_send_file[$i] =~ /\s*Domain=\[.*\]\s*OS=\[.*\]\s*Server=\[.*\]\s*/i) {
                                                $i++;
                                                if($log_send_file[$i] =~ /access\d+\.log|NT_STATUS_NO_SUCH_FILE/i){
                                                        $log_access_flg = 1;
						        last;
                                                }
	       				}
				}
			}else{
				system("/bin/echo $send_path cannot read");
				if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}

		$log_conv_flg      = 0;
		if($log_access_flg eq "1"){
			#2008/11 higashi
			#ファイルリストの取得に成功したのでログに出力
			if(!&writeLog_rotate("[Success] The file list of the file server was acquired",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}

			#2008/11 higashi
			#lsコマンドの結果からaccess(世代数).logの行のみ
			#タイムスタンプをUTC形式に変換して抽出する
			@accesslog_list=();
			$cnt=0;
			$j=0;
			for($i=0;$i < @log_send_file;$i++){
				if ($log_send_file[$i] =~ /\s*access(\d\d?)\.log\s*/) {
					$j++;
					#2008/11 higashi
					#最大世代数を超えているアクセスログはタイムスタンプ変換の対象外
					if($1 <= $log_save_interval){
						@ret_time=&time_conv($log_send_file[$i]);
						if($ret_time[0] > 0){
							$accesslog_list[$cnt][0]=$ret_time[0];
							$accesslog_list[$cnt][1]=$ret_time[1];
							$accesslog_list[$cnt][2]=$1;
							$log_conv_flg = 1;
							$cnt++;
						}elsif($ret_time[0] eq "-1"){
							$log_conv_flg = 0;
							if(!&writeLog_rotate("[Warning] It failed in the conversion of the time stamp",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
								system("/bin/echo $accesslog_transfer_log_path cannot write");
							}
							last;
						}
					}
	       			}
			}

			#2008/11 higashi
			#転送先が空の場合タイムスタンプの変換は成功していることにする
			if($j eq "0"){
				$log_conv_flg = 1;
				$send_time = 0;
			}
			
			if(($log_conv_flg eq "1") && (@accesslog_list > 0) && ($j ne "0")){
				#2008/11 higashi
				#一番新しいファイルのタイムスタンプ、ファイル名を取得する

				#ファイルサーバで新しいファイル名を格納する
				$send_time_work = "";

				#ファイルサーバで新しいファイルのタイムスタンプ
				$send_file_name_work = "";

				#タイムスタンプが新しい順にソート
				@accesslog_list = sort{@$b[0]<=>@$a[0]}@accesslog_list;

				#一番新しいタイムスタンプが複数ないかチェック
				if(@accesslog_list eq "1"){
					$send_time_work = $accesslog_list[0][0];
					$send_file_name_work = $accesslog_list[0][1];
				}elsif($accesslog_list[0][0] > $accesslog_list[1][0]){
					$send_time_work = $accesslog_list[0][0];
					$send_file_name_work = $accesslog_list[0][1];
				}else{
					#一番新しいタイムスタンプが複数ある場合の処理
					$generation = 0;
					$j = $accesslog_list[0][2];

					#タイムスタンプが一番新しいものの中に最大世代数がないか確認
					for($i=1;$i < @accesslog_list && $accesslog_list[0][0] <= $accesslog_list[$i][0];$i++){
						if(($j < $accesslog_list[$i][2])  && ($accesslog_list[$i][2] ne $log_save_interval) && ($j ne $log_save_interval)){
							$j = $accesslog_list[$i][2];
						}elsif($accesslog_list[$i][2] eq $log_save_interval){
							$j = $accesslog_list[$i][2];
						}
					}

					#最大世代数から現在何世代目か確認する
					if($i < @accesslog_list){
						if($j eq $log_save_interval){
							$j++;
							if($j > $log_save_interval){
								$j=1;
							}
							for($cnt=0;$cnt < $i;$cnt++){
								if($accesslog_list[$cnt][2] eq $j){
									$cnt = 0;
									$j++;
								}
							}
							$j--;
							if($j eq "0"){
								$j = $log_save_interval;
							}
						}
						$generation = $j;
					}

					#ファイルのタイムスタンプが全て同じ場合設定ファイルの世代を利用して
					#一番新しいファイルを検索する
					if($generation eq "0"){
						$j = $log_generation - 1;
						$time_flg = 0;
						while(($j ne $log_generation) && ($time_flg eq "0")){
							if($j eq "0"){
								$j = $log_save_interval;
							}
							for($cnt=0;$cnt < @accesslog_list;$cnt++){
								if($accesslog_list[$cnt][2] eq $j){
									$generation = $j;
									$time_flg = 1;
									last;
								}
							}
							$j--;
						}
					}

					#一番新しいファイル名とタイムスタンプを取得
					for($i=0;$i < @accesslog_list;$i++){
						if($accesslog_list[$i][2] eq $generation){
							$send_time_work = $accesslog_list[$i][0];
							$send_file_name_work = $accesslog_list[$i][1];
							last;
						}
					}
				}
				#2008/11 higashi
				#ファイル名とタイムスタンプが取得できたら処理を行う
				if(($send_time_work ne "") && ($send_file_name_work ne "")){
					if(($send_time eq "")  && ($send_file_name eq "")) {
						#2008/10 higashi
						#最初の転送処理の時転送先ファイルサーバの一番新しいタイムスタンプ、ファイル名を取得する
						$send_time = $send_time_work;
						$send_file_name = $send_file_name_work;
					}elsif($send_time ne ""){
						#2008/10 higashi
						#リトライ処理なら転送途中で切れているファイルがないか確認
						if($send_time < $send_time_work){
							#転送途中のファイルを発見
							$log_file_name_new = sprintf("/var/log/roma/%s",$send_file_name_work);
						}
					}
				}
			}
		}else{
			#2008/11 higashi
			#リストの取得失敗時のログを残す
			if(!&writeLog_rotate("[Warning] Cannot get file list of file server ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
			unshift(@send_fail_log_work,`date +%Y/%m/%d%t%H:%M:%S`);
			if(!&writeLog_accesslog($send_fail_log_path,$send_fail_log_rotate_size,@send_fail_log_work)){
				system("/bin/echo $send_fail_log_path cannot write");
				if(!&writeLog_rotate("[Error] $send_fail_log_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
			#書き込んだら使いまわすため初期化する
			@send_fail_log_work = ();
		}

		#2008/09 higashi
		#世代数を超えていないかチェック
		$generation=0;

		if($log_file_name_new eq ""){
			if( (@accesslog_list >= $log_save_interval) && ($log_save_interval ne "1") && ($log_conv_flg eq "1")){
				#タイムスタンプが古い順にソート
				@accesslog_list = sort{@$a[0]<=>@$b[0]}@accesslog_list;

				#一番古いタイムスタンプが複数ないかチェック
				if(@accesslog_list eq "1"){
					$generation = $accesslog_list[0][2];					
				}elsif($accesslog_list[0][0] < $accesslog_list[1][0]){
					$generation = $accesslog_list[0][2];
				}else{
					#一番古いタイムスタンプが複数ある場合の処理
					$j = $accesslog_list[0][2];
					for($i=1;$i < @accesslog_list && $accesslog_list[0][0] >= $accesslog_list[$i][0];$i++){
						if(($j > $accesslog_list[$i][2])  && ($accesslog_list[$i][2] ne $log_save_interval) && ($j ne $log_save_interval)){
							$j = $accesslog_list[$i][2];
						}elsif($accesslog_list[$i][2] eq $log_save_interval){
							$j = $accesslog_list[$i][2];
						}
					}

					if($i < @accesslog_list){
						#最大世代から一番古いファイルを検索する
						if($j eq $log_save_interval){
							$j--;
							for($cnt=0;$cnt < $i;$cnt++){
								if($accesslog_list[$cnt][2] eq $j){
									$cnt = 0;
									$j--;
								}
							}
							$generation = $j + 1;
						}else{
							#最大世代数が古いタイムスタンプの中にないため
							#一番小さい世代が古いファイルとなる
							$generation = $j;
						}
					}
					#ファイルのタイムスタンプが全て同じの場合設定ファイルの世代を使用
					if($generation eq "0"){
						$generation = $log_generation;
					}
				}
			}elsif($log_conv_flg eq "1"){
				#ファイルの並びが穴あき,転送先のファイルサーバが空の状態時の処理
				#空いている世代の一番若い番号を取得する
				if($log_save_interval ne "1"){
					for($i=1;$i <= $log_save_interval;$i++){
						$list_flg=0;
						for($j=0;$j < @accesslog_list;$j++){
							if($accesslog_list[$j][2] eq $i){
								$list_flg = 1;
								last;
							}
						}
						if($list_flg eq "0"){
							$generation=$i;
							last;
						}
					}
				}else{
					$generation = 1;
				}
			}else{
				#2008/09 higashi
				#タイムスタンプの比較ができない場合設定ファイルの値を使用
				$generation = $log_generation;
			}
			$log_file_name_new = sprintf("/var/log/roma/access%d.log",$generation);
		}else{
			#ファイルサーバにある転送途中のファイル名を使用する
			$log_file_name_new =~ /access(\d\d?)\.log/;
			$generation = $1;
		}
		#2008/11 higashi
		#ファイルサーバから取得した世代を使用しない場合LOG_GENERATIONをファイル名に使用
		if($log_get_generation_stat eq "ON"){
			$log_generation = $generation;
		}else{
			$log_file_name_new = sprintf("/var/log/roma/access%d.log",$log_generation);
		}
	}

	#2008/09 higashi
	#転送用のファイル名にリネーム
	if(($log_file_name_new ne "") && ($log_file_name ne $log_file_name_new)){
		system("/bin/mv $log_file_name $log_file_name_new");
		$log_file_name = $log_file_name_new;
	}

	#2008/08 higashi
	#転送結果判定用フラグ
	#成功:1 失敗:0
	$log_send_flg=0;

	#2008/08 higashi	
	#転送結果を格納する
	@log_send_file=();

	#2008/08 higashi
	#転送処理前に転送終了時間と転送コマンドを配列に格納
	$time_out=`date +%s` + $log_timeout;
	@log_time=("FILE_NAME=$log_file_name\n","END_TIME=$time_out\n");
	if($log_trance_type eq "0"){
	  	push(@log_time,"PROCESS=ftp -i -n -d $log_trance_machine\n");
	}else{
		$log_file_name =~ /(access\d+\.log)/;
		push(@log_time,"PROCESS=/usr/bin/smbclient //$log_trance_machine/$log_common $log_passwd -U $log_user $log_workgroup -c lcd /var/log/roma;put $1");
	}

	#2008/11 higashi
	#システム再起動時に転送処理を行えるよう設定値を保持
	@file_send_keep = ();
	push(@file_send_keep,"SEND_STAT=$send_stat\n");
	push(@file_send_keep,"FILE_NAME=$log_file_name\n");
	push(@file_send_keep,"SEND_FILENAME=$send_file_name\n");
	push(@file_send_keep,"SEND_TIME=$send_time\n");
	push(@file_send_keep,"LOG_SAVE_INTERVAL=$log_save_interval\n");
	push(@file_send_keep,"LOG_TRANCETYPE=$log_trance_type\n");
	push(@file_send_keep,"LOG_MACHINE=$log_trance_machine\n");
	push(@file_send_keep,"LOG_COMMON=$log_common\n");
	push(@file_send_keep,"LOG_WORKGROUP=$log_workgroup\n");
	push(@file_send_keep,"LOG_USER=$log_user\n");
	if(!&writeFile($send_keep_stat_path,$send_keep_stat_path_lock,@file_send_keep)){
		system("/bin/echo $send_keep_stat_path cannot write");
		if(!&writeLog_rotate("[Warning] $send_keep_stat_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}
	}

	#2008/09 higashi
	#タイマー監視を行うためtimeファイルを作成
	if(&writeFile($time_path,$time_path_lock,@log_time)){
		#FTPのとき
		if($log_trance_type eq "0"){                        

			#2008/08 higashi
			#Ftpサーバにログイン後実行するコマンドを作成
			@ftp_command_file=();
			push(@ftp_command_file,"user $log_user $log_passwd\n");
			push(@ftp_command_file,"lcd /var/log/roma\n");
			if($log_common ne ""){
				push(@ftp_command_file,"cd $log_common\n");
			}
                        #2008/12 higashi
                        #FTPの転送モードのコマンドを追加
                        if($log_ftp_trance_mode eq "1"){
                                #バイナリモードで転送
				push(@ftp_command_file,"bin\n");
                        }
			$log_file_name =~/^\/var\/log\/roma\/(.*)$/;
			$log_send_file_name = $1;
			push(@ftp_command_file,"put $log_send_file_name\n");
			push(@ftp_command_file,"quit");

			#2008/09 higashi
			#作成したFtpコマンドをファイルに書き込み
			if(&writeFile($ftp_command_path,$ftp_command_path_lock,@ftp_command_file)){
				#2008/09 higashi
				#ファイル転送を行う
				system("ftp -i -n -d $log_trance_machine < $ftp_command_path > $send_path 2>&1");

				#2008/08 higashi
				#転送結果のチェックを行う
				if(&readFile($send_path,$send_path_lock,*log_send_file)){
					@send_fail_log_work=@log_send_file;
					for($i=0;$i < @log_send_file;$i++){
						if($log_send_file[$i] =~ /STOR\s*$log_send_file_name\s*$/){
							$i++;
							if($log_send_file[$i] =~ /QUIT\s*$/){
								$log_send_flg=1;
							}
							last;
						}
					}
				}else{
					#2008/11 higashi
					#send.logの読み込みに失敗したログを出力
					system("/bin/echo $send_path cannot read");
					if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						system("/bin/echo $accesslog_transfer_log_path cannot write");
					}
				}
			}else{
				#2008/11 higashi
				#ftp_commandの書き込みに失敗したログを出力
				system("/bin/echo $ftp_command_path cannot write");
				if(!&writeLog_rotate("[Error] $ftp_command_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}
		#SAMBAのとき
		else{
			#2008/09 higashi
			#ファイル転送を行う
			$log_file_name =~/^\/var\/log\/roma\/(.*)$/;
			system("/usr/bin/smbclient //$log_trance_machine/$log_common $log_passwd -U $log_user $log_workgroup -c 'lcd /var/log/roma;put $1' 2>$send_path 1>&2");

			#2008/08 higashi
			#転送結果のチェックを行う
			if(&readFile($send_path,$send_path_lock,*log_send_file)){
				@send_fail_log_work=@log_send_file;
				for($i=0;$i < @log_send_file;$i++){
					if($log_send_file[$i] =~ /putting\s*file\s*$1\s*as\s*\\$1\s*\(.*\s*.*\/s\)\s*\(average\s*.*\s*.*\/s\)/){
						$log_send_flg=1;
						last;
					}
				}
			}else{
				#2008/11 higashi
				#send.logの読み込みに失敗したログを出力
				system("/bin/echo $send_path cannot read");
				if(!&writeLog_rotate("[Error] $send_path cannot read",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}
	}else{
		#2008/11 higashi
		#tmp_timeの書き込みに失敗したログを出力
		system("/bin/echo $time_path cannot write");
		if(!&writeLog_rotate("[Error] $time_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}
	}

	#タイマー監視のファイルを削除
	if(-e $time_path){
		system("/bin/rm -r $time_path > /dev/null 2>&1");
	}

	if($log_send_flg eq "1"){
		#2008/08 higashi
		#アクセスログ転送成功後の処理
		
		#2008/11 higashi
		#転送に成功したログを出力
		$log_file_name =~ /(access\d+\.log)/;
		if(!&writeLog_rotate("[Success] $1 transfer success",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}

		#2008/09 higashi
		#転送結果のログファイルをリネーム
		system("/bin/mv $send_path $send_true_log_path");

		#2008/09 higashi
		#転送ファイルの削除
		system("/bin/rm -r $log_file_name > /dev/null 2>&1");

		#2008/11 higashi
		#転送に成功したメールを通知
		if($log_mail_stat eq "2"){
			#メールを通知するプロセスが二重起動しないようプロセスをkillする
			open(IN, "/bin/ps -efw |");
			while(<IN>) {
				if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
					@wk = split(/\s+/, $_);
					system("/bin/kill $wk[1]");
					sleep(1);
                                        if(!&writeLog_rotate("[Warning] wbmc_mail process kill. ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				            system("/bin/echo $accesslog_transfer_log_path cannot write");
			                }
				}
			}
			close(IN);
			system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 1 &");
			sleep(1);
		}

		#2008/10 higashi
		#保持している状態を削除
		if(-e $send_keep_stat_path){
			system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
		}

		#
		#LOG_METHOD=generationのとき、世代数を変更する
		#
    		if($log_method eq "generation"){
			#世代数を１増やし、最大世代数を越えたら１に戻す
			++$log_generation;
			if($log_generation > $log_save_interval){
				$log_generation=1;
			}

			for($i=0;$i<@file;$i++){
				if($file[$i] =~ /^LOG_GENERATION/){
					$file[$i]="LOG_GENERATION=$log_generation\n";
					last; 
				}
			}
		}

		#設定ファイルの上書き
		if (!&writeFile($access_path, $access_path_lock, @file)) {
			system("/bin/echo $access_path cannot write");
			if(!&writeLog_rotate("[Error] $access_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
		}else{
			if(!&writeLog_rotate("[Success] $access_path write success",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
		}
	}else{
		#2008/08 higashi
		#アクセスログ転送失敗後の処理

		#2008/08 higashi
		#転送失敗したログを残す
		if(@send_fail_log_work > 0){
			unshift(@send_fail_log_work,`date +%Y/%m/%d%t%H:%M:%S`);
			if(!&writeLog_accesslog($send_fail_log_path,$send_fail_log_rotate_size,@send_fail_log_work)){
				system("/bin/echo $send_fail_log_path cannot write");
				if(!&writeLog_rotate("[Error] $send_fail_log_path cannot write",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
					system("/bin/echo $accesslog_transfer_log_path cannot write");
				}
			}
		}
		if(!&writeLog_rotate("[Warning] $log_file_name transfer failed.",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
			system("/bin/echo $accesslog_transfer_log_path cannot write");
		}

		#一時的に保存していた転送結果のログを削除
		if(-e $send_path){
			system("/bin/rm -r $send_path > /dev/null 2>&1");
		}

		#2008/11 higashi
		#転送に失敗したメールを送信する
		if(($log_mail_stat eq "1") || (($send_stat eq "SEND") && ($log_mail_stat eq "2"))){
			#メールを通知するプロセスが二重起動しないようプロセスをkillする
			open(IN, "/bin/ps -efw |");
			while(<IN>) {
				if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
					@wk = split(/\s+/, $_);
					system("/bin/kill $wk[1]");
                                        if(!&writeLog_rotate("[Warning] wbmc_mail process kill. ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				            system("/bin/echo $accesslog_transfer_log_path cannot write");
			                }
					sleep(1);
				}
			}
			close(IN);
			system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 2 &");
                        sleep(1);
		}

		#2008/11 higashi
		#リトライ処理に移ったことをファイルに残す
		if(($log_max_que ne "0") && ($send_stat eq "SEND")){
			$send_stat = "RETRY";
			for($i=0;$i < @file_send_keep;$i++){
				if($file_send_keep[$i] =~ /^\s*SEND_STAT\s*=.*\s*/){
					$file_send_keep[$i] = "SEND_STAT=$send_stat\n";
				}
			}
			if(!&writeFile($send_keep_stat_path,$send_keep_stat_path_lock,@file_send_keep)){
				system("/bin/echo $send_keep_stat_path cannot write");
			}
		}elsif($log_max_que eq "0" && -e $send_keep_stat_path){
			#リトライしないので保持している設定を削除
			system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
		}

		#2008/09 higashi
		#最大リトライキューが0の場合リトライ処理を行わない
		if($log_max_que eq "0"){
			if(-e $log_file_name){
				system("/bin/rm -r $log_file_name > /dev/null 2>&1");
			}
			if(($log_mail_stat eq "1") || ($log_mail_stat eq "2")){
				#メールを通知するプロセスが二重起動しないようプロセスをkillする
				open(IN, "/bin/ps -efw |");
				while(<IN>) {
					if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
						@wk = split(/\s+/, $_);
						system("/bin/kill $wk[1]");
						sleep(1);
					}
				}
				close(IN);
				system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 3 &");
			}
			if(!&writeLog_rotate("[Fatal] $log_file_name deleted,because retry-queu 0",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
				system("/bin/echo $accesslog_transfer_log_path cannot write");
			}
			$log_send_flg=1;
		}else{
			#2008/08 higashi
			#リトライ間隔分あけてから転送処理を再開
			while(($log_retry_interval > 0) && (-e $log_file_name)){
				if($log_retry_interval > 300){
					sleep 300;
					$log_retry_interval -= 300;
				}else{
					sleep $log_retry_interval;
					$log_retry_interval = 0;
				}
			}
			if(!(-e $log_file_name)){
				#2008/09 higashi
				#転送するファイルが削除されたので転送処理を終了する
				if(-e $send_keep_stat_path){
					system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
				}
				if(($log_mail_stat eq "1") || ($log_mail_stat eq "2")){
					#メールを通知するプロセスが二重起動しないようプロセスをkillする
					open(IN, "/bin/ps -efw |");
					while(<IN>) {
						if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
							@wk = split(/\s+/, $_);
							system("/bin/kill $wk[1]");
		                                        if(!&writeLog_rotate("[Warning] wbmc_mail process kill. ",$accesslog_transfer_log_path,"PUT_ACCESSLOG",$accesslog_transfer_log_rotate_size)){
						            system("/bin/echo $accesslog_transfer_log_path cannot write");
					                }
							sleep(1);
						}
					}
					close(IN);
					system("/opt/nec/wbmc/bin/wbmc_mail $log_from_mail_addr $log_to_mail_addr $log_mail_server 3 &");
                                        sleep(1);
					#保持している状態を削除
					if(-e $send_keep_stat_path){
						system("/bin/rm -r $send_keep_stat_path > /dev/null 2>&1");
					}
				}
				$log_send_flg=1;
			}
		}
	}
}while($log_send_flg eq "0");

#2009/1 higashi
#メール送信のためディレイを行う
if($log_mail_stat ne "0"){
        for($i=0;$i < 300;$i++){
                $flg_mail = 0;
                open(IN, "/bin/ps -efw |");
                while(<IN>) {
                        if( $_ =~ /\/opt\/nec\/wbmc\/bin\/wbmc_mail/ ) {
                                $flg_mail = 1;
                                sleep 1;
                                last;
                        }
                }
                if($flg_mail eq "0"){
                        last;
                }
        }
}
exit(0);
