FileFtpCollThread.java 6.14 KB
package com.sitech.ismp.coll.busi;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.sitech.ismp.coll.busi.util.FilesOperation;
import com.sitech.util.DES3;
import com.sitech.util.JSONUtil;
import com.sitech.util.RandomGUID;
import com.sitech.util.upload.FTPIF;
import com.sitech.util.upload.FTPSrv;
import com.sitech.util.upload.RemoteFile;
import com.sitech.util.upload.SFTPClient;

/**
 * FTP采集配置文件(GX)
 * 本地目录命名规则: ${coll.local.path} + "/" + ip.replace(".","_")/
 * 		例:/bnmsapp2/172_21_1_100/
 * 
 * 1. FTP配置文件到本地目录,文件命名规则为:CFG_$GUID.txt
 * 2. 创建对应的描述文件到本地目录,文件命名规则为:CFG_$GUID.info
 * 
 *******Begin CFG_986C5F24.info***************************************
 * TYPE=FTP
 * IP_ADDR=172.21.1.100
 * DIRECTORY=/bnmsapp2
 * FILE_NAME=AppCenterProxy_error_check.sh
 * PERSSIONS=-rw-r--r--
 * SIZE=1279
 * UPDATE_TIME=2012-10-17 10:02:38
 * USER=bnmsapp2
 * COLL_TIME=2012-11-21 13:18:55
 *******End CFG_986C5F24.info*****************************************
 *
 * @author linxc
 */
public class FileFtpCollThread implements Runnable {
	private Logger logger = Logger.getLogger(FileFtpCollThread.class);

	private String ip;
	private String username;
	private String password;
	
	private int collHa = 0;
	private String ipHa;
	private String usernameHa;
	private String passwordHa;
	
	private String localDir;
	
	private String[] dir;
	private String[] file;
	
	private HashMap<String,String> params;
	
	public FileFtpCollThread(HashMap<String,String> params) {
		this.params = params;
	}
	
	public void run() {
		if (!init()) {
			return;
		}
		
		collHost(ip, username, password);
		
		if(collHa == 1){
			// 采集双机
			collHost(ipHa, usernameHa, passwordHa);
		}
	}
	
	private void collHost(String ip, String username, String password){
		logger.info("Start FileFtpCollThread,IP=" + ip + ",USERNAME=" + username);
		FTPIF ftpClient = getFtpClient(ip, username, password);
		if (ftpClient == null) {
			logger.warn("ftpClient == null, return!");
			return;
		}

		try {
			for (int i = 0; i < file.length; i++) {
				getFile(ftpClient, dir[i], file[i]);
			}			
		} catch (Exception e) {
			logger.error("Exception while FileFtpColl.collFile", e);
		} finally {
			if (ftpClient != null) {
				try {
					ftpClient.logout();
					ftpClient = null;
				} catch (Exception e) {
					logger.error("Exception while ftpClient.logout()", e);
				}
			}
		}
	}
	
	


	public void getFile(FTPIF ftpClient, String remoteDir, String fileName)
			throws Exception {
		fileName = fileName.trim();
		
		logger.info("cd " + remoteDir);
		ftpClient.chdir(remoteDir);

		RemoteFile remoteFile = ftpClient.listRemoteFile(remoteDir, fileName);
		if (remoteFile == null || remoteFile.isDirectory()) {
			return;
		}

		String perssions = remoteFile.getPermissons();
		long size = remoteFile.getSize();

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date updateTime = remoteFile.getLastModifyTime();
		String user = remoteFile.getOwner();

		String descr = "";
		descr += "TYPE=FTP\n";
		descr += "IP_ADDR=" + ip + "\n";
		descr += "DIRECTORY=" + remoteDir + "\n";
		descr += "FILE_NAME=" + fileName + "\n";
		descr += "PERSSIONS=" + perssions + "\n";
		descr += "SIZE=" + size + "\n";
		descr += "UPDATE_TIME=" + dateFormat.format(updateTime) + "\n";
		descr += "USER=" + user + "\n";
		descr += "COLL_TIME=" + dateFormat.format(new Date()) + "\n";

		logger.info("Begin get file[" + fileName + "]...");

		String guid = RandomGUID.getRandomGUID();

		String tempName = localDir + "/CFG_" + guid + ".load";
		String descrFileName = localDir + "/CFG_" + guid + ".info";
		File tempFile = new File(tempName);
		File localFile = new File(localDir + "/CFG_" + guid + ".txt");

		ftpClient.get(fileName, tempName);
		tempFile.renameTo(localFile);

		logger.info("Get file[" + fileName + "] success!");

		FilesOperation.createFiles(descrFileName, descr);
	}

	private FTPIF getFtpClient(String ip, String username, String password) {
		FTPIF ftpClient = new FTPSrv();
		try {
			logger.info("ftp " + ip + ", LOGIN:" + username + "...");

			ftpClient.login(ip, username, password);
		} catch (Exception e) {
			logger.warn("ftp " + ip + ", LOGIN:" + username + " FAILED!");
			try {
				ftpClient = new SFTPClient();
				logger.info("sftp " + username + "@" + ip);
				ftpClient.login(ip, username, password);
			} catch (Exception e1) {
				logger.warn("sftp " + username + "@" + ip + " FAILED!");
				ftpClient = null;
			}
		}
		return ftpClient;
	}
	
	/**
	 * 初始化采集参数
	 */
	private boolean init(){
		try{
			this.ip = params.get("IP_ADDR");
			this.username = params.get("USERNAME");
			this.password = DES3.decrypt(params.get("PASSWORD"));
			
			String haCollFlag = params.get("HA_FLAG");
			if(haCollFlag != null && haCollFlag.trim().equals("1")){
				this.collHa = Integer.parseInt(haCollFlag);
				this.ipHa = params.get("IP_ADDR_HA");
				this.usernameHa = params.get("USERNAME_HA");
				this.passwordHa = DES3.decrypt(params.get("PASSWORD_HA"));
			}
			
			ResourceBundle agentConfig = ResourceBundle.getBundle("agent");
			this.localDir = agentConfig.getString("coll.local.path") + "/" + ip.replace(".", "_");

			File temp = new File(this.localDir);
			if (!temp.exists()) {
				temp.mkdir();
			}
			
			List<String> fileList = new ArrayList<String>();
			for (String key : params.keySet()) {
				if (key.indexOf("FILE_") >= 0) {
					Map<?,?> tempMap = (Map<?,?>) JSONUtil.fromJSON(params.get(key));
					fileList.add((String) tempMap.get("filePath"));
				}
			}
			
			this.dir = new String[fileList.size()];
			this.file = new String[fileList.size()];
			for (int i = 0; i < fileList.size(); i++) {
				String elem = fileList.get(i);
				dir[i] = elem.substring(0, elem.lastIndexOf("/"));
				file[i] = elem.substring(elem.lastIndexOf("/") + 1);
			}
			
		}catch (Exception e) {
			logger.error("初始化文件Ftp采集进程出错!!!", e);			
			return false;
		}		
		return true;
	}
}